forked from OmerNachshon/VersionControlSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.c
More file actions
229 lines (217 loc) · 5.23 KB
/
history.c
File metadata and controls
229 lines (217 loc) · 5.23 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
#include "history.h"
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
/*
struct typedef{
char* filename;
char* revision;
time_t timestamp;
}RevisionEntry;
struct typedef{
RevisionHistory* history;
char* file_absolute_path;
}History;
*/
char* get_history_path(char* abs_path)
{
char* history_path = create_history_path(abs_path);
history_path = (char*)realloc(history_path, strlen(history_path) + 9);
char str[] = "/history";
strcat(history_path, str);
return history_path;
}
char* get_path_folder(char* abs_path)
{
return create_history_path(abs_path);
}
char* create_history_path(char* abs_path)
{
// some function to make a unique folder for history by name
// this function takes the file absolute path and make a new path
// therefore a filename can be transformed into the remote path of
// the file's repository
char* abs_path_cpy = (char*)calloc(sizeof(char),strlen(abs_path)*sizeof(char));
if (!abs_path_cpy)
{
perror("memory error create_history_path");
return 0;
}
strcpy(abs_path_cpy, abs_path);
char signs[] = "'.[]{}/\\ -";
char c;
int replaced = 0;
for(int i=0; i< (int)strlen(abs_path_cpy); i++)
{
replaced = 0;
c = abs_path_cpy[i];
for(int j = 0; j < (int)strlen(signs) ; j++)
{
if (c == signs[j])
{
abs_path_cpy[i] = '_';
replaced = 1;
break;
}
}
if (!replaced) // change to lower letter
{
if (c <= 'Z' && c >= 'A') abs_path_cpy[i] = tolower(c);
}
}
char * full_path=(char*)calloc(sizeof(char),(strlen(abs_path_cpy)+strlen(BASE_PATH)));
strcat(full_path,BASE_PATH);
strcat(full_path,abs_path_cpy);
struct stat st = {0};
free(abs_path_cpy);
if(stat(full_path, &st) == -1)
{
printf("creating dir %s\n", full_path);
if (access(full_path, F_OK) != 0)
mkdir(full_path, 0770);
}
return full_path;
}
History* get_history(File* file)
{
int perms = O_CREAT | O_RDWR;
mode_t mode = 0660;
char* history_path = get_history_path(file->absolutePath);
File* historyFile = open_file(history_path, perms, mode);
// if failed open
if (!historyFile)
{
free(history_path);
perror("file error");
return 0;
}
History* history = (History*)malloc(sizeof(History));
history->fileName=NULL;
history->absolutePath=NULL;
history->history=NULL;
if (!history)
{
perror("memory error");
close_file(historyFile);
return 0;
}
history->absolutePath = (char*)malloc(strlen(file->absolutePath)*sizeof(char));
if (!history->absolutePath)
{
perror("memory error");
close_file(historyFile);
free(history);
return 0;
}
history->fileName = (char*)malloc(strlen(file->relativePath)*sizeof(char));
if (!history->fileName)
{
perror("memory error");
free_history(history);
close_file(historyFile);
return 0;
}
strcpy(history->absolutePath, file->absolutePath);
strcpy(history->fileName, file->relativePath);
history->history = (RevisionEntry**)malloc(0);
history->totalEntries = 0;
if (historyFile->fileSize <= 1)
{
// set file initital data
write_line(historyFile, file->absolutePath);
}
else
{
char* line = read_next_line(historyFile);
char *token;
while(!is_eof(historyFile))
{
line = read_next_line(historyFile);
history->history = (RevisionEntry**)realloc(history->history, (history->totalEntries + 1)*sizeof(RevisionEntry*));
RevisionEntry* entry = (RevisionEntry*)malloc(sizeof(RevisionEntry));
if(!entry)
{
free(line);
free_history(history);
close_file(historyFile);
return NULL;
}
token = strtok(line, " ");
entry->revision = atof(token);
token = strtok(NULL, "\n");
tm tm;
strptime(token, "%Y-%m-%d %H:%M:%S", &tm);
entry->timestamp = mktime(&tm);
history->history[history->totalEntries++] = entry;
}
free(line);
}
free(history_path);
close_file(historyFile);
return history;
}
RevisionEntry* get_revision(History* history, time_t timestamp)
{
for(int i =0;i<history->totalEntries; i++)
{
if ( timestamp == history->history[i]->timestamp)
{
return history->history[i];
}
}
return 0;
}
RevisionEntry* get_last_revision(History* history)
{
if(!history->totalEntries)return NULL;
return history->history[history->totalEntries-1];
}
int add_revision_entry(History* history)
{
double val;
RevisionEntry* entry = get_last_revision(history);
if(entry)
{
val = entry->revision + 1;
}
else
{
val = 1;
}
char* str = (char*)malloc(sizeof(char)*6);
sprintf(str, "%f", val);
time_t t;
t = time(NULL);
char* historyFilePath = get_history_path(history->absolutePath);
File* historyFile = open_file(historyFilePath, O_RDWR | O_CREAT, 0666);
// create line
struct tm *tm = localtime(&t);
char str2[20] = {0};
strftime(str2, sizeof(str2), "%Y-%m-%d %H:%M:%S", tm);
seek_file(historyFile,0,2);
write_data(historyFile, str);
char space[] = " ";
write_data(historyFile, space);
write_line(historyFile, str2);
return 1;
}
void free_history(History* history)
{
if(!history)return;
if(history->history)
{
for(int i = 0; i<history->totalEntries; i++)
{
free(history->history[i]);
}
}
if(history->fileName)
{
free(history->fileName);
}
if(history->absolutePath)
{
free(history->absolutePath);
}
}