-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspill.c
1728 lines (1573 loc) · 49.6 KB
/
spill.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
/*
spill - segregated package install logical linker
**********************************************************************
* Copyright (C) Richard P. Curnow 2003, 2004, 2005, 2006, 2009
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
**********************************************************************
*/
/*
* Naming :
* "source" means the directory tree where the package is actually installed.
* "destination" means the directory where the symbolic links will be which
* point into "source"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "memory.h"
#include "version.h"
#define RECORD_DIR ".spill"
struct string_node {/*{{{*/
struct string_node *next;
char *string;
};
/*}}}*/
static struct string_node *ignores = NULL;
static FILE *conflict_file = NULL;
static char *caten(const char *s1, const char *s2)/*{{{*/
{
int n1, n2, n;
char *res;
n1 = strlen(s1);
n2 = strlen(s2);
n = n1 + n2;
res = new_array(char, n + 1);
if (n1 > 0) memcpy(res, s1, n1);
if (n2 > 0) memcpy(res+n1, s2, n2);
res[n1+n2] = 0;
return res;
}
/*}}}*/
static char *dfcaten(const char *d, const char *f)/*{{{*/
{
int nd, nf, n;
char *res;
nd = strlen(d);
nf = strlen(f);
n = nd + nf;
res = new_array(char, n + 2);
if (nd > 0) memcpy(res, d, nd);
res[nd] = '/';
if (nf > 0) memcpy(res+nd+1, f, nf);
res[nd+nf+1] = 0;
return res;
}
/*}}}*/
static char *dfcaten3(const char *x, const char *y, const char *z)/*{{{*/
{
int nx, ny, nz, n;
char *res;
nx = strlen(x);
ny = strlen(y);
nz = strlen(z);
n = nx + ny + nz;
res = new_array(char, n + 3);
if (nx > 0) memcpy(res, x, nx);
res[nx] = '/';
if (ny > 0) memcpy(res+nx+1, y, ny);
res[nx+ny+1] = '/';
if (nz > 0) memcpy(res+nx+ny+2, z, nz);
res[nx+ny+nz+2] = 0;
return res;
}
/*}}}*/
static char *normalise_dir(const char *dir)/*{{{*/
{
/* Given a relative path, find its absolute path when starting from pwd */
char startdir[PATH_MAX];
char srcdir[PATH_MAX];
if (getcwd(startdir, PATH_MAX) == NULL) {
fprintf(stderr, "Couldn't get start directory!\n");
exit(1);
}
if (chdir(dir) < 0) {
fprintf(stderr, "Couldn't cd to %s : %s!\n", dir, strerror(errno));
exit(1);
}
if (getcwd(srcdir, PATH_MAX) == NULL) {
fprintf(stderr, "Couldn't read absolute src directory!\n");
exit(1);
}
if (chdir(startdir) < 0) {
fprintf(stderr, "Couldn't cd back to starting directory %s!\n", startdir);
exit(1);
}
return new_string(srcdir);
}
/*}}}*/
static char *cleanup_dir(const char *dir)/*{{{*/
{
/* Remove doubled / and trailing /, unless the / was the only char (i.e.
* install into /bin, /sbin etc) */
int len;
char *res;
int slash;
char *p;
const char *q;
len = strlen(dir);
res = new_array(char, len + 1);
p = res;
q = dir;
slash = 0;
while (*q) {
if (*q == '/') {
if (!slash) *p++ = *q;
slash = 1;
} else {
*p++ = *q;
slash = 0;
}
q++;
}
if (slash) p--;
*p = 0;
return res;
}
/*}}}*/
static int check_sane(const char *tag, const char *dir)/*{{{*/
{
/* Look at contents of directory, make sure the expected directories are present. */
DIR *d;
struct dirent *de;
struct stat sb;
int has_bindir = 0;
int has_sbindir = 0;
int has_libdir = 0;
if (!strcmp(dir, "")) d = opendir("/");
else d = opendir(dir);
if (d) {
while ((de = readdir(d))) {
char *full_path = dfcaten(dir, de->d_name);
if (stat(full_path, &sb) < 0) {
fprintf(stderr, "Couldn't stat %s", full_path);
} else {
if (S_ISDIR(sb.st_mode)) {
has_bindir |= !strcmp(de->d_name, "bin");
has_sbindir |= !strcmp(de->d_name, "sbin");
has_libdir |= !strcmp(de->d_name, "lib");
}
}
free(full_path);
}
closedir(d);
} else {
fprintf(stderr, "%s directory %s coudn't be opened.\n", tag, dir);
return 0;
exit(1);
}
if (!has_bindir && !has_libdir && !has_sbindir) {
fprintf(stderr, "%s directory %s doesn't have bin, sbin or lib subdirectory\n", tag, dir);
return 0;
}
return 1;
}
/*}}}*/
static void extract_package_details(const char *src, char **pkg, char **version)/*{{{*/
{
/* Given the path to the installation area, pull the final two pathname
* components out as the package and version+build */
const char *end, *s1, *s2;
end = src;
while (*end) end++;
s1 = end;
while ((s1 >= src) && (*s1 != '/')) s1--;
s2 = s1 - 1;
while ((s2 >= src) && (*s2 != '/')) s2--;
*pkg = new_array(char, (s1 - s2));
*version = new_array(char, (end - s1));
memcpy(*pkg, s2+1, s1-s2-1);
(*pkg)[s1-s2-1] = 0;
memcpy(*version, s1+1, end-s1);
return;
}
/*}}}*/
static void add_ignore(char *path)/*{{{*/
{
struct string_node *n;
n = new(struct string_node);
n->string = new_string(path);
n->next = ignores;
ignores = n;
}
/*}}}*/
static int check_ignore(const char *head, const char *tail)/*{{{*/
{
int len;
char *path;
struct string_node *a;
int result;
/* Early out */
if (!ignores) return 0;
/* If 'head' starts with a '/', we drop it. We put a '/' between head and
* tail. */
len = strlen(head) + strlen(tail) + 2;
path = new_array(char, len);
if (head[0] == '/') {
strcpy(path, head + 1);
} else {
strcpy(path, head);
}
if (head[0] != '\0') {
strcat(path, "/");
}
strcat(path, tail);
result = 0;
a = ignores;
do {
if (!strcmp(a->string, path)) {
result = 1;
break;
}
a = a->next;
} while (a);
free(path);
return result;
}
/*}}}*/
struct options {/*{{{*/
unsigned quiet:1;
unsigned dry_run:1;
unsigned expand:1;
unsigned force:1;
unsigned override:1;
};
/*}}}*/
enum source_type {/*{{{*/
ST_ERROR,
ST_DIR,
ST_OTHER
};
/*}}}*/
enum dest_type {/*{{{*/
DT_VOID, /* nothing there */
DT_ERROR, /* some error examining it */
DT_LINK_EXACT, /* readlink(dest) identical with source */
DT_LINK_SAME_SAME, /* readlink(dest) appears to link to same version of
same package as source, but installed elsewhere (or
with a different representation of the same path.)
NOTE .. don't check (dev,inode) to see if it's
LINK_EXACT, because the user might want to replace
the package with relative links to replace absolute
etc.
*/
DT_LINK_SAME_OTHER, /* link to same package, other version */
DT_LINK_OTHER_DIR, /* link to directory in something else */
DT_LINK_OTHER_FILE, /* link to file/dev/fifo/socket in something else */
DT_LINK_UNKNOWN, /* link to something, readlink() doesn't have right format. */
DT_DIRECTORY, /* directory */
DT_OTHER /* file, device, fifo, socket etc */
};
/*}}}*/
typedef int (*action_fn)/*{{{*/
(enum source_type src_type,
enum dest_type dest_type,
const char *relative_path,
const char *full_src_path,
const char *full_dest_path,
const char *src,
const char *dest,
const char *taildir,
const char *tailfile,
const char *pkg,
const char *version,
const char *other_pkg,
const char *other_version,
struct options *opt
);
/*}}}*/
static int
traverse_action(const char *rel_path,
const char *src,
const char *dest,
const char *pkg,
const char *version,
const char *tail,
struct options *opt,
action_fn fn);
/*{{{ static enum dest_type find_dest_type*/
static enum dest_type
find_dest_type(const char *full_dest_path,
const char *full_src_path,
const char *relative_path,
const char *taildir,
const char *tailfile,
const char *pkg,
const char *version,
char **res_other_pkg,
char **res_other_version
)
{
struct stat dsb;
enum dest_type result;
char *src_path;
if (relative_path) {
char *tail = dfcaten(taildir, tailfile);
src_path = caten(relative_path, tail);
free(tail);
} else {
src_path = new_string(full_src_path);
}
if (lstat(full_dest_path, &dsb) < 0) {
if (errno == ENOENT) {
result = DT_VOID;
} else {
fprintf(stderr, "Couldn't stat <%s> : %s!\n", full_dest_path, strerror(errno));
result = DT_ERROR;
}
} else {
/* stat ok */
if (S_ISDIR(dsb.st_mode)) {
result = DT_DIRECTORY;
} else if (S_ISLNK(dsb.st_mode)) {
/* Decide whether the link is to the same package or not. */
char linkbuf[PATH_MAX];
int link_len;
link_len = readlink(full_dest_path, linkbuf, PATH_MAX);
if (link_len < 0) {
fprintf(stderr, "Couldn't readlink on <%s> : %s!\n", full_dest_path, strerror(errno));
result = DT_ERROR;
} else {
/* linkbuf is where the link points to. The tail part of it
* should be '/tail/de->d_name', the part before this should be
* the path to the package base for which the link has already
* been installed. */
char *tail_part;
int tail_len;
linkbuf[link_len] = 0;
tail_part = dfcaten(taildir, tailfile);
tail_len = strlen(tail_part);
if (tail_len > link_len) {
result = DT_ERROR;
/* Obviously can't be a link pointing to an install area like the
sort spill creates itself, it must be a link pointing to something
else. No point reporting this specifically, it's just an
uncorrectable error. */
} else {
if (!strcmp(tail_part, linkbuf + link_len - tail_len)) {
/* Matched, deal with prefix. */
char *link_prefix;
char *other_pkg, *other_version;
int prefix_len;
prefix_len = link_len - tail_len;
link_prefix = new_array(char, 1 + prefix_len);
memcpy(link_prefix, linkbuf, prefix_len);
link_prefix[prefix_len] = 0;
extract_package_details(link_prefix, &other_pkg, &other_version);
if (res_other_pkg) *res_other_pkg = new_string(other_pkg);
if (res_other_version) *res_other_version = new_string(other_version);
if (!strcmp(linkbuf, src_path)) {
result = DT_LINK_EXACT;
} else if (!strcmp(other_pkg, pkg)) {
if (!strcmp(other_version, version)) {
/* Same version */
/* FIXME : what if the links point to another place where the package is installed,
* insead of the "source" we're doing now? Ought to fix this. */
result = DT_LINK_SAME_SAME;
} else {
result = DT_LINK_SAME_OTHER;
}
} else {
/* Links to another package. */
/* Check if link is to a directory, then explode it and retry. */
struct stat lsb;
if (stat(linkbuf, &lsb) < 0) {
fprintf(stderr, "** ERROR, link at <%s> is stale, remove this and retry!\n", full_dest_path);
result = DT_ERROR;
} else {
if (S_ISDIR(lsb.st_mode)) {
result = DT_LINK_OTHER_DIR;
} else {
result = DT_LINK_OTHER_FILE;
}
}
}
free(link_prefix);
free(other_pkg);
free(other_version);
} else {
result = DT_LINK_UNKNOWN;
}
}
free(tail_part);
}
} else {
result = DT_OTHER;
}
}
free(src_path);
return result;
}
/*}}}*/
struct string_list {/*{{{*/
struct string_list *next;
struct string_list *prev;
char *string;
};
/*}}}*/
static struct string_list *new_string_list(void)/*{{{*/
{
struct string_list *res;
res = new(struct string_list);
res->next = res->prev = res;
res->string = NULL;
return res;
}
/*}}}*/
static void append(struct string_list *head, char *string) {/*{{{*/
struct string_list *new_cell;
new_cell = new_string_list();
new_cell->string = new_string(string);
new_cell->prev = head->prev;
new_cell->next = head;
head->prev->next = new_cell;
head->prev = new_cell;
}
/*}}}*/
static void free_string_list(struct string_list *head)/*{{{*/
{
struct string_list *x, *next;
for (x=head->next; x!=head; x=next) {
next = x->next;
if (x->string) free(x->string);
free(x);
}
}
/*}}}*/
static void emit_conflict(const char *full_dest_path)/*{{{*/
{
if (conflict_file) {
fprintf(conflict_file, "%s\n", full_dest_path);
}
return;
}
/*}}}*/
static int files_differ(const char *name1, const char *name2)/*{{{*/
{
/* Return 0 if files match, 1 if they differ, 2 if there was a problem. */
FILE *x1, *x2;
struct stat sb;
if ((stat(name1, &sb) < 0) || (!S_ISREG(sb.st_mode))) return 2;
if ((stat(name2, &sb) < 0) || (!S_ISREG(sb.st_mode))) return 2;
x1 = fopen(name1, "rb");
x2 = fopen(name2, "rb");
if (x1 && x2) {
int result = 0;
int c1, c2;
do {
c1 = getc(x1);
c2 = getc(x2);
if (c1 == EOF) {
if (c2 == EOF) {
result = 0;
break;
} else {
result = 1;
break;
}
} else if (c2 == EOF) {
result = 1;
break;
} else if (c1 != c2) {
result = 1;
break;
}
} while (1);
fclose(x1);
fclose(x2);
return result;
} else {
if (x1) fclose(x1);
if (x2) fclose(x2);
return 2;
}
}
/*}}}*/
static int do_expand(const char *dir_link, struct options *opt)/*{{{*/
{
/* Given the path to a symbolic link that points at a directory, replace that
link by a directory, and inside that new directory create symbolic links
that point at each entry in the directory to which the removed link used
to point.
If the existing link was absolute, create absolute links. Otherwise,
create relative links.
Return 1 if an error occurs, 0 otherwise.
*/
char buffer[PATH_MAX];
int link_len;
int is_absolute;
DIR *d;
struct dirent *de;
struct stat link_stat;
link_len = readlink(dir_link, buffer, PATH_MAX);
if (link_len < 0) {
printf("!! ERROR Could not expand link <%s> into a directory : %s\n",
dir_link, strerror(errno));
return 1;
}
buffer[link_len] = '\0';
/* Get the stat record for the directory that the link points to. We'll use
its mode when creating the replacement directory, for want of something
better. */
if (stat(buffer, &link_stat) < 0) {
printf("!! ERROR Could not stat link <%s> : %s\n",
buffer, strerror(errno));
return 1;
}
is_absolute = (buffer[0] == '/') ? 1 : 0;
/* TODO */
d = opendir(buffer);
if (d) {
/* Build list of directory entries. */
struct string_list *sl = new_string_list();
struct string_list *x;
while ((de = readdir(d))) {
if (!strcmp(de->d_name, ".")) continue;
if (!strcmp(de->d_name, "..")) continue;
append(sl, de->d_name);
}
closedir(d);
/* Now clear the link, put a directory in its place and create a set of
* links inside. */
if (unlink(dir_link) < 0) {
printf("!! ERROR Could not remove the link at <%s> : %s\n",
dir_link, strerror(errno));
free_string_list(sl);
return 1;
}
if (mkdir (dir_link, link_stat.st_mode) < 0) {
printf("!! ERROR Could not create new directory at <%s> : %s\n",
dir_link, strerror(errno));
free_string_list(sl);
return 1;
}
/* Now populate it with links */
for (x=sl->next; x!=sl; x=x->next) {
char *link_site, *target_site;
link_site = dfcaten(dir_link, x->string);
if (is_absolute) {
target_site = dfcaten(buffer, x->string);
} else {
target_site = dfcaten3("..", buffer, x->string);
}
if (symlink(target_site, link_site) < 0) {
printf("!! ERROR Could not create symlink from <%s> to <%s> : %s\n",
link_site, target_site, strerror(errno));
free_string_list(sl);
return 1;
}
if (!opt->quiet) {
printf("** EXPANDDIR Created link from <%s> to <%s>\n",
link_site, target_site);
}
free(link_site);
free(target_site);
}
free_string_list(sl);
} else {
printf("!! ERROR Could not open directory <%s> to read contents : %s\n",
dir_link, strerror(errno));
return 1;
}
return 0;
}
/*}}}*/
/*{{{ static int pre_install*/
static int
pre_install(enum source_type src_type,
enum dest_type dest_type,
const char *relative_path,
const char *full_src_path,
const char *full_dest_path,
const char *src,
const char *dest,
const char *taildir,
const char *tailfile,
const char *pkg,
const char *version,
const char *other_pkg,
const char *other_version,
struct options *opt
)
{
char *new_tail;
int result = -1;
int verbose = opt->dry_run && !opt->quiet;
char *src_path;
char *new_relative_path;
if (relative_path) {
char *tail = dfcaten(taildir, tailfile);
src_path = caten(relative_path, tail);
free(tail);
} else {
src_path = new_string(full_src_path);
}
/* (otherwise user gets messages twice.) */
switch (src_type) {
case ST_ERROR:
fprintf(stderr, "Could not examine source <%s>!\n", src_path);
result = 1;
break;
case ST_DIR:
switch (dest_type) {
case DT_VOID:
if (verbose) printf("** NEWDIRLINK from <%s> to <%s>\n", full_dest_path, src_path);
result = 0;
break;
case DT_ERROR:
printf("!! ERROR can't examine path <%s>\n", full_dest_path);
emit_conflict(full_dest_path);
result = 1;
break;
case DT_LINK_EXACT:
if (verbose) printf("** OK dir <%s> already linked to the required path\n",
full_dest_path);
result = 0;
break;
case DT_LINK_SAME_SAME:
if (verbose) printf("** REPLACEDIR dir <%s> linked to <%s> through another path\n",
full_dest_path, pkg);
result = 0;
break;
case DT_LINK_SAME_OTHER:
if (verbose) printf("** REPLACEDIR <%s> linked to other version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 0;
break;
case DT_LINK_OTHER_DIR:
if (!opt->expand) {
printf("!! NEEDEXPN <%s> linked to a directory in version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 1; /* User has to manually resolve this one. */
break;
} else {
result = do_expand(full_dest_path, opt);
if (result) break; /* Error occurred whilst expanding, don't proceed */
/* OK, expansion worked, now treat as though it's a directory. */
}
/* NOTE: DELIBERATE FALL THROUGH FROM ELSE BRANCH */
case DT_DIRECTORY:
new_tail = dfcaten(taildir, tailfile);
new_relative_path = relative_path ? dfcaten("..", relative_path) : NULL;
result = traverse_action(new_relative_path, src, dest, pkg, version, new_tail, opt, pre_install);
free(new_tail);
if (new_relative_path) free(new_relative_path);
break;
case DT_LINK_OTHER_FILE:
if (opt->override) {
printf("** OVERRIDE <%s> linked to a non-directory in version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 0;
} else {
printf("!! CONFLICT <%s> linked to a non-directory in version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 1; /* User has to manually resolve this one. */
emit_conflict(full_dest_path);
}
break;
case DT_LINK_UNKNOWN:
if (opt->override) {
printf("** OVERWRITE <%s> linked to something I don't understand\n",
full_dest_path);
result = 0;
} else {
printf("!! CONFLICT <%s> linked to something I don't understand\n",
full_dest_path);
result = 1; /* User has to manually resolve this one. */
emit_conflict(full_dest_path);
}
break;
case DT_OTHER:
printf("!! CONFLICT <%s> is not a link or directory\n",
full_dest_path);
result = 1;
emit_conflict(full_dest_path);
break;
}
break;
case ST_OTHER:
switch (dest_type) {
case DT_VOID:
if (verbose) printf("** NEWLINK from <%s> to <%s>\n", full_dest_path, src_path);
result = 0;
break;
case DT_ERROR:
printf("!! ERROR can't examine path <%s>\n", full_dest_path);
result = 1;
emit_conflict(full_dest_path);
break;
case DT_LINK_EXACT:
if (verbose) printf("** OK <%s> already linked to the required path\n",
full_dest_path);
result = 0;
break;
case DT_LINK_SAME_SAME:
if (verbose) printf("** REPLACE <%s> linked to <%s> through another path\n",
full_dest_path, pkg);
result = 0;
break;
case DT_LINK_SAME_OTHER:
if (verbose) printf("** REPLACE <%s> linked to other version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 0;
break;
case DT_LINK_OTHER_DIR:
if (opt->override) {
printf("** OVERRIDE <%s> linked to a directory in version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 0;
} else {
printf("!! CONFLICT <%s> linked to a directory in version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 1; /* User has to manually resolve this one. */
emit_conflict(full_dest_path);
}
break;
case DT_LINK_OTHER_FILE:
if (opt->override) {
int d = files_differ(full_dest_path, src_path);
printf("** OVERRIDE <%s> linked to a non-directory in version <%s> of package <%s>%s\n",
full_dest_path, other_version, other_pkg,
(d == 0) ? " (content identical)" :
(d == 1) ? " (content differs)" : ""
);
result = 0;
} else {
printf("!! CONFLICT <%s> linked to a non-directory in version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
result = 1; /* User has to manually resolve this one. */
emit_conflict(full_dest_path);
}
break;
case DT_LINK_UNKNOWN:
if (opt->override) {
int d = files_differ(full_dest_path, src_path);
printf("** OVERWRITE <%s> linked to something I don't understand%s\n",
full_dest_path,
(d == 0) ? " (content identical)" :
(d == 1) ? " (content differs)" : ""
);
result = 0;
} else {
printf("!! CONFLICT <%s> linked to something I don't understand\n",
full_dest_path);
result = 1; /* User has to manually resolve this one. */
emit_conflict(full_dest_path);
}
break;
case DT_DIRECTORY:
printf("!! CONFLICT <%s> is a directory, can't link to <%s>\n",
full_dest_path, src_path);
result = 1; /* User has to manually resolve this one. */
emit_conflict(full_dest_path);
break;
case DT_OTHER:
printf("!! CONFLICT <%s> is not a link or directory, can't link to <%s>\n",
full_dest_path, src_path);
result = 1;
emit_conflict(full_dest_path);
break;
}
break;
}
free(src_path);
assert (result >= 0);
return result;
}
/*}}}*/
/*{{{ static int do_install */
static int
do_install (enum source_type src_type,
enum dest_type dest_type,
const char *relative_path,
const char *full_src_path,
const char *full_dest_path,
const char *src,
const char *dest,
const char *taildir,
const char *tailfile,
const char *pkg,
const char *version,
const char *other_pkg,
const char *other_version,
struct options *opt
)
{
/* FIXME : There's lots of common code here that should be made explicitly common */
char *new_tail;
char *new_relative_path;
char *linked_path;
int result;
if (relative_path) {
char *tail = dfcaten(taildir, tailfile);
linked_path = caten(relative_path, tail);
free(tail);
} else {
linked_path = new_string(full_src_path);
}
switch (src_type) {
case ST_ERROR:
fprintf(stderr, "Could not examine source <%s>!\n", full_src_path);
free(linked_path);
return 1;
break;
case ST_DIR:
switch (dest_type) {
case DT_VOID:
if (symlink(linked_path, full_dest_path) < 0) {
printf("!! FAILED : can't create symlink from <%s> to <%s> : %s\n",
full_dest_path, linked_path, strerror(errno));
free(linked_path);
return 1;
}
if (!opt->quiet) printf("** NEWDIRLINK from <%s> to <%s>\n", full_dest_path, linked_path);
free(linked_path);
return 0;
case DT_LINK_EXACT:
/* Link already exists pointing to the right place. No-op for installing. */
if (!opt->quiet) printf("** OK dir <%s> already linked to the required path <%s>\n",
full_dest_path, linked_path);
free(linked_path);
return 0;
case DT_LINK_SAME_SAME:
case DT_LINK_SAME_OTHER:
if (unlink(full_dest_path) < 0) {
printf("!! FAILED : can't remove old link <%s> : %s\n", full_dest_path, strerror(errno));
free(linked_path);
return 1;
} else {
if (symlink(linked_path, full_dest_path) < 0) {
printf("!! FAILED : can't create symlink from <%s> to <%s> : %s\n",
full_dest_path, linked_path, strerror(errno));
free(linked_path);
return 1;
} else {
if (!opt->quiet) {
printf("** REPLACEDIR <%s> previously linked to version <%s> of package <%s>\n",
full_dest_path, other_version, other_pkg);
}
}
free(linked_path);
}
return 0;
case DT_DIRECTORY:
new_tail = dfcaten(taildir, tailfile);
new_relative_path = relative_path ? dfcaten("..", relative_path) : NULL;
result = traverse_action(new_relative_path, src, dest, pkg, version, new_tail, opt, do_install);
free(new_tail);
free(new_relative_path);
free(linked_path);
return result;
case DT_LINK_OTHER_DIR:
case DT_LINK_OTHER_FILE:
case DT_LINK_UNKNOWN:
if (opt->override) {
if (unlink(full_dest_path) < 0) {
printf("!! FAILED : can't remove old link <%s> : <%s>\n", full_dest_path, strerror(errno));
free(linked_path);
return 1;
} else {
if (symlink(linked_path, full_dest_path) < 0) {
printf("!! FAILED : can't create override symlink from <%s> to <%s> : %s\n",
full_dest_path, linked_path, strerror(errno));
free(linked_path);
return 1;
}
if (!opt->quiet) printf("** NEWDIRLINK (OVERRIDE) from <%s> to <%s>\n", full_dest_path, linked_path);
free(linked_path);
return 0;
}
} else {
/* No override, fall through */
}
case DT_ERROR:
case DT_OTHER:
printf("!! CALAMITY : I shouldn't be here, my pre-install check should have failed (problem path=<%s>)!\n",
full_dest_path);
free(linked_path);
return 1;
}
break;
case ST_OTHER:
switch (dest_type) {
case DT_VOID:
if (symlink(linked_path, full_dest_path) < 0) {