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 return (iter ? iter->ts : NULL);
34 TFI_t fsmGetFi(const FSM_t fsm)
36 const FSMI_t iter = fsm->iter;
37 return (iter ? iter->fi : NULL);
40 #define SUFFIX_RPMORIG ".rpmorig"
41 #define SUFFIX_RPMSAVE ".rpmsave"
42 #define SUFFIX_RPMNEW ".rpmnew"
45 * Build path to file from file info, ornamented with subdir and suffix.
46 * @param fsm file state machine data
47 * @param st file stat info
48 * @param subdir subdir to use (NULL disables)
49 * @param suffix suffix to use (NULL disables)
50 * @retval path to file
52 static /*@only@*//*@null@*/
53 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
54 /*@null@*/ const struct stat * st,
55 /*@null@*/ const char * subdir,
56 /*@null@*/ const char * suffix)
57 /*@uses fsm->dirName, fsm->baseName */
60 const char * s = NULL;
65 nb = strlen(fsm->dirName) +
66 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
67 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
68 strlen(fsm->baseName) + 1;
70 t = stpcpy(t, fsm->dirName);
71 if (st && !S_ISDIR(st->st_mode))
72 if (subdir) t = stpcpy(t, subdir);
73 t = stpcpy(t, fsm->baseName);
74 if (st && !S_ISDIR(st->st_mode))
75 if (suffix) t = stpcpy(t, suffix);
81 * Destroy file info iterator.
82 * @param p file info iterator
85 /*@-mustmod@*/ /* LCL: *p is modified */
86 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
94 * Create file info iterator.
95 * @param a transaction set
96 * @param b transaction element file info
97 * @return file info iterator
100 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
103 rpmTransactionSet ts = (void *)a;
104 TFI_t fi = (void *)b;
107 iter = xcalloc(1, sizeof(*iter));
110 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
111 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
112 iter->isave = iter->i;
117 * Return next index into file info.
118 * @param a file info iterator
119 * @return next index, -1 on termination
121 /*@-mustmod@*/ /* LCL: *a is modified */
122 static int mapNextIterator(/*@null@*/ void * a)
129 const TFI_t fi = iter->fi;
131 if (iter->i >= 0) i = iter->i--;
133 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 a 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@*/ void * a, const char * fsmPath)
173 const TFI_t fi = iter->fi;
174 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
175 const char ** p = NULL;
177 if (fi->apath != NULL)
178 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
181 iter->i = p - fi->apath;
182 ix = mapNextIterator(iter);
190 * Directory name iterator.
192 typedef struct dnli_s {
193 /*@dependent@*/ TFI_t fi;
194 /*@only@*/ /*@null@*/ char * active;
201 * Destroy directory name iterator.
202 * @param a directory name iterator
203 * @retval NULL always
205 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
209 DNLI_t dnli = (void *)a;
210 if (dnli->active) free(dnli->active);
217 static inline int dnlCount(const DNLI_t dnli)
220 return (dnli ? dnli->fi->dc : 0);
225 static inline int dnlIndex(const DNLI_t dnli)
228 return (dnli ? dnli->isave : -1);
232 * Create directory name iterator.
233 * @param fsm file state machine data
234 * @param reverse traverse directory names in reverse order?
235 * @return directory name iterator
237 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
239 /*@uses fsm->iter @*/
242 TFI_t fi = fsmGetFi(fsm);
248 dnli = xcalloc(1, sizeof(*dnli));
250 dnli->reverse = reverse;
251 dnli->i = (reverse ? fi->dc : 0);
254 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
256 /* Identify parent directories not skipped. */
257 for (i = 0; i < fi->fc; i++)
258 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
260 /* Exclude parent directories that are explicitly included. */
261 for (i = 0; i < fi->fc; i++) {
262 int dil, dnlen, bnlen;
264 if (!S_ISDIR(fi->fmodes[i]))
268 dnlen = strlen(fi->dnl[dil]);
269 bnlen = strlen(fi->bnl[i]);
271 for (j = 0; j < fi->dc; j++) {
275 if (!dnli->active[j] || j == dil)
276 /*@innercontinue@*/ continue;
279 if (jlen != (dnlen+bnlen+1))
280 /*@innercontinue@*/ continue;
281 if (strncmp(dnl, fi->dnl[dil], dnlen))
282 /*@innercontinue@*/ continue;
283 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
284 /*@innercontinue@*/ continue;
285 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
286 /*@innercontinue@*/ continue;
287 /* This directory is included in the package. */
289 /*@innerbreak@*/ break;
293 /* Print only once per package. */
296 for (i = 0; i < fi->dc; i++) {
297 if (!dnli->active[i]) continue;
300 rpmMessage(RPMMESS_DEBUG,
301 _("========== Directories not explictly included in package:\n"));
303 rpmMessage(RPMMESS_DEBUG, _("%10d %s\n"), i, fi->dnl[i]);
306 rpmMessage(RPMMESS_DEBUG, "==========\n");
313 * Return next directory name (from file info).
314 * @param dnli directory name iterator
315 * @return next directory name
317 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
320 const char * dn = NULL;
328 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
329 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
331 if (i >= 0 && i < fi->dc)
341 * Save hard link in chain.
342 * @param fsm file state machine data
343 * @return Is chain only partially filled?
345 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
346 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
347 /*@defines fsm->li @*/
348 /*@releases fsm->path @*/
349 /*@globals fileSystem@*/
350 /*@modifies fsm, fileSystem @*/
352 struct stat * st = &fsm->sb;
357 /* Find hard link set. */
359 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
360 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
365 /* New hard link encountered, add new link to set. */
367 if (fsm->li == NULL) {
368 fsm->li = xcalloc(1, sizeof(*fsm->li));
369 fsm->li->next = NULL;
370 fsm->li->sb = *st; /* structure assignment */
371 fsm->li->nlink = st->st_nlink;
372 fsm->li->linkIndex = fsm->ix;
373 fsm->li->createdPath = -1;
375 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
376 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
377 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
379 if (fsm->goal == FSM_PKGBUILD)
380 fsm->li->linksLeft = st->st_nlink;
381 if (fsm->goal == FSM_PKGINSTALL)
382 fsm->li->linksLeft = 0;
385 fsm->li->next = fsm->links;
387 fsm->links = fsm->li;
391 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
392 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
393 /*@-observertrans -dependenttrans@*/
394 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
395 /*@=observertrans =dependenttrans@*/
396 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
398 if (fsm->goal == FSM_PKGBUILD)
399 return (fsm->li->linksLeft > 0);
401 if (fsm->goal != FSM_PKGINSTALL)
404 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
407 /* Here come the bits, time to choose a non-skipped file name. */
408 { TFI_t fi = fsmGetFi(fsm);
410 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
411 ix = fsm->li->filex[j];
412 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
418 /* Are all links skipped or not encountered yet? */
420 return 1; /* XXX W2DO? */
422 /* Save the non-skipped file name and map index. */
423 fsm->li->linkIndex = j;
424 fsm->path = _free(fsm->path);
426 rc = fsmStage(fsm, FSM_MAP);
431 * Destroy set of hard links.
432 * @param li set of hard links
434 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
438 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
439 li->filex = _free(li->filex);
446 FSM_t fsm = xcalloc(1, sizeof(*fsm));
450 FSM_t freeFSM(FSM_t fsm)
453 fsm->path = _free(fsm->path);
455 while ((fsm->li = fsm->links) != NULL) {
456 fsm->links = fsm->li->next;
457 fsm->li->next = NULL;
458 fsm->li = freeHardLink(fsm->li);
461 fsm->dnlx = _free(fsm->dnlx);
462 fsm->ldn = _free(fsm->ldn);
463 fsm->iter = mapFreeIterator(fsm->iter);
468 int fsmSetup(FSM_t fsm, fileStage goal,
469 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
470 unsigned int * archiveSize, const char ** failedFile)
477 /*@-type@*/ /* FIX: cast? */
478 fsm->cfd = fdLink(cfd, "persist (fsm)");
480 pos = fdGetCpioPos(fsm->cfd);
481 fdSetCpioPos(fsm->cfd, 0);
483 fsm->iter = mapInitIterator(ts, fi);
485 if (fsm->goal == FSM_PKGINSTALL) {
486 if (ts && ts->notify) {
487 /*@-type@*/ /* FIX: cast? */
488 /*@-noeffectuncon @*/ /* FIX: check rc */
489 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
490 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
491 /*@=noeffectuncon @*/
497 fsm->archiveSize = archiveSize;
498 if (fsm->archiveSize)
499 *fsm->archiveSize = 0;
500 fsm->failedFile = failedFile;
502 *fsm->failedFile = NULL;
505 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
506 if (fsm->goal == FSM_PKGINSTALL) {
507 if (ts && ts->id > 0)
508 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
512 rc = fsmStage(fsm, FSM_CREATE);
513 if (rc && !ec) ec = rc;
515 rc = fsmStage(fsm, fsm->goal);
516 if (rc && !ec) ec = rc;
518 if (fsm->archiveSize && ec == 0)
519 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
524 int fsmTeardown(FSM_t fsm)
529 rc = fsmStage(fsm, FSM_DESTROY);
531 fsm->iter = mapFreeIterator(fsm->iter);
533 /*@-type@*/ /* FIX: cast? */
534 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
538 fsm->failedFile = NULL;
542 int fsmMapPath(FSM_t fsm)
544 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
551 fsm->action = FA_UNKNOWN;
555 if (fi && i >= 0 && i < fi->fc) {
557 fsm->astriplen = fi->astriplen;
558 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
559 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
560 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
562 /* src rpms have simple base name in payload. */
563 fsm->dirName = fi->dnl[fi->dil[i]];
564 fsm->baseName = fi->bnl[i];
566 switch (fsm->action) {
569 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
578 assert(fi->type == TR_ADDED);
582 if (fi->fstates && fi->type == TR_ADDED)
583 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
586 case FA_SKIPNETSHARED:
587 if (fi->fstates && fi->type == TR_ADDED)
588 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
592 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
595 fsm->osuffix = SUFFIX_RPMORIG;
596 /*@innerbreak@*/ break;
598 fsm->osuffix = SUFFIX_RPMSAVE;
599 /*@innerbreak@*/ break;
604 assert(fi->type == TR_ADDED);
605 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
606 fsm->nsuffix = SUFFIX_RPMNEW;
610 assert(fi->type == TR_ADDED);
611 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
612 fsm->osuffix = SUFFIX_RPMSAVE;
615 assert(fi->type == TR_REMOVED);
617 * XXX TODO: %ghost probably shouldn't be removed, but that changes
618 * legacy rpm behavior.
625 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
626 const struct stat * st = &fsm->sb;
627 fsm->path = _free(fsm->path);
628 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
629 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
635 int fsmMapAttrs(FSM_t fsm)
637 struct stat * st = &fsm->sb;
638 TFI_t fi = fsmGetFi(fsm);
641 if (fi && i >= 0 && i < fi->fc) {
643 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
645 (fi->fmodes ? fi->fmodes[i] : perms);
647 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
649 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
651 (fi->frdevs ? fi->frdevs[i] : 0);
653 (fi->fmtimes ? fi->fmtimes[i] : 0);
655 if (fsm->mapFlags & CPIO_MAP_MODE)
656 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
657 if (fsm->mapFlags & CPIO_MAP_TYPE) {
658 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
659 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
660 && st->st_nlink == 0)
662 st->st_rdev = finalRdev;
663 st->st_mtime = finalMtime;
665 if (fsm->mapFlags & CPIO_MAP_UID)
666 st->st_uid = finalUid;
667 if (fsm->mapFlags & CPIO_MAP_GID)
668 st->st_gid = finalGid;
670 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
677 * Create file from payload stream.
678 * @param fsm file state machine data
679 * @return 0 on success
681 static int expandRegular(/*@special@*/ FSM_t fsm)
683 /*@globals fileSystem@*/
684 /*@modifies fsm, fileSystem @*/
686 const char * fmd5sum;
687 const struct stat * st = &fsm->sb;
688 int left = st->st_size;
691 rc = fsmStage(fsm, FSM_WOPEN);
695 /* XXX md5sum's will break on repackaging that includes modified files. */
696 fmd5sum = fsm->fmd5sum;
698 if (st->st_size > 0 && fmd5sum)
699 fdInitMD5(fsm->wfd, 0);
703 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
704 rc = fsmStage(fsm, FSM_DREAD);
708 rc = fsmStage(fsm, FSM_WRITE);
714 /* don't call this with fileSize == fileComplete */
716 (void) fsmStage(fsm, FSM_NOTIFY);
719 if (st->st_size > 0 && fmd5sum) {
720 const char * md5sum = NULL;
722 (void) Fflush(fsm->wfd);
723 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
725 if (md5sum == NULL) {
726 rc = CPIOERR_MD5SUM_MISMATCH;
728 if (strcmp(md5sum, fmd5sum))
729 rc = CPIOERR_MD5SUM_MISMATCH;
730 md5sum = _free(md5sum);
735 (void) fsmStage(fsm, FSM_WCLOSE);
740 * Write next item to payload stream.
741 * @param fsm file state machine data
742 * @param writeData should data be written?
743 * @return 0 on success
745 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
746 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
747 /*@globals fileSystem@*/
748 /*@modifies fsm, fileSystem @*/
750 const char * path = fsm->path;
751 const char * opath = fsm->opath;
752 struct stat * st = &fsm->sb;
753 struct stat * ost = &fsm->osb;
754 size_t pos = fdGetCpioPos(fsm->cfd);
755 char * symbuf = NULL;
759 st->st_size = (writeData ? ost->st_size : 0);
762 if (S_ISDIR(st->st_mode)) {
764 } else if (S_ISLNK(st->st_mode)) {
766 * While linux puts the size of a symlink in the st_size field,
767 * I don't think that's a specified standard.
769 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
770 rc = fsmStage(fsm, FSM_READLINK);
772 st->st_size = fsm->rdnb;
773 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
777 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
778 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
779 char * t = alloca(nb);
782 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
784 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
785 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
786 TFI_t fi = fsmGetFi(fsm);
788 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
791 rc = fsmStage(fsm, FSM_HWRITE);
795 if (writeData && S_ISREG(st->st_mode)) {
798 void * mapped = (void *)-1;
802 rc = fsmStage(fsm, FSM_ROPEN);
805 /* XXX unbuffered mmap generates *lots* of fdio debugging */
808 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
809 if (mapped != (void *)-1) {
811 fsm->rdbuf = (char *) mapped;
812 fsm->rdlen = nmapped = st->st_size;
820 if (mapped != (void *)-1) {
825 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
826 rc = fsmStage(fsm, FSM_READ);
830 /* XXX DWRITE uses rdnb for I/O length. */
831 rc = fsmStage(fsm, FSM_DWRITE);
838 if (mapped != (void *)-1) {
839 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
844 } else if (writeData && S_ISLNK(st->st_mode)) {
845 /* XXX DWRITE uses rdnb for I/O length. */
846 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
847 fsm->rdnb = strlen(symbuf);
848 rc = fsmStage(fsm, FSM_DWRITE);
852 rc = fsmStage(fsm, FSM_PAD);
855 { const rpmTransactionSet ts = fsmGetTs(fsm);
856 TFI_t fi = fsmGetFi(fsm);
857 if (ts && ts->notify && fi) {
858 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
859 /*@-type@*/ /* FIX: cast? */
860 /*@-noeffectuncon @*/ /* FIX: check rc */
861 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
862 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
863 /*@=noeffectuncon @*/
872 (void) fsmStage(fsm, FSM_RCLOSE);
873 /*@-dependenttrans@*/
876 /*@=dependenttrans@*/
881 * Write set of linked files to payload stream.
882 * @param fsm file state machine data
883 * @return 0 on success
885 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
886 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
887 /*@globals fileSystem@*/
888 /*@modifies fsm, fileSystem @*/
890 const char * path = fsm->path;
891 const char * nsuffix = fsm->nsuffix;
892 int iterIndex = fsm->ix;
901 for (i = fsm->li->nlink - 1; i >= 0; i--) {
903 if (fsm->li->filex[i] < 0) continue;
905 fsm->ix = fsm->li->filex[i];
906 rc = fsmStage(fsm, FSM_MAP);
908 /* Write data after last link. */
909 rc = writeFile(fsm, (i == 0));
910 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
912 *fsm->failedFile = xstrdup(fsm->path);
915 fsm->path = _free(fsm->path);
916 fsm->li->filex[i] = -1;
920 fsm->nsuffix = nsuffix;
926 * Create pending hard links to existing file.
927 * @param fsm file state machine data
928 * @return 0 on success
930 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
931 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
932 /*@globals fileSystem@*/
933 /*@modifies fsm, fileSystem @*/
935 const char * path = fsm->path;
936 const char * opath = fsm->opath;
937 const char * nsuffix = fsm->nsuffix;
938 int iterIndex = fsm->ix;
948 fsm->ix = fsm->li->filex[fsm->li->createdPath];
949 rc = fsmStage(fsm, FSM_MAP);
950 fsm->opath = fsm->path;
953 for (i = 0; i < fsm->li->nlink; i++) {
954 if (fsm->li->filex[i] < 0) continue;
955 if (i == fsm->li->createdPath) continue;
957 fsm->ix = fsm->li->filex[i];
958 fsm->path = _free(fsm->path);
959 rc = fsmStage(fsm, FSM_MAP);
960 rc = fsmStage(fsm, FSM_VERIFY);
962 if (rc != CPIOERR_LSTAT_FAILED) break;
964 /* XXX link(fsm->opath, fsm->path) */
965 rc = fsmStage(fsm, FSM_LINK);
966 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
968 *fsm->failedFile = xstrdup(fsm->path);
971 fsm->li->linksLeft--;
974 fsm->path = _free(fsm->path);
975 fsm->opath = _free(fsm->opath);
978 fsm->nsuffix = nsuffix;
985 * Commit hard linked file set atomically.
986 * @param fsm file state machine data
987 * @return 0 on success
989 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
990 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
991 fsm->li, fsm->links @*/
992 /*@globals fileSystem@*/
993 /*@modifies fsm, fileSystem @*/
995 const char * path = fsm->path;
996 const char * nsuffix = fsm->nsuffix;
997 int iterIndex = fsm->ix;
998 struct stat * st = &fsm->sb;
1003 fsm->nsuffix = NULL;
1007 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1008 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1013 for (i = 0; i < fsm->li->nlink; i++) {
1014 if (fsm->li->filex[i] < 0) continue;
1015 fsm->ix = fsm->li->filex[i];
1016 rc = fsmStage(fsm, FSM_MAP);
1017 rc = fsmStage(fsm, FSM_COMMIT);
1018 fsm->path = _free(fsm->path);
1019 fsm->li->filex[i] = -1;
1022 fsm->ix = iterIndex;
1023 fsm->nsuffix = nsuffix;
1029 * Remove (if created) directories not explicitly included in package.
1030 * @param fsm file state machine data
1031 * @return 0 on success
1033 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1034 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1035 /*@globals fileSystem@*/
1036 /*@modifies fsm, fileSystem @*/
1038 const char * path = fsm->path;
1039 void * dnli = dnlInitIterator(fsm, 1);
1040 char * dn = fsm->rdbuf;
1041 int dc = dnlCount(dnli);
1046 /*@-observertrans -dependenttrans@*/
1047 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1048 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1049 int dnlen = strlen(fsm->path);
1052 dc = dnlIndex(dnli);
1053 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1056 /* Copy to avoid const on fsm->path. */
1057 te = stpcpy(dn, fsm->path) - 1;
1060 /* Remove generated directories. */
1061 /*@-usereleased@*/ /* LCL: te used after release? */
1065 rc = fsmStage(fsm, FSM_RMDIR);
1069 /*@innerbreak@*/ break;
1071 } while ((te - fsm->path) > fsm->dnlx[dc]);
1074 dnli = dnlFreeIterator(dnli);
1075 /*@=observertrans =dependenttrans@*/
1082 * Create (if necessary) directories not explicitly included in package.
1083 * @param fsm file state machine data
1084 * @return 0 on success
1086 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1087 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1088 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1089 /*@defines fsm->dnlx, fsm->ldn @*/
1090 /*@globals fileSystem@*/
1091 /*@modifies fsm, fileSystem @*/
1093 struct stat * st = &fsm->sb;
1094 struct stat * ost = &fsm->osb;
1095 const char * path = fsm->path;
1096 mode_t st_mode = st->st_mode;
1097 void * dnli = dnlInitIterator(fsm, 0);
1098 char * dn = fsm->rdbuf;
1099 int dc = dnlCount(dnli);
1106 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1107 /*@-observertrans -dependenttrans@*/
1108 if (fsm->dnlx != NULL)
1109 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1110 int dnlen = strlen(fsm->path);
1113 dc = dnlIndex(dnli);
1114 if (dc < 0) continue;
1115 fsm->dnlx[dc] = dnlen;
1119 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1120 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1122 /*@=compdef =nullpass@*/
1124 /* Copy to avoid const on fsm->path. */
1125 (void) stpcpy(dn, fsm->path);
1128 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1129 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1131 /*@innercontinue@*/ continue;
1135 /* Already validated? */
1136 /*@-usedef -compdef -nullpass -nullderef@*/
1137 if (i < fsm->ldnlen &&
1138 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1139 !strncmp(fsm->path, fsm->ldn, i))
1142 /* Move pre-existing path marker forward. */
1143 fsm->dnlx[dc] = (te - dn);
1144 /*@innercontinue@*/ continue;
1146 /*@=usedef =compdef =nullpass =nullderef@*/
1148 /* Validate next component of path. */
1149 rc = fsmStage(fsm, FSM_LSTAT);
1152 /* Directory already exists? */
1153 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1154 /* Move pre-existing path marker forward. */
1155 fsm->dnlx[dc] = (te - dn);
1156 } else if (rc == CPIOERR_LSTAT_FAILED) {
1157 TFI_t fi = fsmGetFi(fsm);
1159 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1160 rc = fsmStage(fsm, FSM_MKDIR);
1162 rpmMessage(RPMMESS_DEBUG,
1163 _("%s directory created with perms %04o.\n"),
1164 fsm->path, (unsigned)(st->st_mode & 07777));
1168 /*@innerbreak@*/ break;
1172 /* Save last validated path. */
1173 if (fsm->ldnalloc < (dnlen + 1)) {
1174 fsm->ldnalloc = dnlen + 100;
1175 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1177 if (fsm->ldn != NULL) { /* XXX can't happen */
1178 strcpy(fsm->ldn, fsm->path);
1179 fsm->ldnlen = dnlen;
1182 dnli = dnlFreeIterator(dnli);
1183 /*@=observertrans =dependenttrans@*/
1186 st->st_mode = st_mode; /* XXX restore st->st_mode */
1192 * Check for file on disk.
1193 * @param fsm file state machine data
1194 * @return 0 on success
1196 static int fsmStat(FSM_t fsm)
1198 int saveerrno = errno;
1201 if (fsm->path != NULL) {
1202 int saveernno = errno;
1203 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1204 ? FSM_LSTAT : FSM_STAT));
1205 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1209 } else if (rc == 0) {
1213 /* Skip %ghost files on build. */
1221 int fsmStage(FSM_t fsm, fileStage stage)
1224 fileStage prevStage = fsm->stage;
1225 const char * const prev = fileStageString(prevStage);
1227 static int modulo = 4;
1228 const char * const cur = fileStageString(stage);
1229 struct stat * st = &fsm->sb;
1230 struct stat * ost = &fsm->osb;
1231 int saveerrno = errno;
1236 #define _fafilter(_a) \
1237 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1238 ? fileActionString(_a) : "")
1240 if (stage & FSM_DEAD) {
1242 } else if (stage & FSM_INTERNAL) {
1243 if (_fsm_debug && !(stage & FSM_SYSCALL))
1244 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1246 (unsigned)st->st_mode, (int)st->st_nlink,
1247 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1248 (fsm->path ? fsm->path : ""),
1249 _fafilter(fsm->action));
1252 if (_fsm_debug || !(stage & FSM_VERBOSE))
1253 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1255 (unsigned)st->st_mode, (int)st->st_nlink,
1256 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1257 (fsm->path ? fsm->path + fsm->astriplen : ""),
1258 _fafilter(fsm->action));
1266 case FSM_PKGINSTALL:
1268 /* Clean fsm, free'ing memory. Read next archive header. */
1269 rc = fsmStage(fsm, FSM_INIT);
1271 /* Exit on end-of-payload. */
1272 if (rc == CPIOERR_HDR_TRAILER) {
1274 /*@loopbreak@*/ break;
1277 /* Exit on error. */
1280 (void) fsmStage(fsm, FSM_UNDO);
1281 /*@loopbreak@*/ break;
1284 /* Extract file from archive. */
1285 rc = fsmStage(fsm, FSM_PROCESS);
1287 (void) fsmStage(fsm, FSM_UNDO);
1288 /*@loopbreak@*/ break;
1291 /* Notify on success. */
1292 (void) fsmStage(fsm, FSM_NOTIFY);
1294 rc = fsmStage(fsm, FSM_FINI);
1296 /*@loopbreak@*/ break;
1303 /* Clean fsm, free'ing memory. */
1304 rc = fsmStage(fsm, FSM_INIT);
1306 /* Exit on end-of-payload. */
1307 if (rc == CPIOERR_HDR_TRAILER) {
1309 /*@loopbreak@*/ break;
1312 /* Rename/erase next item. */
1313 if (fsmStage(fsm, FSM_FINI))
1314 /*@loopbreak@*/ break;
1320 rc = fsmStage(fsm, FSM_INIT);
1322 /* Exit on end-of-payload. */
1323 if (rc == CPIOERR_HDR_TRAILER) {
1325 /*@loopbreak@*/ break;
1328 /* Exit on error. */
1331 (void) fsmStage(fsm, FSM_UNDO);
1332 /*@loopbreak@*/ break;
1335 /* Copy file into archive. */
1336 rc = fsmStage(fsm, FSM_PROCESS);
1338 (void) fsmStage(fsm, FSM_UNDO);
1339 /*@loopbreak@*/ break;
1342 if (fsmStage(fsm, FSM_FINI))
1343 /*@loopbreak@*/ break;
1346 /* Flush partial sets of hard linked files. */
1347 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1349 while ((fsm->li = fsm->links) != NULL) {
1350 fsm->links = fsm->li->next;
1351 fsm->li->next = NULL;
1353 /* Re-calculate link count for archive header. */
1354 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1355 if (fsm->li->filex[i] < 0)
1356 /*@innercontinue@*/ continue;
1360 /* XXX force the contents out as well. */
1362 fsm->li->filex[0] = fsm->li->filex[j];
1363 fsm->li->filex[j] = -1;
1365 fsm->li->sb.st_nlink = nlink;
1367 fsm->sb = fsm->li->sb; /* structure assignment */
1368 fsm->osb = fsm->sb; /* structure assignment */
1370 if (!rc) rc = writeLinkedFile(fsm);
1372 fsm->li = freeHardLink(fsm->li);
1377 rc = fsmStage(fsm, FSM_TRAILER);
1381 { rpmTransactionSet ts = fsmGetTs(fsm);
1382 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1383 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1384 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1387 fsm->path = _free(fsm->path);
1388 fsm->opath = _free(fsm->opath);
1389 fsm->dnlx = _free(fsm->dnlx);
1391 fsm->ldn = _free(fsm->ldn);
1392 fsm->ldnalloc = fsm->ldnlen = 0;
1394 fsm->rdsize = fsm->wrsize = 0;
1395 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1396 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1397 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1398 fsm->rdsize = 8 * BUFSIZ;
1399 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1400 fsm->wrsize = 8 * BUFSIZ;
1401 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1404 fsm->mkdirsdone = 0;
1409 errno = 0; /* XXX get rid of EBADF */
1412 /* Detect and create directories not explicitly in package. */
1413 if (fsm->goal == FSM_PKGINSTALL) {
1414 rc = fsmStage(fsm, FSM_MKDIRS);
1415 if (!rc) fsm->mkdirsdone = 1;
1420 fsm->path = _free(fsm->path);
1422 fsm->diskchecked = fsm->exists = 0;
1424 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1425 fsm->action = FA_UNKNOWN;
1426 fsm->osuffix = NULL;
1427 fsm->nsuffix = NULL;
1429 if (fsm->goal == FSM_PKGINSTALL) {
1430 /* Read next header from payload, checking for end-of-payload. */
1431 rc = fsmStage(fsm, FSM_NEXT);
1435 /* Identify mapping index. */
1436 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1437 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1439 /* Detect end-of-loop and/or mapping error. */
1441 if (fsm->goal == FSM_PKGINSTALL) {
1443 rpmMessage(RPMMESS_WARNING,
1444 _("archive file %s was not found in header file list\n"),
1447 if (fsm->failedFile && *fsm->failedFile == NULL)
1448 *fsm->failedFile = xstrdup(fsm->path);
1449 rc = CPIOERR_UNMAPPED_FILE;
1451 rc = CPIOERR_HDR_TRAILER;
1456 /* On non-install, mode must be known so that dirs don't get suffix. */
1457 if (fsm->goal != FSM_PKGINSTALL) {
1458 TFI_t fi = fsmGetFi(fsm);
1459 st->st_mode = fi->fmodes[fsm->ix];
1462 /* Generate file path. */
1463 rc = fsmStage(fsm, FSM_MAP);
1466 /* Perform lstat/stat for disk file. */
1470 if (fsm->path != NULL) {
1471 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1472 ? FSM_LSTAT : FSM_STAT));
1473 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1479 } else if (rc == 0) {
1483 /* Skip %ghost files on build. */
1487 fsm->diskchecked = 1;
1490 /* On non-install, the disk file stat is what's remapped. */
1491 if (fsm->goal != FSM_PKGINSTALL)
1492 *st = *ost; /* structure assignment */
1494 /* Remap file perms, owner, and group. */
1495 rc = fsmMapAttrs(fsm);
1498 fsm->postpone = XFA_SKIPPING(fsm->action);
1499 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1500 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1501 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1502 fsm->postpone = saveHardLink(fsm);
1509 rc = fsmMapPath(fsm);
1512 rc = fsmMkdirs(fsm);
1516 rc = fsmRmdirs(fsm);
1519 if (fsm->postpone) {
1520 if (fsm->goal == FSM_PKGINSTALL)
1521 rc = fsmStage(fsm, FSM_EAT);
1525 if (fsm->goal == FSM_PKGBUILD) {
1526 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1527 struct hardLink * li, * prev;
1529 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1530 rc = writeLinkedFile(fsm);
1531 if (rc) break; /* W2DO? */
1533 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1535 /*@loopbreak@*/ break;
1538 fsm->links = fsm->li->next;
1540 prev->next = fsm->li->next;
1541 fsm->li->next = NULL;
1542 fsm->li = freeHardLink(fsm->li);
1544 rc = writeFile(fsm, 1);
1549 if (fsm->goal != FSM_PKGINSTALL)
1552 if (S_ISREG(st->st_mode)) {
1553 const char * path = fsm->path;
1555 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1556 rc = fsmStage(fsm, FSM_VERIFY);
1558 if (rc == 0 && fsm->osuffix) {
1559 const char * opath = fsm->opath;
1560 fsm->opath = fsm->path;
1561 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1562 rc = fsmStage(fsm, FSM_RENAME);
1564 rpmMessage(RPMMESS_WARNING,
1565 _("%s saved as %s\n"), fsm->opath, fsm->path);
1566 fsm->path = _free(fsm->path);
1570 /*@-dependenttrans@*/
1572 /*@=dependenttrans@*/
1573 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1574 rc = expandRegular(fsm);
1575 } else if (S_ISDIR(st->st_mode)) {
1576 mode_t st_mode = st->st_mode;
1577 rc = fsmStage(fsm, FSM_VERIFY);
1578 if (rc == CPIOERR_LSTAT_FAILED) {
1579 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1580 st->st_mode |= 00700;
1581 rc = fsmStage(fsm, FSM_MKDIR);
1582 st->st_mode = st_mode; /* XXX restore st->st_mode */
1584 } else if (S_ISLNK(st->st_mode)) {
1585 const char * opath = fsm->opath;
1587 if ((st->st_size + 1) > fsm->rdsize) {
1588 rc = CPIOERR_HDR_SIZE;
1592 fsm->wrlen = st->st_size;
1593 rc = fsmStage(fsm, FSM_DREAD);
1594 if (!rc && fsm->rdnb != fsm->wrlen)
1595 rc = CPIOERR_READ_FAILED;
1598 fsm->wrbuf[st->st_size] = '\0';
1599 /* XXX symlink(fsm->opath, fsm->path) */
1600 /*@-dependenttrans@*/
1601 fsm->opath = fsm->wrbuf;
1602 /*@=dependenttrans@*/
1603 rc = fsmStage(fsm, FSM_VERIFY);
1604 if (rc == CPIOERR_LSTAT_FAILED)
1605 rc = fsmStage(fsm, FSM_SYMLINK);
1606 fsm->opath = opath; /* XXX restore fsm->path */
1607 } else if (S_ISFIFO(st->st_mode)) {
1608 mode_t st_mode = st->st_mode;
1609 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1610 rc = fsmStage(fsm, FSM_VERIFY);
1611 if (rc == CPIOERR_LSTAT_FAILED) {
1612 st->st_mode = 0000; /* XXX abuse st->st_mode */
1613 rc = fsmStage(fsm, FSM_MKFIFO);
1614 st->st_mode = st_mode; /* XXX restore st->st_mode */
1616 } else if (S_ISCHR(st->st_mode) ||
1617 S_ISBLK(st->st_mode) ||
1618 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1620 rc = fsmStage(fsm, FSM_VERIFY);
1621 if (rc == CPIOERR_LSTAT_FAILED)
1622 rc = fsmStage(fsm, FSM_MKNOD);
1624 rc = CPIOERR_UNKNOWN_FILETYPE;
1626 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1627 fsm->li->createdPath = fsm->li->linkIndex;
1628 rc = fsmMakeLinks(fsm);
1634 rc = fsmMakeLinks(fsm);
1636 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1637 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1638 rpmTransactionSet ts = fsmGetTs(fsm);
1639 TFI_t fi = fsmGetFi(fsm);
1640 if (ts && ts->notify && fi) {
1641 /*@-type@*/ /* FIX: cast? */
1642 /*@-noeffectuncon @*/ /* FIX: check rc */
1643 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1644 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1645 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1646 /*@=noeffectuncon @*/
1654 if (fsm->goal == FSM_PKGINSTALL) {
1655 (void) fsmStage(fsm,
1656 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1658 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1660 (void) fsmStage(fsm, FSM_RMDIRS);
1666 if (fsm->failedFile && *fsm->failedFile == NULL)
1667 *fsm->failedFile = xstrdup(fsm->path);
1670 if (!fsm->postpone && fsm->commit) {
1671 if (fsm->goal == FSM_PKGINSTALL)
1672 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1673 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1674 if (fsm->goal == FSM_PKGCOMMIT)
1675 rc = fsmStage(fsm, FSM_COMMIT);
1676 if (fsm->goal == FSM_PKGERASE)
1677 rc = fsmStage(fsm, FSM_COMMIT);
1679 fsm->path = _free(fsm->path);
1680 fsm->opath = _free(fsm->opath);
1681 memset(st, 0, sizeof(*st));
1682 memset(ost, 0, sizeof(*ost));
1685 /* Rename pre-existing modified or unmanaged file. */
1686 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1687 const char * opath = fsm->opath;
1688 const char * path = fsm->path;
1689 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1690 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1691 rc = fsmStage(fsm, FSM_RENAME);
1693 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1694 fsm->opath, fsm->path);
1696 fsm->path = _free(fsm->path);
1698 fsm->opath = _free(fsm->opath);
1702 /* Remove erased files. */
1703 if (fsm->goal == FSM_PKGERASE) {
1704 if (fsm->action == FA_ERASE) {
1705 TFI_t fi = fsmGetFi(fsm);
1706 if (S_ISDIR(st->st_mode)) {
1707 rc = fsmStage(fsm, FSM_RMDIR);
1710 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1712 /* XXX make sure that build side permits %missingok on directories. */
1713 if (fsm->fflags & RPMFILE_MISSINGOK)
1714 /*@innerbreak@*/ break;
1716 /* XXX common error message. */
1718 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1719 _("%s rmdir of %s failed: Directory not empty\n"),
1720 fiTypeString(fi), fsm->path);
1721 /*@innerbreak@*/ break;
1724 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1725 _("%s rmdir of %s failed: %s\n"),
1726 fiTypeString(fi), fsm->path, strerror(errno));
1727 /*@innerbreak@*/ break;
1730 rc = fsmStage(fsm, FSM_UNLINK);
1732 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1734 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1735 _("%s unlink of %s failed: %s\n"),
1736 fiTypeString(fi), fsm->path, strerror(errno));
1739 /* XXX Failure to remove is not (yet) cause for failure. */
1740 if (!strict_erasures) rc = 0;
1744 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1745 /* Rename temporary to final file name. */
1746 if (!S_ISDIR(st->st_mode) &&
1747 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1749 fsm->opath = fsm->path;
1750 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1751 rc = fsmStage(fsm, FSM_RENAME);
1752 if (!rc && fsm->nsuffix) {
1753 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1754 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1755 (opath ? opath : ""), fsm->path);
1756 opath = _free(opath);
1758 fsm->opath = _free(fsm->opath);
1760 if (S_ISLNK(st->st_mode)) {
1761 if (!rc && !getuid())
1762 rc = fsmStage(fsm, FSM_LCHOWN);
1764 if (!rc && !getuid())
1765 rc = fsmStage(fsm, FSM_CHOWN);
1767 rc = fsmStage(fsm, FSM_CHMOD);
1769 time_t mtime = st->st_mtime;
1770 TFI_t fi = fsmGetFi(fsm);
1772 st->st_mtime = fi->fmtimes[fsm->ix];
1773 rc = fsmStage(fsm, FSM_UTIME);
1774 st->st_mtime = mtime;
1779 /* Notify on success. */
1780 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1781 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1782 *fsm->failedFile = fsm->path;
1787 fsm->path = _free(fsm->path);
1789 /* Check for hard links missing from payload. */
1790 while ((fsm->li = fsm->links) != NULL) {
1791 fsm->links = fsm->li->next;
1792 fsm->li->next = NULL;
1793 if (fsm->goal == FSM_PKGINSTALL &&
1794 fsm->commit && fsm->li->linksLeft)
1796 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1797 if (fsm->li->filex[i] < 0)
1798 /*@innercontinue@*/ continue;
1799 rc = CPIOERR_MISSING_HARDLINK;
1800 if (fsm->failedFile && *fsm->failedFile == NULL) {
1801 fsm->ix = fsm->li->filex[i];
1802 if (!fsmStage(fsm, FSM_MAP)) {
1803 *fsm->failedFile = fsm->path;
1807 /*@loopbreak@*/ break;
1810 if (fsm->goal == FSM_PKGBUILD &&
1811 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1813 rc = CPIOERR_MISSING_HARDLINK;
1815 fsm->li = freeHardLink(fsm->li);
1817 fsm->ldn = _free(fsm->ldn);
1818 fsm->ldnalloc = fsm->ldnlen = 0;
1819 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1820 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1823 if (fsm->diskchecked && !fsm->exists) {
1824 rc = CPIOERR_LSTAT_FAILED;
1827 if (S_ISREG(st->st_mode)) {
1828 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1829 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1831 * XXX HP-UX (and other os'es) don't permit unlink on busy
1834 fsm->opath = fsm->path;
1836 rc = fsmStage(fsm, FSM_RENAME);
1838 (void) fsmStage(fsm, FSM_UNLINK);
1840 rc = CPIOERR_UNLINK_FAILED;
1841 fsm->path = fsm->opath;
1843 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1844 /*@notreached@*/ break;
1845 } else if (S_ISDIR(st->st_mode)) {
1846 if (S_ISDIR(ost->st_mode)) return 0;
1847 if (S_ISLNK(ost->st_mode)) {
1848 rc = fsmStage(fsm, FSM_STAT);
1849 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1854 if (S_ISDIR(ost->st_mode)) return 0;
1856 } else if (S_ISLNK(st->st_mode)) {
1857 if (S_ISLNK(ost->st_mode)) {
1858 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1859 rc = fsmStage(fsm, FSM_READLINK);
1864 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1866 } else if (S_ISFIFO(st->st_mode)) {
1867 if (S_ISFIFO(ost->st_mode)) return 0;
1868 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1869 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1870 (ost->st_rdev == st->st_rdev)) return 0;
1871 } else if (S_ISSOCK(st->st_mode)) {
1872 if (S_ISSOCK(ost->st_mode)) return 0;
1874 /* XXX shouldn't do this with commit/undo. */
1876 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1877 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1878 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1879 /*@notreached@*/ break;
1882 rc = Unlink(fsm->path);
1883 if (_fsm_debug && (stage & FSM_SYSCALL))
1884 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1885 fsm->path, (rc < 0 ? strerror(errno) : ""));
1886 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1889 rc = Rename(fsm->opath, fsm->path);
1890 if (_fsm_debug && (stage & FSM_SYSCALL))
1891 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1892 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1893 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1896 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1897 if (_fsm_debug && (stage & FSM_SYSCALL))
1898 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1899 fsm->path, (unsigned)(st->st_mode & 07777),
1900 (rc < 0 ? strerror(errno) : ""));
1901 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1904 rc = Rmdir(fsm->path);
1905 if (_fsm_debug && (stage & FSM_SYSCALL))
1906 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1907 fsm->path, (rc < 0 ? strerror(errno) : ""));
1908 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1911 rc = chown(fsm->path, st->st_uid, st->st_gid);
1912 if (_fsm_debug && (stage & FSM_SYSCALL))
1913 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1914 fsm->path, (int)st->st_uid, (int)st->st_gid,
1915 (rc < 0 ? strerror(errno) : ""));
1916 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1919 #if ! CHOWN_FOLLOWS_SYMLINK
1920 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1921 if (_fsm_debug && (stage & FSM_SYSCALL))
1922 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1923 fsm->path, (int)st->st_uid, (int)st->st_gid,
1924 (rc < 0 ? strerror(errno) : ""));
1925 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1929 rc = chmod(fsm->path, (st->st_mode & 07777));
1930 if (_fsm_debug && (stage & FSM_SYSCALL))
1931 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1932 fsm->path, (unsigned)(st->st_mode & 07777),
1933 (rc < 0 ? strerror(errno) : ""));
1934 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1937 { struct utimbuf stamp;
1938 stamp.actime = st->st_mtime;
1939 stamp.modtime = st->st_mtime;
1940 rc = utime(fsm->path, &stamp);
1941 if (_fsm_debug && (stage & FSM_SYSCALL))
1942 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1943 fsm->path, (unsigned)st->st_mtime,
1944 (rc < 0 ? strerror(errno) : ""));
1945 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1949 rc = symlink(fsm->opath, fsm->path);
1950 if (_fsm_debug && (stage & FSM_SYSCALL))
1951 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1952 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1953 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1956 rc = Link(fsm->opath, fsm->path);
1957 if (_fsm_debug && (stage & FSM_SYSCALL))
1958 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1959 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1960 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1963 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1964 if (_fsm_debug && (stage & FSM_SYSCALL))
1965 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1966 fsm->path, (unsigned)(st->st_mode & 07777),
1967 (rc < 0 ? strerror(errno) : ""));
1968 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1971 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
1972 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1973 /*@=unrecog =portability @*/
1974 if (_fsm_debug && (stage & FSM_SYSCALL))
1975 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1976 fsm->path, (unsigned)(st->st_mode & ~07777),
1977 (unsigned)st->st_rdev,
1978 (rc < 0 ? strerror(errno) : ""));
1979 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1982 rc = Lstat(fsm->path, ost);
1983 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1984 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1985 fsm->path, (rc < 0 ? strerror(errno) : ""));
1986 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1989 rc = Stat(fsm->path, ost);
1990 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1991 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1992 fsm->path, (rc < 0 ? strerror(errno) : ""));
1993 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1996 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1997 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1998 if (_fsm_debug && (stage & FSM_SYSCALL))
1999 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
2000 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2001 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
2004 fsm->rdbuf[fsm->rdnb] = '\0';
2012 rc = fsmStage(fsm, FSM_HREAD);
2014 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2015 fsm->path = _free(fsm->path);
2016 rc = CPIOERR_HDR_TRAILER;
2019 rc = fsmStage(fsm, FSM_POS);
2022 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2023 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2024 rc = fsmStage(fsm, FSM_DREAD);
2026 /*@loopbreak@*/ break;
2030 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2033 (void) fsmStage(fsm, FSM_DREAD);
2037 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2039 memset(fsm->rdbuf, 0, left);
2040 /* XXX DWRITE uses rdnb for I/O length. */
2042 (void) fsmStage(fsm, FSM_DWRITE);
2046 rc = cpioTrailerWrite(fsm);
2049 rc = fsmStage(fsm, FSM_POS);
2051 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2054 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2057 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2058 if (_fsm_debug && (stage & FSM_SYSCALL))
2059 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2060 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2061 (int)fsm->wrlen, (int)fsm->rdnb);
2062 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2063 rc = CPIOERR_READ_FAILED;
2065 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2068 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2069 if (_fsm_debug && (stage & FSM_SYSCALL))
2070 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2071 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2072 (int)fsm->rdnb, (int)fsm->wrnb);
2073 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2074 rc = CPIOERR_WRITE_FAILED;
2076 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2080 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2081 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2082 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2084 rc = CPIOERR_OPEN_FAILED;
2087 if (_fsm_debug && (stage & FSM_SYSCALL))
2088 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2089 fsm->path, fsm->rfd, fsm->rdbuf);
2092 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2093 if (_fsm_debug && (stage & FSM_SYSCALL))
2094 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2095 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2096 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2097 rc = CPIOERR_READ_FAILED;
2101 if (_fsm_debug && (stage & FSM_SYSCALL))
2102 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2103 (void) Fclose(fsm->rfd);
2111 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2112 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2113 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2115 rc = CPIOERR_OPEN_FAILED;
2117 if (_fsm_debug && (stage & FSM_SYSCALL))
2118 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2119 fsm->path, fsm->wfd, fsm->wrbuf);
2122 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2123 if (_fsm_debug && (stage & FSM_SYSCALL))
2124 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2125 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2126 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2127 rc = CPIOERR_WRITE_FAILED;
2131 if (_fsm_debug && (stage & FSM_SYSCALL))
2132 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2133 (void) Fclose(fsm->wfd);
2146 if (!(stage & FSM_INTERNAL)) {
2147 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2153 /*@obserever@*/ const char *const fileActionString(fileAction a)
2156 case FA_UNKNOWN: return "unknown";
2157 case FA_CREATE: return "create";
2158 case FA_COPYOUT: return "copyout";
2159 case FA_COPYIN: return "copyin";
2160 case FA_BACKUP: return "backup";
2161 case FA_SAVE: return "save";
2162 case FA_SKIP: return "skip";
2163 case FA_ALTNAME: return "altname";
2164 case FA_ERASE: return "erase";
2165 case FA_SKIPNSTATE: return "skipnstate";
2166 case FA_SKIPNETSHARED: return "skipnetshared";
2167 case FA_SKIPMULTILIB: return "skipmultilib";
2168 default: return "???";
2173 /*@observer@*/ const char *const fileStageString(fileStage a) {
2175 case FSM_UNKNOWN: return "unknown";
2177 case FSM_PKGINSTALL:return "pkginstall";
2178 case FSM_PKGERASE: return "pkgerase";
2179 case FSM_PKGBUILD: return "pkgbuild";
2180 case FSM_PKGCOMMIT: return "pkgcommit";
2181 case FSM_PKGUNDO: return "pkgundo";
2183 case FSM_CREATE: return "create";
2184 case FSM_INIT: return "init";
2185 case FSM_MAP: return "map";
2186 case FSM_MKDIRS: return "mkdirs";
2187 case FSM_RMDIRS: return "rmdirs";
2188 case FSM_PRE: return "pre";
2189 case FSM_PROCESS: return "process";
2190 case FSM_POST: return "post";
2191 case FSM_MKLINKS: return "mklinks";
2192 case FSM_NOTIFY: return "notify";
2193 case FSM_UNDO: return "undo";
2194 case FSM_FINI: return "fini";
2195 case FSM_COMMIT: return "commit";
2196 case FSM_DESTROY: return "destroy";
2197 case FSM_VERIFY: return "verify";
2199 case FSM_UNLINK: return "Unlink";
2200 case FSM_RENAME: return "Rename";
2201 case FSM_MKDIR: return "Mkdir";
2202 case FSM_RMDIR: return "rmdir";
2203 case FSM_CHOWN: return "chown";
2204 case FSM_LCHOWN: return "lchown";
2205 case FSM_CHMOD: return "chmod";
2206 case FSM_UTIME: return "utime";
2207 case FSM_SYMLINK: return "symlink";
2208 case FSM_LINK: return "Link";
2209 case FSM_MKFIFO: return "mkfifo";
2210 case FSM_MKNOD: return "mknod";
2211 case FSM_LSTAT: return "Lstat";
2212 case FSM_STAT: return "Stat";
2213 case FSM_READLINK: return "Readlink";
2214 case FSM_CHROOT: return "chroot";
2216 case FSM_NEXT: return "next";
2217 case FSM_EAT: return "eat";
2218 case FSM_POS: return "pos";
2219 case FSM_PAD: return "pad";
2220 case FSM_TRAILER: return "trailer";
2221 case FSM_HREAD: return "hread";
2222 case FSM_HWRITE: return "hwrite";
2223 case FSM_DREAD: return "Fread";
2224 case FSM_DWRITE: return "Fwrite";
2226 case FSM_ROPEN: return "Fopen";
2227 case FSM_READ: return "Fread";
2228 case FSM_RCLOSE: return "Fclose";
2229 case FSM_WOPEN: return "Fopen";
2230 case FSM_WRITE: return "Fwrite";
2231 case FSM_WCLOSE: return "Fclose";
2233 default: return "???";