3 * File state machine to handle a payload from a package.
8 #include <rpmio_internal.h>
14 #define fsmUNSAFE fsmStage
18 #define _RPMFI_INTERNAL
26 /*@access FD_t @*/ /* XXX void ptr args */
32 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
36 int _fsm_debug = _FSM_DEBUG;
38 /*@-exportheadervar@*/
41 /*@=exportheadervar@*/
43 /* XXX Failure to remove is not (yet) cause for failure. */
44 /*@-exportlocal -exportheadervar@*/
46 int strict_erasures = 0;
47 /*@=exportlocal =exportheadervar@*/
49 rpmts fsmGetTs(const FSM_t fsm) {
50 const FSMI_t iter = fsm->iter;
51 /*@-compdef -refcounttrans -retexpose -usereleased @*/
52 return (iter ? iter->ts : NULL);
53 /*@=compdef =refcounttrans =retexpose =usereleased @*/
56 rpmfi fsmGetFi(const FSM_t fsm)
58 const FSMI_t iter = fsm->iter;
59 /*@-compdef -refcounttrans -retexpose -usereleased @*/
60 return (iter ? iter->fi : NULL);
61 /*@=compdef =refcounttrans =retexpose =usereleased @*/
64 #define SUFFIX_RPMORIG ".rpmorig"
65 #define SUFFIX_RPMSAVE ".rpmsave"
66 #define SUFFIX_RPMNEW ".rpmnew"
69 * Build path to file from file info, ornamented with subdir and suffix.
70 * @param fsm file state machine data
71 * @param st file stat info
72 * @param subdir subdir to use (NULL disables)
73 * @param suffix suffix to use (NULL disables)
74 * @retval path to file
76 static /*@only@*//*@null@*/
77 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
78 /*@null@*/ const struct stat * st,
79 /*@null@*/ const char * subdir,
80 /*@null@*/ const char * suffix)
81 /*@uses fsm->dirName, fsm->baseName */
84 const char * s = NULL;
89 nb = strlen(fsm->dirName) +
90 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
91 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
92 strlen(fsm->baseName) + 1;
95 t = stpcpy(t, fsm->dirName);
96 if (st && !S_ISDIR(st->st_mode))
97 if (subdir) t = stpcpy(t, subdir);
98 t = stpcpy(t, fsm->baseName);
99 if (st && !S_ISDIR(st->st_mode))
100 if (suffix) t = stpcpy(t, suffix);
107 * Destroy file info iterator.
108 * @param p file info iterator
109 * @retval NULL always
111 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/ void * p)
112 /*@globals fileSystem @*/
113 /*@modifies fileSystem @*/
117 iter->ts = rpmtsFree(iter->ts);
118 iter->fi = rpmfiUnlink(iter->fi, "mapIterator");
124 * Create file info iterator.
125 * @param ts transaction set
126 * @param fi transaction element file info
127 * @return file info iterator
130 mapInitIterator(rpmts ts, rpmfi fi)
131 /*@modifies ts, fi @*/
135 iter = xcalloc(1, sizeof(*iter));
136 iter->ts = rpmtsLink(ts, "mapIterator");
137 iter->fi = rpmfiLink(fi, "mapIterator");
138 iter->reverse = (rpmteType(fi->te) == TR_REMOVED && fi->action != FA_COPYOUT);
139 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
140 iter->isave = iter->i;
145 * Return next index into file info.
146 * @param a file info iterator
147 * @return next index, -1 on termination
149 static int mapNextIterator(/*@null@*/ void * a)
156 const rpmfi fi = iter->fi;
158 if (iter->i >= 0) i = iter->i--;
160 if (iter->i < fi->fc) i = iter->i++;
170 static int cpioStrCmp(const void * a, const void * b)
173 const char * afn = *(const char **)a;
174 const char * bfn = *(const char **)b;
176 /* XXX Some 4+ year old rpm packages have basename only in payloads. */
177 #ifdef VERY_OLD_BUGGY_RPM_PACKAGES
178 if (strchr(afn, '/') == NULL)
179 bfn = strrchr(bfn, '/') + 1;
182 /* Match rpm-4.0 payloads with ./ prefixes. */
183 if (afn[0] == '.' && afn[1] == '/') afn += 2;
184 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
186 /* If either path is absolute, make it relative. */
187 if (afn[0] == '/') afn += 1;
188 if (bfn[0] == '/') bfn += 1;
190 return strcmp(afn, bfn);
195 * Locate archive path in file info.
196 * @param iter file info iterator
197 * @param fsmPath archive path
198 * @return index into file info, -1 if archive path was not found
201 static int mapFind(/*@null@*/ FSMI_t iter, const char * fsmPath)
207 const rpmfi fi = iter->fi;
208 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
209 const char ** p = NULL;
212 if (fi->apath != NULL)
213 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
217 iter->i = p - fi->apath;
218 ix = mapNextIterator(iter);
227 * Directory name iterator.
229 typedef struct dnli_s {
231 /*@only@*/ /*@null@*/
239 * Destroy directory name iterator.
240 * @param a directory name iterator
241 * @retval NULL always
243 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
247 DNLI_t dnli = (void *)a;
248 if (dnli->active) free(dnli->active);
255 static inline int dnlCount(/*@null@*/ const DNLI_t dnli)
258 return (dnli ? dnli->fi->dc : 0);
263 static inline int dnlIndex(/*@null@*/ const DNLI_t dnli)
266 return (dnli ? dnli->isave : -1);
270 * Create directory name iterator.
271 * @param fsm file state machine data
272 * @param reverse traverse directory names in reverse order?
273 * @return directory name iterator
277 static /*@only@*/ /*@null@*/
278 void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
280 /*@uses fsm->iter @*/
283 rpmfi fi = fsmGetFi(fsm);
289 dnli = xcalloc(1, sizeof(*dnli));
291 dnli->reverse = reverse;
293 dnli->i = (reverse ? fi->dc : 0);
297 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
299 /* Identify parent directories not skipped. */
301 for (i = 0; i < fi->fc; i++)
302 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
305 /* Exclude parent directories that are explicitly included. */
306 for (i = 0; i < fi->fc; i++) {
307 int dil, dnlen, bnlen;
309 if (!S_ISDIR(fi->fmodes[i]))
313 dnlen = strlen(fi->dnl[dil]);
314 bnlen = strlen(fi->bnl[i]);
316 for (j = 0; j < fi->dc; j++) {
320 if (!dnli->active[j] || j == dil)
321 /*@innercontinue@*/ continue;
324 if (jlen != (dnlen+bnlen+1))
325 /*@innercontinue@*/ continue;
326 if (strncmp(dnl, fi->dnl[dil], dnlen))
327 /*@innercontinue@*/ continue;
328 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
329 /*@innercontinue@*/ continue;
330 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
331 /*@innercontinue@*/ continue;
332 /* This directory is included in the package. */
336 /*@innerbreak@*/ break;
340 /* Print only once per package. */
343 for (i = 0; i < fi->dc; i++) {
344 if (!dnli->active[i]) continue;
347 rpmMessage(RPMMESS_DEBUG,
348 _("========== Directories not explictly included in package:\n"));
350 rpmMessage(RPMMESS_DEBUG, _("%10d %s\n"), i, fi->dnl[i]);
353 rpmMessage(RPMMESS_DEBUG, "==========\n");
362 * Return next directory name (from file info).
363 * @param dnli directory name iterator
364 * @return next directory name
367 static /*@observer@*/ /*@null@*/
368 const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
371 const char * dn = NULL;
379 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
380 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
382 if (i >= 0 && i < fi->dc)
392 static void * fsmThread(void * arg)
393 /*@globals fileSystem, internalState @*/
394 /*@modifies arg, fileSystem, internalState @*/
397 /*@-unqualifiedtrans@*/
398 return ((void *) fsmStage(fsm, fsm->nstage));
399 /*@=unqualifiedtrans@*/
402 int fsmNext(FSM_t fsm, fileStage nstage)
403 /*@globals fileSystem, internalState @*/
404 /*@modifies fsm, fileSystem, internalState @*/
406 fsm->nstage = nstage;
408 return rpmsqThread(fsmThread, fsm);
409 return fsmStage(fsm, fsm->nstage);
413 * Save hard link in chain.
414 * @param fsm file state machine data
415 * @return Is chain only partially filled?
418 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
419 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
420 /*@defines fsm->li @*/
421 /*@releases fsm->path @*/
422 /*@globals fileSystem, internalState @*/
423 /*@modifies fsm, fileSystem, internalState @*/
425 struct stat * st = &fsm->sb;
430 /* Find hard link set. */
432 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
433 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
438 /* New hard link encountered, add new link to set. */
441 if (fsm->li == NULL) {
442 fsm->li = xcalloc(1, sizeof(*fsm->li));
443 fsm->li->next = NULL;
444 fsm->li->sb = *st; /* structure assignment */
445 fsm->li->nlink = st->st_nlink;
446 fsm->li->linkIndex = fsm->ix;
447 fsm->li->createdPath = -1;
449 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
450 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
451 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
453 if (fsm->goal == FSM_PKGBUILD)
454 fsm->li->linksLeft = st->st_nlink;
455 if (fsm->goal == FSM_PKGINSTALL)
456 fsm->li->linksLeft = 0;
459 fsm->li->next = fsm->links;
461 fsm->links = fsm->li;
466 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
468 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
469 /*@-observertrans -dependenttrans@*/
470 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
471 /*@=observertrans =dependenttrans@*/
473 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
475 if (fsm->goal == FSM_PKGBUILD)
476 return (fsm->li->linksLeft > 0);
478 if (fsm->goal != FSM_PKGINSTALL)
481 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
484 /* Here come the bits, time to choose a non-skipped file name. */
485 { rpmfi fi = fsmGetFi(fsm);
487 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
488 ix = fsm->li->filex[j];
489 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
495 /* Are all links skipped or not encountered yet? */
497 return 1; /* XXX W2DO? */
499 /* Save the non-skipped file name and map index. */
500 fsm->li->linkIndex = j;
501 fsm->path = _free(fsm->path);
503 rc = fsmNext(fsm, FSM_MAP);
509 * Destroy set of hard links.
510 * @param li set of hard links
511 * @return NULL always
513 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink_s * li)
517 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
518 li->filex = _free(li->filex);
525 FSM_t fsm = xcalloc(1, sizeof(*fsm));
529 FSM_t freeFSM(FSM_t fsm)
532 fsm->path = _free(fsm->path);
534 while ((fsm->li = fsm->links) != NULL) {
535 fsm->links = fsm->li->next;
536 fsm->li->next = NULL;
537 fsm->li = freeHardLink(fsm->li);
540 fsm->dnlx = _free(fsm->dnlx);
541 fsm->ldn = _free(fsm->ldn);
542 fsm->iter = mapFreeIterator(fsm->iter);
547 int fsmSetup(FSM_t fsm, fileStage goal,
548 const rpmts ts, const rpmfi fi, FD_t cfd,
549 unsigned int * archiveSize, const char ** failedFile)
556 fsm->cfd = fdLink(cfd, "persist (fsm)");
557 pos = fdGetCpioPos(fsm->cfd);
558 fdSetCpioPos(fsm->cfd, 0);
560 fsm->iter = mapInitIterator(ts, fi);
562 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
565 ptr = rpmtsNotify(ts, fi->te,
566 RPMCALLBACK_INST_START, fi->archivePos, fi->archiveSize);
571 fsm->archiveSize = archiveSize;
572 if (fsm->archiveSize)
573 *fsm->archiveSize = 0;
574 fsm->failedFile = failedFile;
576 *fsm->failedFile = NULL;
580 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
581 if (fsm->goal == FSM_PKGINSTALL) {
582 if (ts && rpmtsGetTid(ts) > 0)
583 sprintf(fsm->sufbuf, ";%08x", (unsigned)rpmtsGetTid(ts));
587 rc = fsmUNSAFE(fsm, FSM_CREATE);
588 if (rc && !ec) ec = rc;
590 rc = fsmUNSAFE(fsm, fsm->goal);
591 if (rc && !ec) ec = rc;
594 if (fsm->archiveSize && ec == 0)
595 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
598 /*@-nullstate@*/ /* FIX: *fsm->failedFile may be NULL */
603 int fsmTeardown(FSM_t fsm)
608 rc = fsmUNSAFE(fsm, FSM_DESTROY);
610 fsm->iter = mapFreeIterator(fsm->iter);
611 if (fsm->cfd != NULL) {
612 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
615 fsm->failedFile = NULL;
619 int fsmMapPath(FSM_t fsm)
621 rpmfi fi = fsmGetFi(fsm); /* XXX const except for fstates */
628 fsm->action = FA_UNKNOWN;
632 if (fi && i >= 0 && i < fi->fc) {
635 fsm->astriplen = fi->astriplen;
636 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
637 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
638 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
640 /* src rpms have simple base name in payload. */
641 fsm->dirName = fi->dnl[fi->dil[i]];
642 fsm->baseName = fi->bnl[i];
646 switch (fsm->action) {
656 assert(rpmteType(fi->te) == TR_ADDED);
660 if (fi->fstates && rpmteType(fi->te) == TR_ADDED)
661 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
664 case FA_SKIPNETSHARED:
665 if (fi->fstates && rpmteType(fi->te) == TR_ADDED)
666 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
670 if (fi->fstates && rpmteType(fi->te) == TR_ADDED)
671 fi->fstates[i] = RPMFILE_STATE_WRONGCOLOR;
675 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
676 switch (rpmteType(fi->te)) {
678 fsm->osuffix = SUFFIX_RPMORIG;
679 /*@innerbreak@*/ break;
681 fsm->osuffix = SUFFIX_RPMSAVE;
682 /*@innerbreak@*/ break;
687 assert(rpmteType(fi->te) == TR_ADDED);
688 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
689 fsm->nsuffix = SUFFIX_RPMNEW;
693 assert(rpmteType(fi->te) == TR_ADDED);
694 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
695 fsm->osuffix = SUFFIX_RPMSAVE;
698 #if 0 /* XXX is this a genhdlist fix? */
699 assert(rpmteType(fi->te) == TR_REMOVED);
702 * XXX TODO: %ghost probably shouldn't be removed, but that changes
703 * legacy rpm behavior.
711 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
712 const struct stat * st = &fsm->sb;
713 fsm->path = _free(fsm->path);
714 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
715 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
721 int fsmMapAttrs(FSM_t fsm)
723 struct stat * st = &fsm->sb;
724 rpmfi fi = fsmGetFi(fsm);
727 if (fi && i >= 0 && i < fi->fc) {
729 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
731 (fi->fmodes ? fi->fmodes[i] : perms);
733 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
735 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
737 (fi->frdevs ? fi->frdevs[i] : 0);
739 (fi->fmtimes ? fi->fmtimes[i] : 0);
741 if (fsm->mapFlags & CPIO_MAP_MODE)
742 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
743 if (fsm->mapFlags & CPIO_MAP_TYPE) {
744 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
745 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
746 && st->st_nlink == 0)
748 st->st_rdev = finalRdev;
749 st->st_mtime = finalMtime;
751 if (fsm->mapFlags & CPIO_MAP_UID)
752 st->st_uid = finalUid;
753 if (fsm->mapFlags & CPIO_MAP_GID)
754 st->st_gid = finalGid;
756 { rpmts ts = fsmGetTs(fsm);
758 if (ts != NULL && !(rpmtsFlags(ts) & RPMTRANS_FLAG_NOMD5)) {
759 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
760 fsm->md5sum = (fi->md5s ? (fi->md5s + (16 * i)) : NULL);
772 * Create file from payload stream.
773 * @param fsm file state machine data
774 * @return 0 on success
777 static int expandRegular(/*@special@*/ FSM_t fsm)
778 /*@uses fsm->fmd5sum, fsm->md5sum, fsm->sb, fsm->wfd @*/
779 /*@globals fileSystem, internalState @*/
780 /*@modifies fsm, fileSystem, internalState @*/
782 const struct stat * st = &fsm->sb;
783 int left = st->st_size;
786 rc = fsmNext(fsm, FSM_WOPEN);
790 if (st->st_size > 0 && (fsm->fmd5sum != NULL || fsm->md5sum != NULL))
791 fdInitDigest(fsm->wfd, PGPHASHALGO_MD5, 0);
795 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
796 rc = fsmNext(fsm, FSM_DREAD);
800 rc = fsmNext(fsm, FSM_WRITE);
806 /* don't call this with fileSize == fileComplete */
808 (void) fsmNext(fsm, FSM_NOTIFY);
811 if (st->st_size > 0 && (fsm->fmd5sum || fsm->md5sum)) {
812 void * md5sum = NULL;
813 int asAscii = (fsm->md5sum == NULL ? 1 : 0);
815 (void) Fflush(fsm->wfd);
816 fdFiniDigest(fsm->wfd, PGPHASHALGO_MD5, &md5sum, NULL, asAscii);
818 if (md5sum == NULL) {
819 rc = CPIOERR_MD5SUM_MISMATCH;
823 if (fsm->md5sum != NULL) {
824 if (memcmp(md5sum, fsm->md5sum, 16))
825 rc = CPIOERR_MD5SUM_MISMATCH;
827 if (strcmp(md5sum, fsm->fmd5sum))
828 rc = CPIOERR_MD5SUM_MISMATCH;
830 md5sum = _free(md5sum);
834 (void) fsmNext(fsm, FSM_WCLOSE);
840 * Write next item to payload stream.
841 * @param fsm file state machine data
842 * @param writeData should data be written?
843 * @return 0 on success
845 /*@-compdef -compmempass@*/
846 static int writeFile(/*@special@*/ /*@partial@*/ FSM_t fsm, int writeData)
847 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
848 /*@globals fileSystem, internalState @*/
849 /*@modifies fsm, fileSystem, internalState @*/
851 const char * path = fsm->path;
852 const char * opath = fsm->opath;
853 struct stat * st = &fsm->sb;
854 struct stat * ost = &fsm->osb;
855 char * symbuf = NULL;
860 st->st_size = (writeData ? ost->st_size : 0);
863 if (S_ISDIR(st->st_mode)) {
865 } else if (S_ISLNK(st->st_mode)) {
867 * While linux puts the size of a symlink in the st_size field,
868 * I don't think that's a specified standard.
870 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
871 rc = fsmUNSAFE(fsm, FSM_READLINK);
873 st->st_size = fsm->rdnb;
874 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
878 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
880 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
881 char * t = alloca(nb);
884 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
886 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
888 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
889 rpmfi fi = fsmGetFi(fsm);
891 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
894 rc = fsmNext(fsm, FSM_HWRITE);
898 if (writeData && S_ISREG(st->st_mode)) {
901 void * mapped = (void *)-1;
905 rc = fsmNext(fsm, FSM_ROPEN);
908 /* XXX unbuffered mmap generates *lots* of fdio debugging */
911 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
912 if (mapped != (void *)-1) {
914 fsm->rdbuf = (char *) mapped;
915 fsm->rdlen = nmapped = st->st_size;
916 #if defined(MADV_DONTNEED)
917 xx = madvise(mapped, nmapped, MADV_DONTNEED);
926 if (mapped != (void *)-1) {
931 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
932 rc = fsmNext(fsm, FSM_READ);
936 /* XXX DWRITE uses rdnb for I/O length. */
937 rc = fsmNext(fsm, FSM_DWRITE);
945 if (mapped != (void *)-1) {
946 xx = msync(mapped, nmapped, MS_ASYNC);
947 #if defined(MADV_DONTNEED)
948 xx = madvise(mapped, nmapped, MADV_DONTNEED);
951 xx = munmap(mapped, nmapped);
958 } else if (writeData && S_ISLNK(st->st_mode)) {
959 /* XXX DWRITE uses rdnb for I/O length. */
961 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
963 fsm->rdnb = strlen(symbuf);
964 rc = fsmNext(fsm, FSM_DWRITE);
968 rc = fsmNext(fsm, FSM_PAD);
974 if (fsm->rfd != NULL)
975 (void) fsmNext(fsm, FSM_RCLOSE);
976 /*@-dependenttrans@*/
979 /*@=dependenttrans@*/
982 /*@=compdef =compmempass@*/
985 * Write set of linked files to payload stream.
986 * @param fsm file state machine data
987 * @return 0 on success
989 static int writeLinkedFile(/*@special@*/ /*@partial@*/ FSM_t fsm)
990 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
991 /*@globals fileSystem, internalState @*/
992 /*@modifies fsm, fileSystem, internalState @*/
994 const char * path = fsm->path;
995 const char * nsuffix = fsm->nsuffix;
996 int iterIndex = fsm->ix;
1002 fsm->nsuffix = NULL;
1007 for (i = fsm->li->nlink - 1; i >= 0; i--) {
1009 if (fsm->li->filex[i] < 0) continue;
1011 fsm->ix = fsm->li->filex[i];
1013 rc = fsmNext(fsm, FSM_MAP);
1016 /* Write data after last link. */
1017 rc = writeFile(fsm, (i == 0));
1018 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
1020 *fsm->failedFile = xstrdup(fsm->path);
1023 fsm->path = _free(fsm->path);
1024 fsm->li->filex[i] = -1;
1029 fsm->ix = iterIndex;
1030 fsm->nsuffix = nsuffix;
1035 /** \ingroup payload
1036 * Create pending hard links to existing file.
1037 * @param fsm file state machine data
1038 * @return 0 on success
1042 static int fsmMakeLinks(/*@special@*/ /*@partial@*/ FSM_t fsm)
1043 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
1044 /*@globals fileSystem, internalState @*/
1045 /*@modifies fsm, fileSystem, internalState @*/
1047 const char * path = fsm->path;
1048 const char * opath = fsm->opath;
1049 const char * nsuffix = fsm->nsuffix;
1050 int iterIndex = fsm->ix;
1057 fsm->nsuffix = NULL;
1060 fsm->ix = fsm->li->filex[fsm->li->createdPath];
1061 rc = fsmNext(fsm, FSM_MAP);
1062 fsm->opath = fsm->path;
1065 for (i = 0; i < fsm->li->nlink; i++) {
1066 if (fsm->li->filex[i] < 0) continue;
1067 if (fsm->li->createdPath == i) continue;
1069 fsm->ix = fsm->li->filex[i];
1070 fsm->path = _free(fsm->path);
1071 rc = fsmNext(fsm, FSM_MAP);
1072 if (XFA_SKIPPING(fsm->action)) continue;
1074 rc = fsmUNSAFE(fsm, FSM_VERIFY);
1076 if (!(rc == CPIOERR_ENOENT)) break;
1078 /* XXX link(fsm->opath, fsm->path) */
1079 rc = fsmNext(fsm, FSM_LINK);
1080 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
1083 *fsm->failedFile = xstrdup(fsm->path);
1087 fsm->li->linksLeft--;
1090 fsm->path = _free(fsm->path);
1091 fsm->opath = _free(fsm->opath);
1093 fsm->ix = iterIndex;
1094 fsm->nsuffix = nsuffix;
1102 /** \ingroup payload
1103 * Commit hard linked file set atomically.
1104 * @param fsm file state machine data
1105 * @return 0 on success
1108 static int fsmCommitLinks(/*@special@*/ /*@partial@*/ FSM_t fsm)
1109 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
1110 fsm->li, fsm->links @*/
1111 /*@globals fileSystem, internalState @*/
1112 /*@modifies fsm, fileSystem, internalState @*/
1114 const char * path = fsm->path;
1115 const char * nsuffix = fsm->nsuffix;
1116 int iterIndex = fsm->ix;
1117 struct stat * st = &fsm->sb;
1122 fsm->nsuffix = NULL;
1126 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1127 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1133 for (i = 0; i < fsm->li->nlink; i++) {
1134 if (fsm->li->filex[i] < 0) continue;
1135 fsm->ix = fsm->li->filex[i];
1136 rc = fsmNext(fsm, FSM_MAP);
1137 if (!XFA_SKIPPING(fsm->action))
1138 rc = fsmNext(fsm, FSM_COMMIT);
1139 fsm->path = _free(fsm->path);
1140 fsm->li->filex[i] = -1;
1144 fsm->ix = iterIndex;
1145 fsm->nsuffix = nsuffix;
1152 * Remove (if created) directories not explicitly included in package.
1153 * @param fsm file state machine data
1154 * @return 0 on success
1156 static int fsmRmdirs(/*@special@*/ /*@partial@*/ FSM_t fsm)
1157 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1158 /*@globals fileSystem, internalState @*/
1159 /*@modifies fsm, fileSystem, internalState @*/
1161 const char * path = fsm->path;
1162 void * dnli = dnlInitIterator(fsm, 1);
1163 char * dn = fsm->rdbuf;
1164 int dc = dnlCount(dnli);
1170 /*@-observertrans -dependenttrans@*/
1171 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1172 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1173 int dnlen = strlen(fsm->path);
1176 dc = dnlIndex(dnli);
1177 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1180 /* Copy to avoid const on fsm->path. */
1181 te = stpcpy(dn, fsm->path) - 1;
1184 /* Remove generated directories. */
1185 /*@-usereleased@*/ /* LCL: te used after release? */
1190 rc = fsmNext(fsm, FSM_RMDIR);
1195 /*@innerbreak@*/ break;
1197 } while ((te - fsm->path) > fsm->dnlx[dc]);
1201 dnli = dnlFreeIterator(dnli);
1202 /*@=observertrans =dependenttrans@*/
1209 * Create (if necessary) directories not explicitly included in package.
1210 * @param fsm file state machine data
1211 * @return 0 on success
1213 static int fsmMkdirs(/*@special@*/ /*@partial@*/ FSM_t fsm)
1214 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1215 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1216 /*@defines fsm->dnlx, fsm->ldn @*/
1217 /*@globals fileSystem, internalState @*/
1218 /*@modifies fsm, fileSystem, internalState @*/
1220 struct stat * st = &fsm->sb;
1221 struct stat * ost = &fsm->osb;
1222 const char * path = fsm->path;
1223 mode_t st_mode = st->st_mode;
1224 void * dnli = dnlInitIterator(fsm, 0);
1225 char * dn = fsm->rdbuf;
1226 int dc = dnlCount(dnli);
1234 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1235 /*@-observertrans -dependenttrans@*/
1236 if (fsm->dnlx != NULL)
1237 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1238 int dnlen = strlen(fsm->path);
1241 dc = dnlIndex(dnli);
1242 if (dc < 0) continue;
1243 fsm->dnlx[dc] = dnlen;
1247 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1248 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1250 /*@=compdef =nullpass@*/
1252 /* Copy to avoid const on fsm->path. */
1253 (void) stpcpy(dn, fsm->path);
1256 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1257 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1259 /*@innercontinue@*/ continue;
1263 /* Already validated? */
1264 /*@-usedef -compdef -nullpass -nullderef@*/
1265 if (i < fsm->ldnlen &&
1266 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1267 !strncmp(fsm->path, fsm->ldn, i))
1270 /* Move pre-existing path marker forward. */
1271 fsm->dnlx[dc] = (te - dn);
1272 /*@innercontinue@*/ continue;
1274 /*@=usedef =compdef =nullpass =nullderef@*/
1276 /* Validate next component of path. */
1277 rc = fsmUNSAFE(fsm, FSM_LSTAT);
1280 /* Directory already exists? */
1281 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1282 /* Move pre-existing path marker forward. */
1283 fsm->dnlx[dc] = (te - dn);
1284 } else if (rc == CPIOERR_ENOENT) {
1285 rpmfi fi = fsmGetFi(fsm);
1287 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1288 rc = fsmNext(fsm, FSM_MKDIR);
1290 rpmMessage(RPMMESS_DEBUG,
1291 _("%s directory created with perms %04o.\n"),
1292 fsm->path, (unsigned)(st->st_mode & 07777));
1296 /*@innerbreak@*/ break;
1300 /* Save last validated path. */
1301 /*@-compdef@*/ /* FIX: ldn/path annotations ? */
1302 if (fsm->ldnalloc < (dnlen + 1)) {
1303 fsm->ldnalloc = dnlen + 100;
1304 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1306 if (fsm->ldn != NULL) { /* XXX can't happen */
1307 strcpy(fsm->ldn, fsm->path);
1308 fsm->ldnlen = dnlen;
1313 dnli = dnlFreeIterator(dnli);
1314 /*@=observertrans =dependenttrans@*/
1317 st->st_mode = st_mode; /* XXX restore st->st_mode */
1318 /*@-compdef@*/ /* FIX: ldn/path annotations ? */
1325 * Check for file on disk.
1326 * @param fsm file state machine data
1327 * @return 0 on success
1329 static int fsmStat(/*@special@*/ /*@partial@*/ FSM_t fsm)
1330 /*@globals fileSystem, internalState @*/
1331 /*@modifies fsm, fileSystem, internalState @*/
1335 if (fsm->path != NULL) {
1336 int saveernno = errno;
1337 rc = fsmUNSAFE(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1338 ? FSM_LSTAT : FSM_STAT));
1339 if (rc == CPIOERR_ENOENT) {
1343 } else if (rc == 0) {
1347 /* Skip %ghost files on build. */
1354 #define IS_DEV_LOG(_x) \
1355 ((_x) != NULL && strlen(_x) >= (sizeof("/dev/log")-1) && \
1356 !strncmp((_x), "/dev/log", sizeof("/dev/log")-1) && \
1357 ((_x)[sizeof("/dev/log")-1] == '\0' || \
1358 (_x)[sizeof("/dev/log")-1] == ';'))
1362 int fsmStage(FSM_t fsm, fileStage stage)
1365 fileStage prevStage = fsm->stage;
1366 const char * const prev = fileStageString(prevStage);
1368 static int modulo = 4;
1369 const char * const cur = fileStageString(stage);
1370 struct stat * st = &fsm->sb;
1371 struct stat * ost = &fsm->osb;
1372 int saveerrno = errno;
1377 #define _fafilter(_a) \
1378 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1379 ? fileActionString(_a) : "")
1381 if (stage & FSM_DEAD) {
1383 } else if (stage & FSM_INTERNAL) {
1384 if (_fsm_debug && !(stage & FSM_SYSCALL))
1385 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1387 (unsigned)st->st_mode, (int)st->st_nlink,
1388 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1389 (fsm->path ? fsm->path : ""),
1390 _fafilter(fsm->action));
1393 if (_fsm_debug || !(stage & FSM_VERBOSE))
1394 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1396 (unsigned)st->st_mode, (int)st->st_nlink,
1397 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1398 (fsm->path ? fsm->path + fsm->astriplen : ""),
1399 _fafilter(fsm->action));
1407 case FSM_PKGINSTALL:
1409 /* Clean fsm, free'ing memory. Read next archive header. */
1410 rc = fsmUNSAFE(fsm, FSM_INIT);
1412 /* Exit on end-of-payload. */
1413 if (rc == CPIOERR_HDR_TRAILER) {
1415 /*@loopbreak@*/ break;
1418 /* Exit on error. */
1421 (void) fsmNext(fsm, FSM_UNDO);
1422 /*@loopbreak@*/ break;
1425 /* Extract file from archive. */
1426 rc = fsmNext(fsm, FSM_PROCESS);
1428 (void) fsmNext(fsm, FSM_UNDO);
1429 /*@loopbreak@*/ break;
1432 /* Notify on success. */
1433 (void) fsmNext(fsm, FSM_NOTIFY);
1435 rc = fsmNext(fsm, FSM_FINI);
1437 /*@loopbreak@*/ break;
1444 /* Clean fsm, free'ing memory. */
1445 rc = fsmUNSAFE(fsm, FSM_INIT);
1447 /* Exit on end-of-payload. */
1448 if (rc == CPIOERR_HDR_TRAILER) {
1450 /*@loopbreak@*/ break;
1453 /* Rename/erase next item. */
1454 if (fsmNext(fsm, FSM_FINI))
1455 /*@loopbreak@*/ break;
1461 rc = fsmUNSAFE(fsm, FSM_INIT);
1463 /* Exit on end-of-payload. */
1464 if (rc == CPIOERR_HDR_TRAILER) {
1466 /*@loopbreak@*/ break;
1469 /* Exit on error. */
1472 (void) fsmNext(fsm, FSM_UNDO);
1473 /*@loopbreak@*/ break;
1476 /* Copy file into archive. */
1477 rc = fsmNext(fsm, FSM_PROCESS);
1479 (void) fsmNext(fsm, FSM_UNDO);
1480 /*@loopbreak@*/ break;
1483 /* Notify on success. */
1484 (void) fsmNext(fsm, FSM_NOTIFY);
1486 if (fsmNext(fsm, FSM_FINI))
1487 /*@loopbreak@*/ break;
1490 /* Flush partial sets of hard linked files. */
1491 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1493 while ((fsm->li = fsm->links) != NULL) {
1494 fsm->links = fsm->li->next;
1495 fsm->li->next = NULL;
1497 /* Re-calculate link count for archive header. */
1498 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1499 if (fsm->li->filex[i] < 0)
1500 /*@innercontinue@*/ continue;
1504 /* XXX force the contents out as well. */
1507 fsm->li->filex[0] = fsm->li->filex[j];
1508 fsm->li->filex[j] = -1;
1511 fsm->li->sb.st_nlink = nlink;
1513 fsm->sb = fsm->li->sb; /* structure assignment */
1514 fsm->osb = fsm->sb; /* structure assignment */
1516 if (!rc) rc = writeLinkedFile(fsm);
1518 fsm->li = freeHardLink(fsm->li);
1523 rc = fsmNext(fsm, FSM_TRAILER);
1527 { rpmts ts = fsmGetTs(fsm);
1528 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1529 fsm->commit = ((ts && (rpmtsFlags(ts) & _tsmask) &&
1530 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1533 fsm->path = _free(fsm->path);
1534 fsm->opath = _free(fsm->opath);
1535 fsm->dnlx = _free(fsm->dnlx);
1537 fsm->ldn = _free(fsm->ldn);
1538 fsm->ldnalloc = fsm->ldnlen = 0;
1540 fsm->rdsize = fsm->wrsize = 0;
1541 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1542 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1543 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1544 fsm->rdsize = 8 * BUFSIZ;
1545 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1546 fsm->wrsize = 8 * BUFSIZ;
1547 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1550 fsm->mkdirsdone = 0;
1554 errno = 0; /* XXX get rid of EBADF */
1556 /* Detect and create directories not explicitly in package. */
1557 if (fsm->goal == FSM_PKGINSTALL) {
1559 rc = fsmNext(fsm, FSM_MKDIRS);
1561 if (!rc) fsm->mkdirsdone = 1;
1566 fsm->path = _free(fsm->path);
1568 fsm->diskchecked = fsm->exists = 0;
1570 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1571 fsm->action = FA_UNKNOWN;
1572 fsm->osuffix = NULL;
1573 fsm->nsuffix = NULL;
1575 if (fsm->goal == FSM_PKGINSTALL) {
1576 /* Read next header from payload, checking for end-of-payload. */
1577 rc = fsmUNSAFE(fsm, FSM_NEXT);
1581 /* Identify mapping index. */
1582 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1583 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1585 /* Detect end-of-loop and/or mapping error. */
1587 if (fsm->goal == FSM_PKGINSTALL) {
1589 rpmMessage(RPMMESS_WARNING,
1590 _("archive file %s was not found in header file list\n"),
1594 if (fsm->failedFile && *fsm->failedFile == NULL)
1595 *fsm->failedFile = xstrdup(fsm->path);
1597 rc = CPIOERR_UNMAPPED_FILE;
1599 rc = CPIOERR_HDR_TRAILER;
1604 /* On non-install, mode must be known so that dirs don't get suffix. */
1605 if (fsm->goal != FSM_PKGINSTALL) {
1606 rpmfi fi = fsmGetFi(fsm);
1607 st->st_mode = fi->fmodes[fsm->ix];
1610 /* Generate file path. */
1611 rc = fsmNext(fsm, FSM_MAP);
1614 /* Perform lstat/stat for disk file. */
1618 if (fsm->path != NULL &&
1619 !(fsm->goal == FSM_PKGINSTALL && S_ISREG(st->st_mode)))
1621 rc = fsmUNSAFE(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1622 ? FSM_LSTAT : FSM_STAT));
1623 if (rc == CPIOERR_ENOENT) {
1627 } else if (rc == 0) {
1631 /* Skip %ghost files on build. */
1635 fsm->diskchecked = 1;
1638 /* On non-install, the disk file stat is what's remapped. */
1640 if (fsm->goal != FSM_PKGINSTALL)
1641 *st = *ost; /* structure assignment */
1644 /* Remap file perms, owner, and group. */
1645 rc = fsmMapAttrs(fsm);
1648 fsm->postpone = XFA_SKIPPING(fsm->action);
1649 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1650 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1651 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1652 fsm->postpone = saveHardLink(fsm);
1659 rc = fsmMapPath(fsm);
1662 rc = fsmMkdirs(fsm);
1666 rc = fsmRmdirs(fsm);
1669 if (fsm->postpone) {
1670 if (fsm->goal == FSM_PKGINSTALL)
1671 rc = fsmNext(fsm, FSM_EAT);
1675 if (fsm->goal == FSM_PKGBUILD) {
1676 if (fsm->fflags & RPMFILE_GHOST) /* XXX Don't if %ghost file. */
1678 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1679 struct hardLink_s * li, * prev;
1681 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1682 rc = writeLinkedFile(fsm);
1683 if (rc) break; /* W2DO? */
1685 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1687 /*@loopbreak@*/ break;
1690 fsm->links = fsm->li->next;
1692 prev->next = fsm->li->next;
1693 fsm->li->next = NULL;
1694 fsm->li = freeHardLink(fsm->li);
1696 rc = writeFile(fsm, 1);
1701 if (fsm->goal != FSM_PKGINSTALL)
1704 if (S_ISREG(st->st_mode)) {
1705 const char * path = fsm->path;
1707 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1708 rc = fsmUNSAFE(fsm, FSM_VERIFY);
1710 if (rc == 0 && fsm->osuffix) {
1711 const char * opath = fsm->opath;
1712 fsm->opath = fsm->path;
1713 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1714 rc = fsmNext(fsm, FSM_RENAME);
1716 rpmMessage(RPMMESS_WARNING,
1717 _("%s saved as %s\n"),
1718 (fsm->opath ? fsm->opath : ""),
1719 (fsm->path ? fsm->path : ""));
1720 fsm->path = _free(fsm->path);
1724 /*@-dependenttrans@*/
1726 /*@=dependenttrans@*/
1727 if (!(rc == CPIOERR_ENOENT)) return rc;
1728 rc = expandRegular(fsm);
1729 } else if (S_ISDIR(st->st_mode)) {
1730 mode_t st_mode = st->st_mode;
1731 rc = fsmUNSAFE(fsm, FSM_VERIFY);
1732 if (rc == CPIOERR_ENOENT) {
1733 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1734 st->st_mode |= 00700;
1735 rc = fsmNext(fsm, FSM_MKDIR);
1736 st->st_mode = st_mode; /* XXX restore st->st_mode */
1738 } else if (S_ISLNK(st->st_mode)) {
1739 const char * opath = fsm->opath;
1741 if ((st->st_size + 1) > fsm->rdsize) {
1742 rc = CPIOERR_HDR_SIZE;
1746 fsm->wrlen = st->st_size;
1747 rc = fsmNext(fsm, FSM_DREAD);
1748 if (!rc && fsm->rdnb != fsm->wrlen)
1749 rc = CPIOERR_READ_FAILED;
1753 fsm->wrbuf[st->st_size] = '\0';
1755 /* XXX symlink(fsm->opath, fsm->path) */
1756 /*@-dependenttrans@*/
1757 fsm->opath = fsm->wrbuf;
1758 /*@=dependenttrans@*/
1759 rc = fsmUNSAFE(fsm, FSM_VERIFY);
1760 if (rc == CPIOERR_ENOENT)
1761 rc = fsmNext(fsm, FSM_SYMLINK);
1762 fsm->opath = opath; /* XXX restore fsm->path */
1763 } else if (S_ISFIFO(st->st_mode)) {
1764 mode_t st_mode = st->st_mode;
1765 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1766 rc = fsmUNSAFE(fsm, FSM_VERIFY);
1767 if (rc == CPIOERR_ENOENT) {
1768 st->st_mode = 0000; /* XXX abuse st->st_mode */
1769 rc = fsmNext(fsm, FSM_MKFIFO);
1770 st->st_mode = st_mode; /* XXX restore st->st_mode */
1772 } else if (S_ISCHR(st->st_mode) ||
1773 S_ISBLK(st->st_mode) ||
1774 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1776 rc = fsmUNSAFE(fsm, FSM_VERIFY);
1777 if (rc == CPIOERR_ENOENT)
1778 rc = fsmNext(fsm, FSM_MKNOD);
1780 /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1781 if (!IS_DEV_LOG(fsm->path))
1782 rc = CPIOERR_UNKNOWN_FILETYPE;
1784 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1785 fsm->li->createdPath = fsm->li->linkIndex;
1786 rc = fsmMakeLinks(fsm);
1792 rc = fsmMakeLinks(fsm);
1794 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1795 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1796 rpmts ts = fsmGetTs(fsm);
1797 rpmfi fi = fsmGetFi(fsm);
1799 unsigned int archivePos = fdGetCpioPos(fsm->cfd);
1800 if (archivePos > fi->archivePos) {
1801 fi->archivePos = archivePos;
1802 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS,
1803 fi->archivePos, fi->archiveSize);
1810 if (fsm->goal == FSM_PKGINSTALL) {
1812 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1814 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1816 (void) fsmNext(fsm, FSM_RMDIRS);
1821 if (fsm->failedFile && *fsm->failedFile == NULL)
1822 *fsm->failedFile = xstrdup(fsm->path);
1826 if (!fsm->postpone && fsm->commit) {
1827 if (fsm->goal == FSM_PKGINSTALL)
1828 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1829 ? fsmCommitLinks(fsm) : fsmNext(fsm, FSM_COMMIT));
1830 if (fsm->goal == FSM_PKGCOMMIT)
1831 rc = fsmNext(fsm, FSM_COMMIT);
1832 if (fsm->goal == FSM_PKGERASE)
1833 rc = fsmNext(fsm, FSM_COMMIT);
1835 fsm->path = _free(fsm->path);
1836 fsm->opath = _free(fsm->opath);
1838 memset(st, 0, sizeof(*st));
1839 memset(ost, 0, sizeof(*ost));
1843 /* Rename pre-existing modified or unmanaged file. */
1844 if (fsm->osuffix && fsm->diskchecked &&
1845 (fsm->exists || (fsm->goal == FSM_PKGINSTALL && S_ISREG(st->st_mode))))
1847 const char * opath = fsm->opath;
1848 const char * path = fsm->path;
1849 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1850 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1851 rc = fsmNext(fsm, FSM_RENAME);
1853 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1854 (fsm->opath ? fsm->opath : ""),
1855 (fsm->path ? fsm->path : ""));
1857 fsm->path = _free(fsm->path);
1859 fsm->opath = _free(fsm->opath);
1863 /* Remove erased files. */
1864 if (fsm->goal == FSM_PKGERASE) {
1865 if (fsm->action == FA_ERASE) {
1866 rpmfi fi = fsmGetFi(fsm);
1867 if (S_ISDIR(st->st_mode)) {
1868 rc = fsmNext(fsm, FSM_RMDIR);
1871 case CPIOERR_ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1872 case CPIOERR_ENOTEMPTY:
1873 /* XXX make sure that build side permits %missingok on directories. */
1874 if (fsm->fflags & RPMFILE_MISSINGOK)
1875 /*@innerbreak@*/ break;
1877 /* XXX common error message. */
1879 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1880 _("%s rmdir of %s failed: Directory not empty\n"),
1881 rpmfiTypeString(fi), fsm->path);
1882 /*@innerbreak@*/ break;
1885 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1886 _("%s rmdir of %s failed: %s\n"),
1887 rpmfiTypeString(fi), fsm->path, strerror(errno));
1888 /*@innerbreak@*/ break;
1891 rc = fsmNext(fsm, FSM_UNLINK);
1894 case CPIOERR_ENOENT:
1895 if (fsm->fflags & RPMFILE_MISSINGOK)
1896 /*@innerbreak@*/ break;
1900 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1901 _("%s unlink of %s failed: %s\n"),
1902 rpmfiTypeString(fi), fsm->path, strerror(errno));
1903 /*@innerbreak@*/ break;
1907 /* XXX Failure to remove is not (yet) cause for failure. */
1908 if (!strict_erasures) rc = 0;
1912 /* XXX Special case /dev/log, which shouldn't be packaged anyways */
1913 if (!S_ISSOCK(st->st_mode) && !IS_DEV_LOG(fsm->path)) {
1914 /* Rename temporary to final file name. */
1915 if (!S_ISDIR(st->st_mode) &&
1916 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1918 fsm->opath = fsm->path;
1919 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1920 rc = fsmNext(fsm, FSM_RENAME);
1921 if (!rc && fsm->nsuffix) {
1922 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1923 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1924 (opath ? opath : ""),
1925 (fsm->path ? fsm->path : ""));
1926 opath = _free(opath);
1928 fsm->opath = _free(fsm->opath);
1930 if (S_ISLNK(st->st_mode)) {
1931 if (!rc && !getuid())
1932 rc = fsmNext(fsm, FSM_LCHOWN);
1934 if (!rc && !getuid())
1935 rc = fsmNext(fsm, FSM_CHOWN);
1937 rc = fsmNext(fsm, FSM_CHMOD);
1939 time_t mtime = st->st_mtime;
1940 rpmfi fi = fsmGetFi(fsm);
1942 st->st_mtime = fi->fmtimes[fsm->ix];
1943 rc = fsmNext(fsm, FSM_UTIME);
1944 st->st_mtime = mtime;
1949 /* Notify on success. */
1950 if (!rc) rc = fsmNext(fsm, FSM_NOTIFY);
1951 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1953 *fsm->failedFile = fsm->path;
1959 fsm->path = _free(fsm->path);
1961 /* Check for hard links missing from payload. */
1962 while ((fsm->li = fsm->links) != NULL) {
1963 fsm->links = fsm->li->next;
1964 fsm->li->next = NULL;
1965 if (fsm->goal == FSM_PKGINSTALL &&
1966 fsm->commit && fsm->li->linksLeft)
1968 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1969 if (fsm->li->filex[i] < 0)
1970 /*@innercontinue@*/ continue;
1971 rc = CPIOERR_MISSING_HARDLINK;
1972 if (fsm->failedFile && *fsm->failedFile == NULL) {
1973 fsm->ix = fsm->li->filex[i];
1974 if (!fsmNext(fsm, FSM_MAP)) {
1976 *fsm->failedFile = fsm->path;
1981 /*@loopbreak@*/ break;
1984 if (fsm->goal == FSM_PKGBUILD &&
1985 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1987 rc = CPIOERR_MISSING_HARDLINK;
1989 fsm->li = freeHardLink(fsm->li);
1991 fsm->ldn = _free(fsm->ldn);
1992 fsm->ldnalloc = fsm->ldnlen = 0;
1993 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1994 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1997 if (fsm->diskchecked && !fsm->exists) {
1998 rc = CPIOERR_ENOENT;
2001 if (S_ISREG(st->st_mode)) {
2002 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
2004 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
2007 * XXX HP-UX (and other os'es) don't permit unlink on busy
2010 fsm->opath = fsm->path;
2012 rc = fsmNext(fsm, FSM_RENAME);
2014 (void) fsmNext(fsm, FSM_UNLINK);
2016 rc = CPIOERR_UNLINK_FAILED;
2017 fsm->path = fsm->opath;
2019 return (rc ? rc : CPIOERR_ENOENT); /* XXX HACK */
2020 /*@notreached@*/ break;
2021 } else if (S_ISDIR(st->st_mode)) {
2022 if (S_ISDIR(ost->st_mode)) return 0;
2023 if (S_ISLNK(ost->st_mode)) {
2024 rc = fsmUNSAFE(fsm, FSM_STAT);
2025 if (rc == CPIOERR_ENOENT) rc = 0;
2028 if (S_ISDIR(ost->st_mode)) return 0;
2030 } else if (S_ISLNK(st->st_mode)) {
2031 if (S_ISLNK(ost->st_mode)) {
2032 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
2033 rc = fsmUNSAFE(fsm, FSM_READLINK);
2036 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
2038 } else if (S_ISFIFO(st->st_mode)) {
2039 if (S_ISFIFO(ost->st_mode)) return 0;
2040 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
2041 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
2042 (ost->st_rdev == st->st_rdev)) return 0;
2043 } else if (S_ISSOCK(st->st_mode)) {
2044 if (S_ISSOCK(ost->st_mode)) return 0;
2046 /* XXX shouldn't do this with commit/undo. */
2048 if (fsm->stage == FSM_PROCESS) rc = fsmNext(fsm, FSM_UNLINK);
2049 if (rc == 0) rc = CPIOERR_ENOENT;
2050 return (rc ? rc : CPIOERR_ENOENT); /* XXX HACK */
2051 /*@notreached@*/ break;
2054 rc = Unlink(fsm->path);
2055 if (_fsm_debug && (stage & FSM_SYSCALL))
2056 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
2057 fsm->path, (rc < 0 ? strerror(errno) : ""));
2059 rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_UNLINK_FAILED);
2062 rc = Rename(fsm->opath, fsm->path);
2063 #if defined(ETXTBSY)
2064 if (rc && errno == ETXTBSY) {
2065 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
2066 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
2068 * XXX HP-UX (and other os'es) don't permit rename to busy
2071 rc = Rename(fsm->path, path);
2072 if (!rc) rc = Rename(fsm->opath, fsm->path);
2075 if (_fsm_debug && (stage & FSM_SYSCALL))
2076 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
2077 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
2078 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
2081 rc = Mkdir(fsm->path, (st->st_mode & 07777));
2082 if (_fsm_debug && (stage & FSM_SYSCALL))
2083 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2084 fsm->path, (unsigned)(st->st_mode & 07777),
2085 (rc < 0 ? strerror(errno) : ""));
2086 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
2089 rc = Rmdir(fsm->path);
2090 if (_fsm_debug && (stage & FSM_SYSCALL))
2091 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
2092 fsm->path, (rc < 0 ? strerror(errno) : ""));
2095 case ENOENT: rc = CPIOERR_ENOENT; /*@switchbreak@*/ break;
2096 case ENOTEMPTY: rc = CPIOERR_ENOTEMPTY; /*@switchbreak@*/ break;
2097 default: rc = CPIOERR_RMDIR_FAILED; /*@switchbreak@*/ break;
2101 rc = chown(fsm->path, st->st_uid, st->st_gid);
2102 if (_fsm_debug && (stage & FSM_SYSCALL))
2103 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
2104 fsm->path, (int)st->st_uid, (int)st->st_gid,
2105 (rc < 0 ? strerror(errno) : ""));
2106 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
2109 #if ! CHOWN_FOLLOWS_SYMLINK
2110 rc = lchown(fsm->path, st->st_uid, st->st_gid);
2111 if (_fsm_debug && (stage & FSM_SYSCALL))
2112 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
2113 fsm->path, (int)st->st_uid, (int)st->st_gid,
2114 (rc < 0 ? strerror(errno) : ""));
2115 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
2119 rc = chmod(fsm->path, (st->st_mode & 07777));
2120 if (_fsm_debug && (stage & FSM_SYSCALL))
2121 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2122 fsm->path, (unsigned)(st->st_mode & 07777),
2123 (rc < 0 ? strerror(errno) : ""));
2124 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
2127 { struct utimbuf stamp;
2128 stamp.actime = st->st_mtime;
2129 stamp.modtime = st->st_mtime;
2130 rc = utime(fsm->path, &stamp);
2131 if (_fsm_debug && (stage & FSM_SYSCALL))
2132 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
2133 fsm->path, (unsigned)st->st_mtime,
2134 (rc < 0 ? strerror(errno) : ""));
2135 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
2139 rc = symlink(fsm->opath, fsm->path);
2140 if (_fsm_debug && (stage & FSM_SYSCALL))
2141 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
2142 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
2143 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
2146 rc = Link(fsm->opath, fsm->path);
2147 if (_fsm_debug && (stage & FSM_SYSCALL))
2148 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
2149 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
2150 if (rc < 0) rc = CPIOERR_LINK_FAILED;
2153 rc = mkfifo(fsm->path, (st->st_mode & 07777));
2154 if (_fsm_debug && (stage & FSM_SYSCALL))
2155 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
2156 fsm->path, (unsigned)(st->st_mode & 07777),
2157 (rc < 0 ? strerror(errno) : ""));
2158 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
2161 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
2162 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
2163 /*@=unrecog =portability @*/
2164 if (_fsm_debug && (stage & FSM_SYSCALL))
2165 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
2166 fsm->path, (unsigned)(st->st_mode & ~07777),
2167 (unsigned)st->st_rdev,
2168 (rc < 0 ? strerror(errno) : ""));
2169 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
2172 rc = Lstat(fsm->path, ost);
2173 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2174 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2175 fsm->path, (rc < 0 ? strerror(errno) : ""));
2177 rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_LSTAT_FAILED);
2180 rc = Stat(fsm->path, ost);
2181 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2182 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2183 fsm->path, (rc < 0 ? strerror(errno) : ""));
2185 rc = (errno == ENOENT ? CPIOERR_ENOENT : CPIOERR_STAT_FAILED);
2188 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
2190 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
2192 if (_fsm_debug && (stage & FSM_SYSCALL))
2193 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
2194 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2195 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
2199 fsm->rdbuf[fsm->rdnb] = '\0';
2208 rc = fsmUNSAFE(fsm, FSM_HREAD);
2210 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2211 fsm->path = _free(fsm->path);
2212 rc = CPIOERR_HDR_TRAILER;
2215 rc = fsmNext(fsm, FSM_POS);
2218 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2219 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2220 rc = fsmNext(fsm, FSM_DREAD);
2222 /*@loopbreak@*/ break;
2226 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2229 (void) fsmNext(fsm, FSM_DREAD);
2233 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2236 memset(fsm->rdbuf, 0, left);
2238 /* XXX DWRITE uses rdnb for I/O length. */
2240 (void) fsmNext(fsm, FSM_DWRITE);
2244 rc = cpioTrailerWrite(fsm);
2247 rc = fsmNext(fsm, FSM_POS);
2249 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2252 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2256 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2258 if (_fsm_debug && (stage & FSM_SYSCALL))
2259 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2260 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2261 (int)fsm->wrlen, (int)fsm->rdnb);
2262 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2263 rc = CPIOERR_READ_FAILED;
2265 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2268 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2269 if (_fsm_debug && (stage & FSM_SYSCALL))
2270 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2271 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2272 (int)fsm->rdnb, (int)fsm->wrnb);
2273 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2274 rc = CPIOERR_WRITE_FAILED;
2276 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2280 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2281 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2282 if (fsm->rfd != NULL) (void) fsmNext(fsm, FSM_RCLOSE);
2284 rc = CPIOERR_OPEN_FAILED;
2287 if (_fsm_debug && (stage & FSM_SYSCALL))
2288 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2289 fsm->path, fsm->rfd, fsm->rdbuf);
2293 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2295 if (_fsm_debug && (stage & FSM_SYSCALL))
2296 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2297 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2298 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2299 rc = CPIOERR_READ_FAILED;
2302 if (fsm->rfd != NULL) {
2303 if (_fsm_debug && (stage & FSM_SYSCALL))
2304 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2305 (void) Fclose(fsm->rfd);
2311 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2312 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2313 if (fsm->wfd != NULL) (void) fsmNext(fsm, FSM_WCLOSE);
2315 rc = CPIOERR_OPEN_FAILED;
2317 if (_fsm_debug && (stage & FSM_SYSCALL))
2318 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2319 fsm->path, fsm->wfd, fsm->wrbuf);
2322 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2323 if (_fsm_debug && (stage & FSM_SYSCALL))
2324 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2325 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2326 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2327 rc = CPIOERR_WRITE_FAILED;
2330 if (fsm->wfd != NULL) {
2331 if (_fsm_debug && (stage & FSM_SYSCALL))
2332 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2333 (void) Fclose(fsm->wfd);
2344 if (!(stage & FSM_INTERNAL)) {
2345 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2352 /*@obserever@*/ const char *const fileActionString(fileAction a)
2355 case FA_UNKNOWN: return "unknown";
2356 case FA_CREATE: return "create";
2357 case FA_COPYOUT: return "copyout";
2358 case FA_COPYIN: return "copyin";
2359 case FA_BACKUP: return "backup";
2360 case FA_SAVE: return "save";
2361 case FA_SKIP: return "skip";
2362 case FA_ALTNAME: return "altname";
2363 case FA_ERASE: return "erase";
2364 case FA_SKIPNSTATE: return "skipnstate";
2365 case FA_SKIPNETSHARED: return "skipnetshared";
2366 case FA_SKIPCOLOR: return "skipcolor";
2367 default: return "???";
2372 /*@observer@*/ const char *const fileStageString(fileStage a) {
2374 case FSM_UNKNOWN: return "unknown";
2376 case FSM_PKGINSTALL:return "INSTALL";
2377 case FSM_PKGERASE: return "ERASE";
2378 case FSM_PKGBUILD: return "BUILD";
2379 case FSM_PKGCOMMIT: return "COMMIT";
2380 case FSM_PKGUNDO: return "UNDO";
2382 case FSM_CREATE: return "create";
2383 case FSM_INIT: return "init";
2384 case FSM_MAP: return "map";
2385 case FSM_MKDIRS: return "mkdirs";
2386 case FSM_RMDIRS: return "rmdirs";
2387 case FSM_PRE: return "pre";
2388 case FSM_PROCESS: return "process";
2389 case FSM_POST: return "post";
2390 case FSM_MKLINKS: return "mklinks";
2391 case FSM_NOTIFY: return "notify";
2392 case FSM_UNDO: return "undo";
2393 case FSM_FINI: return "fini";
2394 case FSM_COMMIT: return "commit";
2395 case FSM_DESTROY: return "destroy";
2396 case FSM_VERIFY: return "verify";
2398 case FSM_UNLINK: return "Unlink";
2399 case FSM_RENAME: return "Rename";
2400 case FSM_MKDIR: return "Mkdir";
2401 case FSM_RMDIR: return "rmdir";
2402 case FSM_CHOWN: return "chown";
2403 case FSM_LCHOWN: return "lchown";
2404 case FSM_CHMOD: return "chmod";
2405 case FSM_UTIME: return "utime";
2406 case FSM_SYMLINK: return "symlink";
2407 case FSM_LINK: return "Link";
2408 case FSM_MKFIFO: return "mkfifo";
2409 case FSM_MKNOD: return "mknod";
2410 case FSM_LSTAT: return "Lstat";
2411 case FSM_STAT: return "Stat";
2412 case FSM_READLINK: return "Readlink";
2413 case FSM_CHROOT: return "chroot";
2415 case FSM_NEXT: return "next";
2416 case FSM_EAT: return "eat";
2417 case FSM_POS: return "pos";
2418 case FSM_PAD: return "pad";
2419 case FSM_TRAILER: return "trailer";
2420 case FSM_HREAD: return "hread";
2421 case FSM_HWRITE: return "hwrite";
2422 case FSM_DREAD: return "Fread";
2423 case FSM_DWRITE: return "Fwrite";
2425 case FSM_ROPEN: return "Fopen";
2426 case FSM_READ: return "Fread";
2427 case FSM_RCLOSE: return "Fclose";
2428 case FSM_WOPEN: return "Fopen";
2429 case FSM_WRITE: return "Fwrite";
2430 case FSM_WCLOSE: return "Fclose";
2432 default: return "???";