Reputation: 23
I am building a FUSE file system using my Ubuntu22 virtual machine on a macbook. I have implemented the getattr
function below and compiled using gcc fuse-fs.c -o testfs `pkg-config fuse --cflags --libs`
which outputs no errors. However, when I mount using sudo ./testfs -d /dfs
try to test it by running sudo ls /dfs
, I get the following output:
ls: cannot access '/dfs': Input/output error
I'm not sure why this is happening, as my code looks correct based on the FUSE documentation. I have added the related functions to help.
struct filetype *dfs_tree_root = &(struct filetype){ .path="/" };
// Function to traverse the DFS tree and locate a file/directory by path
static struct filetype* get_filetype_from_path(struct filetype* root, const char* path) {
if (root == NULL) {
return NULL;
}
if (path == NULL || strlen(path) == 0) {
return root; // Return root for empty path (refers to root directory)
}
// Split the path into components (separated by '/')
char* path_copy = strdup(path); // Avoid modifying the original path
char* component = strtok(path_copy, "/");
struct filetype* current = root;
while (component) {
// Search for child with matching name in current directory
int found = 0;
for (int i = 0; i < current->num_children; i++) {
if (strcmp(current->children[i]->path, component) == 0) {
current = current->children[i];
found = 1;
break;
}
}
// If not found in current directory, return NULL
if (!found) {
free(path_copy);
return NULL;
}
component = strtok(NULL, "/"); // Get next path component
}
free(path_copy);
return current;
}
// getattr operation for retrieving file attributes
static int dfs_getattr(const char *path, struct stat *stbuf) {
printf( "Called [getattr]\n" );
printf( "\tAttributes of %s requested\n", path );
char *pathname = malloc(strlen(path) + 2);
if (pathname == NULL) {
return -ENOMEM; // Out of memory
}
strcpy(pathname, path);
printf("GETATTR %s\n", pathname);
filetype * file_node = get_filetype_from_path(dfs_tree_root, pathname);
if(file_node == NULL)
return -ENOENT;
stbuf->st_uid = file_node -> user_id; // The owner of the file/directory is the user who mounted the filesystem
stbuf->st_gid = file_node -> group_id; // The group of the file/directory is the same as the group of the user who mounted the filesystem
stbuf->st_atime = file_node -> a_time; // The last "a"ccess of the file/directory is right now
stbuf->st_mtime = file_node -> m_time; // The last "m"odification of the file/directory is right now
stbuf->st_ctime = file_node -> c_time;
stbuf->st_mode = file_node -> permissions;
stbuf->st_nlink = file_node -> num_links + file_node -> num_children;
stbuf->st_size = file_node -> size;
free(pathname);
return 0;
}
Upvotes: 0
Views: 205