3 * File state machine to handle a payload from a package.
13 /*@access rpmTransactionSet @*/
18 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
23 /* XXX Failure to remove is not (yet) cause for failure. */
24 /*@-exportlocal -exportheadervar@*/
26 int strict_erasures = 0;
27 /*@=exportlocal =exportheadervar@*/
29 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
30 const FSMI_t iter = fsm->iter;
31 /*@-compdef -refcounttrans -retexpose -usereleased @*/
32 return (iter ? iter->ts : NULL);
33 /*@=compdef =refcounttrans =retexpose =usereleased @*/
36 TFI_t fsmGetFi(const FSM_t fsm)
38 const FSMI_t iter = fsm->iter;
39 return (iter ? iter->fi : NULL);
42 #define SUFFIX_RPMORIG ".rpmorig"
43 #define SUFFIX_RPMSAVE ".rpmsave"
44 #define SUFFIX_RPMNEW ".rpmnew"
47 * Build path to file from file info, ornamented with subdir and suffix.
48 * @param fsm file state machine data
49 * @param st file stat info
50 * @param subdir subdir to use (NULL disables)
51 * @param suffix suffix to use (NULL disables)
52 * @retval path to file
54 static /*@only@*//*@null@*/
55 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
56 /*@null@*/ const struct stat * st,
57 /*@null@*/ const char * subdir,
58 /*@null@*/ const char * suffix)
59 /*@uses fsm->dirName, fsm->baseName */
62 const char * s = NULL;
67 nb = strlen(fsm->dirName) +
68 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
69 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
70 strlen(fsm->baseName) + 1;
72 t = stpcpy(t, fsm->dirName);
73 if (st && !S_ISDIR(st->st_mode))
74 if (subdir) t = stpcpy(t, subdir);
75 t = stpcpy(t, fsm->baseName);
76 if (st && !S_ISDIR(st->st_mode))
77 if (suffix) t = stpcpy(t, suffix);
83 * Destroy file info iterator.
84 * @param p file info iterator
87 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/ void * p)
92 iter->ts = rpmtsUnlink(iter->ts);
97 * Create file info iterator.
98 * @param a transaction set
99 * @param b transaction element file info
100 * @return file info iterator
103 mapInitIterator(rpmTransactionSet ts, /*@kept@*/ const void * b)
106 TFI_t fi = (void *)b;
109 iter = xcalloc(1, sizeof(*iter));
110 iter->ts = rpmtsLink(ts);
112 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
113 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
114 iter->isave = iter->i;
119 * Return next index into file info.
120 * @param a file info iterator
121 * @return next index, -1 on termination
123 static int mapNextIterator(/*@null@*/ void * a)
130 const TFI_t fi = iter->fi;
132 if (iter->i >= 0) i = iter->i--;
134 if (iter->i < fi->fc) i = iter->i++;
143 static int cpioStrCmp(const void * a, const void * b)
146 const char * afn = *(const char **)a;
147 const char * bfn = *(const char **)b;
149 /* Match rpm-4.0 payloads with ./ prefixes. */
150 if (afn[0] == '.' && afn[1] == '/') afn += 2;
151 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
153 /* If either path is absolute, make it relative. */
154 if (afn[0] == '/') afn += 1;
155 if (bfn[0] == '/') bfn += 1;
157 return strcmp(afn, bfn);
161 * Locate archive path in file info.
162 * @param iter file info iterator
163 * @param fsmPath archive path
164 * @return index into file info, -1 if archive path was not found
166 static int mapFind(/*@null@*/ FSMI_t iter, const char * fsmPath)
172 const TFI_t fi = iter->fi;
173 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
174 const char ** p = NULL;
176 if (fi->apath != NULL)
177 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
180 iter->i = p - fi->apath;
181 ix = mapNextIterator(iter);
189 * Directory name iterator.
191 typedef struct dnli_s {
192 /*@dependent@*/ TFI_t fi;
193 /*@only@*/ /*@null@*/ char * active;
200 * Destroy directory name iterator.
201 * @param a directory name iterator
202 * @retval NULL always
204 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
208 DNLI_t dnli = (void *)a;
209 if (dnli->active) free(dnli->active);
216 static inline int dnlCount(const DNLI_t dnli)
219 return (dnli ? dnli->fi->dc : 0);
224 static inline int dnlIndex(const DNLI_t dnli)
227 return (dnli ? dnli->isave : -1);
231 * Create directory name iterator.
232 * @param fsm file state machine data
233 * @param reverse traverse directory names in reverse order?
234 * @return directory name iterator
236 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
238 /*@uses fsm->iter @*/
241 TFI_t fi = fsmGetFi(fsm);
247 dnli = xcalloc(1, sizeof(*dnli));
249 dnli->reverse = reverse;
250 dnli->i = (reverse ? fi->dc : 0);
253 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
255 /* Identify parent directories not skipped. */
256 for (i = 0; i < fi->fc; i++)
257 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
259 /* Exclude parent directories that are explicitly included. */
260 for (i = 0; i < fi->fc; i++) {
261 int dil, dnlen, bnlen;
263 if (!S_ISDIR(fi->fmodes[i]))
267 dnlen = strlen(fi->dnl[dil]);
268 bnlen = strlen(fi->bnl[i]);
270 for (j = 0; j < fi->dc; j++) {
274 if (!dnli->active[j] || j == dil)
275 /*@innercontinue@*/ continue;
278 if (jlen != (dnlen+bnlen+1))
279 /*@innercontinue@*/ continue;
280 if (strncmp(dnl, fi->dnl[dil], dnlen))
281 /*@innercontinue@*/ continue;
282 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
283 /*@innercontinue@*/ continue;
284 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
285 /*@innercontinue@*/ continue;
286 /* This directory is included in the package. */
288 /*@innerbreak@*/ break;
292 /* Print only once per package. */
295 for (i = 0; i < fi->dc; i++) {
296 if (!dnli->active[i]) continue;
299 rpmMessage(RPMMESS_DEBUG,
300 _("========== Directories not explictly included in package:\n"));
302 rpmMessage(RPMMESS_DEBUG, _("%10d %s\n"), i, fi->dnl[i]);
305 rpmMessage(RPMMESS_DEBUG, "==========\n");
312 * Return next directory name (from file info).
313 * @param dnli directory name iterator
314 * @return next directory name
316 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
319 const char * dn = NULL;
327 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
328 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
330 if (i >= 0 && i < fi->dc)
340 * Save hard link in chain.
341 * @param fsm file state machine data
342 * @return Is chain only partially filled?
344 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
345 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
346 /*@defines fsm->li @*/
347 /*@releases fsm->path @*/
348 /*@globals fileSystem@*/
349 /*@modifies fsm, fileSystem @*/
351 struct stat * st = &fsm->sb;
356 /* Find hard link set. */
358 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
359 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
364 /* New hard link encountered, add new link to set. */
366 if (fsm->li == NULL) {
367 fsm->li = xcalloc(1, sizeof(*fsm->li));
368 fsm->li->next = NULL;
369 fsm->li->sb = *st; /* structure assignment */
370 fsm->li->nlink = st->st_nlink;
371 fsm->li->linkIndex = fsm->ix;
372 fsm->li->createdPath = -1;
374 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
375 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
376 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
378 if (fsm->goal == FSM_PKGBUILD)
379 fsm->li->linksLeft = st->st_nlink;
380 if (fsm->goal == FSM_PKGINSTALL)
381 fsm->li->linksLeft = 0;
384 fsm->li->next = fsm->links;
386 fsm->links = fsm->li;
390 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
391 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
392 /*@-observertrans -dependenttrans@*/
393 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
394 /*@=observertrans =dependenttrans@*/
395 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
397 if (fsm->goal == FSM_PKGBUILD)
398 return (fsm->li->linksLeft > 0);
400 if (fsm->goal != FSM_PKGINSTALL)
403 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
406 /* Here come the bits, time to choose a non-skipped file name. */
407 { TFI_t fi = fsmGetFi(fsm);
409 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
410 ix = fsm->li->filex[j];
411 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
417 /* Are all links skipped or not encountered yet? */
419 return 1; /* XXX W2DO? */
421 /* Save the non-skipped file name and map index. */
422 fsm->li->linkIndex = j;
423 fsm->path = _free(fsm->path);
425 rc = fsmStage(fsm, FSM_MAP);
430 * Destroy set of hard links.
431 * @param li set of hard links
433 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
437 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
438 li->filex = _free(li->filex);
445 FSM_t fsm = xcalloc(1, sizeof(*fsm));
449 FSM_t freeFSM(FSM_t fsm)
452 fsm->path = _free(fsm->path);
454 while ((fsm->li = fsm->links) != NULL) {
455 fsm->links = fsm->li->next;
456 fsm->li->next = NULL;
457 fsm->li = freeHardLink(fsm->li);
460 fsm->dnlx = _free(fsm->dnlx);
461 fsm->ldn = _free(fsm->ldn);
462 fsm->iter = mapFreeIterator(fsm->iter);
467 int fsmSetup(FSM_t fsm, fileStage goal,
468 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
469 unsigned int * archiveSize, const char ** failedFile)
476 /*@-type@*/ /* FIX: cast? */
477 fsm->cfd = fdLink(cfd, "persist (fsm)");
479 pos = fdGetCpioPos(fsm->cfd);
480 fdSetCpioPos(fsm->cfd, 0);
482 fsm->iter = mapInitIterator(ts, fi);
484 if (fsm->goal == FSM_PKGINSTALL) {
485 if (ts && ts->notify) {
486 /*@-type@*/ /* FIX: cast? */
487 /*@-noeffectuncon @*/ /* FIX: check rc */
488 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
489 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
490 /*@=noeffectuncon @*/
496 fsm->archiveSize = archiveSize;
497 if (fsm->archiveSize)
498 *fsm->archiveSize = 0;
499 fsm->failedFile = failedFile;
501 *fsm->failedFile = NULL;
504 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
505 if (fsm->goal == FSM_PKGINSTALL) {
506 if (ts && ts->id > 0)
507 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
511 rc = fsmStage(fsm, FSM_CREATE);
512 if (rc && !ec) ec = rc;
514 rc = fsmStage(fsm, fsm->goal);
515 if (rc && !ec) ec = rc;
517 if (fsm->archiveSize && ec == 0)
518 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
523 int fsmTeardown(FSM_t fsm)
528 rc = fsmStage(fsm, FSM_DESTROY);
530 fsm->iter = mapFreeIterator(fsm->iter);
532 /*@-type@*/ /* FIX: cast? */
533 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
537 fsm->failedFile = NULL;
541 int fsmMapPath(FSM_t fsm)
543 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
550 fsm->action = FA_UNKNOWN;
554 if (fi && i >= 0 && i < fi->fc) {
556 fsm->astriplen = fi->astriplen;
557 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
558 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
559 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
561 /* src rpms have simple base name in payload. */
562 fsm->dirName = fi->dnl[fi->dil[i]];
563 fsm->baseName = fi->bnl[i];
565 switch (fsm->action) {
568 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
577 assert(fi->type == TR_ADDED);
581 if (fi->fstates && fi->type == TR_ADDED)
582 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
585 case FA_SKIPNETSHARED:
586 if (fi->fstates && fi->type == TR_ADDED)
587 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
591 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
594 fsm->osuffix = SUFFIX_RPMORIG;
595 /*@innerbreak@*/ break;
597 fsm->osuffix = SUFFIX_RPMSAVE;
598 /*@innerbreak@*/ break;
603 assert(fi->type == TR_ADDED);
604 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
605 fsm->nsuffix = SUFFIX_RPMNEW;
609 assert(fi->type == TR_ADDED);
610 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
611 fsm->osuffix = SUFFIX_RPMSAVE;
614 assert(fi->type == TR_REMOVED);
616 * XXX TODO: %ghost probably shouldn't be removed, but that changes
617 * legacy rpm behavior.
624 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
625 const struct stat * st = &fsm->sb;
626 fsm->path = _free(fsm->path);
627 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
628 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
634 int fsmMapAttrs(FSM_t fsm)
636 struct stat * st = &fsm->sb;
637 TFI_t fi = fsmGetFi(fsm);
640 if (fi && i >= 0 && i < fi->fc) {
642 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
644 (fi->fmodes ? fi->fmodes[i] : perms);
646 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
648 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
650 (fi->frdevs ? fi->frdevs[i] : 0);
652 (fi->fmtimes ? fi->fmtimes[i] : 0);
654 if (fsm->mapFlags & CPIO_MAP_MODE)
655 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
656 if (fsm->mapFlags & CPIO_MAP_TYPE) {
657 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
658 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
659 && st->st_nlink == 0)
661 st->st_rdev = finalRdev;
662 st->st_mtime = finalMtime;
664 if (fsm->mapFlags & CPIO_MAP_UID)
665 st->st_uid = finalUid;
666 if (fsm->mapFlags & CPIO_MAP_GID)
667 st->st_gid = finalGid;
669 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
676 * Create file from payload stream.
677 * @param fsm file state machine data
678 * @return 0 on success
680 static int expandRegular(/*@special@*/ FSM_t fsm)
682 /*@globals fileSystem@*/
683 /*@modifies fsm, fileSystem @*/
685 const char * fmd5sum;
686 const struct stat * st = &fsm->sb;
687 int left = st->st_size;
690 rc = fsmStage(fsm, FSM_WOPEN);
694 /* XXX md5sum's will break on repackaging that includes modified files. */
695 fmd5sum = fsm->fmd5sum;
697 if (st->st_size > 0 && fmd5sum)
698 fdInitDigest(fsm->wfd, PGPHASHALGO_MD5, 0);
702 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
703 rc = fsmStage(fsm, FSM_DREAD);
707 rc = fsmStage(fsm, FSM_WRITE);
713 /* don't call this with fileSize == fileComplete */
715 (void) fsmStage(fsm, FSM_NOTIFY);
718 if (st->st_size > 0 && fmd5sum) {
719 const char * md5sum = NULL;
721 (void) Fflush(fsm->wfd);
722 fdFiniDigest(fsm->wfd, PGPHASHALGO_MD5, (void **)&md5sum, NULL, 1);
724 if (md5sum == NULL) {
725 rc = CPIOERR_MD5SUM_MISMATCH;
727 if (strcmp(md5sum, fmd5sum))
728 rc = CPIOERR_MD5SUM_MISMATCH;
729 md5sum = _free(md5sum);
734 (void) fsmStage(fsm, FSM_WCLOSE);
739 * Write next item to payload stream.
740 * @param fsm file state machine data
741 * @param writeData should data be written?
742 * @return 0 on success
744 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
745 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
746 /*@globals fileSystem@*/
747 /*@modifies fsm, fileSystem @*/
749 const char * path = fsm->path;
750 const char * opath = fsm->opath;
751 struct stat * st = &fsm->sb;
752 struct stat * ost = &fsm->osb;
753 size_t pos = fdGetCpioPos(fsm->cfd);
754 char * symbuf = NULL;
758 st->st_size = (writeData ? ost->st_size : 0);
761 if (S_ISDIR(st->st_mode)) {
763 } else if (S_ISLNK(st->st_mode)) {
765 * While linux puts the size of a symlink in the st_size field,
766 * I don't think that's a specified standard.
768 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
769 rc = fsmStage(fsm, FSM_READLINK);
771 st->st_size = fsm->rdnb;
772 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
776 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
777 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
778 char * t = alloca(nb);
781 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
783 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
784 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
785 TFI_t fi = fsmGetFi(fsm);
787 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
790 rc = fsmStage(fsm, FSM_HWRITE);
794 if (writeData && S_ISREG(st->st_mode)) {
797 void * mapped = (void *)-1;
801 rc = fsmStage(fsm, FSM_ROPEN);
804 /* XXX unbuffered mmap generates *lots* of fdio debugging */
807 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
808 if (mapped != (void *)-1) {
810 fsm->rdbuf = (char *) mapped;
811 fsm->rdlen = nmapped = st->st_size;
819 if (mapped != (void *)-1) {
824 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
825 rc = fsmStage(fsm, FSM_READ);
829 /* XXX DWRITE uses rdnb for I/O length. */
830 rc = fsmStage(fsm, FSM_DWRITE);
837 if (mapped != (void *)-1) {
838 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
843 } else if (writeData && S_ISLNK(st->st_mode)) {
844 /* XXX DWRITE uses rdnb for I/O length. */
845 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
846 fsm->rdnb = strlen(symbuf);
847 rc = fsmStage(fsm, FSM_DWRITE);
851 rc = fsmStage(fsm, FSM_PAD);
854 { const rpmTransactionSet ts = fsmGetTs(fsm);
855 TFI_t fi = fsmGetFi(fsm);
856 if (ts && ts->notify && fi) {
857 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
858 /*@-type@*/ /* FIX: cast? */
859 /*@-noeffectuncon @*/ /* FIX: check rc */
860 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
861 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
862 /*@=noeffectuncon @*/
871 (void) fsmStage(fsm, FSM_RCLOSE);
872 /*@-dependenttrans@*/
875 /*@=dependenttrans@*/
880 * Write set of linked files to payload stream.
881 * @param fsm file state machine data
882 * @return 0 on success
884 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
885 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
886 /*@globals fileSystem@*/
887 /*@modifies fsm, fileSystem @*/
889 const char * path = fsm->path;
890 const char * nsuffix = fsm->nsuffix;
891 int iterIndex = fsm->ix;
900 for (i = fsm->li->nlink - 1; i >= 0; i--) {
902 if (fsm->li->filex[i] < 0) continue;
904 fsm->ix = fsm->li->filex[i];
905 rc = fsmStage(fsm, FSM_MAP);
907 /* Write data after last link. */
908 rc = writeFile(fsm, (i == 0));
909 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
911 *fsm->failedFile = xstrdup(fsm->path);
914 fsm->path = _free(fsm->path);
915 fsm->li->filex[i] = -1;
919 fsm->nsuffix = nsuffix;
925 * Create pending hard links to existing file.
926 * @param fsm file state machine data
927 * @return 0 on success
929 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
930 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
931 /*@globals fileSystem@*/
932 /*@modifies fsm, fileSystem @*/
934 const char * path = fsm->path;
935 const char * opath = fsm->opath;
936 const char * nsuffix = fsm->nsuffix;
937 int iterIndex = fsm->ix;
947 fsm->ix = fsm->li->filex[fsm->li->createdPath];
948 rc = fsmStage(fsm, FSM_MAP);
949 fsm->opath = fsm->path;
952 for (i = 0; i < fsm->li->nlink; i++) {
953 if (fsm->li->filex[i] < 0) continue;
954 if (i == fsm->li->createdPath) continue;
956 fsm->ix = fsm->li->filex[i];
957 fsm->path = _free(fsm->path);
958 rc = fsmStage(fsm, FSM_MAP);
959 rc = fsmStage(fsm, FSM_VERIFY);
961 if (rc != CPIOERR_LSTAT_FAILED) break;
963 /* XXX link(fsm->opath, fsm->path) */
964 rc = fsmStage(fsm, FSM_LINK);
965 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
967 *fsm->failedFile = xstrdup(fsm->path);
970 fsm->li->linksLeft--;
973 fsm->path = _free(fsm->path);
974 fsm->opath = _free(fsm->opath);
977 fsm->nsuffix = nsuffix;
984 * Commit hard linked file set atomically.
985 * @param fsm file state machine data
986 * @return 0 on success
988 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
989 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
990 fsm->li, fsm->links @*/
991 /*@globals fileSystem@*/
992 /*@modifies fsm, fileSystem @*/
994 const char * path = fsm->path;
995 const char * nsuffix = fsm->nsuffix;
996 int iterIndex = fsm->ix;
997 struct stat * st = &fsm->sb;
1002 fsm->nsuffix = NULL;
1006 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1007 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1012 for (i = 0; i < fsm->li->nlink; i++) {
1013 if (fsm->li->filex[i] < 0) continue;
1014 fsm->ix = fsm->li->filex[i];
1015 rc = fsmStage(fsm, FSM_MAP);
1016 rc = fsmStage(fsm, FSM_COMMIT);
1017 fsm->path = _free(fsm->path);
1018 fsm->li->filex[i] = -1;
1021 fsm->ix = iterIndex;
1022 fsm->nsuffix = nsuffix;
1028 * Remove (if created) directories not explicitly included in package.
1029 * @param fsm file state machine data
1030 * @return 0 on success
1032 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1033 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1034 /*@globals fileSystem@*/
1035 /*@modifies fsm, fileSystem @*/
1037 const char * path = fsm->path;
1038 void * dnli = dnlInitIterator(fsm, 1);
1039 char * dn = fsm->rdbuf;
1040 int dc = dnlCount(dnli);
1045 /*@-observertrans -dependenttrans@*/
1046 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1047 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1048 int dnlen = strlen(fsm->path);
1051 dc = dnlIndex(dnli);
1052 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1055 /* Copy to avoid const on fsm->path. */
1056 te = stpcpy(dn, fsm->path) - 1;
1059 /* Remove generated directories. */
1060 /*@-usereleased@*/ /* LCL: te used after release? */
1064 rc = fsmStage(fsm, FSM_RMDIR);
1068 /*@innerbreak@*/ break;
1070 } while ((te - fsm->path) > fsm->dnlx[dc]);
1073 dnli = dnlFreeIterator(dnli);
1074 /*@=observertrans =dependenttrans@*/
1081 * Create (if necessary) directories not explicitly included in package.
1082 * @param fsm file state machine data
1083 * @return 0 on success
1085 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1086 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1087 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1088 /*@defines fsm->dnlx, fsm->ldn @*/
1089 /*@globals fileSystem@*/
1090 /*@modifies fsm, fileSystem @*/
1092 struct stat * st = &fsm->sb;
1093 struct stat * ost = &fsm->osb;
1094 const char * path = fsm->path;
1095 mode_t st_mode = st->st_mode;
1096 void * dnli = dnlInitIterator(fsm, 0);
1097 char * dn = fsm->rdbuf;
1098 int dc = dnlCount(dnli);
1105 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1106 /*@-observertrans -dependenttrans@*/
1107 if (fsm->dnlx != NULL)
1108 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1109 int dnlen = strlen(fsm->path);
1112 dc = dnlIndex(dnli);
1113 if (dc < 0) continue;
1114 fsm->dnlx[dc] = dnlen;
1118 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1119 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1121 /*@=compdef =nullpass@*/
1123 /* Copy to avoid const on fsm->path. */
1124 (void) stpcpy(dn, fsm->path);
1127 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1128 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1130 /*@innercontinue@*/ continue;
1134 /* Already validated? */
1135 /*@-usedef -compdef -nullpass -nullderef@*/
1136 if (i < fsm->ldnlen &&
1137 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1138 !strncmp(fsm->path, fsm->ldn, i))
1141 /* Move pre-existing path marker forward. */
1142 fsm->dnlx[dc] = (te - dn);
1143 /*@innercontinue@*/ continue;
1145 /*@=usedef =compdef =nullpass =nullderef@*/
1147 /* Validate next component of path. */
1148 rc = fsmStage(fsm, FSM_LSTAT);
1151 /* Directory already exists? */
1152 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1153 /* Move pre-existing path marker forward. */
1154 fsm->dnlx[dc] = (te - dn);
1155 } else if (rc == CPIOERR_LSTAT_FAILED) {
1156 TFI_t fi = fsmGetFi(fsm);
1158 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1159 rc = fsmStage(fsm, FSM_MKDIR);
1161 rpmMessage(RPMMESS_DEBUG,
1162 _("%s directory created with perms %04o.\n"),
1163 fsm->path, (unsigned)(st->st_mode & 07777));
1167 /*@innerbreak@*/ break;
1171 /* Save last validated path. */
1172 if (fsm->ldnalloc < (dnlen + 1)) {
1173 fsm->ldnalloc = dnlen + 100;
1174 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1176 if (fsm->ldn != NULL) { /* XXX can't happen */
1177 strcpy(fsm->ldn, fsm->path);
1178 fsm->ldnlen = dnlen;
1181 dnli = dnlFreeIterator(dnli);
1182 /*@=observertrans =dependenttrans@*/
1185 st->st_mode = st_mode; /* XXX restore st->st_mode */
1191 * Check for file on disk.
1192 * @param fsm file state machine data
1193 * @return 0 on success
1195 static int fsmStat(FSM_t fsm)
1197 int saveerrno = errno;
1200 if (fsm->path != NULL) {
1201 int saveernno = errno;
1202 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1203 ? FSM_LSTAT : FSM_STAT));
1204 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1208 } else if (rc == 0) {
1212 /* Skip %ghost files on build. */
1220 int fsmStage(FSM_t fsm, fileStage stage)
1223 fileStage prevStage = fsm->stage;
1224 const char * const prev = fileStageString(prevStage);
1226 static int modulo = 4;
1227 const char * const cur = fileStageString(stage);
1228 struct stat * st = &fsm->sb;
1229 struct stat * ost = &fsm->osb;
1230 int saveerrno = errno;
1235 #define _fafilter(_a) \
1236 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1237 ? fileActionString(_a) : "")
1239 if (stage & FSM_DEAD) {
1241 } else if (stage & FSM_INTERNAL) {
1242 if (_fsm_debug && !(stage & FSM_SYSCALL))
1243 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1245 (unsigned)st->st_mode, (int)st->st_nlink,
1246 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1247 (fsm->path ? fsm->path : ""),
1248 _fafilter(fsm->action));
1251 if (_fsm_debug || !(stage & FSM_VERBOSE))
1252 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1254 (unsigned)st->st_mode, (int)st->st_nlink,
1255 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1256 (fsm->path ? fsm->path + fsm->astriplen : ""),
1257 _fafilter(fsm->action));
1265 case FSM_PKGINSTALL:
1267 /* Clean fsm, free'ing memory. Read next archive header. */
1268 rc = fsmStage(fsm, FSM_INIT);
1270 /* Exit on end-of-payload. */
1271 if (rc == CPIOERR_HDR_TRAILER) {
1273 /*@loopbreak@*/ break;
1276 /* Exit on error. */
1279 (void) fsmStage(fsm, FSM_UNDO);
1280 /*@loopbreak@*/ break;
1283 /* Extract file from archive. */
1284 rc = fsmStage(fsm, FSM_PROCESS);
1286 (void) fsmStage(fsm, FSM_UNDO);
1287 /*@loopbreak@*/ break;
1290 /* Notify on success. */
1291 (void) fsmStage(fsm, FSM_NOTIFY);
1293 rc = fsmStage(fsm, FSM_FINI);
1295 /*@loopbreak@*/ break;
1302 /* Clean fsm, free'ing memory. */
1303 rc = fsmStage(fsm, FSM_INIT);
1305 /* Exit on end-of-payload. */
1306 if (rc == CPIOERR_HDR_TRAILER) {
1308 /*@loopbreak@*/ break;
1311 /* Rename/erase next item. */
1312 if (fsmStage(fsm, FSM_FINI))
1313 /*@loopbreak@*/ break;
1319 rc = fsmStage(fsm, FSM_INIT);
1321 /* Exit on end-of-payload. */
1322 if (rc == CPIOERR_HDR_TRAILER) {
1324 /*@loopbreak@*/ break;
1327 /* Exit on error. */
1330 (void) fsmStage(fsm, FSM_UNDO);
1331 /*@loopbreak@*/ break;
1334 /* Copy file into archive. */
1335 rc = fsmStage(fsm, FSM_PROCESS);
1337 (void) fsmStage(fsm, FSM_UNDO);
1338 /*@loopbreak@*/ break;
1341 if (fsmStage(fsm, FSM_FINI))
1342 /*@loopbreak@*/ break;
1345 /* Flush partial sets of hard linked files. */
1346 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1348 while ((fsm->li = fsm->links) != NULL) {
1349 fsm->links = fsm->li->next;
1350 fsm->li->next = NULL;
1352 /* Re-calculate link count for archive header. */
1353 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1354 if (fsm->li->filex[i] < 0)
1355 /*@innercontinue@*/ continue;
1359 /* XXX force the contents out as well. */
1361 fsm->li->filex[0] = fsm->li->filex[j];
1362 fsm->li->filex[j] = -1;
1364 fsm->li->sb.st_nlink = nlink;
1366 fsm->sb = fsm->li->sb; /* structure assignment */
1367 fsm->osb = fsm->sb; /* structure assignment */
1369 if (!rc) rc = writeLinkedFile(fsm);
1371 fsm->li = freeHardLink(fsm->li);
1376 rc = fsmStage(fsm, FSM_TRAILER);
1380 { rpmTransactionSet ts = fsmGetTs(fsm);
1381 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1382 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1383 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1386 fsm->path = _free(fsm->path);
1387 fsm->opath = _free(fsm->opath);
1388 fsm->dnlx = _free(fsm->dnlx);
1390 fsm->ldn = _free(fsm->ldn);
1391 fsm->ldnalloc = fsm->ldnlen = 0;
1393 fsm->rdsize = fsm->wrsize = 0;
1394 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1395 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1396 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1397 fsm->rdsize = 8 * BUFSIZ;
1398 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1399 fsm->wrsize = 8 * BUFSIZ;
1400 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1403 fsm->mkdirsdone = 0;
1408 errno = 0; /* XXX get rid of EBADF */
1411 /* Detect and create directories not explicitly in package. */
1412 if (fsm->goal == FSM_PKGINSTALL) {
1413 rc = fsmStage(fsm, FSM_MKDIRS);
1414 if (!rc) fsm->mkdirsdone = 1;
1419 fsm->path = _free(fsm->path);
1421 fsm->diskchecked = fsm->exists = 0;
1423 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1424 fsm->action = FA_UNKNOWN;
1425 fsm->osuffix = NULL;
1426 fsm->nsuffix = NULL;
1428 if (fsm->goal == FSM_PKGINSTALL) {
1429 /* Read next header from payload, checking for end-of-payload. */
1430 rc = fsmStage(fsm, FSM_NEXT);
1434 /* Identify mapping index. */
1435 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1436 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1438 /* Detect end-of-loop and/or mapping error. */
1440 if (fsm->goal == FSM_PKGINSTALL) {
1442 rpmMessage(RPMMESS_WARNING,
1443 _("archive file %s was not found in header file list\n"),
1446 if (fsm->failedFile && *fsm->failedFile == NULL)
1447 *fsm->failedFile = xstrdup(fsm->path);
1448 rc = CPIOERR_UNMAPPED_FILE;
1450 rc = CPIOERR_HDR_TRAILER;
1455 /* On non-install, mode must be known so that dirs don't get suffix. */
1456 if (fsm->goal != FSM_PKGINSTALL) {
1457 TFI_t fi = fsmGetFi(fsm);
1458 st->st_mode = fi->fmodes[fsm->ix];
1461 /* Generate file path. */
1462 rc = fsmStage(fsm, FSM_MAP);
1465 /* Perform lstat/stat for disk file. */
1469 if (fsm->path != NULL) {
1470 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1471 ? FSM_LSTAT : FSM_STAT));
1472 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1478 } else if (rc == 0) {
1482 /* Skip %ghost files on build. */
1486 fsm->diskchecked = 1;
1489 /* On non-install, the disk file stat is what's remapped. */
1490 if (fsm->goal != FSM_PKGINSTALL)
1491 *st = *ost; /* structure assignment */
1493 /* Remap file perms, owner, and group. */
1494 rc = fsmMapAttrs(fsm);
1497 fsm->postpone = XFA_SKIPPING(fsm->action);
1498 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1499 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1500 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1501 fsm->postpone = saveHardLink(fsm);
1508 rc = fsmMapPath(fsm);
1511 rc = fsmMkdirs(fsm);
1515 rc = fsmRmdirs(fsm);
1518 if (fsm->postpone) {
1519 if (fsm->goal == FSM_PKGINSTALL)
1520 rc = fsmStage(fsm, FSM_EAT);
1524 if (fsm->goal == FSM_PKGBUILD) {
1525 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1526 struct hardLink * li, * prev;
1528 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1529 rc = writeLinkedFile(fsm);
1530 if (rc) break; /* W2DO? */
1532 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1534 /*@loopbreak@*/ break;
1537 fsm->links = fsm->li->next;
1539 prev->next = fsm->li->next;
1540 fsm->li->next = NULL;
1541 fsm->li = freeHardLink(fsm->li);
1543 rc = writeFile(fsm, 1);
1548 if (fsm->goal != FSM_PKGINSTALL)
1551 if (S_ISREG(st->st_mode)) {
1552 const char * path = fsm->path;
1554 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1555 rc = fsmStage(fsm, FSM_VERIFY);
1557 if (rc == 0 && fsm->osuffix) {
1558 const char * opath = fsm->opath;
1559 fsm->opath = fsm->path;
1560 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1561 rc = fsmStage(fsm, FSM_RENAME);
1563 rpmMessage(RPMMESS_WARNING,
1564 _("%s saved as %s\n"), fsm->opath, fsm->path);
1565 fsm->path = _free(fsm->path);
1569 /*@-dependenttrans@*/
1571 /*@=dependenttrans@*/
1572 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1573 rc = expandRegular(fsm);
1574 } else if (S_ISDIR(st->st_mode)) {
1575 mode_t st_mode = st->st_mode;
1576 rc = fsmStage(fsm, FSM_VERIFY);
1577 if (rc == CPIOERR_LSTAT_FAILED) {
1578 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1579 st->st_mode |= 00700;
1580 rc = fsmStage(fsm, FSM_MKDIR);
1581 st->st_mode = st_mode; /* XXX restore st->st_mode */
1583 } else if (S_ISLNK(st->st_mode)) {
1584 const char * opath = fsm->opath;
1586 if ((st->st_size + 1) > fsm->rdsize) {
1587 rc = CPIOERR_HDR_SIZE;
1591 fsm->wrlen = st->st_size;
1592 rc = fsmStage(fsm, FSM_DREAD);
1593 if (!rc && fsm->rdnb != fsm->wrlen)
1594 rc = CPIOERR_READ_FAILED;
1597 fsm->wrbuf[st->st_size] = '\0';
1598 /* XXX symlink(fsm->opath, fsm->path) */
1599 /*@-dependenttrans@*/
1600 fsm->opath = fsm->wrbuf;
1601 /*@=dependenttrans@*/
1602 rc = fsmStage(fsm, FSM_VERIFY);
1603 if (rc == CPIOERR_LSTAT_FAILED)
1604 rc = fsmStage(fsm, FSM_SYMLINK);
1605 fsm->opath = opath; /* XXX restore fsm->path */
1606 } else if (S_ISFIFO(st->st_mode)) {
1607 mode_t st_mode = st->st_mode;
1608 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1609 rc = fsmStage(fsm, FSM_VERIFY);
1610 if (rc == CPIOERR_LSTAT_FAILED) {
1611 st->st_mode = 0000; /* XXX abuse st->st_mode */
1612 rc = fsmStage(fsm, FSM_MKFIFO);
1613 st->st_mode = st_mode; /* XXX restore st->st_mode */
1615 } else if (S_ISCHR(st->st_mode) ||
1616 S_ISBLK(st->st_mode) ||
1617 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1619 rc = fsmStage(fsm, FSM_VERIFY);
1620 if (rc == CPIOERR_LSTAT_FAILED)
1621 rc = fsmStage(fsm, FSM_MKNOD);
1623 rc = CPIOERR_UNKNOWN_FILETYPE;
1625 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1626 fsm->li->createdPath = fsm->li->linkIndex;
1627 rc = fsmMakeLinks(fsm);
1633 rc = fsmMakeLinks(fsm);
1635 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1636 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1637 rpmTransactionSet ts = fsmGetTs(fsm);
1638 TFI_t fi = fsmGetFi(fsm);
1639 if (ts && ts->notify && fi) {
1640 /*@-type@*/ /* FIX: cast? */
1641 /*@-noeffectuncon @*/ /* FIX: check rc */
1642 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1643 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1644 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1645 /*@=noeffectuncon @*/
1653 if (fsm->goal == FSM_PKGINSTALL) {
1654 (void) fsmStage(fsm,
1655 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1657 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1659 (void) fsmStage(fsm, FSM_RMDIRS);
1665 if (fsm->failedFile && *fsm->failedFile == NULL)
1666 *fsm->failedFile = xstrdup(fsm->path);
1669 if (!fsm->postpone && fsm->commit) {
1670 if (fsm->goal == FSM_PKGINSTALL)
1671 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1672 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1673 if (fsm->goal == FSM_PKGCOMMIT)
1674 rc = fsmStage(fsm, FSM_COMMIT);
1675 if (fsm->goal == FSM_PKGERASE)
1676 rc = fsmStage(fsm, FSM_COMMIT);
1678 fsm->path = _free(fsm->path);
1679 fsm->opath = _free(fsm->opath);
1680 memset(st, 0, sizeof(*st));
1681 memset(ost, 0, sizeof(*ost));
1684 /* Rename pre-existing modified or unmanaged file. */
1685 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1686 const char * opath = fsm->opath;
1687 const char * path = fsm->path;
1688 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1689 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1690 rc = fsmStage(fsm, FSM_RENAME);
1692 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1693 fsm->opath, fsm->path);
1695 fsm->path = _free(fsm->path);
1697 fsm->opath = _free(fsm->opath);
1701 /* Remove erased files. */
1702 if (fsm->goal == FSM_PKGERASE) {
1703 if (fsm->action == FA_ERASE) {
1704 TFI_t fi = fsmGetFi(fsm);
1705 if (S_ISDIR(st->st_mode)) {
1706 rc = fsmStage(fsm, FSM_RMDIR);
1709 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1711 /* XXX make sure that build side permits %missingok on directories. */
1712 if (fsm->fflags & RPMFILE_MISSINGOK)
1713 /*@innerbreak@*/ break;
1715 /* XXX common error message. */
1717 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1718 _("%s rmdir of %s failed: Directory not empty\n"),
1719 fiTypeString(fi), fsm->path);
1720 /*@innerbreak@*/ break;
1723 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1724 _("%s rmdir of %s failed: %s\n"),
1725 fiTypeString(fi), fsm->path, strerror(errno));
1726 /*@innerbreak@*/ break;
1729 rc = fsmStage(fsm, FSM_UNLINK);
1731 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1733 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1734 _("%s unlink of %s failed: %s\n"),
1735 fiTypeString(fi), fsm->path, strerror(errno));
1738 /* XXX Failure to remove is not (yet) cause for failure. */
1739 if (!strict_erasures) rc = 0;
1743 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1744 /* Rename temporary to final file name. */
1745 if (!S_ISDIR(st->st_mode) &&
1746 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1748 fsm->opath = fsm->path;
1749 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1750 rc = fsmStage(fsm, FSM_RENAME);
1751 if (!rc && fsm->nsuffix) {
1752 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1753 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1754 (opath ? opath : ""), fsm->path);
1755 opath = _free(opath);
1757 fsm->opath = _free(fsm->opath);
1759 if (S_ISLNK(st->st_mode)) {
1760 if (!rc && !getuid())
1761 rc = fsmStage(fsm, FSM_LCHOWN);
1763 if (!rc && !getuid())
1764 rc = fsmStage(fsm, FSM_CHOWN);
1766 rc = fsmStage(fsm, FSM_CHMOD);
1768 time_t mtime = st->st_mtime;
1769 TFI_t fi = fsmGetFi(fsm);
1771 st->st_mtime = fi->fmtimes[fsm->ix];
1772 rc = fsmStage(fsm, FSM_UTIME);
1773 st->st_mtime = mtime;
1778 /* Notify on success. */
1779 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1780 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1781 *fsm->failedFile = fsm->path;
1786 fsm->path = _free(fsm->path);
1788 /* Check for hard links missing from payload. */
1789 while ((fsm->li = fsm->links) != NULL) {
1790 fsm->links = fsm->li->next;
1791 fsm->li->next = NULL;
1792 if (fsm->goal == FSM_PKGINSTALL &&
1793 fsm->commit && fsm->li->linksLeft)
1795 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1796 if (fsm->li->filex[i] < 0)
1797 /*@innercontinue@*/ continue;
1798 rc = CPIOERR_MISSING_HARDLINK;
1799 if (fsm->failedFile && *fsm->failedFile == NULL) {
1800 fsm->ix = fsm->li->filex[i];
1801 if (!fsmStage(fsm, FSM_MAP)) {
1802 *fsm->failedFile = fsm->path;
1806 /*@loopbreak@*/ break;
1809 if (fsm->goal == FSM_PKGBUILD &&
1810 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1812 rc = CPIOERR_MISSING_HARDLINK;
1814 fsm->li = freeHardLink(fsm->li);
1816 fsm->ldn = _free(fsm->ldn);
1817 fsm->ldnalloc = fsm->ldnlen = 0;
1818 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1819 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1822 if (fsm->diskchecked && !fsm->exists) {
1823 rc = CPIOERR_LSTAT_FAILED;
1826 if (S_ISREG(st->st_mode)) {
1827 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1828 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1830 * XXX HP-UX (and other os'es) don't permit unlink on busy
1833 fsm->opath = fsm->path;
1835 rc = fsmStage(fsm, FSM_RENAME);
1837 (void) fsmStage(fsm, FSM_UNLINK);
1839 rc = CPIOERR_UNLINK_FAILED;
1840 fsm->path = fsm->opath;
1842 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1843 /*@notreached@*/ break;
1844 } else if (S_ISDIR(st->st_mode)) {
1845 if (S_ISDIR(ost->st_mode)) return 0;
1846 if (S_ISLNK(ost->st_mode)) {
1847 rc = fsmStage(fsm, FSM_STAT);
1848 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1853 if (S_ISDIR(ost->st_mode)) return 0;
1855 } else if (S_ISLNK(st->st_mode)) {
1856 if (S_ISLNK(ost->st_mode)) {
1857 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1858 rc = fsmStage(fsm, FSM_READLINK);
1863 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1865 } else if (S_ISFIFO(st->st_mode)) {
1866 if (S_ISFIFO(ost->st_mode)) return 0;
1867 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1868 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1869 (ost->st_rdev == st->st_rdev)) return 0;
1870 } else if (S_ISSOCK(st->st_mode)) {
1871 if (S_ISSOCK(ost->st_mode)) return 0;
1873 /* XXX shouldn't do this with commit/undo. */
1875 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1876 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1877 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1878 /*@notreached@*/ break;
1881 rc = Unlink(fsm->path);
1882 if (_fsm_debug && (stage & FSM_SYSCALL))
1883 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1884 fsm->path, (rc < 0 ? strerror(errno) : ""));
1885 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1888 rc = Rename(fsm->opath, fsm->path);
1889 if (_fsm_debug && (stage & FSM_SYSCALL))
1890 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1891 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1892 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1895 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1896 if (_fsm_debug && (stage & FSM_SYSCALL))
1897 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1898 fsm->path, (unsigned)(st->st_mode & 07777),
1899 (rc < 0 ? strerror(errno) : ""));
1900 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1903 rc = Rmdir(fsm->path);
1904 if (_fsm_debug && (stage & FSM_SYSCALL))
1905 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1906 fsm->path, (rc < 0 ? strerror(errno) : ""));
1907 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1910 rc = chown(fsm->path, st->st_uid, st->st_gid);
1911 if (_fsm_debug && (stage & FSM_SYSCALL))
1912 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1913 fsm->path, (int)st->st_uid, (int)st->st_gid,
1914 (rc < 0 ? strerror(errno) : ""));
1915 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1918 #if ! CHOWN_FOLLOWS_SYMLINK
1919 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1920 if (_fsm_debug && (stage & FSM_SYSCALL))
1921 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1922 fsm->path, (int)st->st_uid, (int)st->st_gid,
1923 (rc < 0 ? strerror(errno) : ""));
1924 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1928 rc = chmod(fsm->path, (st->st_mode & 07777));
1929 if (_fsm_debug && (stage & FSM_SYSCALL))
1930 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1931 fsm->path, (unsigned)(st->st_mode & 07777),
1932 (rc < 0 ? strerror(errno) : ""));
1933 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1936 { struct utimbuf stamp;
1937 stamp.actime = st->st_mtime;
1938 stamp.modtime = st->st_mtime;
1939 rc = utime(fsm->path, &stamp);
1940 if (_fsm_debug && (stage & FSM_SYSCALL))
1941 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1942 fsm->path, (unsigned)st->st_mtime,
1943 (rc < 0 ? strerror(errno) : ""));
1944 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1948 rc = symlink(fsm->opath, fsm->path);
1949 if (_fsm_debug && (stage & FSM_SYSCALL))
1950 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1951 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1952 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1955 rc = Link(fsm->opath, fsm->path);
1956 if (_fsm_debug && (stage & FSM_SYSCALL))
1957 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1958 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1959 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1962 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1963 if (_fsm_debug && (stage & FSM_SYSCALL))
1964 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1965 fsm->path, (unsigned)(st->st_mode & 07777),
1966 (rc < 0 ? strerror(errno) : ""));
1967 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1970 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
1971 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1972 /*@=unrecog =portability @*/
1973 if (_fsm_debug && (stage & FSM_SYSCALL))
1974 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1975 fsm->path, (unsigned)(st->st_mode & ~07777),
1976 (unsigned)st->st_rdev,
1977 (rc < 0 ? strerror(errno) : ""));
1978 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1981 rc = Lstat(fsm->path, ost);
1982 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1983 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1984 fsm->path, (rc < 0 ? strerror(errno) : ""));
1985 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1988 rc = Stat(fsm->path, ost);
1989 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1990 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1991 fsm->path, (rc < 0 ? strerror(errno) : ""));
1992 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1995 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1996 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1997 if (_fsm_debug && (stage & FSM_SYSCALL))
1998 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1999 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2000 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
2003 fsm->rdbuf[fsm->rdnb] = '\0';
2011 rc = fsmStage(fsm, FSM_HREAD);
2013 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2014 fsm->path = _free(fsm->path);
2015 rc = CPIOERR_HDR_TRAILER;
2018 rc = fsmStage(fsm, FSM_POS);
2021 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2022 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2023 rc = fsmStage(fsm, FSM_DREAD);
2025 /*@loopbreak@*/ break;
2029 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2032 (void) fsmStage(fsm, FSM_DREAD);
2036 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2038 memset(fsm->rdbuf, 0, left);
2039 /* XXX DWRITE uses rdnb for I/O length. */
2041 (void) fsmStage(fsm, FSM_DWRITE);
2045 rc = cpioTrailerWrite(fsm);
2048 rc = fsmStage(fsm, FSM_POS);
2050 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2053 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2056 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2057 if (_fsm_debug && (stage & FSM_SYSCALL))
2058 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2059 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2060 (int)fsm->wrlen, (int)fsm->rdnb);
2061 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2062 rc = CPIOERR_READ_FAILED;
2064 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2067 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2068 if (_fsm_debug && (stage & FSM_SYSCALL))
2069 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2070 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2071 (int)fsm->rdnb, (int)fsm->wrnb);
2072 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2073 rc = CPIOERR_WRITE_FAILED;
2075 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2079 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2080 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2081 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2083 rc = CPIOERR_OPEN_FAILED;
2086 if (_fsm_debug && (stage & FSM_SYSCALL))
2087 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2088 fsm->path, fsm->rfd, fsm->rdbuf);
2091 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2092 if (_fsm_debug && (stage & FSM_SYSCALL))
2093 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2094 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2095 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2096 rc = CPIOERR_READ_FAILED;
2100 if (_fsm_debug && (stage & FSM_SYSCALL))
2101 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2102 (void) Fclose(fsm->rfd);
2110 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2111 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2112 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2114 rc = CPIOERR_OPEN_FAILED;
2116 if (_fsm_debug && (stage & FSM_SYSCALL))
2117 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2118 fsm->path, fsm->wfd, fsm->wrbuf);
2121 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2122 if (_fsm_debug && (stage & FSM_SYSCALL))
2123 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2124 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2125 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2126 rc = CPIOERR_WRITE_FAILED;
2130 if (_fsm_debug && (stage & FSM_SYSCALL))
2131 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2132 (void) Fclose(fsm->wfd);
2145 if (!(stage & FSM_INTERNAL)) {
2146 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2152 /*@obserever@*/ const char *const fileActionString(fileAction a)
2155 case FA_UNKNOWN: return "unknown";
2156 case FA_CREATE: return "create";
2157 case FA_COPYOUT: return "copyout";
2158 case FA_COPYIN: return "copyin";
2159 case FA_BACKUP: return "backup";
2160 case FA_SAVE: return "save";
2161 case FA_SKIP: return "skip";
2162 case FA_ALTNAME: return "altname";
2163 case FA_ERASE: return "erase";
2164 case FA_SKIPNSTATE: return "skipnstate";
2165 case FA_SKIPNETSHARED: return "skipnetshared";
2166 case FA_SKIPMULTILIB: return "skipmultilib";
2167 default: return "???";
2172 /*@observer@*/ const char *const fileStageString(fileStage a) {
2174 case FSM_UNKNOWN: return "unknown";
2176 case FSM_PKGINSTALL:return "pkginstall";
2177 case FSM_PKGERASE: return "pkgerase";
2178 case FSM_PKGBUILD: return "pkgbuild";
2179 case FSM_PKGCOMMIT: return "pkgcommit";
2180 case FSM_PKGUNDO: return "pkgundo";
2182 case FSM_CREATE: return "create";
2183 case FSM_INIT: return "init";
2184 case FSM_MAP: return "map";
2185 case FSM_MKDIRS: return "mkdirs";
2186 case FSM_RMDIRS: return "rmdirs";
2187 case FSM_PRE: return "pre";
2188 case FSM_PROCESS: return "process";
2189 case FSM_POST: return "post";
2190 case FSM_MKLINKS: return "mklinks";
2191 case FSM_NOTIFY: return "notify";
2192 case FSM_UNDO: return "undo";
2193 case FSM_FINI: return "fini";
2194 case FSM_COMMIT: return "commit";
2195 case FSM_DESTROY: return "destroy";
2196 case FSM_VERIFY: return "verify";
2198 case FSM_UNLINK: return "Unlink";
2199 case FSM_RENAME: return "Rename";
2200 case FSM_MKDIR: return "Mkdir";
2201 case FSM_RMDIR: return "rmdir";
2202 case FSM_CHOWN: return "chown";
2203 case FSM_LCHOWN: return "lchown";
2204 case FSM_CHMOD: return "chmod";
2205 case FSM_UTIME: return "utime";
2206 case FSM_SYMLINK: return "symlink";
2207 case FSM_LINK: return "Link";
2208 case FSM_MKFIFO: return "mkfifo";
2209 case FSM_MKNOD: return "mknod";
2210 case FSM_LSTAT: return "Lstat";
2211 case FSM_STAT: return "Stat";
2212 case FSM_READLINK: return "Readlink";
2213 case FSM_CHROOT: return "chroot";
2215 case FSM_NEXT: return "next";
2216 case FSM_EAT: return "eat";
2217 case FSM_POS: return "pos";
2218 case FSM_PAD: return "pad";
2219 case FSM_TRAILER: return "trailer";
2220 case FSM_HREAD: return "hread";
2221 case FSM_HWRITE: return "hwrite";
2222 case FSM_DREAD: return "Fread";
2223 case FSM_DWRITE: return "Fwrite";
2225 case FSM_ROPEN: return "Fopen";
2226 case FSM_READ: return "Fread";
2227 case FSM_RCLOSE: return "Fclose";
2228 case FSM_WOPEN: return "Fopen";
2229 case FSM_WRITE: return "Fwrite";
2230 case FSM_WCLOSE: return "Fclose";
2232 default: return "???";