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))
22 /* XXX Failure to remove is not (yet) cause for failure. */
24 int strict_erasures = 0;
27 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
28 const FSMI_t iter = fsm->iter;
29 return (iter ? iter->ts : NULL);
32 TFI_t fsmGetFi(const FSM_t fsm)
34 const FSMI_t iter = fsm->iter;
35 return (iter ? iter->fi : NULL);
38 #define SUFFIX_RPMORIG ".rpmorig"
39 #define SUFFIX_RPMSAVE ".rpmsave"
40 #define SUFFIX_RPMNEW ".rpmnew"
43 * Build path to file from file info, ornamented with subdir and suffix.
44 * @param fsm file state machine data
45 * @param st file stat info
46 * @param subdir subdir to use (NULL disables)
47 * @param suffix suffix to use (NULL disables)
48 * @retval path to file
50 static /*@only@*//*@null@*/
51 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
52 /*@null@*/ const struct stat * st,
53 /*@null@*/ const char * subdir,
54 /*@null@*/ const char * suffix)
55 /*@uses fsm->dirName, fsm->baseName */
57 const char * s = NULL;
62 /*@-nullpass@*/ /* LCL: subdir/suffix != NULL */
63 nb = strlen(fsm->dirName) +
64 (st && subdir && !S_ISDIR(st->st_mode) ? strlen(subdir) : 0) +
65 (st && suffix && !S_ISDIR(st->st_mode) ? strlen(suffix) : 0) +
66 strlen(fsm->baseName) + 1;
68 t = stpcpy(t, fsm->dirName);
69 if (st && subdir && !S_ISDIR(st->st_mode))
70 t = stpcpy(t, subdir);
71 t = stpcpy(t, fsm->baseName);
72 if (st && suffix && !S_ISDIR(st->st_mode))
73 t = stpcpy(t, suffix);
80 * Destroy file info iterator.
81 * @param this file info iterator
84 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * this)
86 return _free((void *)this);
90 * Create file info iterator.
91 * @param this transaction set
92 * @param that transaction element file info
93 * @return file info iterator
96 mapInitIterator(/*@kept@*/ const void * this, /*@kept@*/ const void * that)
98 rpmTransactionSet ts = (void *)this;
99 TFI_t fi = (void *)that;
102 iter = xcalloc(1, sizeof(*iter));
105 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
106 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
107 iter->isave = iter->i;
112 * Return next index into file info.
113 * @param this file info iterator
114 * @return next index, -1 on termination
116 static int mapNextIterator(void * this) {
118 const TFI_t fi = iter->fi;
122 if (iter->i >= 0) i = iter->i--;
124 if (iter->i < fi->fc) i = iter->i++;
132 static int cpioStrCmp(const void * a, const void * b)
135 const char * afn = *(const char **)a;
136 const char * bfn = *(const char **)b;
138 /* Match rpm-4.0 payloads with ./ prefixes. */
139 if (afn[0] == '.' && afn[1] == '/') afn += 2;
140 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
142 /* If either path is absolute, make it relative. */
143 if (afn[0] == '/') afn += 1;
144 if (bfn[0] == '/') bfn += 1;
146 return strcmp(afn, bfn);
150 * Locate archive path in file info.
151 * @param this file info iterator
152 * @param fsmPath archive path
153 * @return index into file info, -1 if archive path was not found
155 static int mapFind(void * this, const char * fsmPath)
159 const TFI_t fi = iter->fi;
162 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
165 /*@-nullpass@*/ /* LCL: fi->apath != NULL */
166 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath), cpioStrCmp);
169 fprintf(stderr, "*** not mapped %s\n", fsmPath);
171 iter->i = p - fi->apath;
172 ix = mapNextIterator(iter);
179 * Directory name iterator.
181 typedef struct dnli_s {
182 /*@dependent@*/ TFI_t fi;
183 /*@only@*/ /*@null@*/ char * active;
190 * Destroy directory name iterator.
191 * @param this file info iterator
192 * @retval NULL always
194 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * this)
197 DNLI_t dnli = (void *)this;
198 if (dnli->active) free(dnli->active);
205 static inline int dnlCount(const DNLI_t dnli) {
206 return (dnli ? dnli->fi->dc : 0);
211 static inline int dnlIndex(const DNLI_t dnli) {
212 return (dnli ? dnli->isave : -1);
216 * Create directory name iterator.
217 * @param fsm file state machine data
218 * @param reverse traverse directory names in reverse order?
219 * @return directory name iterator
221 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
223 /*@uses fsm->iter @*/
225 TFI_t fi = fsmGetFi(fsm);
231 dnli = xcalloc(1, sizeof(*dnli));
233 dnli->reverse = reverse;
234 dnli->i = (reverse ? fi->dc : 0);
237 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
239 /* Identify parent directories not skipped. */
240 for (i = 0; i < fi->fc; i++)
241 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
243 /* Exclude parent directories that are explicitly included. */
244 for (i = 0; i < fi->fc; i++) {
245 int dil, dnlen, bnlen;
247 if (!S_ISDIR(fi->fmodes[i]))
251 dnlen = strlen(fi->dnl[dil]);
252 bnlen = strlen(fi->bnl[i]);
254 for (j = 0; j < fi->dc; j++) {
258 if (!dnli->active[j] || j == dil) continue;
261 if (jlen != (dnlen+bnlen+1)) continue;
262 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
263 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
264 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
266 /* This directory is included in the package. */
272 /* Print only once per package. */
275 for (i = 0; i < fi->dc; i++) {
276 if (!dnli->active[i]) continue;
279 rpmMessage(RPMMESS_DEBUG,
280 _("========= Directories not explictly included in package:\n"));
282 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
285 rpmMessage(RPMMESS_DEBUG, "=========\n");
292 * Return next directory name (from file info).
293 * @param dnli directory name iterator
294 * @return next directory name
296 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
298 const char * dn = NULL;
306 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
307 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
309 if (i >= 0 && i < fi->dc)
319 * Save hard link in chain.
320 * @param fsm file state machine data
321 * @return Is chain only partially filled?
324 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
325 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
326 /*@defines fsm->li @*/
327 /*@releases fsm->path @*/
329 struct stat * st = &fsm->sb;
334 /* Find hard link set. */
335 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
336 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
340 /* New hard link encountered, add new link to set. */
341 if (fsm->li == NULL) {
342 fsm->li = xcalloc(1, sizeof(*fsm->li));
343 fsm->li->next = NULL;
344 fsm->li->nlink = st->st_nlink;
345 fsm->li->dev = st->st_dev;
346 fsm->li->inode = st->st_ino;
347 fsm->li->linkIndex = -1;
348 fsm->li->createdPath = -1;
350 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
351 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
352 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
354 if (fsm->goal == FSM_PKGBUILD)
355 fsm->li->linksLeft = st->st_nlink;
356 if (fsm->goal == FSM_PKGINSTALL)
357 fsm->li->linksLeft = 0;
360 fsm->li->next = fsm->links;
362 fsm->links = fsm->li;
365 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
366 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
367 /*@-observertrans -dependenttrans@*/
368 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
369 /*@=observertrans =dependenttrans@*/
370 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
373 fprintf(stderr, "*** %p link[%d:%d] %d filex %d %s\n", fsm->li, fsm->li->linksLeft, st->st_nlink, (int)st->st_size, fsm->li->filex[fsm->li->linksLeft], fsm->li->files[fsm->li->linksLeft]);
376 if (fsm->goal == FSM_PKGBUILD)
377 return (fsm->li->linksLeft > 0);
379 if (fsm->goal != FSM_PKGINSTALL)
382 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
385 /* Here come the bits, time to choose a non-skipped file name. */
386 { TFI_t fi = fsmGetFi(fsm);
388 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
389 ix = fsm->li->filex[j];
390 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
396 /* Are all links skipped or not encountered yet? */
398 return 1; /* XXX W2DO? */
400 /* Save the non-skipped file name and map index. */
401 fsm->li->linkIndex = j;
402 fsm->path = _free(fsm->path);
404 rc = fsmStage(fsm, FSM_MAP);
405 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
412 * Destroy set of hard links.
413 * @param li set of hard links
415 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
418 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
419 li->filex = _free(li->filex);
426 FSM_t fsm = xcalloc(1, sizeof(*fsm));
430 FSM_t freeFSM(FSM_t fsm)
433 fsm->path = _free(fsm->path);
434 while ((fsm->li = fsm->links) != NULL) {
435 fsm->links = fsm->li->next;
436 fsm->li->next = NULL;
437 fsm->li = freeHardLink(fsm->li);
439 fsm->dnlx = _free(fsm->dnlx);
440 fsm->ldn = _free(fsm->ldn);
441 fsm->iter = mapFreeIterator(fsm->iter);
446 int fsmSetup(FSM_t fsm, fileStage goal,
447 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
448 unsigned int * archiveSize, const char ** failedFile)
455 fsm->cfd = fdLink(cfd, "persist (fsm)");
456 pos = fdGetCpioPos(fsm->cfd);
457 fdSetCpioPos(fsm->cfd, 0);
459 fsm->iter = mapInitIterator(ts, fi);
461 if (fsm->goal == FSM_PKGINSTALL) {
462 if (ts && ts->notify) {
463 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
464 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
468 fsm->archiveSize = archiveSize;
469 if (fsm->archiveSize)
470 *fsm->archiveSize = 0;
471 fsm->failedFile = failedFile;
473 *fsm->failedFile = NULL;
475 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
476 if (fsm->goal == FSM_PKGINSTALL) {
477 if (ts && ts->id > 0)
478 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
482 rc = fsmStage(fsm, FSM_CREATE);
484 rc = fsmStage(fsm, fsm->goal);
486 if (fsm->archiveSize && rc == 0)
487 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
492 int fsmTeardown(FSM_t fsm) {
496 rc = fsmStage(fsm, FSM_DESTROY);
498 fsm->iter = mapFreeIterator(fsm->iter);
500 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
503 fsm->failedFile = NULL;
504 /*@-nullstate@*/ /* FIX: fsm->iter null annotation? */
509 int fsmMapPath(FSM_t fsm)
511 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
518 fsm->action = FA_UNKNOWN;
522 if (fi && i >= 0 && i < fi->fc) {
524 fsm->astriplen = fi->astriplen;
525 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
526 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
527 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
529 /* src rpms have simple base name in payload. */
530 fsm->dirName = fi->dnl[fi->dil[i]];
531 fsm->baseName = fi->bnl[i];
533 switch (fsm->action) {
536 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
537 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
540 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
547 assert(fi->type == TR_ADDED);
551 if (fi->fstates && fi->type == TR_ADDED)
552 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
555 case FA_SKIPNETSHARED:
556 if (fi->fstates && fi->type == TR_ADDED)
557 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
563 fsm->osuffix = SUFFIX_RPMORIG;
566 fsm->osuffix = SUFFIX_RPMSAVE;
572 assert(fi->type == TR_ADDED);
573 fsm->nsuffix = SUFFIX_RPMNEW;
577 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
578 assert(fi->type == TR_ADDED);
579 fsm->osuffix = SUFFIX_RPMSAVE;
582 assert(fi->type == TR_REMOVED);
585 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
589 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
590 const struct stat * st = &fsm->sb;
591 fsm->path = _free(fsm->path);
592 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
593 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
594 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
601 int fsmMapAttrs(FSM_t fsm)
603 struct stat * st = &fsm->sb;
604 TFI_t fi = fsmGetFi(fsm);
607 if (fi && i >= 0 && i < fi->fc) {
609 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
611 (fi->fmodes ? fi->fmodes[i] : perms);
613 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
615 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
617 if (fsm->mapFlags & CPIO_MAP_MODE)
618 st->st_mode = (st->st_mode & S_IFMT) | finalMode;
619 if (fsm->mapFlags & CPIO_MAP_UID)
620 st->st_uid = finalUid;
621 if (fsm->mapFlags & CPIO_MAP_GID)
622 st->st_gid = finalGid;
624 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
631 * Create file from payload stream.
632 * @todo Legacy: support brokenEndian MD5 checks?
633 * @param fsm file state machine data
634 * @return 0 on success
636 static int expandRegular(/*@special@*/ FSM_t fsm)
639 const char * fmd5sum;
640 const struct stat * st = &fsm->sb;
641 int left = st->st_size;
644 rc = fsmStage(fsm, FSM_WOPEN);
648 /* XXX md5sum's will break on repackaging that includes modified files. */
649 fmd5sum = fsm->fmd5sum;
651 /* XXX This doesn't support brokenEndian checks. */
652 if (st->st_size > 0 && fmd5sum)
653 fdInitMD5(fsm->wfd, 0);
657 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
658 rc = fsmStage(fsm, FSM_DREAD);
662 rc = fsmStage(fsm, FSM_WRITE);
668 /* don't call this with fileSize == fileComplete */
670 (void) fsmStage(fsm, FSM_NOTIFY);
673 if (st->st_size > 0 && fmd5sum) {
674 const char * md5sum = NULL;
676 (void) Fflush(fsm->wfd);
677 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
679 if (md5sum == NULL) {
680 rc = CPIOERR_MD5SUM_MISMATCH;
682 if (strcmp(md5sum, fmd5sum))
683 rc = CPIOERR_MD5SUM_MISMATCH;
684 md5sum = _free(md5sum);
689 (void) fsmStage(fsm, FSM_WCLOSE);
694 * Write next item to payload stream.
695 * @param fsm file state machine data
696 * @param writeData should data be written?
697 * @return 0 on success
699 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
700 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
702 const char * path = fsm->path;
703 const char * opath = fsm->opath;
704 struct stat * st = &fsm->sb;
705 struct stat * ost = &fsm->osb;
706 size_t pos = fdGetCpioPos(fsm->cfd);
707 char * symbuf = NULL;
711 st->st_size = (writeData ? ost->st_size : 0);
712 if (S_ISDIR(st->st_mode)) {
714 } else if (S_ISLNK(st->st_mode)) {
716 * While linux puts the size of a symlink in the st_size field,
717 * I don't think that's a specified standard.
719 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
720 rc = fsmStage(fsm, FSM_READLINK);
722 st->st_size = fsm->rdnb;
723 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
726 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
727 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
728 char * t = alloca(nb);
731 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
733 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
734 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
735 TFI_t fi = fsmGetFi(fsm);
737 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
740 rc = fsmStage(fsm, FSM_HWRITE);
744 if (writeData && S_ISREG(st->st_mode)) {
747 void * mapped = (void *)-1;
751 rc = fsmStage(fsm, FSM_ROPEN);
754 /* XXX unbuffered mmap generates *lots* of fdio debugging */
757 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
758 if (mapped != (void *)-1) {
760 fsm->rdbuf = (char *) mapped;
761 fsm->rdlen = nmapped = st->st_size;
769 if (mapped != (void *)-1) {
774 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
775 rc = fsmStage(fsm, FSM_READ);
779 /* XXX DWRITE uses rdnb for I/O length. */
780 rc = fsmStage(fsm, FSM_DWRITE);
787 if (mapped != (void *)-1) {
788 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
793 } else if (writeData && S_ISLNK(st->st_mode)) {
794 /* XXX DWRITE uses rdnb for I/O length. */
795 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
796 fsm->rdnb = strlen(symbuf);
797 rc = fsmStage(fsm, FSM_DWRITE);
801 rc = fsmStage(fsm, FSM_PAD);
804 { const rpmTransactionSet ts = fsmGetTs(fsm);
805 TFI_t fi = fsmGetFi(fsm);
806 if (ts && fi && ts->notify) {
807 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
808 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
809 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
817 (void) fsmStage(fsm, FSM_RCLOSE);
818 /*@-dependenttrans@*/
821 /*@=dependenttrans@*/
826 * Write set of linked files to payload stream.
827 * @param fsm file state machine data
828 * @return 0 on success
830 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
831 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
833 const char * path = fsm->path;
834 const char * nsuffix = fsm->nsuffix;
835 int iterIndex = fsm->ix;
844 for (i = fsm->li->nlink - 1; i >= 0; i--) {
845 if (fsm->li->filex[i] < 0) continue;
847 fsm->ix = fsm->li->filex[i];
848 rc = fsmStage(fsm, FSM_MAP);
850 /* Write data after last link. */
851 rc = writeFile(fsm, (i == 0));
852 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
854 *fsm->failedFile = xstrdup(fsm->path);
857 fsm->path = _free(fsm->path);
858 fsm->li->filex[i] = -1;
862 fsm->nsuffix = nsuffix;
868 * Create pending hard links to existing file.
869 * @param fsm file state machine data
870 * @return 0 on success
872 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
873 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
875 const char * path = fsm->path;
876 const char * opath = fsm->opath;
877 const char * nsuffix = fsm->nsuffix;
878 int iterIndex = fsm->ix;
888 fsm->ix = fsm->li->filex[fsm->li->createdPath];
889 rc = fsmStage(fsm, FSM_MAP);
890 fsm->opath = fsm->path;
892 for (i = 0; i < fsm->li->nlink; i++) {
893 if (fsm->li->filex[i] < 0) continue;
894 if (i == fsm->li->createdPath) continue;
896 fsm->ix = fsm->li->filex[i];
897 rc = fsmStage(fsm, FSM_MAP);
898 rc = fsmStage(fsm, FSM_VERIFY);
900 if (rc != CPIOERR_LSTAT_FAILED) break;
902 /* XXX link(fsm->opath, fsm->path) */
903 rc = fsmStage(fsm, FSM_LINK);
904 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
906 *fsm->failedFile = xstrdup(fsm->path);
909 fsm->li->linksLeft--;
911 fsm->opath = _free(fsm->opath);
914 fsm->nsuffix = nsuffix;
921 * Commit hard linked file set atomically.
922 * @param fsm file state machine data
923 * @return 0 on success
925 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
926 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
927 fsm->li, fsm->links @*/
929 const char * path = fsm->path;
930 const char * nsuffix = fsm->nsuffix;
931 int iterIndex = fsm->ix;
932 struct stat * st = &fsm->sb;
940 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
941 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
945 for (i = 0; i < fsm->li->nlink; i++) {
946 if (fsm->li->filex[i] < 0) continue;
947 fsm->ix = fsm->li->filex[i];
948 rc = fsmStage(fsm, FSM_MAP);
949 rc = fsmStage(fsm, FSM_COMMIT);
950 fsm->path = _free(fsm->path);
951 fsm->li->filex[i] = -1;
955 fsm->nsuffix = nsuffix;
961 * Remove (if created) directories not explicitly included in package.
962 * @param fsm file state machine data
963 * @return 0 on success
966 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
967 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
969 const char * path = fsm->path;
970 void * dnli = dnlInitIterator(fsm, 1);
971 char * dn = fsm->rdbuf;
972 int dc = dnlCount(dnli);
977 /*@-observertrans -dependenttrans@*/
978 if (fsm->ldn != NULL && fsm->dnlx != NULL)
979 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
980 int dnlen = strlen(fsm->path);
984 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
987 /* Copy to avoid const on fsm->path. */
988 te = stpcpy(dn, fsm->path) - 1;
991 /* Remove generated directories. */
995 rc = fsmStage(fsm, FSM_RMDIR);
1000 } while ((te - dn) > fsm->dnlx[dc]);
1002 dnli = dnlFreeIterator(dnli);
1003 /*@=observertrans =dependenttrans@*/
1011 * Create (if necessary) directories not explicitly included in package.
1012 * @param fsm file state machine data
1013 * @return 0 on success
1015 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1016 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1017 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1018 /*@defines fsm->dnlx, fsm->ldn @*/
1020 struct stat * st = &fsm->sb;
1021 struct stat * ost = &fsm->osb;
1022 const char * path = fsm->path;
1023 mode_t st_mode = st->st_mode;
1024 void * dnli = dnlInitIterator(fsm, 0);
1025 char * dn = fsm->rdbuf;
1026 int dc = dnlCount(dnli);
1033 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1034 /*@-observertrans -dependenttrans@*/
1035 if (fsm->dnlx != NULL)
1036 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1037 int dnlen = strlen(fsm->path);
1040 dc = dnlIndex(dnli);
1041 if (dc < 0) continue;
1042 fsm->dnlx[dc] = dnlen;
1046 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1047 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1049 /*@=compdef =nullpass@*/
1051 /* Copy to avoid const on fsm->path. */
1052 (void) stpcpy(dn, fsm->path);
1055 /* Assume '/' directory exists, otherwise "mkdir -p" if non-existent. */
1056 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1057 if (*te != '/') continue;
1061 /* Already validated? */
1062 /*@-usedef -compdef -nullpass -nullderef@*/
1063 if (i < fsm->ldnlen &&
1064 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1065 !strncmp(fsm->path, fsm->ldn, i))
1068 /* Move pre-existing path marker forward. */
1069 fsm->dnlx[dc] = (te - dn);
1072 /*@=usedef =compdef =nullpass =nullderef@*/
1074 /* Validate next component of path. */
1075 rc = fsmStage(fsm, FSM_LSTAT);
1078 /* Directory already exists? */
1079 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1080 /* Move pre-existing path marker forward. */
1081 fsm->dnlx[dc] = (te - dn);
1082 } else if (rc == CPIOERR_LSTAT_FAILED) {
1083 TFI_t fi = fsmGetFi(fsm);
1085 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1086 rc = fsmStage(fsm, FSM_MKDIR);
1088 rpmMessage(RPMMESS_WARNING,
1089 _("%s directory created with perms %04o.\n"),
1090 fsm->path, (unsigned)(st->st_mode & 07777));
1097 /* Save last validated path. */
1098 if (fsm->ldnalloc < (dnlen + 1)) {
1099 fsm->ldnalloc = dnlen + 100;
1100 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1102 /*@-nullpass@*/ /* FIX: fsm->ldn NULL. */
1103 strcpy(fsm->ldn, fsm->path);
1105 fsm->ldnlen = dnlen;
1107 dnli = dnlFreeIterator(dnli);
1108 /*@=observertrans =dependenttrans@*/
1111 st->st_mode = st_mode; /* XXX restore st->st_mode */
1117 int fsmStage(FSM_t fsm, fileStage stage)
1120 fileStage prevStage = fsm->stage;
1121 const char * const prev = fileStageString(prevStage);
1123 static int modulo = 4;
1124 const char * const cur = fileStageString(stage);
1125 struct stat * st = &fsm->sb;
1126 struct stat * ost = &fsm->osb;
1127 int saveerrno = errno;
1132 #define _fafilter(_a) \
1133 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1134 ? fileActionString(_a) : "")
1136 if (stage & FSM_DEAD) {
1138 } else if (stage & FSM_INTERNAL) {
1139 if (_fsm_debug && !(stage & FSM_SYSCALL))
1140 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1142 (unsigned)st->st_mode, (int)st->st_nlink,
1143 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1144 (fsm->path ? fsm->path : ""),
1145 _fafilter(fsm->action));
1148 if (_fsm_debug || !(stage & FSM_VERBOSE))
1149 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1151 (unsigned)st->st_mode, (int)st->st_nlink,
1152 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1153 (fsm->path ? fsm->path + fsm->astriplen : ""),
1154 _fafilter(fsm->action));
1161 case FSM_PKGINSTALL:
1163 /* Clean fsm, free'ing memory. Read next archive header. */
1164 rc = fsmStage(fsm, FSM_INIT);
1166 /* Exit on end-of-payload. */
1167 if (rc == CPIOERR_HDR_TRAILER) {
1172 /* Exit on error. */
1175 (void) fsmStage(fsm, FSM_UNDO);
1179 /* Extract file from archive. */
1180 rc = fsmStage(fsm, FSM_PROCESS);
1182 (void) fsmStage(fsm, FSM_UNDO);
1186 /* Notify on success. */
1187 (void) fsmStage(fsm, FSM_NOTIFY);
1189 if (fsmStage(fsm, FSM_FINI))
1196 /* Clean fsm, free'ing memory. */
1197 rc = fsmStage(fsm, FSM_INIT);
1199 /* Exit on end-of-payload. */
1200 if (rc == CPIOERR_HDR_TRAILER) {
1205 /* Rename/erase next item. */
1206 if (fsmStage(fsm, FSM_FINI))
1213 rc = fsmStage(fsm, FSM_INIT);
1215 /* Exit on end-of-payload. */
1216 if (rc == CPIOERR_HDR_TRAILER) {
1221 /* Exit on error. */
1224 (void) fsmStage(fsm, FSM_UNDO);
1228 /* Copy file into archive. */
1229 rc = fsmStage(fsm, FSM_PROCESS);
1231 (void) fsmStage(fsm, FSM_UNDO);
1235 if (fsmStage(fsm, FSM_FINI))
1240 rc = fsmStage(fsm, FSM_TRAILER);
1243 { rpmTransactionSet ts = fsmGetTs(fsm);
1244 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1245 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1246 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1249 fsm->path = _free(fsm->path);
1250 fsm->opath = _free(fsm->opath);
1251 fsm->dnlx = _free(fsm->dnlx);
1253 fsm->ldn = _free(fsm->ldn);
1254 fsm->ldnalloc = fsm->ldnlen = 0;
1256 fsm->rdsize = fsm->wrsize = 0;
1257 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1258 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1259 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1260 fsm->rdsize = 8 * BUFSIZ;
1261 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1262 fsm->wrsize = 8 * BUFSIZ;
1263 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1266 fsm->mkdirsdone = 0;
1270 errno = 0; /* XXX get rid of EBADF */
1272 /* Detect and create directories not explicitly in package. */
1273 if (fsm->goal == FSM_PKGINSTALL) {
1274 rc = fsmStage(fsm, FSM_MKDIRS);
1275 if (!rc) fsm->mkdirsdone = 1;
1280 fsm->path = _free(fsm->path);
1282 fsm->diskchecked = fsm->exists = 0;
1284 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1285 fsm->action = FA_UNKNOWN;
1286 fsm->osuffix = NULL;
1287 fsm->nsuffix = NULL;
1289 if (fsm->goal == FSM_PKGINSTALL) {
1290 /* Read next header from payload, checking for end-of-payload. */
1291 rc = fsmStage(fsm, FSM_NEXT);
1295 /* Identify mapping index. */
1296 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1297 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1299 /* On non-install, detect end-of-loop. */
1300 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1301 rc = CPIOERR_HDR_TRAILER;
1305 /* On non-install, mode must be known so that dirs don't get suffix. */
1306 if (fsm->goal != FSM_PKGINSTALL) {
1307 TFI_t fi = fsmGetFi(fsm);
1308 st->st_mode = fi->fmodes[fsm->ix];
1311 /* Generate file path. */
1312 rc = fsmStage(fsm, FSM_MAP);
1315 /* Perform lstat/stat for disk file. */
1316 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1317 ? FSM_LSTAT : FSM_STAT));
1318 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1322 } else if (rc == 0) {
1325 fsm->diskchecked = 1;
1328 /* On non-install, the disk file stat is what's remapped. */
1329 if (fsm->goal != FSM_PKGINSTALL)
1330 *st = *ost; /* structure assignment */
1332 /* Remap file perms, owner, and group. */
1333 rc = fsmMapAttrs(fsm);
1336 fsm->postpone = XFA_SKIPPING(fsm->action);
1337 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1338 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1339 fsm->postpone = saveHardLink(fsm);
1345 rc = fsmMapPath(fsm);
1348 rc = fsmMkdirs(fsm);
1352 rc = fsmRmdirs(fsm);
1355 if (fsm->postpone) {
1356 if (fsm->goal == FSM_PKGINSTALL)
1357 rc = fsmStage(fsm, FSM_EAT);
1361 if (fsm->goal == FSM_PKGBUILD) {
1362 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1363 struct hardLink * li, * prev;
1364 rc = writeLinkedFile(fsm);
1365 if (rc) break; /* W2DO? */
1367 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1368 if (li == fsm->li) break;
1371 fsm->links = fsm->li->next;
1373 prev->next = fsm->li->next;
1374 fsm->li->next = NULL;
1375 fsm->li = freeHardLink(fsm->li);
1377 rc = writeFile(fsm, 1);
1382 if (fsm->goal != FSM_PKGINSTALL)
1385 if (S_ISREG(st->st_mode)) {
1386 const char * path = fsm->path;
1388 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1389 rc = fsmStage(fsm, FSM_VERIFY);
1391 if (rc == 0 && fsm->osuffix) {
1392 const char * opath = fsm->opath;
1393 fsm->opath = fsm->path;
1394 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1395 rc = fsmStage(fsm, FSM_RENAME);
1397 rpmMessage(RPMMESS_WARNING,
1398 _("%s saved as %s\n"), fsm->opath, fsm->path);
1399 fsm->path = _free(fsm->path);
1403 /*@-dependenttrans@*/
1405 /*@=dependenttrans@*/
1406 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1407 rc = expandRegular(fsm);
1408 } else if (S_ISDIR(st->st_mode)) {
1409 mode_t st_mode = st->st_mode;
1410 rc = fsmStage(fsm, FSM_VERIFY);
1411 if (rc == CPIOERR_LSTAT_FAILED) {
1412 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1413 st->st_mode |= 00700;
1414 rc = fsmStage(fsm, FSM_MKDIR);
1415 st->st_mode = st_mode; /* XXX restore st->st_mode */
1417 } else if (S_ISLNK(st->st_mode)) {
1418 const char * opath = fsm->opath;
1420 if ((st->st_size + 1) > fsm->rdsize) {
1421 rc = CPIOERR_HDR_SIZE;
1425 fsm->wrlen = st->st_size;
1426 rc = fsmStage(fsm, FSM_DREAD);
1427 if (!rc && fsm->rdnb != fsm->wrlen)
1428 rc = CPIOERR_READ_FAILED;
1431 fsm->wrbuf[st->st_size] = '\0';
1432 /* XXX symlink(fsm->opath, fsm->path) */
1433 /*@-dependenttrans@*/
1434 fsm->opath = fsm->wrbuf;
1435 /*@=dependenttrans@*/
1436 rc = fsmStage(fsm, FSM_VERIFY);
1437 if (rc == CPIOERR_LSTAT_FAILED)
1438 rc = fsmStage(fsm, FSM_SYMLINK);
1439 fsm->opath = opath; /* XXX restore fsm->path */
1440 } else if (S_ISFIFO(st->st_mode)) {
1441 mode_t st_mode = st->st_mode;
1442 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1443 rc = fsmStage(fsm, FSM_VERIFY);
1444 if (rc == CPIOERR_LSTAT_FAILED) {
1445 st->st_mode = 0000; /* XXX abuse st->st_mode */
1446 rc = fsmStage(fsm, FSM_MKFIFO);
1447 st->st_mode = st_mode; /* XXX restore st->st_mode */
1449 } else if (S_ISCHR(st->st_mode) ||
1450 S_ISBLK(st->st_mode) ||
1451 S_ISSOCK(st->st_mode))
1453 rc = fsmStage(fsm, FSM_VERIFY);
1454 if (rc == CPIOERR_LSTAT_FAILED)
1455 rc = fsmStage(fsm, FSM_MKNOD);
1457 rc = CPIOERR_UNKNOWN_FILETYPE;
1459 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1460 fsm->li->createdPath = fsm->li->linkIndex;
1461 rc = fsmMakeLinks(fsm);
1467 rc = fsmMakeLinks(fsm);
1469 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1470 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1471 rpmTransactionSet ts = fsmGetTs(fsm);
1472 TFI_t fi = fsmGetFi(fsm);
1473 if (ts && ts->notify && fi)
1474 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1475 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1476 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1482 if (fsm->goal == FSM_PKGINSTALL) {
1483 (void) fsmStage(fsm,
1484 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1486 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1488 (void) fsmStage(fsm, FSM_RMDIRS);
1492 if (fsm->failedFile && *fsm->failedFile == NULL)
1493 *fsm->failedFile = xstrdup(fsm->path);
1496 if (!fsm->postpone && fsm->commit) {
1497 if (fsm->goal == FSM_PKGINSTALL)
1498 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1499 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1500 if (fsm->goal == FSM_PKGCOMMIT)
1501 rc = fsmStage(fsm, FSM_COMMIT);
1502 if (fsm->goal == FSM_PKGERASE)
1503 rc = fsmStage(fsm, FSM_COMMIT);
1505 fsm->path = _free(fsm->path);
1506 fsm->opath = _free(fsm->opath);
1507 memset(st, 0, sizeof(*st));
1508 memset(ost, 0, sizeof(*ost));
1511 /* Rename pre-existing modified or unmanaged file. */
1512 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1513 const char * opath = fsm->opath;
1514 const char * path = fsm->path;
1515 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1516 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1518 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1519 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1521 rc = fsmStage(fsm, FSM_RENAME);
1523 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1524 fsm->opath, fsm->path);
1526 fsm->path = _free(fsm->path);
1528 fsm->opath = _free(fsm->opath);
1532 /* Remove erased files. */
1533 if (fsm->goal == FSM_PKGERASE) {
1534 if (fsm->action == FA_ERASE) {
1535 TFI_t fi = fsmGetFi(fsm);
1536 if (S_ISDIR(st->st_mode)) {
1537 rc = fsmStage(fsm, FSM_RMDIR);
1540 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1542 /* XXX make sure that build side permits %missingok on directories. */
1543 if (fsm->fflags & RPMFILE_MISSINGOK)
1546 /* XXX common error message. */
1548 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1549 _("%s rmdir of %s failed: Directory not empty\n"),
1550 fiTypeString(fi), fsm->path);
1554 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1555 _("%s rmdir of %s failed: %s\n"),
1556 fiTypeString(fi), fsm->path, strerror(errno));
1560 rc = fsmStage(fsm, FSM_UNLINK);
1562 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1564 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1565 _("%s unlink of %s failed: %s\n"),
1566 fiTypeString(fi), fsm->path, strerror(errno));
1569 /* XXX Failure to remove is not (yet) cause for failure. */
1570 if (!strict_erasures) rc = 0;
1574 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1575 /* Rename temporary to final file name. */
1576 if (!S_ISDIR(st->st_mode) &&
1577 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1579 fsm->opath = fsm->path;
1580 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1581 rc = fsmStage(fsm, FSM_RENAME);
1582 if (!rc && fsm->nsuffix) {
1583 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1584 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1585 (opath ? opath : ""), fsm->path);
1586 opath = _free(opath);
1588 fsm->opath = _free(fsm->opath);
1590 if (S_ISLNK(st->st_mode)) {
1591 if (!rc && !getuid())
1592 rc = fsmStage(fsm, FSM_LCHOWN);
1594 if (!rc && !getuid())
1595 rc = fsmStage(fsm, FSM_CHOWN);
1597 rc = fsmStage(fsm, FSM_CHMOD);
1599 time_t st_mtime = st->st_mtime;
1600 TFI_t fi = fsmGetFi(fsm);
1602 st->st_mtime = fi->fmtimes[fsm->ix];
1603 rc = fsmStage(fsm, FSM_UTIME);
1604 st->st_mtime = st_mtime;
1609 /* Notify on success. */
1610 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1613 fsm->path = _free(fsm->path);
1615 /* Create any remaining links (if no error), and clean up. */
1616 while ((fsm->li = fsm->links) != NULL) {
1617 fsm->links = fsm->li->next;
1618 fsm->li->next = NULL;
1619 if (fsm->goal == FSM_PKGINSTALL && fsm->commit && fsm->li->linksLeft)
1621 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1622 if (fsm->li->filex[i] < 0) continue;
1623 rc = CPIOERR_MISSING_HARDLINK;
1624 if (fsm->failedFile && *fsm->failedFile == NULL) {
1625 fsm->ix = fsm->li->filex[i];
1626 if (!fsmStage(fsm, FSM_MAP)) {
1627 *fsm->failedFile = fsm->path;
1634 if (fsm->goal == FSM_PKGBUILD) {
1635 rc = CPIOERR_MISSING_HARDLINK;
1637 fsm->li = freeHardLink(fsm->li);
1639 fsm->ldn = _free(fsm->ldn);
1640 fsm->ldnalloc = fsm->ldnlen = 0;
1641 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1642 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1645 if (fsm->diskchecked && !fsm->exists) {
1646 rc = CPIOERR_LSTAT_FAILED;
1649 if (S_ISREG(st->st_mode)) {
1650 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1651 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1653 * XXX HP-UX (and other os'es) don't permit unlink on busy
1656 fsm->opath = fsm->path;
1658 rc = fsmStage(fsm, FSM_RENAME);
1660 (void) fsmStage(fsm, FSM_UNLINK);
1662 rc = CPIOERR_UNLINK_FAILED;
1663 fsm->path = fsm->opath;
1665 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1666 /*@notreached@*/ break;
1667 } else if (S_ISDIR(st->st_mode)) {
1668 if (S_ISDIR(ost->st_mode)) return 0;
1669 if (S_ISLNK(ost->st_mode)) {
1670 rc = fsmStage(fsm, FSM_STAT);
1671 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1674 if (S_ISDIR(ost->st_mode)) return 0;
1676 } else if (S_ISLNK(st->st_mode)) {
1677 if (S_ISLNK(ost->st_mode)) {
1678 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1679 rc = fsmStage(fsm, FSM_READLINK);
1682 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1684 } else if (S_ISFIFO(st->st_mode)) {
1685 if (S_ISFIFO(ost->st_mode)) return 0;
1686 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1687 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1688 (ost->st_rdev == st->st_rdev)) return 0;
1689 } else if (S_ISSOCK(st->st_mode)) {
1690 if (S_ISSOCK(ost->st_mode)) return 0;
1692 /* XXX shouldn't do this with commit/undo. */
1694 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1695 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1696 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1697 /*@notreached@*/ break;
1700 rc = Unlink(fsm->path);
1701 if (_fsm_debug && (stage & FSM_SYSCALL))
1702 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1703 fsm->path, (rc < 0 ? strerror(errno) : ""));
1704 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1707 rc = Rename(fsm->opath, fsm->path);
1708 if (_fsm_debug && (stage & FSM_SYSCALL))
1709 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1710 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1711 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1714 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1715 if (_fsm_debug && (stage & FSM_SYSCALL))
1716 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1717 fsm->path, (unsigned)(st->st_mode & 07777),
1718 (rc < 0 ? strerror(errno) : ""));
1719 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1722 rc = Rmdir(fsm->path);
1723 if (_fsm_debug && (stage & FSM_SYSCALL))
1724 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1725 fsm->path, (rc < 0 ? strerror(errno) : ""));
1726 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1729 rc = chown(fsm->path, st->st_uid, st->st_gid);
1730 if (_fsm_debug && (stage & FSM_SYSCALL))
1731 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1732 fsm->path, (int)st->st_uid, (int)st->st_gid,
1733 (rc < 0 ? strerror(errno) : ""));
1734 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1737 #if ! CHOWN_FOLLOWS_SYMLINK
1738 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1739 if (_fsm_debug && (stage & FSM_SYSCALL))
1740 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1741 fsm->path, (int)st->st_uid, (int)st->st_gid,
1742 (rc < 0 ? strerror(errno) : ""));
1743 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1747 rc = chmod(fsm->path, (st->st_mode & 07777));
1748 if (_fsm_debug && (stage & FSM_SYSCALL))
1749 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1750 fsm->path, (unsigned)(st->st_mode & 07777),
1751 (rc < 0 ? strerror(errno) : ""));
1752 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1755 { struct utimbuf stamp;
1756 stamp.actime = st->st_mtime;
1757 stamp.modtime = st->st_mtime;
1758 rc = utime(fsm->path, &stamp);
1759 if (_fsm_debug && (stage & FSM_SYSCALL))
1760 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1761 fsm->path, (unsigned)st->st_mtime,
1762 (rc < 0 ? strerror(errno) : ""));
1763 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1767 rc = symlink(fsm->opath, fsm->path);
1768 if (_fsm_debug && (stage & FSM_SYSCALL))
1769 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1770 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1771 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1774 rc = Link(fsm->opath, fsm->path);
1775 if (_fsm_debug && (stage & FSM_SYSCALL))
1776 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1777 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1778 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1781 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1782 if (_fsm_debug && (stage & FSM_SYSCALL))
1783 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1784 fsm->path, (unsigned)(st->st_mode & 07777),
1785 (rc < 0 ? strerror(errno) : ""));
1786 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1790 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1791 if (_fsm_debug && (stage & FSM_SYSCALL))
1792 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1793 fsm->path, (unsigned)(st->st_mode & ~07777),
1794 (unsigned)st->st_rdev,
1795 (rc < 0 ? strerror(errno) : ""));
1796 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1800 rc = Lstat(fsm->path, ost);
1801 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1802 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1803 fsm->path, (rc < 0 ? strerror(errno) : ""));
1804 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1807 rc = Stat(fsm->path, ost);
1808 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1809 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1810 fsm->path, (rc < 0 ? strerror(errno) : ""));
1811 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1814 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1815 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1816 if (_fsm_debug && (stage & FSM_SYSCALL))
1817 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1818 fsm->path, (int)fsm->rdlen, (rc < 0 ? strerror(errno) : ""));
1819 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1822 fsm->rdbuf[fsm->rdnb] = '\0';
1830 rc = fsmStage(fsm, FSM_HREAD);
1832 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1833 fsm->path = _free(fsm->path);
1834 rc = CPIOERR_HDR_TRAILER;
1837 rc = fsmStage(fsm, FSM_POS);
1840 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1841 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1842 rc = fsmStage(fsm, FSM_DREAD);
1847 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1850 (void) fsmStage(fsm, FSM_DREAD);
1854 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1856 memset(fsm->rdbuf, 0, left);
1857 /* XXX DWRITE uses rdnb for I/O length. */
1859 (void) fsmStage(fsm, FSM_DWRITE);
1863 rc = cpioTrailerWrite(fsm);
1866 rc = fsmStage(fsm, FSM_POS);
1868 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1871 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1874 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1875 if (_fsm_debug && (stage & FSM_SYSCALL))
1876 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1877 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1878 (int)fsm->wrlen, (int)fsm->rdnb);
1879 if (fsm->rdnb != fsm->wrlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrlen);
1881 if (Ferror(fsm->rfd))
1882 rc = CPIOERR_READ_FAILED;
1885 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1888 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1889 if (_fsm_debug && (stage & FSM_SYSCALL))
1890 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1891 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
1892 (int)fsm->rdnb, (int)fsm->wrnb);
1893 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1895 if (Ferror(fsm->wfd))
1896 rc = CPIOERR_WRITE_FAILED;
1899 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
1903 fsm->rfd = Fopen(fsm->path, "r.ufdio");
1904 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
1905 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
1907 rc = CPIOERR_OPEN_FAILED;
1910 if (_fsm_debug && (stage & FSM_SYSCALL))
1911 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
1912 fsm->path, fsm->rfd, fsm->rdbuf);
1915 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
1916 if (_fsm_debug && (stage & FSM_SYSCALL))
1917 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
1918 cur, (int)fsm->rdlen, (int)fsm->rdnb);
1919 if (fsm->rdnb != fsm->rdlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->rdlen);
1921 if (Ferror(fsm->rfd))
1922 rc = CPIOERR_READ_FAILED;
1927 if (_fsm_debug && (stage & FSM_SYSCALL))
1928 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
1929 (void) Fclose(fsm->rfd);
1935 fsm->wfd = Fopen(fsm->path, "w.ufdio");
1936 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
1937 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
1939 rc = CPIOERR_OPEN_FAILED;
1941 if (_fsm_debug && (stage & FSM_SYSCALL))
1942 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
1943 fsm->path, fsm->wfd, fsm->wrbuf);
1946 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
1947 if (_fsm_debug && (stage & FSM_SYSCALL))
1948 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
1949 cur, (int)fsm->rdnb, (int)fsm->wrnb);
1950 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write: had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1952 if (Ferror(fsm->wfd))
1953 rc = CPIOERR_WRITE_FAILED;
1958 if (_fsm_debug && (stage & FSM_SYSCALL))
1959 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
1960 (void) Fclose(fsm->wfd);
1970 if (!(stage & FSM_INTERNAL)) {
1971 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
1977 /*@obserever@*/ const char *const fileActionString(fileAction a)
1980 case FA_UNKNOWN: return "unknown";
1981 case FA_CREATE: return "create";
1982 case FA_COPYOUT: return "copyout";
1983 case FA_COPYIN: return "copyin";
1984 case FA_BACKUP: return "backup";
1985 case FA_SAVE: return "save";
1986 case FA_SKIP: return "skip";
1987 case FA_ALTNAME: return "altname";
1988 case FA_ERASE: return "erase";
1989 case FA_SKIPNSTATE: return "skipnstate";
1990 case FA_SKIPNETSHARED: return "skipnetshared";
1991 case FA_SKIPMULTILIB: return "skipmultilib";
1992 default: return "???";
1997 /*@observer@*/ const char *const fileStageString(fileStage a) {
1999 case FSM_UNKNOWN: return "unknown";
2001 case FSM_PKGINSTALL:return "pkginstall";
2002 case FSM_PKGERASE: return "pkgerase";
2003 case FSM_PKGBUILD: return "pkgbuild";
2004 case FSM_PKGCOMMIT: return "pkgcommit";
2005 case FSM_PKGUNDO: return "pkgundo";
2007 case FSM_CREATE: return "create";
2008 case FSM_INIT: return "init";
2009 case FSM_MAP: return "map";
2010 case FSM_MKDIRS: return "mkdirs";
2011 case FSM_RMDIRS: return "rmdirs";
2012 case FSM_PRE: return "pre";
2013 case FSM_PROCESS: return "process";
2014 case FSM_POST: return "post";
2015 case FSM_MKLINKS: return "mklinks";
2016 case FSM_NOTIFY: return "notify";
2017 case FSM_UNDO: return "undo";
2018 case FSM_FINI: return "fini";
2019 case FSM_COMMIT: return "commit";
2020 case FSM_DESTROY: return "destroy";
2021 case FSM_VERIFY: return "verify";
2023 case FSM_UNLINK: return "Unlink";
2024 case FSM_RENAME: return "Rename";
2025 case FSM_MKDIR: return "Mkdir";
2026 case FSM_RMDIR: return "rmdir";
2027 case FSM_CHOWN: return "chown";
2028 case FSM_LCHOWN: return "lchown";
2029 case FSM_CHMOD: return "chmod";
2030 case FSM_UTIME: return "utime";
2031 case FSM_SYMLINK: return "symlink";
2032 case FSM_LINK: return "Link";
2033 case FSM_MKFIFO: return "mkfifo";
2034 case FSM_MKNOD: return "mknod";
2035 case FSM_LSTAT: return "Lstat";
2036 case FSM_STAT: return "Stat";
2037 case FSM_READLINK: return "Readlink";
2038 case FSM_CHROOT: return "chroot";
2040 case FSM_NEXT: return "next";
2041 case FSM_EAT: return "eat";
2042 case FSM_POS: return "pos";
2043 case FSM_PAD: return "pad";
2044 case FSM_TRAILER: return "trailer";
2045 case FSM_HREAD: return "hread";
2046 case FSM_HWRITE: return "hwrite";
2047 case FSM_DREAD: return "Fread";
2048 case FSM_DWRITE: return "Fwrite";
2050 case FSM_ROPEN: return "Fopen";
2051 case FSM_READ: return "Fread";
2052 case FSM_RCLOSE: return "Fclose";
2053 case FSM_WOPEN: return "Fopen";
2054 case FSM_WRITE: return "Fwrite";
2055 case FSM_WCLOSE: return "Fclose";
2057 default: return "???";