forked from OmerNachshon/VersionControlSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.c
174 lines (161 loc) · 4.59 KB
/
lock.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
#include "lock.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void create_lock(File* file, RevisionEntry* revision, char* user)
{
char* abs_path = file->absolutePath;
char* history_path = create_history_path(abs_path);
char* lock_path = (char*)calloc(sizeof(char), sizeof(char)*strlen(history_path)+6);
strcpy(lock_path, history_path);
char str[] = "/lock";
strcat(lock_path, str);
if(!lock_path)
{
perror("memory error");
}
File* stub = open_file(lock_path, O_CREAT | O_RDWR, 0666);
if(!stub)
{
free(lock_path);
perror("file openning error");
return;
}
char str2[10] = {0};
sprintf(str2, "%f", revision->revision);
write_data(stub, str2);
char space[] = " ";
write_data(stub, space);
write_data(stub, user);
write_data(stub, space);
char time[20];
strftime(time, 20, "%Y-%m-%d %H:%M:%S", localtime(&revision->timestamp));
write_line(stub, time);
free(history_path);
free(lock_path);
close_file(stub);
return;
}
void create_local_key(File* file, RevisionEntry* revision, char* user)
{
char* lock_path = (char*)calloc(sizeof(char), 7*sizeof(char));
char str[] = "./lock";
strcat(lock_path, str);
if(!lock_path)
{
perror("memory error");
}
File* stub = open_file(lock_path, O_CREAT | O_RDWR, 0666);
if(!stub)
{
free(lock_path);
perror("file openning error");
return;
}
char str2[10] = {0};
sprintf(str2, "%f", revision->revision);
write_data(stub, str2);
char space[] = " ";
write_data(stub, space);
write_data(stub, user);
write_data(stub, space);
char time[20];
strftime(time, 20, "%Y-%m-%d %H:%M:%S", localtime(&revision->timestamp));
write_line(stub, time);
free(lock_path);
close_file(stub);
return;
}
int is_locked(File* file)
{
char* abs_path = file->absolutePath;
char* history_path = create_history_path(abs_path);
char* lock_path = (char*)calloc(sizeof(char), sizeof(char)*strlen(history_path)+6);
if(!lock_path)
{
perror("memory error");
}
strcpy(lock_path, history_path);
char str[] = "/lock";
strcat(lock_path, str);
// File* lockFile = open_file(lock_path, O_RDWR , 0666); // needs to be deleted , access below solves the error
// if (!lockFile)
// {
// free(history_path);
// free(lock_path);
// return 0;
// }
if (access(lock_path, F_OK) == 0)
{
free(history_path);
free(lock_path);
return 1;
}
free(lock_path);
free(history_path);
//close_file(lockFile);
return 0;
}
int unlock_file(File* file, char* user)
{
if(!is_locked(file)) return 0;
char* abs_path = file->absolutePath;
char* history_path = create_history_path(abs_path);
char* lock_path = (char*)calloc(sizeof(char), sizeof(char)*strlen(history_path)+6);
if(!lock_path)
{
perror("memory error");
}
strcpy(lock_path, history_path);
char str[] = "/lock";
strcat(lock_path, str);
File* lockFile = open_file(lock_path, O_RDWR , 0666);
if(!lockFile)
{
perror("error open file");
}
Lock lock;
char* line = read_next_line(lockFile);
char* token;
token = strtok(line, " ");
lock.revision = atof(token);
token = strtok(NULL, " ");
lock.user = (char*)calloc(sizeof(char), strlen(token));
strcpy(lock.user, token);
token = strtok(NULL, "\n");
tm tm;
strptime(token, "%Y-%m-%d %H:%M:%S", &tm);
lock.timestamp = mktime(&tm);
printf("lock data: %f, %s, %ld\n", lock.revision, lock.user, lock.timestamp);
char str2[] = "./lock";
File* keyFile = open_file(str2, O_RDWR, 0666);
Lock key;
line = read_next_line(keyFile);
token = strtok(line, " ");
key.revision = atof(token);
token = strtok(NULL, " ");
key.user = (char*)calloc(sizeof(char), strlen(token));
strcpy(key.user, token);
token = strtok(NULL, "\n");
strptime(token, "%Y-%m-%d %H:%M:%S", &tm);
key.timestamp = mktime(&tm);
printf("key data: %f, %s, %ld\n", key.revision, key.user, key.timestamp);
close_file(keyFile);
close_file(lockFile);
free(line);
if ( !(key.revision == lock.revision && !strcmp(key.user, lock.user) && key.timestamp == lock.timestamp) )
{
free(lock_path);
return 0;
}
// files matches
printf("matching lock and key\n");
if( remove(lock_path) == 0 )printf("remove success\n"); else printf("remove failed\n");
if( remove(str2) == 0 )printf("remove success\n"); else printf("remove failed\n");
free(lock_path);
return 1;
}
int has_local_key(File* file)
{
return 0;
}