-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand.h
242 lines (226 loc) · 6.87 KB
/
command.h
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h> // Header used for log also.
#include <string.h>
#include <unistd.h> // Header file for Commit command
#include <limits.h> // Header File for commit command "PATH_MAX"#include <dirent.h>
#include <dirent.h> // Header for dirent
#include <sys/types.h> //Header for log file
#include <time.h> // Header for log ctime
// prints the progress bar
void progress()
{
int max = 200000;
long i;
float progress = 0.0;
int c = 0, last_c=20, x = 0;
fprintf(stderr, "%3d%% [", (int)progress);
for(i = 1; i < max+1; i++){
progress = i*100.0/max;
c = (int) progress/3;
// x++;
fprintf(stderr, "\n\033[F");
fprintf(stderr, "%3d%%", (int)progress);
fprintf(stderr, "\033[1C");
fprintf(stderr, "\033[%dC#", last_c);
last_c = c;
}
fprintf(stderr, "]");
}
// Create a new file if already exist give error message.
void create_new_file(char* name[])
{
// Creating a directory
if (mkdir(name[2], 0777) == -1)
printf("\nError\n");
else
{
strcat(name[2], "/temp"); // Created a temp folder inside of the main Repoistry folder.
mkdir(name[2], 0700);
strcat(name[2], "/.count.txt");
FILE * fPtr;
fPtr = fopen(name[2], "w");
fputs("0", fPtr);
fclose(fPtr);
printf("Directory created\n");
}
}
// logs shows all the last active log of a file
void logs()
{
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
struct dirent *de; // Pointer for directory entry
DIR *dr = opendir(cwd); // opendir() returns a pointer of DIR type
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("\nCould not open current Repoistry\n" );
return;
}
while ((de = readdir(dr)) != NULL)
{
if(!strcmp(de->d_name,"temp") || !strcmp(de->d_name,".") || !strcmp(de->d_name,"..") )
{
continue;
}
printf("%s ",de->d_name);
struct stat attr;
stat(de->d_name, &attr);
printf("\nLast modified time: %s\n", ctime(&attr.st_mtime));
}
}
// commit create a new verison in /temp
void commit()
{
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
struct dirent *de;
DIR *dr = opendir(cwd);
if (dr == NULL)
{
printf("\nCould not open current repository\n" );
return;
}
//code to count the verison number
FILE *fr;
int filecount;
fr = fopen("temp/.count.txt", "r");
if (fr == NULL)
{
printf("Error\n");
exit(0);
}
fscanf(fr,"%d",&filecount); //read from file
fclose(fr);
filecount++;
fr = fopen("temp/.count.txt", "w");
fprintf(fr, "%d",filecount); // write to file
fclose(fr);
char tempstr[10];
char source_file[50];
sprintf(tempstr, "%d", filecount); // convert number into string
char target_file[50] = "temp/version";
strcat(target_file,tempstr);
mkdir(target_file,0777);
while ((de = readdir(dr)) != NULL)
{
if(!strcmp(de->d_name,"temp") || !strcmp(de->d_name,".") || !strcmp(de->d_name,"..") )
{
continue;
}
strcpy (source_file, de->d_name);
char sy[] = "cp -r ";
strcat(sy,source_file);
strcat(sy," ");
strcat(sy,target_file);
system(sy);
}
progress(); //prints the progress bar
printf("COMMIT SAVED\n");
closedir(dr);
}
// show the difference from previous version
void diff(char *name[])
{
char previous_file[20] = "temp/version";
char tempstr[20];
FILE *fr;
int filecount;
fr = fopen("temp/.count.txt", "r");
fscanf(fr,"%d",&filecount);
fclose(fr);
sprintf(tempstr, "%d", filecount);
strcat(previous_file,tempstr);
strcat(previous_file,"/");
strcat(previous_file,name[2]);
openfiles( previous_file,name[2]);
}
//retrieve an old version
void reload_util(int v_no)
{
struct dirent *de; // Pointer for directory entry
char buffer[20] = "temp/version";
char strtemp[20];
sprintf(strtemp, "%d", v_no);
strcat(buffer,strtemp);
//printf("%s\n",buffer );
DIR *dr = opendir(buffer); // opendir() returns a pointer of DIR type.
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
exit(0);
}
while ((de = readdir(dr)) != NULL)
{
if(!strcmp(de->d_name,"temp") || !strcmp(de->d_name,".") || !strcmp(de->d_name,".."))
{
continue;
}
char tempstr[30] = "";
strcat(tempstr,buffer);
strcat(tempstr,"/");
strcat(tempstr,de->d_name);
char sy[50] = "cp -r ";
strcat(sy,tempstr);
strcat(sy," ");
strcat(sy,de->d_name);
system(sy);
}
closedir(dr);
}
// reloades the previous version
void reload()
{
printf("Choose a verison nuumber to retrieve\n");
FILE *fr;
int filecount;
fr = fopen("temp/.count.txt", "r");
fscanf(fr,"%d",&filecount); /*read from file*/
fclose(fr);
for(int i = 0; i < filecount; i++)
{
printf(" version%d\n",i+1);
}
int v_no;
printf("> ");
scanf("%d",&v_no);
if(v_no <= filecount && v_no > 0)
{
char temp;
printf("Are you sure to reload the version[Y/N]\n>");
scanf(" %c", &temp);
if(temp == 'y' || temp == 'Y')
{
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
struct dirent *de; // Pointer for directory entry
DIR *dr = opendir(cwd); // opendir() returns a pointer of DIR type.
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("\nCould not open current directory\n" );
return;
}
while ((de = readdir(dr)) != NULL) //Till theres a file in the repository
{
if(!strcmp(de->d_name,"temp") || !strcmp(de->d_name,".") || !strcmp(de->d_name,"..") )
{
continue;
}
char sy[30] = "rm -r ";
strcat(sy,de->d_name);
system(sy);
}
reload_util(v_no);
progress();
printf("\nVersion Reloaded\n");///
}
else
{
printf("\nReloading cancled\n");
}
}
else
{
printf("\nVeriosn Does not exist\n");
}
}