-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.cpp
73 lines (60 loc) · 1.86 KB
/
snapshot.cpp
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
/********************************************************************************/
/* Name: Priyendu Mori */
/* Roll no: 2018201103 */
/********************************************************************************/
#include "header.h"
#include "macro.h"
ofstream dump;
extern string currentPath,home;
extern ofstream myfile;
extern struct winsize w;
/*
this is a helper function for taking snapshot from a
directory in DFS manner and store it in a dumpfile
*/
void snapshotHelper(string path){
string fullpath;
DIR *dir;
struct stat stat_dir, stat_entry;
struct dirent *entry;
stat(path.c_str(), &stat_dir);
if (S_ISDIR(stat_dir.st_mode) != 0) {
dump<<endl<<path<<": "<<endl;
}
if ((dir = opendir(path.c_str())) == NULL) {
commands(true);
return;
}
while ((entry = readdir(dir)) != NULL) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
dump<<"\t"<<(entry->d_name)<<"\t";
}
dump<<endl;
closedir(dir);
if ((dir = opendir(path.c_str())) == NULL) {
commands(true);
return;
}
while ((entry = readdir(dir)) != NULL) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
fullpath=path;
fullpath.append("/").append(entry->d_name);
stat(fullpath.c_str(), &stat_entry);
if (S_ISDIR(stat_entry.st_mode) != 0) {
snapshotHelper(fullpath);
}
}
closedir(dir);
}
/*
this function dumps DFS snapshot of a
directory(dir) to file(dumpfile)
*/
void getSnapshot(string dir, string dumpfile){
dump.open(dumpfile+".txt");
string fullpath=getWholePath(dir);
snapshotHelper(fullpath);
dump.close();
}