-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientcommands.c
More file actions
1569 lines (1185 loc) · 49.6 KB
/
Copy pathclientcommands.c
File metadata and controls
1569 lines (1185 loc) · 49.6 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <errno.h>
#include "clientcommands.h"
//invalid=0, configure=1, checkout=2, update=3, upgrade=4, commit=5, push=6, create=7, destroy=8, add=9, remove_cmnd=10, currentversion=11, history=12, rollback=13
// Will load connection details from a .Config file on local path and attempt to connect with them, returns file descriptor of connection or -1 if it failed
int connectwithconfig() {
Configuration* config = loadConfig();
if (config == NULL) return -1;
int sockfd = connecttohost(config->host, config->port);
freeConfig(config);
return sockfd;
}
int getUID() {
FileContents* config = readfile(".config");
if (!config) {
printf("Error: run config before attempting to commit\n");
return -2;
}
char* content = malloc(config->size + 8);
memset(content, '\0', config->size + 8);
memcpy(content, config->content, config->size);
char* uidline = strstr(config->content, "uid");
if (!uidline) {
memset(content, '\0', config->size + 8);
memcpy(content, "uid:-1\n",8);
write(config->fd, content, 6);
free(content);
freefile(config);
return -1;
}
free(content);
strtok(config->content, "\n");
strtok(NULL, "\n");
strtok(NULL, ":");
char* uid = strtok(NULL, "\n");
if (isNum(uid, strlen(uid))) {
int ret = atoi(uid);
freefile(config);
return ret;
} else return -1;
}
// Initialize and allocate a new NetworkCommand from a ClientCommand to be sent to the server to request some data
NetworkCommand* newrequest(ClientCommand* command, int argc) {
NetworkCommand* request = malloc(sizeof(NetworkCommand));
request->argc = argc;
request->arglengths = malloc(sizeof(int) * argc);
request->argv = malloc(sizeof(char*) * argc);
int i = 0;
for (i = 0; i < argc; i++) {
request->arglengths[i] = strlen(command->args[i]);
request->argv[i] = malloc(request->arglengths[i] + 1);
memset(request->argv[i], '\0', request->arglengths[i] + 1);
memcpy(request->argv[i], command->args[i], request->arglengths[i]);
}
return request;
}
NetworkCommand* newfilerequest(char* project, char** filepaths, int filecount) {
NetworkCommand* request = malloc(sizeof(NetworkCommand));
request->type = filenet;
request->argc = 2 + filecount;
request->arglengths = malloc(sizeof(int) * request->argc);
request->argv = malloc(sizeof(char*) * request->argc);
request->arglengths[0] = strlen(project);
request->argv[0] = malloc(request->arglengths[0] + 1);
memset(request->argv[0], '\0', request->arglengths[0]);
memcpy(request->argv[0], project, request->arglengths[0]);
int arg1len = digitCount(filecount) + 1;
request->argv[1] = malloc(arg1len);
sprintf(request->argv[1], "%d", filecount);
request->arglengths[1] = arg1len - 1;
int i = 0;
for (i = 0; i < filecount; i++) {
request->arglengths[i + 2] = strlen(filepaths[i]);
request->argv[i+2] = malloc(request->arglengths[i+2] + 1);
memset(request->argv[i + 2], '\0', request->arglengths[i+2] + 1);
memcpy(request->argv[i + 2], filepaths[i], request->arglengths[i+2]);
}
return request;
}
NetworkCommand* newDataTransferCmnd(char* project, char* datacontent, int length) {
NetworkCommand* toSend = malloc(sizeof(NetworkCommand));
toSend->type = data;
toSend->argc = 2;
toSend->arglengths = malloc(sizeof(int) * 2);
toSend->argv = malloc(sizeof(char*) * 2);
toSend->arglengths[0] = strlen(project);
toSend->argv[0] = malloc(toSend->arglengths[0] + 1);
memset(toSend->argv[0], '\0', toSend->arglengths[0]);
memcpy(toSend->argv[0], project, toSend->arglengths[0]);
toSend->arglengths[1] = length;
toSend->argv[1] = malloc(length + 1);
memset(toSend->argv[1], '\0', toSend->arglengths[1]);
memcpy(toSend->argv[1], datacontent, toSend->arglengths[1]);
return toSend;
}
int checkresponse(char* command, NetworkCommand* response) {
if (response->argc != 3 || strcmp(response->argv[0], command) != 0) { // valid responses to the create request will contain 3 arguments
printf("Error: Malformed response from server\n");
freeCMND(response);
return -1;
}
if (strcmp(response->argv[1], "failure") == 0) { // if the request failed, tell the user
printf("Error: server failed to execute command: %s\n", response->argv[2]); // argument 3 will contain the reason for the request failing
freeCMND(response);
return -1;
}
return 0;
}
// Print out reason that command was invalid
int _invalidcommand(ClientCommand* command) {
printf("Fatal Error: %s\n", command->args[0]);
return 0;
}
int _configure(ClientCommand* command) {
// Takes in 2 argument <ip/host> and <port>
// ip/host must be at most HOST_NAME_MAX bytes long
// port must be a positive integer no longer than 16 bits, 65535
// a .Config file will be created in the cwd and have both of these values written to it
if (strlen(command->args[0]) > HOST_NAME_MAX + 1){
printf("Error: Invalid host name length, max hostname on this system is %d\n", HOST_NAME_MAX);
return -1;
}
if (strlen(command->args[1]) > 5){
printf("Error: Invalid port length, max port number is 16 bit\n");
return -1;
}
if (!isNum(command->args[1], strlen(command->args[1]))) {
printf("Error: Invalid port, value must be a max 16 bit integer\n");
return -1;
}
remove("./.config");
int fd_config = open("./.config", O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
if (fd_config == -1) {
printf("Error: couldn't create .config file\n");
return -1;
}
int uid = getUID();
int size = strlen(command->args[0]) + strlen(command->args[1]) + 13 + 4 + digitCount(uid);
if (uid < 0) size += 1;
char* filecontents = malloc(sizeof(char) * (size));
memset(filecontents, '\0', size);
snprintf(filecontents, size, "host:%s\nport:%s\nuid:%d", command->args[0], command->args[1],uid);
if (write(fd_config, filecontents, size - 1 ) == -1) {
printf("Error: couldn't write to .config file\n");
return -1;
}
free(filecontents);
close(fd_config);
printf("Successfully saved configuration:\n%s\n%s\n", command->args[0], command->args[1]);
return 0;
}
int _checkout(ClientCommand* command) {
char** files = malloc(sizeof(char*));
files[0] = ".";
NetworkCommand* request = newfilerequest(command->args[0], files, 1);
int sockfd = connectwithconfig();
if (sockfd < 0) {
freeCMND(request);
return sockfd;
}
sendNetworkCommand(request, sockfd);
freeCMND(request);
NetworkCommand* response = readMessage(sockfd);
close(sockfd);
if (checkresponse("file", response) < 0) return -1; // check the response if valid
recreatefile("archive.tar.gz", response->argv[2], response->arglengths[2]);
uncompressfile("archive.tar.gz");
printf("%d compressed bytes recieved\nProject '%s' has been checked out\n", response->arglengths[2], command->args[0]);
freeCMND(response);
return -1;
}
int _update(ClientCommand* command) {
if (!checkForLocalProj(command->args[0])) {
printf("Error: Local project does not exist. Checkout project from server or create a new one.\n");
return -1;
}
int projlen = strlen(command->args[0]);
char* localmanifestpath = malloc(11 + projlen);
memset(localmanifestpath, '\0', 11 + projlen);
sprintf(localmanifestpath, "%s/.Manifest", command->args[0]);
int fd = open(localmanifestpath, O_RDONLY);
if (fd < 0) {
printf("Error: Project does not contain a Manifest\n");
free(localmanifestpath);
return -1;
}
free(localmanifestpath);
close(fd);
// char** requestArgs = malloc(sizeof(char*) * 2);
// requestArgs[0] = malloc(10);
// memcpy(requestArgs[0], ".Manifest", 10);
char* file = ".Manifest";
NetworkCommand* request = newfilerequest(command->args[0], &file, 1);
request->type = updatenet;
int sockfd = connectwithconfig();
if (sockfd < 0) return sockfd;
sendNetworkCommand(request, sockfd);
NetworkCommand* response = readMessage(sockfd);
freeCMND(request);
if (checkresponse("file", response) < 0) return -1;
Update* update = createupdate(response->argv[2], response->arglengths[2], command->args[0], projlen); // Returns NULL if there was a conflict
freeCMND(response);
if (!update) {
printf("Error: couldn't prepare update, conflicts exist. resolve the conflicts and try again.\n");
return -1;
}
if (update->entries == 0) return 0;
printf("Successfuly prepared update with '%d' changes\n", update->entries);
return 0;
}
int _upgrade(ClientCommand* command) {
// proj dont exist
// server no contact
// yes .Conflict
// no .Commit
char* project = command->args[0];
int projlen = strlen(project);
if(!checkForLocalProj(project)) return -1;
char* conflictpath = malloc(projlen + 11);
memset(conflictpath, '\0', projlen + 11);
sprintf(conflictpath, "%s/.Conflict", project);
char* updatepath = malloc(projlen + 9);
memset(updatepath, '\0', projlen + 9);
sprintf(updatepath, "%s/.Update", project);
int fd = open(conflictpath, O_RDONLY);
if (fd > 0) {
printf("Error: Conflicts in project '%s' exist. Resolve conflict before attempting to upgrade.\n", command->args[0]);
free(conflictpath);
close(fd);
return -1;
}
close(fd);
free(conflictpath);
fd = open(updatepath, O_RDONLY);
if (fd < 0) {
printf("Error: Project '%s' has no pending updates. Update before attempting to upgrade.\n", command->args[0]);
free(updatepath);
return -1;
}
close(fd);
FileContents* file = readfile(updatepath);
Commit* update = parseCommit(file);
freefile(file);
char** files = getCommitFilePaths(update);
char** hashes = getCommitHashes(update);
int* versions = getCommitVersions(update);
ModTag* tags = getModificationTags(update);
int sockfd = connectwithconfig();
NetworkCommand* requestversion = malloc(sizeof(NetworkCommand));
if (!requestversion) {
printf("no alloc %s", strerror(errno));
return -1;
}
requestversion->type = upgradenet;
requestversion->argc = 1;
requestversion->argv = malloc(sizeof(char*));
requestversion->arglengths = malloc(sizeof(int));
requestversion->arglengths[0] = strlen(project);
requestversion->argv[0] = malloc(requestversion->arglengths[0] + 1);
memset(requestversion->argv[0], '\0', requestversion->arglengths[0] + 1);
memcpy(requestversion->argv[0], project, requestversion->arglengths[0]);
sendNetworkCommand(requestversion, sockfd);
// Recieve files from server
NetworkCommand* responseversion = readMessage(sockfd);
if(checkresponse("upgrade", responseversion) < 0) return -1;
char* manifestpath = malloc(projlen + 11);
memset(manifestpath, '\0', projlen + 11);
sprintf(manifestpath, "%s/.Manifest", project);
FileContents* manifestfile = readfile(manifestpath);
Manifest* manifest = parseManifest(manifestfile);
freefile(manifestfile);
char** fileslocal = getManifestFiles(manifest);
char* tempmanifestpath = malloc(projlen + 16);
memset(tempmanifestpath, '\0', projlen + 16);
sprintf(tempmanifestpath, "%s/.temp.Manifest", project);
fd = open(tempmanifestpath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
write(fd, responseversion->argv[2], responseversion->arglengths[2]);
write(fd, "\n", 1);
int i = 0;
int j = 0;
for (i = 0; i < manifest->entrycount; i++) {
int checked = 0;
for (j = 0; j < update->entries; j++) {
if (strcmp(fileslocal[i], files[j]) == 0) {
checked = 1;
if (tags[j] == Delete) {
break; // skip, do not add to new .Manifest
}
// Else its modify, replace manifest entry with update entry
int entrylen = digitCount(versions[j]) + 1 + strlen(files[j]) + 1 + 40 + 2;
char entry[entrylen];
snprintf(entry, entrylen, "%d %s %s\n", versions[j], files[j], hashes[j]);
write(fd, entry, entrylen - 1);
}
}
if (!checked) {
// file exists in manifest, not in .Update (local add)
write(fd, manifest->entries[i], strlen(manifest->entries[i]) - 1);
write(fd, "\n", 1);
}
}
for (i = 0; i < update->entries; i++) {
if (tags[i] != Add) continue;
int entrylen = digitCount(versions[i]) + 1 + strlen(files[i]) + 1 + 40 + 2;
char entry[entrylen];
snprintf(entry, entrylen, "%d %s %s\n", versions[i], files[i], hashes[i]);
write(fd, entry, entrylen - 1);
}
close(fd);
int filelen = 1;
for(i = 0; i < update->entries; i++) {
filelen += strlen(files[i]) + 1;
}
int notdeleted = 0;
char** stripedfiles = malloc(filelen * sizeof(char*));
for(i = 0; i < update->entries; i++) {
if (tags[i] == Delete) continue;
strtok(files[i], "/");
char* entry = strtok(NULL, "\0");
stripedfiles[notdeleted] = malloc(strlen(entry) + 1);
memset(stripedfiles[notdeleted], '\0', strlen(entry) + 1);
memcpy(stripedfiles[notdeleted], entry, strlen(entry));
notdeleted++;
}
// Replace files in local repo
if (notdeleted != 0) {
NetworkCommand* request = newfilerequest(project, stripedfiles, notdeleted);
request->type = upgradenet;
freeCommit(update);
sendNetworkCommand(request, sockfd);
// Recieve files from server
NetworkCommand* response = readMessage(sockfd);
if(checkresponse("file", response) < 0) return -1;
recreatefile("archive.tar.gz", response->argv[2], response->arglengths[2]);
uncompressfile("archive.tar.gz");
} else {
NetworkCommand* cancel = newFailureCMND("upgrade", "no files");
sendNetworkCommand(cancel, sockfd);
}
remove(manifestpath);
rename(tempmanifestpath, manifestpath);
remove(updatepath);
printf("Local repo upgraded successfully\n");
return -1;
}
Update* createupdate(char* remoteManifest, int remotelen, char* project, int projlen) {
int checkfolder = 0;
DIR* dir = NULL;
if (!(dir = opendir(".tempfiles"))) checkfolder = mkdir(".tempfiles", 0700);
else closedir(dir);
if (checkfolder != 0) return NULL;
chdir(".tempfiles");
recreatefile("archive.tar.gz", remoteManifest, remotelen);
uncompressfile("archive.tar.gz");
chdir("..");
char* servermanifestpath = malloc(23 + projlen);
memset(servermanifestpath, '\0', 23 + projlen);
sprintf(servermanifestpath, ".tempfiles/%s/.Manifest", project);
FileContents* servermanifest = readfile(servermanifestpath);
free(servermanifestpath);
close(servermanifest->fd);
system("rm -rf .tempfiles > /dev/null 2>&1");
char* localmanifestpath = malloc(11 + projlen);
memset(localmanifestpath, '\0', 11 + projlen);
sprintf(localmanifestpath, "%s/.Manifest", project);
FileContents* localmanifestfile = readfile(localmanifestpath);
free(localmanifestpath);
if (getManifestVersion(servermanifest) == getManifestVersion(localmanifestfile)) {
printf("Local project is up to date!\n");
freefile(localmanifestfile);
freefile(servermanifest);
Update* update = malloc(sizeof(Update));
update->uptodate = 1;
return update;
}
Manifest* localmanifest = parseManifest(localmanifestfile);
char** files = getManifestFiles(localmanifest);
char** hashcodes = getManifestHashcodes(localmanifest);
int* fileversions = getManifestFileVersion(localmanifest);
Manifest* remotemanifest = parseManifest(servermanifest);
char** remotefiles = getManifestFiles(remotemanifest);
char** remotehashcodes = getManifestHashcodes(remotemanifest);
int* remotefileversions = getManifestFileVersion(remotemanifest);
Update* update = malloc(sizeof(Update));
update->uptodate = 0;
update->entries = 0;
update->uid = -1;
if (localmanifest->entrycount == 0 && remotemanifest->entrycount == 0) {
printf("Local project is up to date!\n");
freeManifest(localmanifest);
freeManifest(remotemanifest);
freefile(localmanifestfile);
freefile(servermanifest);
return update;
}
update->filecontent = malloc(localmanifestfile->size + servermanifest->size + 1);
memset(update->filecontent, '\0', localmanifestfile->size + servermanifest->size+ 1);
update->filesize = 0;
freefile(localmanifestfile);
freefile(servermanifest);
/*
Modify Case:
client hash != remote hash
client hash == live hash
Add Case:
exists on remote
doesn't exist on local
Delete Case:
exists on local
doesn't exist on remote
*/
char* updatepath = malloc(11 + projlen);
memset(updatepath, '\0', 11 + projlen);
sprintf(updatepath, "%s/.Update", project);
char* conflictpath = malloc(11 + projlen);
memset(conflictpath, '\0', 11 + projlen);
sprintf(conflictpath, "%s/.Conflict", project);
int update_fd = open(updatepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
int conflict_fd = open(conflictpath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
int conflict = 0;
int i = 0,j = 0;
for ( i = 0; i < localmanifest->entrycount; i++) {
int check = 0;
for (j = 0; j < remotemanifest->entrycount; j++) {
if (strcmp(files[i], remotefiles[j]) != 0) continue;
check = 1;
if (strcmp(hashcodes[i], remotehashcodes[j]) != 0) {
FileContents* livecontent = readfile(files[i]);
unsigned char* livehashcode = hashdata((unsigned char*) livecontent->content, livecontent->size);
char* encodedlivehash = hashtohex(livehashcode);
free(livehashcode);
if (strcmp(hashcodes[i], encodedlivehash) == 0) {
// if (conflict) break;
// Modify Case
int entrylen = 2 + digitCount(fileversions[i]) + 1 + strlen(files[i]) + 1 + 40 + 2;
char entry[entrylen];
sprintf(entry, "M %d %s %s\n", fileversions[i], files[i], hashcodes[i]);
write(update_fd, entry, entrylen - 1);
printf("M %s\n", files[i]);
update->entries++;
update->filesize += entrylen - 1;
} else {
// Conflict Case
conflict = 1;
int entrylen = 2 + digitCount(fileversions[i]) + 1 + strlen(files[i]) + 1 + 40 + 2;
char entry[entrylen];
sprintf(entry, "C %d %s %s\n", fileversions[i], files[i], hashcodes[i]);
write(conflict_fd, entry, entrylen - 1);
printf("C %s\n", files[i]);
}
free(encodedlivehash);
}
break;
}
if (!check) {
// Delete Case
int entrylen = 2 + digitCount(fileversions[i]) + 1 + strlen(files[i]) + 1 + 40 + 2;
char entry[entrylen];
sprintf(entry, "D %d %s %s\n", fileversions[i], files[i], hashcodes[i]);
write(update_fd, entry, entrylen - 1);
printf("D %s\n", files[i]);
update->entries++;
update->filesize += entrylen - 1;
}
}
for ( i = 0; i < remotemanifest->entrycount; i++) {
// if (conflict) break;
int check = 0;
for (j = 0; j < localmanifest->entrycount; j++) {
if (strcmp(files[j], remotefiles[i]) != 0) continue;
check = 1;
break;
}
if (!check) {
// Add Case
int entrylen = 2 + digitCount(remotefileversions[i]) + 1 + strlen(remotefiles[i]) + 1 + 40 + 2;
char entry[entrylen];
sprintf(entry, "A %d %s %s\n", remotefileversions[i], remotefiles[i], remotehashcodes[i]);
write(update_fd, entry, entrylen - 1);
printf("A %s\n", remotefiles[i]);
update->entries++;
update->filesize += entrylen - 1;
}
}
update->filecontent = malloc(update->filesize);
read(update_fd, update->filecontent, update->filesize);
if (conflict) remove(updatepath);
else remove(conflictpath);
if (update->entries == 0) remove(updatepath);
if (conflict) return NULL;
return update;
}
int _commit(ClientCommand* command) {
if (!checkForLocalProj(command->args[0])) {
printf("Error: Local project does not exist. Checkout project from server or create a new one.\n");
return -1;
}
int projlen = strlen(command->args[0]);
char* conflictpath = malloc(projlen + 11);
memset(conflictpath, '\0', projlen + 11);
sprintf(conflictpath, "%s/.Conflict", command->args[0]);
int fd = open(conflictpath, O_RDONLY);
if (fd > 0) {
printf("Error: Conflicts in project '%s' exist. Resolve conflict before attempting to commit.\n", command->args[0]);
free(conflictpath);
close(fd);
return -1;
}
close(fd);
free(conflictpath);
char* updatepath = malloc(projlen + 9);
memset(updatepath, '\0', projlen + 9);
sprintf(updatepath, "%s/.Update", command->args[0]);
fd = open(updatepath, O_RDONLY);
if (fd > 0) {
int size = lseek(fd, 0, SEEK_END);
close(fd);
if (size != 0) {
printf("Error: Project '%s' has pending updates. Upgrade before attempting to commit.\n", command->args[0]);
free(updatepath);
return -1;
} else remove(updatepath);
}
close(fd);
free(updatepath);
char* localmanifestpath = malloc(11 + projlen);
memset(localmanifestpath, '\0', 11 + projlen);
sprintf(localmanifestpath, "%s/.Manifest", command->args[0]);
fd = open(localmanifestpath, O_RDONLY);
if (fd < 0) {
printf("Error: Project does not contain a Manifest\n");
free(localmanifestpath);
return -1;
}
free(localmanifestpath);
close(fd);
char** requestArgs = malloc(sizeof(char*) * 2);
requestArgs[0] = malloc(10);
memcpy(requestArgs[0], ".Manifest", 10);
int uid = getUID();
if (uid == -2) {
free(requestArgs);
return -1;
}
requestArgs[1] = uid == -1 ? malloc(3) : malloc(digitCount(uid) + 1);
sprintf(requestArgs[1], "%d", uid);
NetworkCommand* request = newfilerequest(command->args[0], requestArgs, 2);
request->type = commitnet; // modifying a file request into a commit request
memset(request->argv[1], '\0', request->arglengths[1]);
memcpy(request->argv[1], "1", 2);
free(requestArgs[0]);
free(requestArgs[1]);
free(requestArgs);
int sockfd = connectwithconfig();
if (sockfd < 0) return sockfd;
sendNetworkCommand(request, sockfd); // TODO change for commit
NetworkCommand* response = readMessage(sockfd);
freeCMND(request);
if (checkresponse("file", response) < 0) return -1;
Commit* commit = createcommit(response->argv[2], response->arglengths[2], command->args[0], projlen);
freeCMND(response);
if(commit == NULL) {
printf("Error: couldn't create commit\n");
char* name = malloc(7);
memset(name, '\0', 7);
memcpy(name, "commit", 7);
char* reason = malloc(23);
memset(reason, '\0', 23);
memcpy(reason, "couldn't create commit", 23);
NetworkCommand* cancel = newFailureCMND(name, reason);
sendNetworkCommand(cancel, sockfd);
freeCMND(cancel);
return -1;
}
if (commit->entries == 0) {
freeCommit(commit);
char* name = malloc(7);
memset(name, '\0', 7);
memcpy(name, "commit", 7);
char* reason = malloc(21);
memset(reason, '\0', 21);
memcpy(reason, "no changes to commit", 21);
NetworkCommand* cancel = newFailureCMND(name, reason);
sendNetworkCommand(cancel, sockfd);
freeCMND(cancel);
return -1;
}
NetworkCommand* toSend = newDataTransferCmnd(command->args[0], commit->filecontent, strlen(commit->filecontent)); // Send commit file to server
sendNetworkCommand(toSend, sockfd);
freeCMND(toSend);
NetworkCommand* finalresponse = readMessage(sockfd); // check that server recieved commit successfully
if (checkresponse("data", finalresponse) != 0) {
printf("Error: server couldn't read commit file\n");
freeCommit(commit);
return -1;
}
if (uid != atoi(finalresponse->argv[2])) { // if the server sent a different uid than the one save, replace local
FileContents* config = readfile(".config");
char newconfig[config->size + finalresponse->arglengths[2]];
memset(newconfig, '\0', config->size + finalresponse->arglengths[2]);
strcat(newconfig, strtok(config->content, "\n"));
strcat(newconfig, "\n");
strcat(newconfig, strtok(NULL, "\n"));
strcat(newconfig, "\n");
strcat(newconfig, strtok(NULL, ":"));
strcat(newconfig, ":");
strcat(newconfig, finalresponse->argv[2]);
strcat(newconfig, "\n");
remove(".config");
int newfd = open(".config", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
write(newfd, newconfig, config->size + finalresponse->arglengths[2] - 1);
freefile(config);
}
printf("Successfully commit %d change(s) -> uid: %s\n", commit->entries, finalresponse->argv[2]);
freeCommit(commit);
freeCMND(finalresponse);
return 0;
}
Commit* createcommit(char* remoteManifest, int remotelen, char* project, int projlen) {
int checkfolder = 0;
DIR* dir = NULL;
if (!(dir = opendir(".tempfiles"))) checkfolder = mkdir(".tempfiles", 0700);
else closedir(dir);
if (checkfolder != 0) return NULL;
chdir(".tempfiles");
recreatefile("archive.tar.gz", remoteManifest, remotelen);
uncompressfile("archive.tar.gz");
chdir("..");
char* servermanifestpath = malloc(23 + projlen);
memset(servermanifestpath, '\0', 23 + projlen);
sprintf(servermanifestpath, ".tempfiles/%s/.Manifest", project);
FileContents* servermanifest = readfile(servermanifestpath);
free(servermanifestpath);
close(servermanifest->fd);
system("rm -rf .tempfiles > /dev/null 2>&1");
char* localmanifestpath = malloc(11 + projlen);
memset(localmanifestpath, '\0', 11 + projlen);
sprintf(localmanifestpath, "%s/.Manifest", project);
FileContents* localmanifestfile = readfile(localmanifestpath);
free(localmanifestpath);
if (getManifestVersion(servermanifest) != getManifestVersion(localmanifestfile)) {
printf("Error: Remote project is ahead of local project. Update before attempting to commit.");
freefile(localmanifestfile);
freefile(servermanifest);
return NULL;
}
Manifest* localmanifest = parseManifest(localmanifestfile);
char** files = getManifestFiles(localmanifest);
char** hashcodes = getManifestHashcodes(localmanifest);
int* fileversions = getManifestFileVersion(localmanifest);
Manifest* remotemanifest = parseManifest(servermanifest);
char** remotefiles = getManifestFiles(remotemanifest);
char** remotehashcodes = getManifestHashcodes(remotemanifest);
if (localmanifest->entrycount == 0 && remotemanifest->entrycount == 0) {
printf("There are no changes to commit\n");
freeManifest(localmanifest);
freeManifest(remotemanifest);
freefile(localmanifestfile);
freefile(servermanifest);
Commit* commit = malloc(sizeof(Commit));
commit->entries = 0;
commit->filesize = 0;
commit->filecontent = malloc(1);
commit->uid = 0;
return commit;
}
Commit* commit = malloc(sizeof(Commit));
commit->entries = 0;
commit->uid = -1;
commit->filecontent = malloc(localmanifestfile->size + servermanifest->size + 2);
memset(commit->filecontent, '\0', localmanifestfile->size + servermanifest->size+ 2);
commit->filesize = 0;
freefile(localmanifestfile);
freefile(servermanifest);
/*
Modify
1) livehash != localhash
2) localhash == serverhash
Add
1) file is in localmanifest
2) file is not in remotemanifest
Remove
1) file is in remotemanifest
2) file is not in localmanifest
*/
int i = 0;
for(i = 0; i < localmanifest->entrycount; i++) { // Check if client added/modified any files
int j = 0;
int new = 1;
for (j = 0; j < remotemanifest->entrycount; j++) {
if (strcmp(files[i], remotefiles[j]) != 0) continue; // file is included in remote manifest
new = 0;
break;
}
if (new) {
// Add condition
char* commitentry = malloc(strlen(files[i]) + digitCount(fileversions[i]) + 40 + 6);
memset(commitentry, '\0', strlen(files[i]) + digitCount(fileversions[i]) + 40 + 6);
sprintf(commitentry, "A %s", files[i]);
printf("%s\n", commitentry);
sprintf(commitentry, "A %d %s %s\n",fileversions[i] + 1, files[i], hashcodes[i]);
strcat(commit->filecontent, commitentry);
free(commitentry);
commit->filesize += strlen(files[i]) + digitCount(fileversions[i]) + 45;
commit->entries += 1;
} else {
// file is already in remote, check that is up to date with live file
FileContents* livecontent = readfile(files[i]);
unsigned char* livehashcode = hashdata((unsigned char*) livecontent->content, livecontent->size);
char* encodedlivehash = hashtohex(livehashcode);
free(livehashcode);
if (strcmp(encodedlivehash, hashcodes[i]) != 0) {
if (strcmp((const char*)hashcodes[i],(const char*) remotehashcodes[j]) != 0) { // check that local manifest hash matches the server manifest hash
printf("Error: There is an inconsistency between the remote project and the local project, skiping file '%s'\n", files[i]);
freefile(livecontent);
free(encodedlivehash);
continue;
}
// Modify condition
char* commitentry = malloc(strlen(files[i]) + digitCount(1 +fileversions[i]) + 46);
memset(commitentry, '\0', strlen(files[i]) + digitCount(1 +fileversions[i]) + 46);
printf("M %s\n", files[i]);
sprintf(commitentry, "M %d %s %s\n", 1 + fileversions[i], files[i], encodedlivehash);
strcat(commit->filecontent, commitentry);
free(commitentry);
commit->filesize += strlen(files[i]) + digitCount( 1 + fileversions[i]) + 45;
commit->entries += 1;
}
freefile(livecontent);
}
}
for(i = 0; i < remotemanifest->entrycount; i++) { // check if client deleted any files
int j = 0;
int removed = 1;
for (j = 0; j < localmanifest->entrycount; j++) {
if (strcmp(files[j], remotefiles[i]) != 0) continue;
removed = 0;
break;
}
if (removed) {
// Delete condition
char* commitentry = malloc(strlen(remotefiles[i]) + 40 + 2 + 4 + 1);
memset(commitentry, '\0', strlen(remotefiles[i]) + 40 + 2 + 4 + 1);
sprintf(commitentry, "D %s", remotefiles[i]);
printf("%s\n", commitentry);
sprintf(commitentry, "D 0 %s %s\n", remotefiles[i], remotehashcodes[i]);
strcat(commit->filecontent, commitentry);
free(commitentry);
commit->filesize += strlen(remotefiles[i]) + 40 + 2 + 4;
commit->entries += 1;
}
}
for (i = 0; i < localmanifest->entrycount; i++) {
free(files[i]);
free(hashcodes[i]);
}
for (i = 0; i < remotemanifest->entrycount; i++) {
free(remotefiles[i]);
free(remotehashcodes[i]);
}