Untitled
unknown
c_cpp
2 years ago
1.7 kB
16
Indexable
const int DIR_MAX_LENGTH = 14;
int directory_findname(const struct unixfilesystem *fs, const char *name,
int dirinumber, struct direntv6 *dirEnt) {
struct inode in;
int error = inode_iget(fs, dirinumber, &in); // Get inode
if(error < 0) {
fprintf(stderr, "directory_findname(name=%s dirinumber=%d) "
"inode not found. Returning -1\n", name, dirinumber);
return -1;
}
if((in.i_mode & IFMT) != IFDIR) { // Check if it is a dir
fprintf(stderr, "directory_findname(name=%s dirinumber=%d) "
"inode not a directory. Returning -1\n", name, dirinumber);
return -1;
}
int dir_size = inode_getsize(&in);
if(dir_size <= 0) { // Check if directory is a valid size (>0)
fprintf(stderr, "directory_findname(name=%s dirinumber=%d) "
"directory empty or wrong size. Returning -1\n", name, dirinumber);
return -1;
}
for(int i = 0; i < (dir_size - 1) / DISKIMG_SECTOR_SIZE + 1; i++) { // Loop block
struct direntv6 entries[DISKIMG_SECTOR_SIZE / sizeof(struct direntv6)];
int bytes = file_getblock(fs, dirinumber, i, entries);
if(bytes < 0) {
fprintf(stderr, "directory_findname(name=%s dirinumber=%d) "
"cannot get block (no bytes). Returning -1\n", name, dirinumber);
return -1;
}
int total_entries = bytes / sizeof(struct direntv6);
for(int j = 0; j < total_entries; j++) { // Loop entries
if(strncmp(entries[j].d_name, name, DIR_MAX_LENGTH) == 0) { // Found matching entry
*dirEnt = entries[j];
return 0;
}
}
}
return -1;
}Editor is loading...
Leave a Comment