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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
|
/*
* dpkg - main program for package management
* archives.c - actions that process archive files, mainly unpack
*
* Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
* Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
* Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
* Copyright © 2011 Linaro Limited
* Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This 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, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <compat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <obstack.h>
#define obstack_chunk_alloc m_malloc
#define obstack_chunk_free free
#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg.h>
#include <dpkg/path.h>
#include <dpkg/fdio.h>
#include <dpkg/buffer.h>
#include <dpkg/subproc.h>
#include <dpkg/command.h>
#include <dpkg/file.h>
#include <dpkg/treewalk.h>
#include <dpkg/tarfn.h>
#include <dpkg/options.h>
#include <dpkg/triglib.h>
#include <dpkg/db-ctrl.h>
#include "filesdb.h"
#include "main.h"
#include "archives.h"
#include "filters.h"
static inline void
fd_writeback_init(int fd)
{
/* Ignore the return code as it should be considered equivalent to an
* asynchronous hint for the kernel, we are doing an fsync() later on
* anyway. */
#if defined(SYNC_FILE_RANGE_WRITE)
sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
#elif defined(HAVE_POSIX_FADVISE)
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
#endif
}
static struct obstack tar_pool;
static bool tar_pool_init = false;
/**
* Allocate memory from the tar memory pool.
*/
static void *
tar_pool_alloc(size_t size)
{
if (!tar_pool_init) {
obstack_init(&tar_pool);
tar_pool_init = true;
}
return obstack_alloc(&tar_pool, size);
}
/**
* Free memory from the tar memory pool.
*/
static void
tar_pool_free(void *ptr)
{
obstack_free(&tar_pool, ptr);
}
/**
* Release the tar memory pool.
*/
static void
tar_pool_release(void)
{
if (tar_pool_init) {
obstack_free(&tar_pool, NULL);
tar_pool_init = false;
}
}
struct fileinlist *
tar_filenamenode_queue_push(struct filenamenode_queue *queue,
struct filenamenode *namenode)
{
struct fileinlist *node;
node = tar_pool_alloc(sizeof(*node));
node->namenode = namenode;
node->next = NULL;
*queue->tail = node;
queue->tail = &node->next;
return node;
}
static void
tar_filenamenode_queue_pop(struct filenamenode_queue *queue,
struct fileinlist **tail_prev,
struct fileinlist *node)
{
tar_pool_free(node);
queue->tail = tail_prev;
*tail_prev = NULL;
}
/**
* Check if a file or directory will save a package from disappearance.
*
* A package can only be saved by a file or directory which is part
* only of itself - it must be neither part of the new package being
* installed nor part of any 3rd package (this is important so that
* shared directories don't stop packages from disappearing).
*/
bool
filesavespackage(struct fileinlist *file,
struct pkginfo *pkgtobesaved,
struct pkginfo *pkgbeinginstalled)
{
struct filepackages_iterator *iter;
struct pkgset *divpkgset;
struct pkginfo *thirdpkg;
debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
/* If the file is a contended one and it's overridden by either
* the package we're considering disappearing or the package
* we're installing then they're not actually the same file, so
* we can't disappear the package - it is saved by this file. */
if (file->namenode->divert && file->namenode->divert->useinstead) {
divpkgset = file->namenode->divert->pkgset;
if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
return true;
}
}
/* Is the file in the package being installed? If so then it can't save. */
if (file->namenode->flags & fnnf_new_inarchive) {
debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
return false;
}
/* Look for a 3rd package which can take over the file (in case
* it's a directory which is shared by many packages. */
iter = filepackages_iter_new(file->namenode);
while ((thirdpkg = filepackages_iter_next(iter))) {
debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
pkg_name(thirdpkg, pnaw_always));
/* Is this not the package being installed or the one being
* checked for disappearance? */
if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
continue;
/* A Multi-Arch: same package can share files and their presence in a
* third package of the same set is not a sign that we can get rid of
* it. */
if (pkgtobesaved->installed.multiarch == PKG_MULTIARCH_SAME &&
thirdpkg->set == pkgtobesaved->set)
continue;
/* If !fileslistvalid then we've already disappeared this one, so
* we shouldn't try to make it take over this shared directory. */
debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
if (!thirdpkg->clientdata->fileslistvalid) {
debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
continue;
}
/* We've found a package that can take this file. */
debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
filepackages_iter_free(iter);
return false;
}
filepackages_iter_free(iter);
debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
return true;
}
static void
md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
struct filenamenode *namenode)
{
struct pkginfo *otherpkg;
struct conffile *conff;
debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
namenode->name);
for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
if (otherpkg == pkg)
continue;
/* If we are reinstalling, even if the other package is only unpacked,
* we can always make use of the Conffiles hash value from an initial
* installation, if that happened at all. */
if (otherpkg->status <= PKG_STAT_UNPACKED &&
dpkg_version_compare(&otherpkg->installed.version,
&otherpkg->configversion) != 0)
continue;
for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
if (conff->obsolete)
continue;
if (strcmp(conff->name, namenode->name) == 0)
break;
}
if (conff) {
strcpy(oldhash, conff->hash);
debug(dbg_conffdetail,
"tarobject found shared conffile, from pkg %s (%s); hash=%s",
pkg_name(otherpkg, pnaw_always),
pkg_status_name(otherpkg), oldhash);
break;
}
}
/* If no package was found with a valid Conffiles field, we make the
* risky assumption that the hash of the current .dpkg-new file is
* the one of the previously unpacked package. */
if (otherpkg == NULL) {
md5hash(pkg, oldhash, oldname);
debug(dbg_conffdetail,
"tarobject found shared conffile, from disk; hash=%s", oldhash);
}
}
void cu_pathname(int argc, void **argv) {
path_remove_tree((char*)(argv[0]));
}
int tarfileread(void *ud, char *buf, int len) {
struct tarcontext *tc= (struct tarcontext*)ud;
int r;
r = fd_read(tc->backendpipe, buf, len);
if (r < 0)
ohshite(_("error reading from dpkg-deb pipe"));
return r;
}
static void
tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
{
struct dpkg_error err;
size_t r;
r = te->size % TARBLKSZ;
if (r == 0)
return;
if (fd_skip(tc->backendpipe, TARBLKSZ - r, &err) < 0)
ohshit(_("cannot skip padding for file '%.255s': %s"), te->name, err.str);
}
static void
tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
{
/* We need to advance the tar file to the next object, so read the
* file data and set it to oblivion. */
if (ti->type == TAR_FILETYPE_FILE) {
struct dpkg_error err;
char fnamebuf[256];
if (fd_skip(tc->backendpipe, ti->size, &err) < 0)
ohshit(_("cannot skip file '%.255s' (replaced or excluded?) from pipe: %s"),
path_quote_filename(fnamebuf, ti->name, 256), err.str);
tarobject_skip_padding(tc, ti);
}
}
struct varbuf_state fname_state;
struct varbuf fnamevb;
struct varbuf fnametmpvb;
struct varbuf fnamenewvb;
struct pkg_deconf_list *deconfigure = NULL;
static time_t currenttime;
static int
does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
{
struct dependency *dep;
debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
versiondescribe(&old_pkgbin->version, vdew_always));
for (dep = new_pkgbin->depends; dep; dep = dep->next) {
if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
continue;
debug(dbg_depcondetail,"does_replace ... found old, version %s",
versiondescribe(&dep->list->version,vdew_always));
if (!versionsatisfied(old_pkgbin, dep->list))
continue;
/* The test below can only trigger if dep_replaces start having
* arch qualifiers different from “any”. */
if (!archsatisfied(old_pkgbin, dep->list))
continue;
debug(dbg_depcon,"does_replace ... yes");
return true;
}
debug(dbg_depcon,"does_replace ... no");
return false;
}
static void
tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
const char *path, struct file_stat *st,
struct filenamenode *namenode)
{
static struct varbuf hardlinkfn;
static int fd;
struct dpkg_error err;
struct filenamenode *linknode;
char fnamebuf[256];
char fnamenewbuf[256];
char *newhash;
switch (te->type) {
case TAR_FILETYPE_FILE:
/* We create the file with mode 0 to make sure nobody can do anything with
* it until we apply the proper mode, which might be a statoverride. */
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
if (fd < 0)
ohshite(_("unable to create '%.255s' (while processing '%.255s')"),
path, te->name);
push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
debug(dbg_eachfiledetail, "tarobject file open size=%jd",
(intmax_t)te->size);
/* We try to tell the filesystem how much disk space we are going to
* need to let it reduce fragmentation and possibly improve performance,
* as we do know the size beforehand. */
fd_allocate_size(fd, 0, te->size);
newhash = nfmalloc(MD5HASHLEN + 1);
if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
path_quote_filename(fnamebuf, te->name, 256),
path_quote_filename(fnamenewbuf, fnamenewvb.buf, 256), err.str);
namenode->newhash = newhash;
debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
tarobject_skip_padding(tc, te);
fd_writeback_init(fd);
if (namenode->statoverride)
debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
namenode->statoverride->uid,
namenode->statoverride->gid,
namenode->statoverride->mode);
if (fchown(fd, st->uid, st->gid))
ohshite(_("error setting ownership of '%.255s'"), te->name);
if (fchmod(fd, st->mode & ~S_IFMT))
ohshite(_("error setting permissions of '%.255s'"), te->name);
/* Postpone the fsync, to try to avoid massive I/O degradation. */
if (!fc_unsafe_io)
namenode->flags |= fnnf_deferred_fsync;
pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
if (close(fd))
ohshite(_("error closing/writing '%.255s'"), te->name);
break;
case TAR_FILETYPE_FIFO:
if (mkfifo(path, 0))
ohshite(_("error creating pipe '%.255s'"), te->name);
debug(dbg_eachfiledetail, "tarobject fifo");
break;
case TAR_FILETYPE_CHARDEV:
if (mknod(path, S_IFCHR, te->dev))
ohshite(_("error creating device '%.255s'"), te->name);
debug(dbg_eachfiledetail, "tarobject chardev");
break;
case TAR_FILETYPE_BLOCKDEV:
if (mknod(path, S_IFBLK, te->dev))
ohshite(_("error creating device '%.255s'"), te->name);
debug(dbg_eachfiledetail, "tarobject blockdev");
break;
case TAR_FILETYPE_HARDLINK:
varbuf_reset(&hardlinkfn);
varbuf_add_str(&hardlinkfn, instdir);
linknode = findnamenode(te->linkname, 0);
varbuf_add_str(&hardlinkfn,
namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
if (linknode->flags & (fnnf_deferred_rename | fnnf_new_conff))
varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
varbuf_end_str(&hardlinkfn);
if (link(hardlinkfn.buf, path))
ohshite(_("error creating hard link '%.255s'"), te->name);
namenode->newhash = linknode->newhash;
debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
break;
case TAR_FILETYPE_SYMLINK:
/* We've already checked for an existing directory. */
if (symlink(te->linkname, path))
ohshite(_("error creating symbolic link '%.255s'"), te->name);
debug(dbg_eachfiledetail, "tarobject symlink creating");
break;
case TAR_FILETYPE_DIR:
/* We've already checked for an existing directory. */
if (mkdir(path, 0))
ohshite(_("error creating directory '%.255s'"), te->name);
debug(dbg_eachfiledetail, "tarobject directory creating");
break;
default:
internerr("unknown tar type '%d', but already checked", te->type);
}
}
static void
tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
struct filenamenode *namenode)
{
if (te->type == TAR_FILETYPE_FILE) {
struct dpkg_error err;
char fnamebuf[256];
char *newhash;
newhash = nfmalloc(MD5HASHLEN + 1);
if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
ohshit(_("cannot compute MD5 hash for tar file '%.255s': %s"),
path_quote_filename(fnamebuf, te->name, 256), err.str);
tarobject_skip_padding(tc, te);
namenode->newhash = newhash;
debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
} else if (te->type == TAR_FILETYPE_HARDLINK) {
struct filenamenode *linknode;
linknode = findnamenode(te->linkname, 0);
namenode->newhash = linknode->newhash;
debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
}
}
static void
tarobject_set_mtime(struct tar_entry *te, const char *path)
{
struct timeval tv[2];
tv[0].tv_sec = currenttime;
tv[0].tv_usec = 0;
tv[1].tv_sec = te->mtime;
tv[1].tv_usec = 0;
if (te->type == TAR_FILETYPE_SYMLINK) {
#ifdef HAVE_LUTIMES
if (lutimes(path, tv) && errno != ENOSYS)
ohshite(_("error setting timestamps of '%.255s'"), path);
#endif
} else {
if (utimes(path, tv))
ohshite(_("error setting timestamps of '%.255s'"), path);
}
}
static void
tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
{
if (te->type == TAR_FILETYPE_FILE)
return; /* Already handled using the file descriptor. */
if (te->type == TAR_FILETYPE_SYMLINK) {
if (lchown(path, st->uid, st->gid))
ohshite(_("error setting ownership of symlink '%.255s'"), path);
} else {
if (chown(path, st->uid, st->gid))
ohshite(_("error setting ownership of '%.255s'"), path);
if (chmod(path, st->mode & ~S_IFMT))
ohshite(_("error setting permissions of '%.255s'"), path);
}
}
static void
tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
{
dpkg_selabel_set_context(matchpath, path, mode);
}
static void
tarobject_matches(struct tarcontext *tc,
const char *fn_old, struct stat *stab, char *oldhash,
const char *fn_new, struct tar_entry *te,
struct filenamenode *namenode)
{
char *linkname;
ssize_t linksize;
debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
switch (te->type) {
case TAR_FILETYPE_DIR:
/* Nothing to check for a new directory. */
return;
case TAR_FILETYPE_SYMLINK:
/* Symlinks to existing dirs have already been dealt with, only
* remain real symlinks where we can compare the target. */
if (!S_ISLNK(stab->st_mode))
break;
linkname = m_malloc(stab->st_size + 1);
linksize = readlink(fn_old, linkname, stab->st_size + 1);
if (linksize < 0)
ohshite(_("unable to read link '%.255s'"), fn_old);
else if (linksize > stab->st_size)
ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
fn_old, (intmax_t)stab->st_size, linksize);
else if (linksize < stab->st_size)
warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
fn_old, (intmax_t)stab->st_size, linksize);
linkname[linksize] = '\0';
if (strcmp(linkname, te->linkname) == 0) {
free(linkname);
return;
} else {
free(linkname);
}
break;
case TAR_FILETYPE_CHARDEV:
if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
return;
break;
case TAR_FILETYPE_BLOCKDEV:
if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
return;
break;
case TAR_FILETYPE_FIFO:
if (S_ISFIFO(stab->st_mode))
return;
break;
case TAR_FILETYPE_HARDLINK:
/* Fall through. */
case TAR_FILETYPE_FILE:
/* Only check metadata for non-conffiles. */
if (!(namenode->flags & fnnf_new_conff) &&
!(S_ISREG(stab->st_mode) && te->size == stab->st_size))
break;
if (strcmp(oldhash, namenode->newhash) == 0)
return;
break;
default:
internerr("unknown tar type '%d', but already checked", te->type);
}
forcibleerr(fc_overwrite,
_("trying to overwrite shared '%.250s', which is different "
"from other instances of package %.250s"),
namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
}
void setupfnamevbs(const char *filename) {
varbuf_rollback(&fnamevb, &fname_state);
varbuf_add_str(&fnamevb, filename);
varbuf_end_str(&fnamevb);
varbuf_rollback(&fnametmpvb, &fname_state);
varbuf_add_str(&fnametmpvb, filename);
varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
varbuf_end_str(&fnametmpvb);
varbuf_rollback(&fnamenewvb, &fname_state);
varbuf_add_str(&fnamenewvb, filename);
varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
varbuf_end_str(&fnamenewvb);
debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
}
static bool
linktosameexistingdir(const struct tar_entry *ti, const char *fname,
struct varbuf *symlinkfn)
{
struct stat oldstab, newstab;
int statr;
const char *lastslash;
statr= stat(fname, &oldstab);
if (statr) {
if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
ohshite(_("failed to stat (dereference) existing symlink '%.250s'"),
fname);
return false;
}
if (!S_ISDIR(oldstab.st_mode))
return false;
/* But is it to the same dir? */
varbuf_reset(symlinkfn);
if (ti->linkname[0] == '/') {
varbuf_add_str(symlinkfn, instdir);
} else {
lastslash= strrchr(fname, '/');
if (lastslash == NULL)
internerr("tar entry filename '%s' does not contain '/'", fname);
varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
}
varbuf_add_str(symlinkfn, ti->linkname);
varbuf_end_str(symlinkfn);
statr= stat(symlinkfn->buf, &newstab);
if (statr) {
if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
ohshite(_("failed to stat (dereference) proposed new symlink target"
" '%.250s' for symlink '%.250s'"), symlinkfn->buf, fname);
return false;
}
if (!S_ISDIR(newstab.st_mode))
return false;
if (newstab.st_dev != oldstab.st_dev ||
newstab.st_ino != oldstab.st_ino)
return false;
return true;
}
int
tarobject(void *ctx, struct tar_entry *ti)
{
static struct varbuf conffderefn, symlinkfn;
const char *usename;
struct filenamenode *usenode;
struct conffile *conff;
struct tarcontext *tc = ctx;
bool existingdir, keepexisting;
bool refcounting;
char oldhash[MD5HASHLEN + 1];
int statr;
ssize_t r;
struct stat stab, stabtmp;
struct file_stat nodestat;
struct fileinlist *nifd, **oldnifd;
struct pkgset *divpkgset;
struct pkginfo *otherpkg;
tar_entry_update_from_system(ti);
/* Perform some sanity checks on the tar entry. */
if (strchr(ti->name, '\n'))
ohshit(_("newline not allowed in archive object name '%.255s'"), ti->name);
/* Append to list of files.
* The trailing ‘/’ put on the end of names in tarfiles has already
* been stripped by tar_extractor(). */
oldnifd = tc->newfiles_queue->tail;
nifd = tar_filenamenode_queue_push(tc->newfiles_queue,
findnamenode(ti->name, 0));
nifd->namenode->flags |= fnnf_new_inarchive;
debug(dbg_eachfile,
"tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
" ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
ti->name, (long)ti->stat.mode,
(unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
ti->type,
ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
ti->linkname,
nifd->namenode->name, nifd->namenode->flags,
nifd->namenode->divert && nifd->namenode->divert->useinstead
? nifd->namenode->divert->useinstead->name : "<none>");
if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
divpkgset = nifd->namenode->divert->pkgset;
if (divpkgset) {
forcibleerr(fc_overwritediverted,
_("trying to overwrite '%.250s', which is the "
"diverted version of '%.250s' (package: %.100s)"),
nifd->namenode->name, nifd->namenode->divert->camefrom->name,
divpkgset->name);
} else {
forcibleerr(fc_overwritediverted,
_("trying to overwrite '%.250s', which is the "
"diverted version of '%.250s'"),
nifd->namenode->name, nifd->namenode->divert->camefrom->name);
}
}
if (nifd->namenode->statoverride) {
nodestat = *nifd->namenode->statoverride;
nodestat.mode |= ti->stat.mode & S_IFMT;
} else {
nodestat = ti->stat;
}
usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
usename = usenode->name;
trig_file_activate(usenode, tc->pkg);
if (nifd->namenode->flags & fnnf_new_conff) {
/* If it's a conffile we have to extract it next to the installed
* version (i.e. we do the usual link-following). */
if (conffderef(tc->pkg, &conffderefn, usename))
usename= conffderefn.buf;
debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
}
setupfnamevbs(usename);
statr= lstat(fnamevb.buf,&stab);
if (statr) {
/* The lstat failed. */
if (errno != ENOENT && errno != ENOTDIR)
ohshite(_("unable to stat '%.255s' (which I was about to install)"),
ti->name);
/* OK, so it doesn't exist.
* However, it's possible that we were in the middle of some other
* backup/restore operation and were rudely interrupted.
* So, we see if we have .dpkg-tmp, and if so we restore it. */
if (rename(fnametmpvb.buf,fnamevb.buf)) {
if (errno != ENOENT && errno != ENOTDIR)
ohshite(_("unable to clean up mess surrounding '%.255s' before "
"installing another version"), ti->name);
debug(dbg_eachfiledetail,"tarobject nonexistent");
} else {
debug(dbg_eachfiledetail,"tarobject restored tmp to main");
statr= lstat(fnamevb.buf,&stab);
if (statr)
ohshite(_("unable to stat restored '%.255s' before installing"
" another version"), ti->name);
}
} else {
debug(dbg_eachfiledetail,"tarobject already exists");
}
/* Check to see if it's a directory or link to one and we don't need to
* do anything. This has to be done now so that we don't die due to
* a file overwriting conflict. */
existingdir = false;
switch (ti->type) {
case TAR_FILETYPE_SYMLINK:
/* If it's already an existing directory, do nothing. */
if (!statr && S_ISDIR(stab.st_mode)) {
debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
existingdir = true;
} else if (!statr && S_ISLNK(stab.st_mode)) {
if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
existingdir = true;
}
break;
case TAR_FILETYPE_DIR:
/* If it's already an existing directory, do nothing. */
if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
debug(dbg_eachfiledetail, "tarobject directory exists");
existingdir = true;
}
break;
case TAR_FILETYPE_FILE:
case TAR_FILETYPE_CHARDEV:
case TAR_FILETYPE_BLOCKDEV:
case TAR_FILETYPE_FIFO:
case TAR_FILETYPE_HARDLINK:
break;
default:
ohshit(_("archive contained object '%.255s' of unknown type 0x%x"),
ti->name, ti->type);
}
keepexisting = false;
refcounting = false;
if (!existingdir) {
struct filepackages_iterator *iter;
iter = filepackages_iter_new(nifd->namenode);
while ((otherpkg = filepackages_iter_next(iter))) {
if (otherpkg == tc->pkg)
continue;
debug(dbg_eachfile, "tarobject ... found in %s",
pkg_name(otherpkg, pnaw_always));
/* A pkgset can share files between its instances. Overwriting
* is allowed when they are not getting in sync, otherwise the
* file content must match the installed file. */
if (otherpkg->set == tc->pkg->set &&
otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
if (statr == 0 && tc->pkgset_getting_in_sync)
refcounting = true;
debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
pkg_name(otherpkg, pnaw_always),
versiondescribe(&otherpkg->installed.version, vdew_nonambig),
tc->pkgset_getting_in_sync);
continue;
}
if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
/* Right, so we may be diverting this file. This makes the conflict
* OK iff one of us is the diverting package (we don't need to
* check for both being the diverting package, obviously). */
divpkgset = nifd->namenode->divert->pkgset;
debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
divpkgset ? divpkgset->name : "<none>");
if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
continue;
}
/* If the new object is a directory and the previous object does
* not exist assume it's also a directory and skip further checks.
* XXX: Ideally with more information about the installed files we
* could perform more clever checks. */
if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
debug(dbg_eachfile, "tarobject ... assuming shared directory");
continue;
}
/* Nope? Hmm, file conflict, perhaps. Check Replaces. */
switch (otherpkg->clientdata->replacingfilesandsaid) {
case 2:
keepexisting = true;
/* Fall through. */
case 1:
continue;
}
/* Is the package with the conflicting file in the “config files only”
* state? If so it must be a config file and we can silently take it
* over. */
if (otherpkg->status == PKG_STAT_CONFIGFILES)
continue;
/* Perhaps we're removing a conflicting package? */
if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
continue;
/* Is the file an obsolete conffile in the other package
* and a conffile in the new package? */
if ((nifd->namenode->flags & fnnf_new_conff) &&
!statr && S_ISREG(stab.st_mode)) {
for (conff = otherpkg->installed.conffiles;
conff;
conff = conff->next) {
if (!conff->obsolete)
continue;
if (strcmp(conff->name, nifd->namenode->name) == 0)
break;
}
if (conff) {
debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
/* process_archive() will have copied its hash already. */
continue;
}
}
if (does_replace(tc->pkg, &tc->pkg->available,
otherpkg, &otherpkg->installed)) {
printf(_("Replacing files in old package %s (%s) ...\n"),
pkg_name(otherpkg, pnaw_nonambig),
versiondescribe(&otherpkg->installed.version, vdew_nonambig));
otherpkg->clientdata->replacingfilesandsaid = 1;
} else if (does_replace(otherpkg, &otherpkg->installed,
tc->pkg, &tc->pkg->available)) {
printf(_("Replaced by files in installed package %s (%s) ...\n"),
pkg_name(otherpkg, pnaw_nonambig),
versiondescribe(&otherpkg->installed.version, vdew_nonambig));
otherpkg->clientdata->replacingfilesandsaid = 2;
nifd->namenode->flags &= ~fnnf_new_inarchive;
keepexisting = true;
} else {
/* At this point we are replacing something without a Replaces. */
if (!statr && S_ISDIR(stab.st_mode)) {
forcibleerr(fc_overwritedir,
_("trying to overwrite directory '%.250s' "
"in package %.250s %.250s with nondirectory"),
nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
versiondescribe(&otherpkg->installed.version,
vdew_nonambig));
} else {
forcibleerr(fc_overwrite,
_("trying to overwrite '%.250s', "
"which is also in package %.250s %.250s"),
nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
versiondescribe(&otherpkg->installed.version,
vdew_nonambig));
}
}
}
filepackages_iter_free(iter);
}
if (keepexisting) {
if (nifd->namenode->flags & fnnf_new_conff)
nifd->namenode->flags |= fnnf_obs_conff;
tar_filenamenode_queue_pop(tc->newfiles_queue, oldnifd, nifd);
tarobject_skip_entry(tc, ti);
return 0;
}
if (filter_should_skip(ti)) {
nifd->namenode->flags &= ~fnnf_new_inarchive;
nifd->namenode->flags |= fnnf_filtered;
tarobject_skip_entry(tc, ti);
return 0;
}
if (existingdir)
return 0;
/* Compute the hash of the previous object, before we might replace it
* with the new version on forced overwrites. */
if (refcounting) {
debug(dbg_eachfiledetail, "tarobject hashing on-disk file '%s', refcounting",
fnamevb.buf);
if (nifd->namenode->flags & fnnf_new_conff) {
md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
} else if (S_ISREG(stab.st_mode)) {
md5hash(tc->pkg, oldhash, fnamevb.buf);
} else {
strcpy(oldhash, EMPTYHASHFLAG);
}
}
if (refcounting && !fc_overwrite) {
/* If we are not forced to overwrite the path and are refcounting,
* just compute the hash w/o extracting the object. */
tarobject_hash(tc, ti, nifd->namenode);
} else {
/* Now, at this stage we want to make sure neither of .dpkg-new and
* .dpkg-tmp are hanging around. */
path_remove_tree(fnamenewvb.buf);
path_remove_tree(fnametmpvb.buf);
/* Now we start to do things that we need to be able to undo
* if something goes wrong. Watch out for the CLEANUP comments to
* keep an eye on what's installed on the disk at each point. */
push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
/*
* CLEANUP: Now we either have the old file on the disk, or not, in
* its original filename.
*/
/* Extract whatever it is as .dpkg-new ... */
tarobject_extract(tc, ti, fnamenewvb.buf, &nodestat, nifd->namenode);
}
/* For shared files, check now if the object matches. */
if (refcounting)
tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
fnamenewvb.buf, ti, nifd->namenode);
/* If we didn't extract anything, there's nothing else to do. */
if (refcounting && !fc_overwrite)
return 0;
tarobject_set_perms(ti, fnamenewvb.buf, &nodestat);
tarobject_set_mtime(ti, fnamenewvb.buf);
tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, nodestat.mode);
/*
* CLEANUP: Now we have extracted the new object in .dpkg-new (or,
* if the file already exists as a directory and we were trying to
* extract a directory or symlink, we returned earlier, so we don't
* need to worry about that here).
*
* The old file is still in the original filename,
*/
/* First, check to see if it's a conffile. If so we don't install
* it now - we leave it in .dpkg-new for --configure to take care of. */
if (nifd->namenode->flags & fnnf_new_conff) {
debug(dbg_conffdetail,"tarobject conffile extracted");
nifd->namenode->flags |= fnnf_elide_other_lists;
return 0;
}
/* Now we move the old file out of the way, the backup file will
* be deleted later. */
if (statr) {
/* Don't try to back it up if it didn't exist. */
debug(dbg_eachfiledetail,"tarobject new - no backup");
} else {
if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
/* One of the two is a directory - can't do atomic install. */
debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
nifd->namenode->flags |= fnnf_no_atomic_overwrite;
if (rename(fnamevb.buf,fnametmpvb.buf))
ohshite(_("unable to move aside '%.255s' to install new version"),
ti->name);
} else if (S_ISLNK(stab.st_mode)) {
/* We can't make a symlink with two hardlinks, so we'll have to
* copy it. (Pretend that making a copy of a symlink is the same
* as linking to it.) */
varbuf_reset(&symlinkfn);
varbuf_grow(&symlinkfn, stab.st_size + 1);
r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
if (r < 0)
ohshite(_("unable to read link '%.255s'"), ti->name);
else if (r > stab.st_size)
ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
fnamevb.buf, (intmax_t)stab.st_size, r);
else if (r < stab.st_size)
warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
fnamevb.buf, (intmax_t)stab.st_size, r);
varbuf_trunc(&symlinkfn, r);
varbuf_end_str(&symlinkfn);
if (symlink(symlinkfn.buf,fnametmpvb.buf))
ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
} else {
debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
if (link(fnamevb.buf,fnametmpvb.buf))
ohshite(_("unable to make backup link of '%.255s' before installing new version"),
ti->name);
}
}
/*
* CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
* in .dpkg-new.
*/
if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
ti->type == TAR_FILETYPE_SYMLINK) {
nifd->namenode->flags |= fnnf_deferred_rename;
debug(dbg_eachfiledetail, "tarobject done and installation deferred");
} else {
if (rename(fnamenewvb.buf, fnamevb.buf))
ohshite(_("unable to install new version of '%.255s'"), ti->name);
/*
* CLEANUP: Now the new file is in the destination file, and the
* old file is in .dpkg-tmp to be cleaned up later. We now need
* to take a different attitude to cleanup, because we need to
* remove the new file.
*/
nifd->namenode->flags |= fnnf_placed_on_disk;
nifd->namenode->flags |= fnnf_elide_other_lists;
debug(dbg_eachfiledetail, "tarobject done and installed");
}
return 0;
}
#if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
static void
tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
{
struct fileinlist *cfile;
for (cfile = files; cfile; cfile = cfile->next) {
struct filenamenode *usenode;
int fd;
if (!(cfile->namenode->flags & fnnf_deferred_fsync))
continue;
usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
setupfnamevbs(usenode->name);
fd = open(fnamenewvb.buf, O_WRONLY);
if (fd < 0)
ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
/* Ignore the return code as it should be considered equivalent to an
* asynchronous hint for the kernel, we are doing an fsync() later on
* anyway. */
sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
if (close(fd))
ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
}
}
#else
static void
tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
{
}
#endif
void
tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
{
struct fileinlist *cfile;
struct filenamenode *usenode;
tar_writeback_barrier(files, pkg);
for (cfile = files; cfile; cfile = cfile->next) {
debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
if (!(cfile->namenode->flags & fnnf_deferred_rename))
continue;
usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
setupfnamevbs(usenode->name);
if (cfile->namenode->flags & fnnf_deferred_fsync) {
int fd;
debug(dbg_eachfiledetail, "deferred extract needs fsync");
fd = open(fnamenewvb.buf, O_WRONLY);
if (fd < 0)
ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
if (fsync(fd))
ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
if (close(fd))
ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
cfile->namenode->flags &= ~fnnf_deferred_fsync;
}
debug(dbg_eachfiledetail, "deferred extract needs rename");
if (rename(fnamenewvb.buf, fnamevb.buf))
ohshite(_("unable to install new version of '%.255s'"),
cfile->namenode->name);
cfile->namenode->flags &= ~fnnf_deferred_rename;
/*
* CLEANUP: Now the new file is in the destination file, and the
* old file is in .dpkg-tmp to be cleaned up later. We now need
* to take a different attitude to cleanup, because we need to
* remove the new file.
*/
cfile->namenode->flags |= fnnf_placed_on_disk;
cfile->namenode->flags |= fnnf_elide_other_lists;
debug(dbg_eachfiledetail, "deferred extract done and installed");
}
}
void
enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
{
struct pkg_deconf_list *newdeconf;
ensure_package_clientdata(pkg);
pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
newdeconf->next = deconfigure;
newdeconf->pkg = pkg;
newdeconf->pkg_removal = pkg_removal;
deconfigure = newdeconf;
}
void
clear_deconfigure_queue(void)
{
struct pkg_deconf_list *deconf, *deconf_next;
for (deconf = deconfigure; deconf; deconf = deconf_next) {
deconf_next = deconf->next;
free(deconf);
}
deconfigure = NULL;
}
/**
* Try if we can deconfigure the package and queue it if so.
*
* Also checks whether the pdep is forced, first, according to force_p.
* force_p may be NULL in which case nothing is considered forced.
*
* Action is a string describing the action which causes the
* deconfiguration:
*
* "removal of <package>" (due to Conflicts+Depends; removal != NULL)
* "installation of <package>" (due to Breaks; removal == NULL)
*
* @retval 0 Not possible (why is printed).
* @retval 1 Deconfiguration queued ok (no message printed).
* @retval 2 Forced (no deconfiguration needed, why is printed).
*/
static int
try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
struct deppossi *pdep, const char *action,
struct pkginfo *removal, const char *why)
{
if (force_p && force_p(pdep)) {
warning(_("ignoring dependency problem with %s:\n%s"), action, why);
return 2;
} else if (f_autodeconf) {
if (pkg->installed.essential) {
if (fc_removeessential) {
warning(_("considering deconfiguration of essential\n"
" package %s, to enable %s"),
pkg_name(pkg, pnaw_nonambig), action);
} else {
notice(_("no, %s is essential, will not deconfigure\n"
" it in order to enable %s"),
pkg_name(pkg, pnaw_nonambig), action);
return 0;
}
}
enqueue_deconfigure(pkg, removal);
return 1;
} else {
notice(_("no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
action, why);
return 0;
}
}
static int try_remove_can(struct deppossi *pdep,
struct pkginfo *fixbyrm,
const char *why) {
char action[512];
sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
return try_deconfigure_can(force_depends, pdep->up->up, pdep,
action, fixbyrm, why);
}
void check_breaks(struct dependency *dep, struct pkginfo *pkg,
const char *pfilename) {
struct pkginfo *fixbydeconf;
struct varbuf why = VARBUF_INIT;
int ok;
fixbydeconf = NULL;
if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
varbuf_destroy(&why);
return;
}
varbuf_end_str(&why);
if (fixbydeconf && f_autodeconf) {
char action[512];
ensure_package_clientdata(fixbydeconf);
if (fixbydeconf->clientdata->istobe != PKG_ISTOBE_NORMAL)
internerr("package %s being fixed by deconf is not to be normal, "
"is to be %d",
pkg_name(pkg, pnaw_always), fixbydeconf->clientdata->istobe);
sprintf(action, _("installation of %.250s"),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
notice(_("considering deconfiguration of %s, which would be broken by %s ..."),
pkg_name(fixbydeconf, pnaw_nonambig), action);
ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
action, NULL, why.buf);
if (ok == 1) {
notice(_("yes, will deconfigure %s (broken by %s)"),
pkg_name(fixbydeconf, pnaw_nonambig),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
}
} else {
notice(_("regarding %s containing %s:\n%s"), pfilename,
pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
ok= 0;
}
varbuf_destroy(&why);
if (ok > 0) return;
if (force_breaks(dep->list)) {
warning(_("ignoring breakage, may proceed anyway!"));
return;
}
if (fixbydeconf && !f_autodeconf) {
ohshit(_("installing %.250s would break %.250s, and\n"
" deconfiguration is not permitted (--auto-deconfigure might help)"),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
pkg_name(fixbydeconf, pnaw_nonambig));
} else {
ohshit(_("installing %.250s would break existing software"),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
}
}
void check_conflict(struct dependency *dep, struct pkginfo *pkg,
const char *pfilename) {
struct pkginfo *fixbyrm;
struct deppossi *pdep, flagdeppossi;
struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
struct dependency *providecheck;
fixbyrm = NULL;
if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
varbuf_destroy(&conflictwhy);
varbuf_destroy(&removalwhy);
return;
}
if (fixbyrm) {
ensure_package_clientdata(fixbyrm);
if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
fixbyrm= dep->up;
ensure_package_clientdata(fixbyrm);
}
if (((pkg->available.essential && fixbyrm->installed.essential) ||
(((fixbyrm->want != PKG_WANT_INSTALL &&
fixbyrm->want != PKG_WANT_HOLD) ||
does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
(!fixbyrm->installed.essential || fc_removeessential)))) {
if (fixbyrm->clientdata->istobe != PKG_ISTOBE_NORMAL &&
fixbyrm->clientdata->istobe != PKG_ISTOBE_DECONFIGURE)
internerr("package %s to be fixed by removal is not to be normal "
"nor deconfigure, is to be %d",
pkg_name(pkg, pnaw_always), fixbyrm->clientdata->istobe);
fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
notice(_("considering removing %s in favour of %s ..."),
pkg_name(fixbyrm, pnaw_nonambig),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
notice(_("%s is not properly installed; ignoring any dependencies on it"),
pkg_name(fixbyrm, pnaw_nonambig));
pdep = NULL;
} else {
for (pdep = fixbyrm->set->depended.installed;
pdep;
pdep = pdep->rev_next) {
if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
continue;
if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
continue;
varbuf_end_str(&removalwhy);
if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
break;
}
if (!pdep) {
/* If we haven't found a reason not to yet, let's look some more. */
for (providecheck= fixbyrm->installed.depends;
providecheck;
providecheck= providecheck->next) {
if (providecheck->type != dep_provides) continue;
for (pdep = providecheck->list->ed->depended.installed;
pdep;
pdep = pdep->rev_next) {
if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
continue;
if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
continue;
varbuf_end_str(&removalwhy);
notice(_("may have trouble removing %s, as it provides %s ..."),
pkg_name(fixbyrm, pnaw_nonambig),
providecheck->list->ed->name);
if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
goto break_from_both_loops_at_once;
}
}
break_from_both_loops_at_once:;
}
}
if (!pdep && skip_due_to_hold(fixbyrm)) {
pdep= &flagdeppossi;
}
if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
if (fc_removereinstreq) {
notice(_("package %s requires reinstallation, but will "
"remove anyway as you requested"),
pkg_name(fixbyrm, pnaw_nonambig));
} else {
notice(_("package %s requires reinstallation, will not remove"),
pkg_name(fixbyrm, pnaw_nonambig));
pdep= &flagdeppossi;
}
}
if (!pdep) {
/* This conflict is OK - we'll remove the conflictor. */
enqueue_conflictor(fixbyrm);
varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
notice(_("yes, will remove %s in favour of %s"),
pkg_name(fixbyrm, pnaw_nonambig),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
return;
}
/* Put it back. */
fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
}
}
varbuf_end_str(&conflictwhy);
notice(_("regarding %s containing %s:\n%s"), pfilename,
pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
if (!force_conflicts(dep->list))
ohshit(_("conflicting packages - not installing %.250s"),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
warning(_("ignoring conflict, may proceed anyway!"));
varbuf_destroy(&conflictwhy);
return;
}
void cu_cidir(int argc, void **argv) {
char *cidir= (char*)argv[0];
char *cidirrest= (char*)argv[1];
cidirrest[-1] = '\0';
path_remove_tree(cidir);
}
void cu_fileslist(int argc, void **argv) {
tar_pool_release();
}
int
archivefiles(const char *const *argv)
{
const char *const *volatile argp;
const char **volatile arglist = NULL;
int i;
jmp_buf ejbuf;
enum modstatdb_rw msdbflags;
trigproc_install_hooks();
if (f_noact)
msdbflags = msdbrw_readonly;
else if (cipaction->arg_int == act_avail)
msdbflags = msdbrw_readonly | msdbrw_available_write;
else if (fc_nonroot)
msdbflags = msdbrw_write;
else
msdbflags = msdbrw_needsuperuser;
modstatdb_open(msdbflags);
checkpath();
pkg_infodb_upgrade();
log_message("startup archives %s", cipaction->olong);
if (f_recursive) {
const char *const *ap;
int nfiles = 0;
if (!*argv)
badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
for (ap = argv; *ap; ap++) {
struct treeroot *tree;
struct treenode *node;
tree = treewalk_open((const char *)*ap, TREEWALK_FOLLOW_LINKS, NULL);
while ((node = treewalk_next(tree))) {
const char *nodename;
if (!S_ISREG(treenode_get_mode(node)))
continue;
/* Check if it looks like a .deb file. */
nodename = treenode_get_pathname(node);
if (strcmp(nodename + strlen(nodename) - 4, ".deb") != 0)
continue;
arglist = m_realloc(arglist, sizeof(char *) * (nfiles + 2));
arglist[nfiles++] = m_strdup(nodename);
}
treewalk_close(tree);
}
if (!nfiles)
ohshit(_("searched, but found no packages (files matching *.deb)"));
arglist[nfiles] = NULL;
argp= arglist;
} else {
if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
cipaction->olong);
argp= argv;
}
/* Perform some sanity checks on the passed archives. */
for (i = 0; argp[i]; i++) {
struct stat st;
/* We need the filename to exist. */
if (stat(argp[i], &st) < 0)
ohshite(_("cannot access archive '%s'"), argp[i]);
/* We cannot work with anything that is not a regular file. */
if (!S_ISREG(st.st_mode))
ohshit(_("archive '%s' is not a regular file"), argp[i]);
}
currenttime = time(NULL);
/* Initialize fname variables contents. */
varbuf_reset(&fnamevb);
varbuf_reset(&fnametmpvb);
varbuf_reset(&fnamenewvb);
varbuf_add_str(&fnamevb, instdir);
varbuf_add_str(&fnametmpvb, instdir);
varbuf_add_str(&fnamenewvb, instdir);
varbuf_snapshot(&fnamevb, &fname_state);
ensure_diversions();
ensure_statoverrides(STATDB_PARSE_NORMAL);
for (i = 0; argp[i]; i++) {
if (setjmp(ejbuf)) {
pop_error_context(ehflag_bombout);
if (abort_processing)
break;
continue;
}
push_error_context_jump(&ejbuf, print_error_perarchive, argp[i]);
dpkg_selabel_load();
process_archive(argp[i]);
onerr_abort++;
m_output(stdout, _("<standard output>"));
m_output(stderr, _("<standard error>"));
onerr_abort--;
pop_error_context(ehflag_normaltidy);
}
dpkg_selabel_close();
free(arglist);
switch (cipaction->arg_int) {
case act_install:
case act_configure:
case act_triggers:
case act_remove:
case act_purge:
process_queue();
case act_unpack:
case act_avail:
break;
default:
internerr("unknown action '%d'", cipaction->arg_int);
}
trigproc_run_deferred();
modstatdb_shutdown();
return 0;
}
/**
* Decide whether we want to install a new version of the package.
*
* @param pkg The package with the version we might want to install
*
* @retval true If the package should be skipped.
* @retval false If the package should be installed.
*/
bool
wanttoinstall(struct pkginfo *pkg)
{
int rc;
if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
if (f_alsoselect) {
printf(_("Selecting previously unselected package %s.\n"),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
return true;
} else {
printf(_("Skipping unselected package %s.\n"),
pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
return false;
}
}
if (pkg->eflag & PKG_EFLAG_REINSTREQ)
return true;
if (pkg->status < PKG_STAT_UNPACKED)
return true;
rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
if (rc > 0) {
return true;
} else if (rc == 0) {
/* Same version fully installed. */
if (f_skipsame) {
notice(_("version %.250s of %.250s already installed, skipping"),
versiondescribe(&pkg->installed.version, vdew_nonambig),
pkg_name(pkg, pnaw_nonambig));
return false;
} else {
return true;
}
} else {
if (fc_downgrade) {
warning(_("downgrading %.250s from %.250s to %.250s"),
pkg_name(pkg, pnaw_nonambig),
versiondescribe(&pkg->installed.version, vdew_nonambig),
versiondescribe(&pkg->available.version, vdew_nonambig));
return true;
} else {
notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
pkg_name(pkg, pnaw_nonambig),
versiondescribe(&pkg->installed.version, vdew_nonambig),
versiondescribe(&pkg->available.version, vdew_nonambig));
return false;
}
}
}
|