forked from ebertland/archivemount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
archivemount.c
executable file
·2924 lines (2705 loc) · 69.4 KB
/
archivemount.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
/*
Copyright (c) 2005-2018 Andre Landwehr <andrel@cybernoia.de>
This program can be distributed under the terms of the GNU LGPL.
See the file COPYING.
Based on: fusexmp.c and sshfs.c by Miklos Szeredi <miklos@szeredi.hu>
Contributions by: Niels de Vos <niels@nixpanic.net>
Thomas J. Duck
Andrew Brampton <me at bramp dot net>
Tomáš Čech <sleep_walker at suse dot cz>
Timothy Hobbs <timothyhobbs at seznam dot cz>
Lee Leahu
Alain Parmentier <pa at infodata.lu>
*/
#ifdef linux
/* For pread()/pwrite() */
#define _XOPEN_SOURCE 500
#endif
#define FUSE_USE_VERSION 26
#define MAXBUF 4096
#include "config.h"
#include <fuse.h>
#include <fuse/fuse_lowlevel.h>
#include <fuse_opt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <grp.h>
#include <pwd.h>
#include <utime.h>
#include <string.h>
#include <wchar.h>
#include <archive.h>
#include <archive_entry.h>
#include <pthread.h>
#include <regex.h>
#include "archivecomp.h"
#include "uthash.h"
/**********/
/* macros */
/**********/
#ifdef NDEBUG
# define log(format, ...)
#else
# define log(format, ...) \
{ \
FILE *FH = fopen("/tmp/archivemount.log", "a"); \
if (FH) { \
fprintf(FH, "l. %4d: " format "\n", __LINE__, ##__VA_ARGS__); \
fclose(FH); \
} \
}
#endif
/*******************/
/* data structures */
/*******************/
typedef struct node {
struct node *parent;
struct node *child; /* first child for directories */
char *name; /* fully qualified with prepended '/' */
char *basename; /* every after the last '/' */
char *location; /* location on disk for new/modified files, else NULL */
int namechanged; /* true when file was renamed */
struct archive_entry *entry; /* libarchive header data */
int modified; /* true when node was modified */
UT_hash_handle hh;
} NODE;
struct options {
int readonly;
int nobackup;
int nosave;
char *subtree_filter;
int formatraw;
};
typedef struct formatraw_cache {
struct stat st;
struct archive *archive;
int opened;
off_t offset_uncompressed;
} FORMATRAW_CACHE;
enum {
KEY_VERSION,
KEY_HELP,
};
#define AR_OPT(t, p, v) { t, offsetof(struct options, p), v }
static struct fuse_opt ar_opts[] =
{
AR_OPT("readonly", readonly, 1),
AR_OPT("nobackup", nobackup, 1),
AR_OPT("nosave" , nosave , 1),
AR_OPT("subtree=%s", subtree_filter, 1),
AR_OPT("formatraw", formatraw, 1),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_END
};
/***********/
/* globals */
/***********/
static int archiveFd; /* file descriptor of archive file, just to keep the
beast alive in case somebody deletes the file while
it is mounted */
static int archiveModified = 0;
static int archiveWriteable = 0;
static NODE *root;
static FORMATRAW_CACHE *rawcache;
struct options options;
char *mtpt = NULL;
char *archiveFile = NULL;
pthread_mutex_t lock; /* global node tree lock */
/* Taken from the GNU under the GPL */
char *
strchrnul (const char *s, int c_in)
{
char c = c_in;
while (*s && (*s != c))
s++;
return (char *) s;
}
/**********************/
/* internal functions */
/**********************/
static void
usage(const char *progname)
{
fprintf(stderr,
"usage: %s archivepath mountpoint [options]\n"
"\n"
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n"
"archivemount options:\n"
" -o readonly disable write support\n"
" -o nobackup remove archive file backups\n"
" -o nosave do not save changes upon unmount.\n"
" Good if you want to change something\n"
" and save it as a diff,\n"
" or use a format for saving which is\n"
" not supported by archivemount.\n"
"\n"
" -o subtree=<regexp> use only subtree matching ^\\.\\?<regexp> from archive\n"
" it implies readonly\n"
"\n"
" -o formatraw treat input as a single element archive\n"
" it implies readonly\n"
"\n",progname);
}
static struct fuse_operations ar_oper;
static int
ar_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
{
(void) data;
switch(key) {
case FUSE_OPT_KEY_OPT:
return 1;
case FUSE_OPT_KEY_NONOPT:
if (!archiveFile) {
archiveFile = strdup(arg);
return 0;
} else if (!mtpt) {
mtpt = strdup(arg);
}
return 1;
case KEY_HELP:
usage(outargs->argv[0]);
fuse_opt_add_arg(outargs, "-ho");
fuse_main(outargs->argc, outargs->argv, &ar_oper, NULL);
exit(1);
case KEY_VERSION:
fprintf(stderr, "archivemount version %s\n", VERSION);
fuse_opt_add_arg(outargs, "--version");
fuse_main(outargs->argc, outargs->argv, &ar_oper, NULL);
exit(0);
default:
fprintf(stderr, "internal error\n");
abort();
}
}
static NODE *
init_node()
{
NODE *node;
if ((node = malloc(sizeof(NODE))) == NULL) {
log("Out of memory");
return NULL;
}
node->parent = NULL;
node->child = NULL;
node->name = NULL;
node->basename = NULL;
node->location = NULL;
node->namechanged = 0;
node->entry = archive_entry_new();
node->modified = 0;
memset(&node->hh, 0, sizeof(node->hh));
if (node->entry == NULL) {
log("Out of memory");
free(node);
return NULL;
}
return node;
}
static FORMATRAW_CACHE *
init_rawcache()
{
FORMATRAW_CACHE *rawcache;
if ((rawcache = malloc(sizeof(FORMATRAW_CACHE))) == NULL) {
log("Out of memory");
return NULL;
}
memset(&rawcache->st, 0, sizeof(rawcache->st));
memset(&rawcache->archive, 0, sizeof(rawcache->archive));
rawcache->opened=0;
rawcache->offset_uncompressed=0;
return rawcache;
}
static void
free_node(NODE *node)
{
NODE *child, *tmp;
free(node->name);
archive_entry_free(node->entry);
// Clean up any children
HASH_ITER(hh, node->child, child, tmp) {
HASH_DEL(node->child, child);
free_node(child);
}
free(node);
}
/* not used now
#static void
free_rawcache(FORMATRAW_CACHE *rawcache)
{
free(rawcache);
}
*/
static void
remove_child(NODE *node)
{
if (node->parent) {
HASH_DEL(node->parent->child, node);
log("removed '%s' from parent '%s' (was first child)", node->name, node->parent->name);
} else {
root = NULL;
}
}
static void
insert_as_child(NODE *node, NODE *parent)
{
node->parent = parent;
HASH_ADD_KEYPTR(hh, parent->child, node->basename, strlen(node->basename), node);
log("inserted '%s' as child of '%s'", node->name, parent->name);
}
/*
* inserts "node" into tree starting at "root" according to the path
* specified in node->name
* @return 0 on success, 0-errno else (ENOENT or ENOTDIR)
*/
static int
insert_by_path(NODE *root, NODE *node)
{
char *temp;
NODE *cur = root;
char *key = node->name;
key++;
while ((temp = strchr(key, '/'))) {
size_t namlen = temp - key;
char nam[namlen + 1];
NODE *last = cur;
strncpy(nam, key, namlen);
nam[namlen] = '\0';
if (cur != root || strcmp(cur->basename, nam) != 0) {
cur = cur->child;
while (cur && strcmp(cur->basename, nam) != 0)
{
cur = cur->hh.next;
}
}
if (! cur) {
/* parent path not found, create a temporary one */
NODE *tempnode;
if ((tempnode = init_node()) == NULL)
return -ENOMEM;
if ((tempnode->name = malloc(
strlen(last->name) + namlen + 2)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
if (last != root) {
sprintf(tempnode->name, "%s/%s", last->name, nam);
} else {
sprintf(tempnode->name, "/%s", nam);
}
tempnode->basename = strrchr(tempnode->name, '/') + 1;
archive_entry_free(tempnode->entry);
if ((tempnode->entry = archive_entry_clone(root->entry)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
/* insert it recursively */
insert_by_path(root, tempnode);
/* now inserting node should work, correct cur for it */
cur = tempnode;
}
/* iterate */
key = temp + 1;
}
if (S_ISDIR(archive_entry_mode(cur->entry))) {
/* check if a child of this name already exists */
NODE *tempnode = NULL;
HASH_FIND(hh, cur->child, node->basename, strlen(node->basename), tempnode);
if (tempnode) {
/* this is a dupe due to a temporarily inserted
node, just update the entry */
archive_entry_free(node->entry);
if ((node->entry = archive_entry_clone(
tempnode->entry)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
} else {
insert_as_child(node, cur);
}
} else {
return -ENOTDIR;
}
return 0;
}
static int
build_tree(const char *mtpt)
{
struct archive *archive;
struct stat st;
int format;
int compression;
NODE *cur;
char *subtree_filter = NULL;
regex_t subtree;
int regex_error;
regmatch_t regmatch;
char error_buffer[256];
#define PREFIX "^\\.\\?"
if (options.subtree_filter) {
subtree_filter = malloc(strlen(options.subtree_filter) + strlen(PREFIX) + 1);
if (!subtree_filter) {
log("Not enough memory");
return -ENOMEM;
}
strcpy(subtree_filter, PREFIX);
subtree_filter = strcat(subtree_filter, options.subtree_filter);
regex_error = regcomp(&subtree, subtree_filter, 0);
if (regex_error) {
regerror(regex_error, &subtree, error_buffer, 256);
log("Regex build error: %s\n", error_buffer);
return -regex_error;
}
options.readonly = 1;
}
/* open archive */
if ((archive = archive_read_new()) == NULL) {
log("Out of memory");
return -ENOMEM;
}
if (archive_read_support_filter_all(archive) != ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(archive));
return archive_errno(archive);
}
if (options.formatraw) {
if (archive_read_support_format_raw(archive) != ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(archive));
return archive_errno(archive);
}
options.readonly = 1;
} else {
if (archive_read_support_format_all(archive) != ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(archive));
return archive_errno(archive);
}
}
if (archive_read_open_fd(archive, archiveFd, 10240) != ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(archive));
return archive_errno(archive);
}
/* check if format or compression prohibits writability */
format = archive_format(archive);
// log("FORMAT=%s",archive_format_name(archive));
compression = archive_filter_code(archive, 0);
// log("COMPRESSION=%s",archive_compression_name(archive));
if (format & ARCHIVE_FORMAT_ISO9660
|| format & ARCHIVE_FORMAT_ISO9660_ROCKRIDGE
|| format & ARCHIVE_FORMAT_ZIP
|| compression == ARCHIVE_COMPRESSION_COMPRESS)
{
archiveWriteable = 0;
}
/* create root node */
if ((root = init_node()) == NULL)
return -ENOMEM;
root->name = strdup("/");
root->basename = &root->name[1];
/* fill root->entry */
if (fstat(archiveFd, &st) != 0) {
perror("Error stat'ing archiveFile");
return errno;
}
archive_entry_set_gid(root->entry, getgid());
archive_entry_set_uid(root->entry, getuid());
archive_entry_set_mode(root->entry, st.st_mtime);
archive_entry_set_pathname(root->entry, "/");
archive_entry_set_size(root->entry, st.st_size);
stat(mtpt, &st);
archive_entry_set_mode(root->entry, st.st_mode);
if ((cur = init_node()) == NULL) {
return -ENOMEM;
}
/* read all entries in archive, create node for each */
while (archive_read_next_header2(archive, cur->entry) == ARCHIVE_OK) {
const char *name;
/* find name of node */
name = archive_entry_pathname(cur->entry);
if (memcmp(name, "./\0", 3) == 0) {
/* special case: the directory "./" must be skipped! */
continue;
}
if (options.subtree_filter) {
regex_error = regexec(&subtree, name, 1, ®match, REG_NOTEOL);
if (regex_error) {
if (regex_error == REG_NOMATCH)
continue;
regerror(regex_error, &subtree, error_buffer, 256);
log("Regex match error: %s\n", error_buffer);
return -regex_error;
}
/* strip subtree from name */
name += regmatch.rm_eo;
}
/* create node and clone the entry */
/* normalize the name to start with "/" */
if (strncmp(name, "./", 2) == 0) {
/* remove the "." of "./" */
cur->name = strdup(name + 1);
} else if (name[0] != '/') {
/* prepend a '/' to name */
if ((cur->name = malloc(strlen(name) + 2)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
sprintf(cur->name, "/%s", name);
} else {
/* just set the name */
cur->name = strdup(name);
}
int len = strlen(cur->name) - 1;
if (0 < len) {
/* remove trailing '/' for directories */
if (cur->name[len] == '/') {
cur->name[len] = '\0';
}
cur->basename = strrchr(cur->name, '/') + 1;
/* references */
if (insert_by_path(root, cur) != 0) {
log("ERROR: could not insert %s into tree",
cur->name);
return -ENOENT;
}
} else {
/* this is the directory the subtree filter matches,
do not respect it */
}
if ((cur = init_node()) == NULL) {
return -ENOMEM;
}
archive_read_data_skip(archive);
}
/* free the last unused NODE */
free_node(cur);
/* close archive */
archive_read_free(archive);
lseek(archiveFd, 0, SEEK_SET);
if (options.subtree_filter) {
regfree(&subtree);
free(subtree_filter);
}
return 0;
}
static NODE *
find_modified_node(NODE *start)
{
NODE *ret = NULL;
NODE *run = start;
while (run) {
if (run->modified) {
ret = run;
break;
}
if (run->child) {
if ((ret = find_modified_node(run->child))) {
break;
}
}
run = run->hh.next;
}
return ret;
}
static void
correct_hardlinks_to_node(const NODE *start, const char *old_name,
const char *new_name)
{
const NODE *run = start;
while (run) {
const char *tmp;
if ((tmp = archive_entry_hardlink(run->entry))) {
if (strcmp(tmp, old_name) == 0) {
/* the node in "run" is a hardlink to "node",
* correct the path */
//log("correcting hardlink '%s' from '%s' to '%s'", run->name, old_name, new_name);
archive_entry_set_hardlink(
run->entry, new_name);
}
}
if (run->child) {
correct_hardlinks_to_node(run->child, old_name, new_name);
}
run = run->hh.next;
}
}
void
correct_name_in_entry (NODE *node)
{
if (root->child &&
node->name[0] == '/' &&
archive_entry_pathname(root->child->entry)[0] != '/')
{
log ("correcting name in entry to '%s'", node->name+1);
archive_entry_set_pathname(node->entry, node->name + 1);
} else {
log ("correcting name in entry to '%s'", node->name);
archive_entry_set_pathname(node->entry, node->name);
}
}
static NODE *
get_node_for_path(NODE *start, const char *path)
{
NODE *ret = NULL;
//log("get_node_for_path path: '%s' start: '%s'", path, start->name);
/* Check if start is a perfect match */
if (strcmp(path, start->name + (*path=='/'?0:1)) == 0) {
//log(" get_node_for_path path: '%s' start: '%s' return: '%s'", path, start->name, start->name);
return start;
}
/* Check if one of the children match */
if (start->child) {
const char * basename;
const char * baseend;
/* Find the part of the path we are now looking for */
basename = path + strlen(start->name) - (*path=='/'?0:1);
if (*basename == '/')
basename++;
baseend = strchrnul(basename, '/');
//log("get_node_for_path path: '%s' start: '%s' basename: '%s' len: %ld", path, start->name, basename, baseend - basename);
HASH_FIND(hh, start->child, basename, baseend - basename, ret);
if (ret) {
ret = get_node_for_path(ret, path);
}
}
//log(" get_node_for_path path: '%s' start: '%s' return: '%s'", path, start->name, ret == NULL ? "(null)" : ret->name);
return ret;
}
static NODE *
get_node_for_entry(NODE *start, struct archive_entry *entry)
{
NODE *ret = NULL;
NODE *run = start;
const char *path = archive_entry_pathname(entry);
if (*path == '/') {
path++;
}
while (run) {
const char *name = archive_entry_pathname(run->entry);
if (*name == '/') {
name++;
}
if (strcmp(path, name) == 0) {
ret = run;
break;
}
if (run->child) {
if ((ret = get_node_for_entry(run->child, entry))) {
break;
}
}
run = run->hh.next;
}
return ret;
}
static int
rename_recursively(NODE *start, const char *from, const char *to)
{
char *individualName;
char *newName;
int ret = 0;
NODE *node = start;
/* removing and re-inserting nodes while iterating through
the hashtable is a bad idea, so we copy all node ptrs
into an array first and iterate over that instead */
size_t count = HASH_COUNT(start);
NODE *nodes[count];
log ("%s has %u items", start->parent->name, count);
NODE **dst = &nodes[0];
while (node) {
*dst = node;
++dst;
node = node->hh.next;
}
size_t i;
for (i=0; i<count; ++i) {
node = nodes[i];
if (node->child) {
/* recurse */
ret = rename_recursively(node->child, from, to);
}
remove_child(node);
/* change node name */
individualName = node->name + strlen(from);
if (*to != '/') {
if ((newName = (char *)malloc(strlen(to) +
strlen(individualName) + 2)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
sprintf(newName, "/%s%s", to, individualName);
} else {
if ((newName = (char *)malloc(strlen(to) +
strlen(individualName) + 1)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
sprintf(newName, "%s%s", to, individualName);
}
log ("new name: '%s'", newName);
correct_hardlinks_to_node(root, node->name, newName);
free(node->name);
node->name = newName;
node->basename = strrchr(node->name, '/') + 1;
node->namechanged = 1;
insert_by_path(root, node);
}
return ret;
}
static int
get_temp_file_name(const char *path, char **location)
{
int fh;
/* create name for temp file */
if ((*location = (char *)malloc(
strlen(P_tmpdir) +
strlen("/archivemount_XXXXXX") +
1)) == NULL) {
log("Out of memory");
return -ENOMEM;
}
sprintf(*location, "%s/archivemount_XXXXXX", P_tmpdir);
if ((fh = mkstemp(*location)) == -1) {
log("Could not create temp file name %s: %s",
*location, strerror(errno));
free(*location);
return 0 - errno;
}
close(fh);
unlink(*location);
return 0;
}
/**
* Updates given nodes node->entry by stat'ing node->location. Does not update
* the name!
*/
static int
update_entry_stat(NODE *node)
{
struct stat st;
struct passwd *pwd;
struct group *grp;
if (lstat(node->location, &st) != 0) {
return 0 - errno;
}
archive_entry_set_gid(node->entry, st.st_gid);
archive_entry_set_uid(node->entry, st.st_uid);
archive_entry_set_mtime(node->entry, st.st_mtime, 0);
archive_entry_set_size(node->entry, st.st_size);
archive_entry_set_mode(node->entry, st.st_mode);
archive_entry_set_rdevmajor(node->entry, st.st_dev);
archive_entry_set_rdevminor(node->entry, st.st_dev);
pwd = getpwuid(st.st_uid);
if (pwd) {
archive_entry_set_uname(node->entry, strdup(pwd->pw_name));
}
grp = getgrgid(st.st_gid);
if (grp) {
archive_entry_set_gname(node->entry, strdup(grp->gr_name));
}
return 0;
}
/*
* write a new or modified file to the new archive; used from save()
*/
static void
write_new_modded_file(NODE *node, struct archive_entry *wentry,
struct archive *newarc)
{
if (node->location) {
struct stat st;
int fh = 0;
off_t offset = 0;
void *buf;
ssize_t len = 0;
/* copy stat info */
if (lstat(node->location, &st) != 0) {
log("Could not lstat temporary file %s: %s",
node->location,
strerror(errno));
return;
}
archive_entry_copy_stat(wentry, &st);
/* open temporary file */
fh = open(node->location, O_RDONLY);
if (fh == -1) {
log("Fatal error opening modified file %s at "
"location %s, giving up",
node->name, node->location);
return;
}
/* write header */
archive_write_header(newarc, wentry);
if (S_ISREG(st.st_mode)) {
/* regular file, copy data */
if ((buf = malloc(MAXBUF)) == NULL) {
log("Out of memory");
return;
}
while ((len = pread(fh, buf, (size_t)MAXBUF,
offset)) > 0)
{
archive_write_data(newarc, buf, len);
offset += len;
}
free(buf);
}
if (len == -1) {
log("Error reading temporary file %s for file %s: %s",
node->location,
node->name,
strerror(errno));
close(fh);
return;
}
/* clean up */
close(fh);
if (S_ISDIR(st.st_mode)) {
if (rmdir(node->location) == -1) {
log("WARNING: rmdir '%s' failed: %s",
node->location, strerror(errno));
}
} else {
if (unlink(node->location) == -1) {
log("WARNING: unlinking '%s' failed: %s",
node->location, strerror(errno));
}
}
} else {
/* no data, only write header (e.g. when node is a link!) */
//log("writing header for file %s", archive_entry_pathname(wentry));
archive_write_header(newarc, wentry);
}
/* mark file as written */
node->modified = 0;
}
static int
save(const char *archiveFile)
{
struct archive *oldarc;
struct archive *newarc;
struct archive_entry *entry;
int tempfile;
int format;
int compression;
char *oldfilename;
NODE *node;
oldfilename = malloc(strlen(archiveFile) + 5 + 1);
if (!oldfilename) {
log("Could not allocate memory for oldfilename");
return 0 - ENOMEM;
}
/* unfortunately libarchive does not support modification of
* compressed archives, so a new archive has to be written */
/* rename old archive */
sprintf(oldfilename, "%s.orig", archiveFile);
close(archiveFd);
if (rename(archiveFile, oldfilename) < 0) {
int err = errno;
char *buf = NULL;
char *unknown = "<unknown>";
if (getcwd(buf, 0) == NULL) {
/* getcwd failed, set buf to sth. reasonable */
buf = unknown;
}
log("Could not rename old archive file (%s/%s): %s",
buf, archiveFile, strerror(err));
free(buf);
archiveFd = open(archiveFile, O_RDONLY);
return 0 - err;
}
archiveFd = open(oldfilename, O_RDONLY);
free(oldfilename);
/* open old archive */
if ((oldarc = archive_read_new()) == NULL) {
log("Out of memory");
return -ENOMEM;
}
if (archive_read_support_filter_all(oldarc) != ARCHIVE_OK) {
log("%s", archive_error_string(oldarc));
return archive_errno(oldarc);
}
if (archive_read_support_format_all(oldarc) != ARCHIVE_OK) {
log("%s", archive_error_string(oldarc));
return archive_errno(oldarc);
}
if (archive_read_open_fd(oldarc, archiveFd, 10240) != ARCHIVE_OK) {
log("%s", archive_error_string(oldarc));
return archive_errno(oldarc);
}
format = archive_format(oldarc);
compression = archive_filter_code(oldarc, 0);
/*
log("format of old archive is %s (%d)",
archive_format_name(oldarc),
format);
log("compression of old archive is %s (%d)",
archive_compression_name(oldarc),
compression);
*/
/* open new archive */
if ((newarc = archive_write_new()) == NULL) {
log("Out of memory");
return -ENOMEM;
}
switch(compression) {
case ARCHIVE_COMPRESSION_GZIP:
archive_write_add_filter_gzip(newarc);
break;
case ARCHIVE_COMPRESSION_BZIP2:
archive_write_add_filter_bzip2(newarc);
break;
case ARCHIVE_COMPRESSION_COMPRESS:
case ARCHIVE_COMPRESSION_NONE:
default:
archive_write_add_filter_none(newarc);
break;
}
#if 0
if (archive_write_set_format(newarc, format) != ARCHIVE_OK) {
return -ENOTSUP;
}
#endif
if (format & ARCHIVE_FORMAT_CPIO
|| format & ARCHIVE_FORMAT_CPIO_POSIX)
{
archive_write_set_format_cpio(newarc);
//log("set write format to posix-cpio");
} else if (format & ARCHIVE_FORMAT_SHAR
|| format & ARCHIVE_FORMAT_SHAR_BASE
|| format & ARCHIVE_FORMAT_SHAR_DUMP)
{
archive_write_set_format_shar(newarc);
//log("set write format to binary shar");
} else if (format & ARCHIVE_FORMAT_TAR_PAX_RESTRICTED)
{
archive_write_set_format_pax_restricted(newarc);
//log("set write format to binary pax restricted");
} else if (format & ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE)
{
archive_write_set_format_pax(newarc);
//log("set write format to binary pax interchange");
} else if (format & ARCHIVE_FORMAT_TAR_USTAR
|| format & ARCHIVE_FORMAT_TAR
|| format & ARCHIVE_FORMAT_TAR_GNUTAR
|| format == 0)
{
archive_write_set_format_ustar(newarc);
//log("set write format to ustar");
} else {
log("writing archives of format %d (%s) is not "
"supported", format,