-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_inode.c
75 lines (65 loc) · 1.88 KB
/
fs_inode.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
#include "fs_inode.h"
#include "inodeyou.h"
inodenode *get_fs_inodes(const char path[])
{
struct stat stats;
// https://codeforwin.org/2018/03/c-program-find-file-properties-using-stat-function.html
if (stat(path, &stats) != 0)
{
printf("Unable to get file properties.\n");
printf("Please check whether '%s' file exists.\n", path);
exit(1);
}
inodenode *fs_ll = create_inode_ll(-1);
if (fs_ll == NULL)
{
printf("Error: Failed to create fs_ll inodenode node\n");
exit(1);
}
fs_ll = fs_walk_path(path, fs_ll);
return fs_ll;
}
inodenode *fs_walk_path(const char path[], inodenode *fs_ll)
{
DIR *folder = opendir(path);
if (folder == NULL)
{
printf("Error: Unable to read directory (%s)", path);
return fs_ll;
}
struct dirent *entry = NULL;
int inode_number = -1;
while ((entry = readdir(folder)) != NULL)
{
// Directory
if (entry->d_type == 4)
{
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
{
// "." and ".."
continue;
}
inode_number = (int)entry->d_ino;
// printf("%s (Dir)\n", entry->d_name);
fs_ll = insert_inode_ll(fs_ll, (long)inode_number);
// Recursive functionality
if (RECURSIVE_TEST)
{
char buffer[BUF_LEN_LARGE];
sprintf(buffer, "%s/%s", path, entry->d_name);
fs_ll = fs_walk_path(buffer, fs_ll);
}
}
// File
else if (entry->d_type == 8)
{
// Regular file
inode_number = (int)entry->d_ino;
// printf("%s (File)\n", entry->d_name);
fs_ll = insert_inode_ll(fs_ll, (long)inode_number);
}
}
// Cleanup
closedir(folder);
return fs_ll;
}