-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
188 lines (157 loc) · 5.95 KB
/
Copy pathls.c
File metadata and controls
188 lines (157 loc) · 5.95 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <libgen.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "allocator.h"
#include "dir.h"
#include "fs_utils.h"
#include "superblock.h"
#define VERSION "0.0.2"
/* command line options */
static int opt_flag_f = 0;
/* define usage function */
static void usage(void) {
printf(
"ls - Version %s\n"
"usage: ls.pangya -f|--filename <PangYa filesystem disk image filename> [<path_name>]\n"
" [-h|--help]\n", VERSION
);
}
int main(int argc, char *argv[]) {
/* define command line options */
char *short_opts = "f:h";
struct option long_opts[] = {
{"filename", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
char filename[PATH_MAX];
int disk_image_fd = -1;
struct superblock sb;
char *pathname = "/";
/* suppress default getopt error messages */
opterr = 0;
int c;
while (1) {
c = getopt_long(argc, argv, short_opts, long_opts, NULL);
if (c < 0) {
break;
}
switch (c) {
case 'f':
strcpy(filename, optarg);
opt_flag_f = 1;
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
case '?':
fprintf(stderr, "ERROR: Unknown option\n\n");
usage();
exit(EXIT_FAILURE);
default:
fprintf(stderr, "ERROR: Unimplemented option\n\n");
usage();
exit(EXIT_FAILURE);
}
}
/* check if necessary options are specified */
if (!opt_flag_f) {
usage();
exit(EXIT_FAILURE);
}
/* handle path name parameter */
if (optind < argc) {
pathname = argv[optind];
}
/* open disk image file */
disk_image_fd = open(filename, O_RDONLY);
if (disk_image_fd < 0) {
fprintf(stderr, "ERROR: failed to open disk image file %s: %s\n", filename, strerror(errno));
goto error_handler;
}
/* read superblock */
if (read_superblock(disk_image_fd, &sb) < 0) {
fprintf(stderr, "ERROR: failed to read superblock from disk image file %s\n", filename);
goto error_handler;
}
/* get target path name inode */
struct inode *pathname_inode = namei(disk_image_fd, &sb, pathname);
if (pathname_inode == NULL) {
fprintf(stderr, "ERROR: failed to get path name %s inode\n", pathname);
goto error_handler;
}
/*
* output format:
* inode number | number of links | file size in byte | file type | filename
*
*/
printf("%12s %4s %12s %5s %14s\n", "INODE", "LINK", "SIZE", "TYPE", "NAME");
/* scenario #1: regular file */
if ((pathname_inode->i_mode & IFREG) != 0) {
char *pathname_copy = strdup(pathname);
if (pathname_copy != NULL) {
printf("%12" PRIu32 " %4" PRIu16 "%12" PRIu32 " %5s %14s\n", pathname_inode->i_num, pathname_inode->i_nlink, pathname_inode->i_size0, "REG", basename(pathname_copy));
free(pathname_copy);
} else {
printf("%12" PRIu32 " %4" PRIu16 " %12" PRIu32 " %5s %14s\n", pathname_inode->i_num, pathname_inode->i_nlink, pathname_inode->i_size0, "REG", pathname);
}
}
/* scenario #2: directory */
if ((pathname_inode->i_mode & IFDIR) != 0) {
/* iterate dirent entries from each physical block in i_addr[] array */
uint32_t physical_block;
struct inode *entry_inode;
size_t dirent_entries = get_dirent_per_block();
struct dirent dirent_entries_buffer[dirent_entries];
for (uint8_t logical_block = 0; logical_block < NDIRECT; ++logical_block) {
physical_block = pathname_inode->i_addr[logical_block];
/* only processing non-zero physical block */
if (physical_block != 0) {
if (read_block(disk_image_fd, physical_block, dirent_entries_buffer, sizeof(dirent_entries_buffer), 0) < 0) {
fprintf(stderr, "ERROR: failed to read physical block %" PRIu32 "\n", physical_block);
continue;
}
for (size_t i = 0; i < dirent_entries; ++i) {
/* only processing dirent entry with inode number is not zero */
if (dirent_entries_buffer[i].inode_number != 0) {
char *entry_mode = "UNK"; /* file type: UNKNOWN */
/* get entry inode struct and acquire metadata */
entry_inode = get_inode(disk_image_fd, &sb, dirent_entries_buffer[i].inode_number);
if (entry_inode == NULL) {
fprintf(stderr, "ERROR: failed to get inode struct for filename %s\n", dirent_entries_buffer[i].dir_name);
continue;
}
/* check mode of entry */
if ((entry_inode->i_mode & IFREG) != 0) {
entry_mode = "REG";
}
if ((entry_inode->i_mode & IFDIR) != 0) {
entry_mode = "DIR";
}
/* print output */
printf("%12" PRIu32 " %4" PRIu16 " %12" PRIu32 " %5s %14s\n", entry_inode->i_num, entry_inode->i_nlink, entry_inode->i_size0, entry_mode, dirent_entries_buffer[i].dir_name);
/* release resource */
put_inode(disk_image_fd, &sb, entry_inode);
}
}
}
}
}
/* release resource */
put_inode(disk_image_fd, &sb, pathname_inode);
close(disk_image_fd);
exit(EXIT_SUCCESS);
error_handler:
if (disk_image_fd >= 0) {
close(disk_image_fd);
}
exit(EXIT_FAILURE);
}