-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytar.c
1320 lines (1175 loc) · 38 KB
/
mytar.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
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 <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include "mytar.h"
#define NAME_OFFSET 0
#define MODE_OFFSET 100
#define UID_OFFSET 108
#define GID_OFFSET 116
#define SIZE_OFFSET 124
#define MTIME_OFFSET 136
#define CHKSUM_OFFSET 148
#define TYPEFLAG_OFFSET 156
#define LINKNAME_OFFSET 157
#define MAGIC_OFFSET 257
#define VERSION_OFFSET 263
#define UNAME_OFFSET 265
#define GNAME_OFFSET 297
#define DEVMAJOR_OFFSET 329
#define DEVMINOR_OFFSET 337
#define PREFIX_OFFSET 345
#define NAME_SIZE 100
#define MODE_SIZE 8
#define UID_SIZE 8
#define GID_SIZE 8
#define SIZE_SIZE 12
#define MTIME_SIZE 12
#define CHKSUM_SIZE 8
#define TYPEFLAG_SIZE 1
#define LINKNAME_SIZE 100
#define MAGIC_SIZE 6
#define VERSION_SIZE 2
#define UNAME_SIZE 32
#define GNAME_SIZE 32
#define DEVMAJOR_SIZE 8
#define DEVMINOR_SIZE 8
#define PREFIX_SIZE 155
#define MALLOC_SIZE 500
#define BLOCK_SIZE 512
#define PERMS_SIZE 10
#define TIME_SIZE 16
typedef struct __attribute__ ((packed))
{
char name[NAME_SIZE];
char mode[MODE_SIZE];
char uid[UID_SIZE];
char gid[GID_SIZE];
char size[SIZE_SIZE];
char mtime[MTIME_SIZE];
char chksum[CHKSUM_SIZE];
char typeflag[TYPEFLAG_SIZE];
char linkname[LINKNAME_SIZE];
char magic[MAGIC_SIZE];
char version[VERSION_SIZE];
char uname[UNAME_SIZE];
char gname[GNAME_SIZE];
char devmajor[DEVMAJOR_SIZE];
char devminor[DEVMINOR_SIZE];
char prefix[PREFIX_SIZE];
char padding[12];
} header;
int f_flag, c_flag, t_flag, x_flag, v_flag, S_flag;
uint32_t extract_special_int(const char *where, int len) {
/* For interoperability with GNU tar. GNU seems to
* set the high–order bit of the first byte, then
* treat the rest of the field as a binary integer
* in network byte order.
* I don’t know for sure if it’s a 32 or 64–bit int, but for
* this version, we’ll only support 32. (well, 31)
* returns the integer on success, –1 on failure.
* In spite of the name of htonl(), it converts int32 t
*/
int32_t val = -1;
if ((len >= sizeof(val)) && (where[0] & 0x80)) {
/* the top bit is set, and we have space
* extract the last four bytes */
val = *(int32_t *) (where + len - sizeof(val));
val = ntohl(val); /* convert to host byte order */
}
return val;
}
int insert_special_int(char *where, size_t size, int32_t val) {
/* For interoperability with GNU tar. GNU seems to
* set the high–order bit of the first byte, then
* treat the rest of the field as a binary integer
* in network byte order.
* Insert the given integer into the given field
* using this technique. Returns 0 on success, nonzero
* otherwise
*/
int err = 0;
if (val < 0 || (size < sizeof(val))) {
/* if it’s negative, bit 31 is set and we can’t use the flag
* if len is too small, we can’t write it. Either way, we’re
* done.
*/
err++;
} else {
/* game on....*/
memset(where, 0, size); /* Clear out the buffer */
*(int32_t *) (where + size - sizeof(val)) = htonl(val); /* place the int */
*where |= 0x80; /* set that high–order bit */
}
return err;
}
/* extract files from the archive */
int extract_archive(char *tar_file, char **paths,
int supplied_path, int path_count) {
int fd;
int new_fd;
char *name;
char *mode;
char *uid;
char *gid;
char *size;
char *mtime;
char *chksum;
char *typeflag;
char *linkname;
char *magic;
char *version;
char *uname;
char *gname;
char *devmajor;
char *devminor;
char *prefix;
char *contents;
int size_read;
int file_chunk_size;
int converted_mode;
int i;
int j;
int match;
int our_sum;
/* open the tar file for reading */
if ((fd = open(tar_file, O_RDONLY)) == -1) {
perror(tar_file);
exit(25);
}
/* save the file chunk size */
file_chunk_size = 0;
while ((size_read = read(fd, NULL, 512)) != 0) {
/* reset fd back to beginning of block */
lseek(fd, -(size_read + 1), SEEK_CUR);
if (!(name = (char *) malloc(NAME_SIZE))) {
perror("malloc:");
exit(9);
}
if (!(mode = malloc(MODE_SIZE))) {
perror("malloc:");
exit(10);
}
if (!(uid = malloc(UID_SIZE))) {
perror("malloc:");
exit(11);
}
if (!(gid = malloc(GID_SIZE))) {
perror("malloc:");
exit(12);
}
if (!(size = malloc(SIZE_SIZE))) {
perror("malloc:");
exit(13);
}
if (!(mtime = malloc(MTIME_SIZE))) {
perror("malloc:");
exit(14);
}
if (!(chksum = malloc(CHKSUM_SIZE))) {
perror("malloc:");
exit(15);
}
if (!(typeflag = malloc(TYPEFLAG_SIZE))) {
perror("malloc:");
exit(16);
}
if (!(linkname = malloc(LINKNAME_SIZE))) {
perror("malloc:");
exit(17);
}
if (!(magic = malloc(MAGIC_SIZE))) {
perror("malloc:");
exit(18);
}
if (!(version = malloc(VERSION_SIZE))) {
perror("malloc:");
exit(19);
}
if (!(uname = malloc(UNAME_SIZE))) {
perror("malloc:");
exit(20);
}
if (!(gname = malloc(GNAME_SIZE))) {
perror("malloc:");
exit(21);
}
if (!(devmajor = malloc(DEVMAJOR_SIZE))) {
perror("malloc:");
exit(22);
}
if (!(devminor = malloc(DEVMINOR_SIZE))) {
perror("malloc:");
exit(23);
}
if (!(prefix = malloc(PREFIX_SIZE))) {
perror("malloc:");
exit(24);
}
if (read(fd, name, NAME_SIZE) == -1) {
perror(name);
exit(26);
}
our_sum = 0;
/* split the block into appropriate fields with
* increment our_sum to verify with chksum */
for (i = 0; i < NAME_SIZE; i++) {
our_sum += (unsigned char) name[i];
}
if (read(fd, mode, MODE_SIZE) == -1) {
perror(mode);
exit(130);
}
for (i = 0; i < MODE_SIZE; i++) {
our_sum += (unsigned char) mode[i];
}
if (read(fd, uid, UID_SIZE) == -1) {
perror(uid);
exit(131);
}
for (i = 0; i < UID_SIZE; i++) {
our_sum += (unsigned char) uid[i];
}
if (read(fd, gid, GID_SIZE) == -1) {
perror(gid);
exit(132);
}
for (i = 0; i < GID_SIZE; i++) {
our_sum += (unsigned char) gid[i];
}
if (read(fd, size, SIZE_SIZE) == -1) {
perror(size);
exit(133);
}
for (i = 0; i < SIZE_SIZE; i++) {
our_sum += (unsigned char) size[i];
}
if (read(fd, mtime, MTIME_SIZE) == -1) {
perror(mtime);
exit(134);
}
for (i = 0; i < MTIME_SIZE; i++) {
our_sum += (unsigned char) mtime[i];
}
if (read(fd, chksum, CHKSUM_SIZE) == -1) {
perror(mtime);
exit(135);
}
for (i = 0; i < CHKSUM_SIZE; i++) {
our_sum += 32;
}
if (read(fd, typeflag, TYPEFLAG_SIZE) == -1) {
perror(typeflag);
exit(136);
}
for (i = 0; i < TYPEFLAG_SIZE; i++) {
our_sum += (unsigned char) typeflag[i];
}
if (read(fd, linkname, LINKNAME_SIZE) == -1) {
perror(linkname);
exit(137);
}
for (i = 0; i < LINKNAME_SIZE; i++) {
our_sum += (unsigned char) linkname[i];
}
if (read(fd, magic, MAGIC_SIZE) == -1) {
perror(magic);
exit(138);
}
for (i = 0; i < MAGIC_SIZE; i++) {
our_sum += (unsigned char) magic[i];
}
if (read(fd, version, VERSION_SIZE) == -1) {
perror(version);
exit(139);
}
for (i = 0; i < VERSION_SIZE; i++) {
our_sum += (unsigned char) version[i];
}
if (read(fd, uname, UNAME_SIZE) == -1) {
perror(uname);
exit(140);
}
for (i = 0; i < UNAME_SIZE; i++) {
our_sum += (unsigned char) uname[i];
}
if (read(fd, gname, GNAME_SIZE) == -1) {
perror(gname);
exit(141);
}
for (i = 0; i < GNAME_SIZE; i++) {
our_sum += (unsigned char) gname[i];
}
if (read(fd, devmajor, DEVMAJOR_SIZE) == -1) {
perror(devmajor);
exit(142);
}
for (i = 0; i < DEVMAJOR_SIZE; i++) {
our_sum += (unsigned char) devmajor[i];
}
if (read(fd, devminor, DEVMINOR_SIZE) == -1) {
perror(devminor);
exit(143);
}
for (i = 0; i < DEVMINOR_SIZE; i++) {
our_sum += (unsigned char) devminor[i];
}
if (read(fd, prefix, PREFIX_SIZE) == -1) {
perror(prefix);
exit(144);
}
for (i = 0; i < PREFIX_SIZE; i++) {
our_sum += (unsigned char) prefix[i];
}
/* chksun failed: abort */
if ((our_sum) != strtol(chksum, NULL, 8)) {
free(name);
free(mode);
free(uid);
free(gid);
free(size);
free(mtime);
free(chksum);
free(typeflag);
free(linkname);
free(magic);
free(version);
free(uname);
free(gname);
free(devmajor);
free(devminor);
exit(150);
}
/* go to the next block */
lseek(fd, 12, SEEK_CUR);
if (!(contents = malloc((strtol(size, NULL, 8)) + 1))) {
perror("malloc:");
exit(25);
}
/* read the contents of the file */
if (read(fd, contents, (strtol(size, NULL, 8) + 1)) == -1) {
perror(contents);
exit(145);
}
/* concat prefix and name */
if(strlen(prefix) != 0) {
prefix[strlen(prefix)] = '/';
}
strcat(prefix, name);
if (S_flag) {
/* if strict mode, ensure magic null terminated
* and version is 00
*/
if (memcmp(magic, "ustar\0", MAGIC_SIZE) != 0) {
fprintf(stderr, "incorrect magic\n");
exit(100);
}
if (memcmp(version, "00", VERSION_SIZE) != 0) {
fprintf(stderr, "incorrect version\n");
exit(101);
}
} else {
/* if non-strict, check for ustar */
if (memcmp(magic, "ustar", MAGIC_SIZE - 1) != 0) {
fprintf(stderr, "incorrect magic\n");
exit(102);
}
}
/* check if a specific path was supplied on the command line */
if(supplied_path) {
match = 0;
for(j = 0; j < path_count; j++) {
/* check to see if the
* header file name is a prefix of our target */
if(strncmp(prefix, paths[j], strlen(paths[j])) == 0
&& (prefix[strlen(paths[j])] == '\0'
|| prefix[strlen(paths[j])] == '/')) {
match = 1;
if (memcmp(typeflag, "0", 1) == 0
|| memcmp(typeflag, "\0", 1) == 0) {
/* we have a regular file */
converted_mode = (int) strtol(mode, NULL, 8);
if(((S_IXUSR | S_IXGRP | S_IXOTH)
& converted_mode) != 0) {
/* offer execute perms to everybody */
new_fd = open(name, O_WRONLY | O_CREAT
, S_IRWXU, S_IRWXG, S_IRWXO);
} else {
new_fd = open(name, O_WRONLY | O_CREAT,
S_IRUSR | S_IWUSR
| S_IRGRP | S_IWGRP
| S_IROTH | S_IWOTH);
}
/* write the contents of the file
* to the newly created file */
write(new_fd, contents, (strtol(size, NULL, 8)));
free(contents);
close(new_fd);
/* calculate how far back to lseek */
file_chunk_size = (int)
(strtol(size, NULL, 8) / 512) + 1;
lseek(fd, -(strtol(size, NULL, 8) + 1), SEEK_CUR);
/* lseek to next header */
lseek(fd, (512 * file_chunk_size), SEEK_CUR);
} else if (memcmp(typeflag, "5", 1) == 0) {
/* we've found a directory */
mkdir(prefix,
S_IRUSR | S_IWUSR | S_IXUSR
| S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
lseek(fd, -1, SEEK_CUR);
} else if (memcmp(typeflag, "2", 1) == 0) {
/* symbolic link */
if(symlink(linkname, prefix) == -1) {
perror("symlink");
exit(40);
}
lseek(fd, -1, SEEK_CUR);
} else {
fprintf(stderr, "Unsupported file type supplied\n");
}
/* verbose */
if(v_flag) {
printf("%s\n", prefix);
}
}
}
/* this chunk of the tape was not
* targeted by the command line input
*/
if(!match) {
/* skip past this */
if (memcmp(typeflag, "0", 1) == 0
|| memcmp(typeflag, "\0", 1) == 0) {
/* we have a regular file */
file_chunk_size = (int) (strtol(size, NULL, 8) / 512) + 1;
lseek(fd, -(strtol(size, NULL, 8) + 1), SEEK_CUR);
lseek(fd, (512 * file_chunk_size), SEEK_CUR);
} else if (memcmp(typeflag, "5", 1) == 0) {
lseek(fd, -1, SEEK_CUR);
} else if (memcmp(typeflag, "2", 1) == 0) {
/* symbolic link */
lseek(fd, -1, SEEK_CUR);
}
free(contents);
}
} else {
/* no targets were supplied on the command line
* extract all files
*/
if (memcmp(typeflag, "0", TYPEFLAG_SIZE) == 0
|| memcmp(typeflag, "\0", TYPEFLAG_SIZE) == 0) {
/* we have a regular file */
converted_mode = (int) strtol(mode, NULL, 8);
if (((S_IXUSR | S_IXGRP | S_IXOTH) & converted_mode) != 0) {
/* offer execute permissions to everybody */
new_fd = open(prefix, O_WRONLY | O_CREAT,
S_IRWXU, S_IRWXG, S_IRWXO);
} else {
/* nobody had execute permissions */
new_fd = open(prefix,
O_WRONLY | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP
| S_IWGRP | S_IROTH | S_IWOTH);
}
write(new_fd, contents, (strtol(size, NULL, 8)));
free(contents);
close(new_fd);
/* calculate how many 512 block chunks this file occupied */
file_chunk_size = (int) (strtol(size, NULL, 8) / 512) + 1;
/* find the next file header */
lseek(fd, -(strtol(size, NULL, 8) + 1), SEEK_CUR);
lseek(fd, (512 * file_chunk_size), SEEK_CUR);
} else if (memcmp(typeflag, "5", TYPEFLAG_SIZE) == 0) {
mkdir(prefix, S_IRUSR | S_IWUSR
| S_IXUSR |
S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
lseek(fd, -1, SEEK_CUR);
} else if (memcmp(typeflag, "2", TYPEFLAG_SIZE) == 0) {
/* symbolic link */
if (symlink(linkname, prefix) == -1) {
perror("symlink");
exit(40);
}
lseek(fd, -1, SEEK_CUR);
} else {
fprintf(stderr, "Unsupported file type supplied\n");
}
/* verbose list files as extracted */
if (v_flag) {
printf("%s\n", prefix);
}
}
free(name);
free(mode);
free(uid);
free(gid);
free(size);
free(mtime);
free(chksum);
free(typeflag);
free(linkname);
free(magic);
free(version);
free(uname);
free(gname);
free(devmajor);
free(devminor);
}
return 1;
}
int print_archive(char *tarfile, char **files, int numFiles) {
int fd;
/*keep track of the current block we are on*/
int blckIndex = 0;
char perms[PERMS_SIZE+1] = "-rwxrwxrwx";
char rbuff[BLOCK_SIZE];
char *endptr;
/*put octal strings from strtol into octalstr*/
long octalstr = 0;
int i =0, j=0;
uint32_t chksum = 0;
struct tm *time;
/*put mtime formatted as a string into this var*/
char pbuff[TIME_SIZE+1];
char *fname;
if((fd = open(tarfile, O_RDONLY)) == -1){
perror("open: tarfile");
exit(EXIT_FAILURE);
}
for(;;){
if(read(fd, rbuff, BLOCK_SIZE) == -1){
perror("read");
exit(EXIT_FAILURE);
}
/*check the magic is "ustar"*/
if(strncmp(rbuff+MAGIC_OFFSET, "ustar", 5)){
fprintf(stderr, "Magic no. wasn't 'ustar'\n");
exit(EXIT_FAILURE);
}
/*if in strict mode, check magic is null terminated, that the
* version is 00, and that all ocatal strings are properly formatted*/
if(S_flag){
if(rbuff[TYPEFLAG_OFFSET-1] != '\0'){
fprintf(stderr,
"Magic # is not null terminated.\n");
exit(EXIT_FAILURE);
}
if(strncmp(rbuff+VERSION_OFFSET, "00", 2)){
fprintf(stderr, "Invalid Version #\n");
exit(EXIT_FAILURE);
}
if((extract_special_int(rbuff+MODE_OFFSET, 8) != -1) ||
(extract_special_int(rbuff+CHKSUM_OFFSET, 8) != -1) ||
(extract_special_int(rbuff+UID_OFFSET, 8) != -1) ||
(extract_special_int(rbuff+GID_OFFSET, 8) != -1) ||
(extract_special_int(rbuff+DEVMAJOR_OFFSET, 8) != -1) ||
(extract_special_int(rbuff+DEVMINOR_OFFSET, 8) != -1) ||
(extract_special_int(rbuff+SIZE_OFFSET, 12) != -1) ||
(extract_special_int(rbuff+MTIME_OFFSET, 12) != -1)){
fprintf(stderr,"Bad octal strings in header\n");
exit(EXIT_FAILURE);
}
}
/*get the filename from the
* header's prefix and name fields*/
fname = calloc(PREFIX_SIZE+NAME_SIZE+2, sizeof(char));
if(fname == NULL){
perror("calloc");
exit(EXIT_FAILURE);
}
/*get prefix if there is one*/
if(rbuff[PREFIX_OFFSET] != '\0'){
j = snprintf(fname,PREFIX_SIZE+1,
"%s",rbuff+PREFIX_OFFSET);
/*get name field if there
* is one and put it after prefix*/
if(rbuff[NAME_OFFSET] != '\0'){
snprintf(fname+j,NAME_SIZE+2,
"/%s",rbuff+NAME_OFFSET);
}
} else{
snprintf(fname, NAME_SIZE+1, "%s", rbuff+NAME_OFFSET);
}
/*calculate chksum*/
i=0;
while(i<BLOCK_SIZE){
/*treat the chksum area as 8 spaces*/
if(i == CHKSUM_OFFSET){
chksum += 32*8;
i = TYPEFLAG_OFFSET;
}
chksum += (uint8_t)rbuff[i++];
}
octalstr = strtol(rbuff+CHKSUM_OFFSET, &endptr, 8);
/*if my chksum is 256 but the one in file
* is 0, we might be at the end or its corrupt*/
if(chksum == 256 && octalstr == 0){
/*check if end*/
if(read(fd, rbuff, BLOCK_SIZE) == -1){
perror("read");
exit(EXIT_FAILURE);
}
i = 0;
/*if the next block is all 0s
* stop, otherwise header is corrupt*/
while(i<BLOCK_SIZE){
if(rbuff[i++] != '\0'){
fprintf(stderr, "invalid chksum");
exit(EXIT_FAILURE);
}
}
close(fd);
return 1;
/*if chksums differ then corrupt header*/
} else if(chksum != octalstr){
fprintf(stderr, "invalid chksum");
exit(EXIT_FAILURE);
}
chksum = 0;
/*check filenames if they are given*/
if(numFiles !=0){
j = 0;
i = 0;
while(j<numFiles){
i=0;
/*keep going until all
* chars in given filename are read*/
while(files[j][i] != '\0'){
/*if strings don't
* match, check next given file name*/
if(fname[i] != files[j][i]){
break;
}
i++;
}
/*check if read the whole filename*/
/*if i did read the whole filename,
* is the header name the same*/
if( (files[j][i] != '\0') &&
(fname[i] != '\0' || fname[i] != '/')){
if(++j == numFiles){
i = -1;
}
continue;
} else{
break;
}
}
/*if we header name wasn't any requested
* file name or member of a requested directory
* go to next header by changing the block index*/
if(i==-1){
blckIndex += 1;
octalstr =strtol(rbuff+SIZE_OFFSET, &endptr, 8);
blckIndex += octalstr/BLOCK_SIZE;
if( (octalstr%BLOCK_SIZE) != 0){
blckIndex += 1;
}
if(lseek(fd, blckIndex*BLOCK_SIZE,SEEK_SET)
== -1){
perror("lseek");
exit(EXIT_FAILURE);
}
free(fname);
/*like a break statement but forces another
* iteration of a loop instead of forcing termination*/
continue;
}
}
/*do this stuff if we are in verbose mode*/
if(v_flag){
/*check file type*/
if(rbuff[TYPEFLAG_OFFSET] == '5'){
perms[0] = 'd';
} else if(rbuff[TYPEFLAG_OFFSET] == '2'){
perms[0] = 'l';
}
/*check mode bits*/
/*probably should have been a switch statement but I
* forgot about those and this works*/
octalstr = strtol(rbuff+MODE_OFFSET, &endptr, 8);
if( !(octalstr & S_IRUSR)){
perms[1] = '-';
}
if( !(octalstr & S_IWUSR)){
perms[2] = '-';
}
if( !(octalstr & S_IXUSR)){
perms[3] = '-';
}
if( !(octalstr & S_IRGRP)){
perms[4] = '-';
}
if( !(octalstr & S_IWGRP)){
perms[5] = '-';
}
if( !(octalstr & S_IXGRP)){
perms[6] = '-';
}
if( !(octalstr & S_IROTH)){
perms[7] = '-';
}
if( !(octalstr & S_IWOTH)){
perms[8] = '-';
}
if( !(octalstr & S_IXOTH)){
perms[9] = '-';
}
printf("%s ", perms);
strcpy(perms, "-rwxrwxrwx");
/*uname/gname*/
/*if uname or gname not there, use uid & gid*/
if( rbuff[UNAME_OFFSET] == '\0'){
if(rbuff[UID_OFFSET] == 0x80){
octalstr =
extract_special_int(rbuff+UID_OFFSET, 8);
}else{
octalstr =
strtol(rbuff+UID_OFFSET, &endptr, 8);
}
printf("%ld/", octalstr);
}else{
printf("%s/", rbuff+UNAME_OFFSET);
}
if( rbuff[GNAME_OFFSET] == '\0'){
if(rbuff[GID_OFFSET] == 0x80){
octalstr =
extract_special_int(rbuff+GID_OFFSET, 8);
}else{
octalstr =
strtol(rbuff+GID_OFFSET, &endptr, 8);
}
printf("%ld/", octalstr);
}else{
printf("%s ", rbuff+GNAME_OFFSET);
}
/*printf the size*/
octalstr = strtol(rbuff+SIZE_OFFSET, &endptr, 8);
printf("%8ld ", octalstr);
/*print the mtime in the format specified*/
octalstr = strtol(rbuff+MTIME_OFFSET, &endptr, 8);
if((time = localtime(&octalstr)) == NULL){
perror("localtime");
exit(EXIT_FAILURE);
}
strftime(pbuff, TIME_SIZE+1, "%Y-%m-%d %H:%M", time);
printf("%s ", pbuff);
}
/*print the file name*/
printf("%s\n", fname);
free(fname);
/*change the block index to go
* to the block with the next header*/
blckIndex += 1;
octalstr = strtol(rbuff+SIZE_OFFSET, &endptr, 8);
blckIndex += octalstr/BLOCK_SIZE;
if( (octalstr%BLOCK_SIZE) != 0){
blckIndex += 1;
}
if(lseek(fd, blckIndex*BLOCK_SIZE,SEEK_SET) == -1){
perror("lseek");
exit(EXIT_FAILURE);
}
}
return 1;
}
void tapeFile(int tarFd, char *file){
struct stat *lbuff = malloc(sizeof(struct stat));
struct group *grp;
struct passwd *pass;
char *fileDir;
DIR *dir;
struct dirent *df;
char *fileContents;
int i = 0, fnameLength = 0, fd, fd2;
uint32_t chksum = 0, mode = 0;
header *head = calloc(1, sizeof(header));
/*used to add up all the bytes in the header*/
uint8_t *altHead = (uint8_t *)head;
if(lstat(file, lbuff) == -1){
perror("lstat");
return;
}
if(lbuff == NULL){
perror("malloc");
exit(EXIT_FAILURE);
}
if(head == NULL){
perror("calloc");
exit(EXIT_FAILURE);
}
fd = open(file, O_RDONLY);
/*cant open?, skip and go to next file*/
if(fd ==-1){
perror("open failed... skipping");
return;
}
/*add a '/' to the end of a directory name*/
fnameLength = strlen(file);
if(S_ISDIR(lbuff->st_mode)){
strncat(file, "/", 2);
fnameLength++;
}
/*if the filename is bigger
* than 256 then we can't do anything*/
if(fnameLength > PREFIX_SIZE+NAME_SIZE+1){
fprintf(stderr, "path name is too long");
return;
/*cut up a name into prefix and name fields*/
} else if(fnameLength > 100){
/*look for a '/' to break up the file name*/
i = fnameLength - NAME_SIZE-1;
while(i<fnameLength){
/*if I can't break up the file
* name then we can't do anything*/
if( (i > PREFIX_SIZE) || (i==fnameLength)){
fprintf(stderr, "prefix: can't be broken on a '/'");
return;
}
/*break up the prefix and name if we found a '/'*/
if(file[i] == '/'){
strncpy(head->prefix, file, i);
strncpy(head->name, file+i+1, NAME_SIZE);
break;
}
i++;
}
/*only put in the name if it is 100 or less than characters*/
} else{
strncpy(head->name, file, NAME_SIZE);
}
/*get the permissions, S_ISUID, S_ISGID, and sticky bit*/
mode = (((uint32_t)lbuff->st_mode) & (S_ISUID | S_ISGID
| S_ISVTX | S_IRUSR | S_IWUSR |
S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP
| S_IROTH | S_IWOTH | S_IXOTH));
snprintf(head->mode, MODE_SIZE, "%07o", mode);
/*use the insert function if the
* uid is too big for a 7 digit octal*/
if(lbuff->st_uid > 07777777){
/*we can't do anything if the octal is
* too big and we are in strict*/
if(S_flag){
fprintf(stderr, "uid is too big for an octal string\n");
free(head);
/*if dir, then put in all the files*/
if( S_ISDIR(lbuff->st_mode)){
dir = opendir(file);
if(dir == NULL){
perror("opendir");
return;
}
/*no point in adding '.' and '..'
* directories to the tar file*/
readdir(dir);
readdir(dir);
/*call the tapefile function for
* every file in the directory*/
while( (df = readdir(dir))){
fileDir =
malloc((fnameLength+2+
strlen(df->d_name))*sizeof(char));
if(fileDir == NULL){
perror("malloc");
exit(EXIT_FAILURE);
}
sprintf(fileDir, "%s%s",
file, df->d_name);
tapeFile(tarFd, fileDir);
free(fileDir);
}
free(dir);
}
free(lbuff);
close(fd);
return;
}
/*put in the special int if not in strict*/
if(S_flag != 1){
insert_special_int(head->uid, 8,(int32_t)lbuff->st_uid);
}
/*put in uid if it is not too big*/
} else {