forked from sameenjalal/Linux-File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_dir.c
81 lines (70 loc) · 1.62 KB
/
file_dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This file contains functions for creating, deleting, and reading directories
#include "api.h"
#include "private_api.h"
#include <string.h>
// Creates the specified directory.
// Returns an error or SUCCESS.
int file_mkdir(char *path)
{
return file_create_generic(path, 1);
}
// Removes the specified directory.
// Returns an error or SUCCESS.
int file_rmdir(char *path)
{
return file_delete_generic(path, 1);
}
// Lists the given directory.
// Returns an array of character strings.
char **file_listdir(char *path)
{
char **ret;
direntry *dp = NULL;
direntry **dirents;
int inumber;
inode *iptr;
int dirent_count = 0, i;
inumber = path_to_inumber(path);
iptr = get_inode(inumber);
dirents = get_dirents(iptr);
for (i = 0; dirents[i] != NULL; i++){
dp = dirents[dirent_count];
if (!isDirentZeroed(dp)){
dirent_count++;
}
}
ret = (char **)malloc(sizeof(char) * (1 + dirent_count));
for (i = 0; i < dirent_count; i++){
ret[i] = strdup(dirents[i]->name);
}
ret[dirent_count] = NULL;
free(iptr);
//free_dirents(dirents);
return ret;
/*
char *dup = strdup(path);
char *tokenized_str;
int i;
char *current_dir;
myread(fs_disk, current_dir, 1);
char *tmp = NULL;
while ( tokenized_str = strtok(dup, '/') )
{
if ( tmp = dir_contains(current_dir, tokenized_str) )
current_dir = tmp;
else break;
}
*/
}
// Prints a directory listing to the screen.
void file_printdir(char *path)
{
char **names;
char *ptr;
int i;
names = file_listdir(path);
for (i = 0; names[i] != NULL; i++){
fprintf(stdout, "%s\n", names[i]);
}
//free_path_tokens(names);
}