-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
468 lines (356 loc) · 10.5 KB
/
Copy pathutil.c
File metadata and controls
468 lines (356 loc) · 10.5 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "util.h"
#include "compressor.h"
void computeFileHash(char *filename, unsigned char hash[HASH_STRING_LEN]) {
int i;
FILE *inFile = fopen (filename, "rb");
MD5_CTX mdContext;
int bytes;
unsigned char data[1024];
if (inFile == NULL) {
printf ("%s can't be opened.\n", filename);
return;
}
// read file in chunks
MD5_Init (&mdContext);
while ((bytes = fread (data, 1, 1024, inFile)) != 0)
MD5_Update (&mdContext, data, bytes);
// convert the hex
unsigned char tmp[MD5_DIGEST_LENGTH];
MD5_Final (tmp, &mdContext);
// one by one convert character to hex chars.
for(i=0; i<MD5_DIGEST_LENGTH; i++) {
sprintf(hash + 2*i, "%02x", tmp[i]);
}
fclose (inFile);
}
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
return te.tv_sec; // Only consider seconds, no millis
}
long long current_timestamp_millis() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
return te.tv_sec * 1000 + te.tv_usec / 1000; // millis
}
long long getLastModifiedTime(char *filePath) {
struct stat statinfo;
stat(filePath, &statinfo);
// Elapsed seconds since EPOCH till now.
long long value = statinfo.st_mtime;
return value;
}
int checkDirectoryExists(char *dirName) {
struct stat s = {0};
if (!stat(dirName, &s)) {
return (s.st_mode & S_IFDIR);
} else {
return 0;
}
}
int checkFileExists(char *fileName) {
struct stat buffer;
return (stat(fileName, &buffer) == 0);
}
void createDirectory(char *dirName) {
struct stat s = {0};
if (stat(dirName, &s) == -1) {
mkdir(dirName, 0777);
}
}
long int findFileSize(char *fileName) {
struct stat buffer;
int status = stat(fileName, &buffer);
// if permission available
if(status == 0) {
return buffer.st_size;
}
return -1;
}
// Returns dynamically created string, delete it Yourself.
char *readFileContents(char *fileName) {
if(!checkFileExists(fileName)) {
printf("Unable to read file contents. File %s does not exist.", fileName);
return NULL;
}
char *result = malloc(sizeof(char) * (findFileSize(fileName) + 10));
// Now write file char by char on the socket
int fileFd = open(fileName, O_RDONLY, 0777);
int i = 0;
while (read(fileFd, &result[i], 1) == 1) {
i++;
}
close(fileFd);
result[i] = '\0';
return result;
}
// Open system call fails for nester directories before file.
// So for a/b/c/d.txt, It create a, b & c directories.
void createDirStructureIfNeeded(char *path) {
char *x = strchr(path, '/');
int l = strlen(path);
char dir[l + 1];
while(x != NULL) {
strcpy(dir, path);
int i = (int)(x - path);
dir[i] = '\0';
if(!checkDirectoryExists(dir)) {
createDirectory(dir);
}
x = strchr(x + 1, '/');
}
}
/*
Manifest file format:
<project Name>
<project Version>
<numFiles>
<md5hash><space><version><space><file path>
<md5hash><space><version><space><file path>
<md5hash><space><version><space><file path>
..
The filePaths are relative to the manifest file.
*/
void writeFileDetailsInManifest(int manifestFd, char *filePath) {
unsigned char fileHash[HASH_STRING_LEN];
computeFileHash(filePath, fileHash);
write(manifestFd, fileHash, HASH_STRING_LEN);
write(manifestFd, " ", 1);
char timeStamp[20];
sprintf(timeStamp, "%lld", getLastModifiedTime(filePath));
write(manifestFd, timeStamp, strlen(timeStamp));
write(manifestFd, " ", 1);
write(manifestFd, filePath, strlen(filePath));
write(manifestFd, "\n", 1);
}
/*
This is helper method to write the contents to the file, which are sent
back from server.
Format:
<FileNameLen>:<FileName><FileLenBytes>:<FileContents>
Precondition: This method is called once we are sure that server is going to supply the contents.
*/
void writeFileFromSocket(int sockToRead, char *baseDir) {
SocketBuffer *socketBuffer = createBuffer();
readTillDelimiter(socketBuffer, sockToRead, ':');
char *nameLenStr = readAllBuffer(socketBuffer);
long nameLen = atol(nameLenStr);
readNBytes(socketBuffer, sockToRead, nameLen);
char *filePath = readAllBuffer(socketBuffer);
readTillDelimiter(socketBuffer, sockToRead, ':');
char *contentLenStr = readAllBuffer(socketBuffer);
long contentLen = atol(contentLenStr);
char *fullpath = malloc(sizeof(char) * (strlen(filePath) + 15 + strlen(baseDir)));
sprintf(fullpath, "%s/%s", baseDir, filePath);
// Create the directory structure if needed.
createDirStructureIfNeeded(fullpath);
// Write data to the file now.
int fd = open(fullpath, O_CREAT | O_WRONLY | O_TRUNC, 0777);
char c;
long i = 0;
while(i++ < contentLen) {
read(sockToRead, &c, 1);
if(c == '\0') {
break; // Socket disconnected.
}
write(fd, &c, 1);
}
close(fd);
// de-allocate memory
freeSocketBuffer(socketBuffer);
free(nameLenStr);
free(filePath);
free(contentLenStr);
free(fullpath);
}
/*
<File1NameLen>:<File1Name><File1LenBytes>:<File1Contents>
*/
void writeFileDetailsToSocket(char *filePath, char *baseDir, int socket) {
char buffer[100];
char *path = malloc(sizeof(char) * (strlen(filePath) + strlen(baseDir) + 25));
sprintf(path, "%s/%s", baseDir, filePath);
sprintf(buffer, "%d:", strlen(filePath));
write(socket, buffer, strlen(buffer));
write(socket, filePath, strlen(filePath));
long size = findFileSize(path);
sprintf(buffer, "%ld:", size);
write(socket, buffer, strlen(buffer));
// write size bytes from file to socket.
int fd = open(path, O_RDONLY, 0777);
char c;
long i = 0;
while(i++ < size) {
read(fd, &c, 1);
if(c == '\0') {
break; // Socket disconnected.
}
write(socket, &c, 1);
}
close(fd);
free(path);
}
int removeDirectoryCompletely(char *path) {
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;
if (d) {
struct dirent *p;
r = 0;
while (!r && (p=readdir(d))) {
int r2 = -1;
char *buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) {
continue;
}
len = path_len + strlen(p->d_name) + 2;
buf = malloc(len);
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf)) {
if (S_ISDIR(statbuf.st_mode)) {
r2 = removeDirectoryCompletely(buf);
} else {
r2 = unlink(buf);
}
} else {
printf("Stat error while deleting complete folder: %s\n", buf);
fflush(stdout);
}
free(buf);
r = r2;
}
} else {
printf("Could not open dir while deleting complete folder: %s.\n", path);
fflush(stdout);
}
closedir(d);
if (!r) {
r = rmdir(path);
}
return r;
}
/*
This function searches for any file
*/
int checkForFileMatch(char *filePath, char *dirToSearch, char *prefix) {
char buffer1[100], buffer2[100];
char *path = malloc(sizeof(char) * (strlen(dirToSearch) + 25));
computeFileHash(filePath, buffer1);
buffer1[HASH_STRING_LEN] = '\0';
DIR *d;
struct dirent *dir;
d = opendir(dirToSearch);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
char *fName = dir->d_name;
sprintf(path, "%s/%s", dirToSearch, fName);
// if file starts with prefix.
if(strstr(fName, prefix) == fName) {
computeFileHash(path, buffer2);
buffer2[HASH_STRING_LEN] = '\0';
// same file
if(strcmp(buffer1, buffer2) == 0) {
return 1;
}
}
}
}
}
closedir(d);
free(path);
return 0;
}
void deleteFilesWithPrefix(char *dirToSearch, char *prefix) {
char *path = malloc(sizeof(char) * (strlen(dirToSearch) + 25));
DIR *d;
struct dirent *dir;
d = opendir(dirToSearch);
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
char *fName = dir->d_name;
// if file starts with prefix.
if(strstr(fName, prefix) == fName) {
sprintf(path, "%s/%s", dirToSearch, fName);
unlink(path);
}
}
}
}
closedir(d);
free(path);
}
void copyFile(char *srcFilePath, char *destFilePath) {
createDirStructureIfNeeded(destFilePath);
int src_fd = open(srcFilePath, O_RDONLY);
int dst_fd = open(destFilePath, O_CREAT | O_WRONLY | O_TRUNC, 0777);
char buffer[4096];
while (1) {
int n = read(src_fd, buffer, 4096);
if (n > 0) {
write(dst_fd, buffer, n);
}
if (n == 0) break;
}
close(src_fd);
close(dst_fd);
}
void writeNBytesToFile(long nBytes, int sockToRead, int sockToWrite) {
while(nBytes-- > 0) {
char c;
read(sockToRead, &c, 1);
write(sockToWrite, &c, 1);
}
}
// This function writes the response after decompressing it
// Server sends the compressed zlib response from socket.
// Client passes the path of file on which the unencrypted data
// should be written.
//
// The server Format is below:
// numBytes:<content>
//
// Error checking is done before calling this function
void convertZlibToResponse(int sockFd, char *responseFile, char *baseDir) {
SocketBuffer *socketBuffer = createBuffer();
readTillDelimiter(socketBuffer, sockFd, ':');
char *numBytesStr = readAllBuffer(socketBuffer);
long numBytes = atol(numBytesStr);
printf("Reading %ld bytes from socket\n", numBytes); fflush(stdout);
char path[100];
sprintf(path, "%s/tmp_res%lld_%d", baseDir, current_timestamp(), rand());
createDirStructureIfNeeded(path);
int writeFd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0777);
// first right encrypted data to a temp file.
writeNBytesToFile(numBytes, sockFd, writeFd);
close(writeFd);
// now unecrypt data from this file, and write to response file.
decompressFile(path, responseFile);
// delete temp file.
unlink(path);
free(numBytesStr);
freeSocketBuffer(socketBuffer);
}
// This function reads the response file
// And writes to socket in below format:
// <contentLen>:<compressed data>
void convertResponseToZlib(int sockFd, char *responseFile, char *baseDir) {
char *path = malloc(sizeof(char) * (strlen(baseDir) + 50));
// convert response file to zlib compressed.
char buffer[100];
sprintf(path, "%s/tmp_res%lld_%d", baseDir, current_timestamp_millis(), rand());
compressFile(responseFile, path);
long numBytes = findFileSize(path);
int readFd = open(path, O_RDONLY, 0777);
sprintf(buffer, "%ld:", numBytes);
write(sockFd, buffer, strlen(buffer));
// now write compressed data to socket.
writeNBytesToFile(numBytes, readFd, sockFd);
close(readFd);
unlink(path);
free(path);
}