-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting.c
214 lines (187 loc) · 6.63 KB
/
listing.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "header.h"
#include "util.h"
#include <sys/stat.h>
/* get the permissions string from the mode */
void get_permissions(char permissions[], mode_t mode, FILE *tarfile) {
int index = 0;
if (is_dir(tarfile))
permissions[index++] = 'd';
else if (is_symlink(tarfile))
permissions[index++] = 'l';
else
permissions[index++] = '-';
permissions[index++] = (mode & S_IRUSR) ? 'r' : '-';
permissions[index++] = (mode & S_IWUSR) ? 'w' : '-';
permissions[index++] = (mode & S_IXUSR) ? 'x' : '-';
permissions[index++] = (mode & S_IRGRP) ? 'r' : '-';
permissions[index++] = (mode & S_IWGRP) ? 'w' : '-';
permissions[index++] = (mode & S_IXGRP) ? 'x' : '-';
permissions[index++] = (mode & S_IROTH) ? 'r' : '-';
permissions[index++] = (mode & S_IWOTH) ? 'w' : '-';
permissions[index++] = (mode & S_IXOTH) ? 'x' : '-';
permissions[index] = '\0';
}
/* get owner string from uid and gid */
void get_owner(uid_t uid, char uname[], gid_t gid, char gname[], char owner[]) {
#define OWNER_WIDTH 17
#define MAX_NAME_LENGTH 8
#define USER_NAME_LENGTH 32
char buffer[USER_NAME_LENGTH];
owner[0] = '\0';
if (uname[0] == '\0') {
sprintf(buffer, "%d", uid);
strcat(owner, buffer);
} else {
snprintf(buffer, MAX_NAME_LENGTH, "%s", uname);
strcat(owner, buffer);
}
strcat(owner, "/");
if (gname[0] == '\0') {
sprintf(buffer, "%d", gid);
strcat(owner, buffer);
} else {
snprintf(buffer, MAX_NAME_LENGTH, "%s", gname);
strcat(owner, buffer);
}
}
/* get time string in yyyy:mm:dd hh:mm format */
void get_time(time_t time, char timestr[]) {
struct tm *tm = localtime(&time);
int year;
int month;
int day;
int hour;
int min;
timestr[0] = '\0';
year = tm->tm_year + 1900;
month = tm->tm_mon + 1;
day = tm->tm_mday;
hour = tm->tm_hour;
min = tm->tm_min;
sprintf(timestr, "%d-%02d-%02d %02d:%02d", year, month, day, hour, min);
}
/* list the contents of a file */
void list_contents(FILE* tarfile, char path[], int isverbose, int isstrict) {
#define EXTRA_SPACE (BLOCK_LENGTH - HEADER_LENGTH)
#define PERMISSION_WIDTH 11
#define OWNER_WIDTH 17
#define TIME_WIDTH 17
mode_t mode;
off_t size;
time_t time;
uid_t uid;
gid_t gid;
char gname[GNAME_LENGTH];
char uname[UNAME_LENGTH];
char timestr[TIME_WIDTH];
char owner[OWNER_WIDTH];
char permissions[PERMISSION_WIDTH];
char buffer[PATH_MAX];
int blocks;
if (validate_header(tarfile, isstrict) != 0) {
fprintf(stderr, "Invalid Header\n");
exit(EXIT_FAILURE);
}
memset(buffer, 0, PATH_MAX);
mode = get_mode(tarfile);
size = get_size(tarfile);
if (isverbose) {
/* verbose print */
get_permissions(permissions, mode, tarfile);
fseek(tarfile, UID_OFFSET, SEEK_CUR);
fread(owner, 1, UID_LENGTH, tarfile);
uid = (uid_t)strtol(owner, NULL, 8);
fread(owner, 1, GID_LENGTH, tarfile);
gid = (gid_t)strtol(owner, NULL, 8);
fseek(tarfile, -GID_OFFSET - GID_LENGTH, SEEK_CUR);
fseek(tarfile, UNAME_OFFSET, SEEK_CUR);
fread(uname, 1, UNAME_LENGTH, tarfile);
fread(gname, 1, GNAME_LENGTH, tarfile);
fseek(tarfile, -GNAME_LENGTH - GNAME_OFFSET, SEEK_CUR);
get_owner(uid, uname, gid, gname, owner);
fseek(tarfile, MTIME_OFFSET, SEEK_CUR);
fread(buffer, 1, MTIME_LENGTH, tarfile);
time = strtol(buffer, NULL, 8);
get_time(time, timestr);
printf("%10s %-17s %8d %16s %s\n", permissions, owner,
(int)size, timestr, path);
fseek(tarfile, -MTIME_OFFSET - MTIME_LENGTH, SEEK_CUR);
} else {
/* standard print */
puts(path);
}
/* go to next header */
if (is_reg(tarfile)) {
blocks = size_to_blocks(size);
fseek(tarfile, blocks * BLOCK_LENGTH + BLOCK_LENGTH, SEEK_CUR);
} else {
/* continue extracting until the prefix of the next file
does not contain this directory's path */
fseek(tarfile, BLOCK_LENGTH, SEEK_CUR);
get_path(buffer, tarfile);
while (strncmp(buffer, path, strlen(path)) == 0) {
list_contents(tarfile, buffer, isverbose, isstrict);
get_path(buffer, tarfile);
if (buffer[0] == '\0')
return;
}
}
}
/* find and list the files matching the given paths */
void find_listings(FILE *tarfile, char *paths[],
int elements, int verbose, int strict) {
char actual_path[PATH_MAX];
int i;
get_path(actual_path, tarfile);
if (elements == 0) {
while (actual_path[0] != '\0') {
list_contents(tarfile, actual_path, verbose, strict);
get_path(actual_path, tarfile);
}
}
while (actual_path[0] != '\0') {
int listed = 0;
for (i = 0; i < elements; i++) {
if (paths[i] == NULL)
continue;
if (strcmp(actual_path, paths[i]) == 0) {
list_contents(tarfile, actual_path, verbose, strict);
listed++;
paths[i] = NULL; /* don't search for this path again */
} else {
/* check if they named a directory without
putting a '/' at the end */
int path_length = (int)strlen(actual_path);
int ends_with_slash = actual_path[path_length - 1] == '/';
int lengths_match = strlen(paths[i]) == path_length - 1;
int paths_match = strncmp(actual_path, paths[i],
path_length - 1) == 0;
if (ends_with_slash && lengths_match && paths_match) {
list_contents(tarfile, actual_path, verbose, strict);
listed++;
paths[i] = NULL; /* don't search for this path again */
}
}
}
if (!listed) {
/* listing moves us along in the tarfile,
but if we don't list, we have to go forward anyway */
off_t size = is_dir(tarfile) ? 0 : get_size(tarfile);
int blocks = size_to_blocks(size);
/* +1 for the header block */
fseek(tarfile, BLOCK_LENGTH * (blocks + 1), SEEK_CUR);
}
get_path(actual_path, tarfile);
}
for (i = 0; i < elements; i++) {
/* print elements failed to list */
if (paths[i] != NULL)
fprintf(stderr, "Could not list: %s\n", paths[i]);
}
}