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 assert(fi->type == TR_ADDED);
578 fsm->osuffix = SUFFIX_RPMSAVE;
581 assert(fi->type == TR_REMOVED);
584 fprintf(stderr, "*** %s:%s %s\n", fiTypeString(fi), fileActionString(fsm->action), (fsm->path ? fsm->path : ""));
588 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
589 const struct stat * st = &fsm->sb;
590 fsm->path = _free(fsm->path);
591 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
592 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
593 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
600 int fsmMapAttrs(FSM_t fsm)
602 struct stat * st = &fsm->sb;
603 TFI_t fi = fsmGetFi(fsm);
606 if (fi && i >= 0 && i < fi->fc) {
608 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
610 (fi->fmodes ? fi->fmodes[i] : perms);
612 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
614 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
616 if (fsm->mapFlags & CPIO_MAP_MODE)
617 st->st_mode = (st->st_mode & S_IFMT) | finalMode;
618 if (fsm->mapFlags & CPIO_MAP_UID)
619 st->st_uid = finalUid;
620 if (fsm->mapFlags & CPIO_MAP_GID)
621 st->st_gid = finalGid;
623 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
630 * Create file from payload stream.
631 * @todo Legacy: support brokenEndian MD5 checks?
632 * @param fsm file state machine data
633 * @return 0 on success
635 static int expandRegular(/*@special@*/ FSM_t fsm)
638 const char * fmd5sum;
639 const struct stat * st = &fsm->sb;
640 int left = st->st_size;
643 rc = fsmStage(fsm, FSM_WOPEN);
647 /* XXX md5sum's will break on repackaging that includes modified files. */
648 fmd5sum = fsm->fmd5sum;
650 /* XXX This doesn't support brokenEndian checks. */
651 if (st->st_size > 0 && fmd5sum)
652 fdInitMD5(fsm->wfd, 0);
656 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
657 rc = fsmStage(fsm, FSM_DREAD);
661 rc = fsmStage(fsm, FSM_WRITE);
667 /* don't call this with fileSize == fileComplete */
669 (void) fsmStage(fsm, FSM_NOTIFY);
672 if (st->st_size > 0 && fmd5sum) {
673 const char * md5sum = NULL;
675 (void) Fflush(fsm->wfd);
676 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
678 if (md5sum == NULL) {
679 rc = CPIOERR_MD5SUM_MISMATCH;
681 if (strcmp(md5sum, fmd5sum))
682 rc = CPIOERR_MD5SUM_MISMATCH;
683 md5sum = _free(md5sum);
688 (void) fsmStage(fsm, FSM_WCLOSE);
693 * Write next item to payload stream.
694 * @param fsm file state machine data
695 * @param writeData should data be written?
696 * @return 0 on success
698 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
699 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
701 const char * path = fsm->path;
702 const char * opath = fsm->opath;
703 struct stat * st = &fsm->sb;
704 struct stat * ost = &fsm->osb;
705 size_t pos = fdGetCpioPos(fsm->cfd);
706 char * symbuf = NULL;
710 st->st_size = (writeData ? ost->st_size : 0);
711 if (S_ISDIR(st->st_mode)) {
713 } else if (S_ISLNK(st->st_mode)) {
715 * While linux puts the size of a symlink in the st_size field,
716 * I don't think that's a specified standard.
718 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
719 rc = fsmStage(fsm, FSM_READLINK);
721 st->st_size = fsm->rdnb;
722 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
725 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
726 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
727 char * t = alloca(nb);
730 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
732 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
733 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
734 TFI_t fi = fsmGetFi(fsm);
736 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
739 rc = fsmStage(fsm, FSM_HWRITE);
743 if (writeData && S_ISREG(st->st_mode)) {
746 void * mapped = (void *)-1;
750 rc = fsmStage(fsm, FSM_ROPEN);
753 /* XXX unbuffered mmap generates *lots* of fdio debugging */
756 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
757 if (mapped != (void *)-1) {
759 fsm->rdbuf = (char *) mapped;
760 fsm->rdlen = nmapped = st->st_size;
768 if (mapped != (void *)-1) {
773 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
774 rc = fsmStage(fsm, FSM_READ);
778 /* XXX DWRITE uses rdnb for I/O length. */
779 rc = fsmStage(fsm, FSM_DWRITE);
786 if (mapped != (void *)-1) {
787 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
792 } else if (writeData && S_ISLNK(st->st_mode)) {
793 /* XXX DWRITE uses rdnb for I/O length. */
794 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
795 fsm->rdnb = strlen(symbuf);
796 rc = fsmStage(fsm, FSM_DWRITE);
800 rc = fsmStage(fsm, FSM_PAD);
803 { const rpmTransactionSet ts = fsmGetTs(fsm);
804 TFI_t fi = fsmGetFi(fsm);
805 if (ts && fi && ts->notify) {
806 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
807 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
808 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
816 (void) fsmStage(fsm, FSM_RCLOSE);
817 /*@-dependenttrans@*/
820 /*@=dependenttrans@*/
825 * Write set of linked files to payload stream.
826 * @param fsm file state machine data
827 * @return 0 on success
829 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
830 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
832 const char * path = fsm->path;
833 const char * nsuffix = fsm->nsuffix;
834 int iterIndex = fsm->ix;
843 for (i = fsm->li->nlink - 1; i >= 0; i--) {
844 if (fsm->li->filex[i] < 0) continue;
846 fsm->ix = fsm->li->filex[i];
847 rc = fsmStage(fsm, FSM_MAP);
849 /* Write data after last link. */
850 rc = writeFile(fsm, (i == 0));
851 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
853 *fsm->failedFile = xstrdup(fsm->path);
856 fsm->path = _free(fsm->path);
857 fsm->li->filex[i] = -1;
861 fsm->nsuffix = nsuffix;
867 * Create pending hard links to existing file.
868 * @param fsm file state machine data
869 * @return 0 on success
871 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
872 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
874 const char * path = fsm->path;
875 const char * opath = fsm->opath;
876 const char * nsuffix = fsm->nsuffix;
877 int iterIndex = fsm->ix;
887 fsm->ix = fsm->li->filex[fsm->li->createdPath];
888 rc = fsmStage(fsm, FSM_MAP);
889 fsm->opath = fsm->path;
891 for (i = 0; i < fsm->li->nlink; i++) {
892 if (fsm->li->filex[i] < 0) continue;
893 if (i == fsm->li->createdPath) continue;
895 fsm->ix = fsm->li->filex[i];
896 rc = fsmStage(fsm, FSM_MAP);
897 rc = fsmStage(fsm, FSM_VERIFY);
899 if (rc != CPIOERR_LSTAT_FAILED) break;
901 /* XXX link(fsm->opath, fsm->path) */
902 rc = fsmStage(fsm, FSM_LINK);
903 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
905 *fsm->failedFile = xstrdup(fsm->path);
908 fsm->li->linksLeft--;
910 fsm->opath = _free(fsm->opath);
913 fsm->nsuffix = nsuffix;
920 * Commit hard linked file set atomically.
921 * @param fsm file state machine data
922 * @return 0 on success
924 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
925 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
926 fsm->li, fsm->links @*/
928 const char * path = fsm->path;
929 const char * nsuffix = fsm->nsuffix;
930 int iterIndex = fsm->ix;
931 struct stat * st = &fsm->sb;
939 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
940 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
944 for (i = 0; i < fsm->li->nlink; i++) {
945 if (fsm->li->filex[i] < 0) continue;
946 fsm->ix = fsm->li->filex[i];
947 rc = fsmStage(fsm, FSM_MAP);
948 rc = fsmStage(fsm, FSM_COMMIT);
949 fsm->path = _free(fsm->path);
950 fsm->li->filex[i] = -1;
954 fsm->nsuffix = nsuffix;
960 * Remove (if created) directories not explicitly included in package.
961 * @param fsm file state machine data
962 * @return 0 on success
965 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
966 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
968 const char * path = fsm->path;
969 void * dnli = dnlInitIterator(fsm, 1);
970 char * dn = fsm->rdbuf;
971 int dc = dnlCount(dnli);
976 /*@-observertrans -dependenttrans@*/
977 if (fsm->ldn != NULL && fsm->dnlx != NULL)
978 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
979 int dnlen = strlen(fsm->path);
983 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
986 /* Copy to avoid const on fsm->path. */
987 te = stpcpy(dn, fsm->path) - 1;
990 /* Remove generated directories. */
994 rc = fsmStage(fsm, FSM_RMDIR);
999 } while ((te - dn) > fsm->dnlx[dc]);
1001 dnli = dnlFreeIterator(dnli);
1002 /*@=observertrans =dependenttrans@*/
1010 * Create (if necessary) directories not explicitly included in package.
1011 * @param fsm file state machine data
1012 * @return 0 on success
1014 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1015 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1016 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1017 /*@defines fsm->dnlx, fsm->ldn @*/
1019 struct stat * st = &fsm->sb;
1020 struct stat * ost = &fsm->osb;
1021 const char * path = fsm->path;
1022 mode_t st_mode = st->st_mode;
1023 void * dnli = dnlInitIterator(fsm, 0);
1024 char * dn = fsm->rdbuf;
1025 int dc = dnlCount(dnli);
1032 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1033 /*@-observertrans -dependenttrans@*/
1034 if (fsm->dnlx != NULL)
1035 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1036 int dnlen = strlen(fsm->path);
1039 dc = dnlIndex(dnli);
1040 if (dc < 0) continue;
1041 fsm->dnlx[dc] = dnlen;
1045 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1046 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1048 /*@=compdef =nullpass@*/
1050 /* Copy to avoid const on fsm->path. */
1051 (void) stpcpy(dn, fsm->path);
1054 /* Assume '/' directory exists, otherwise "mkdir -p" if non-existent. */
1055 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1056 if (*te != '/') continue;
1060 /* Already validated? */
1061 /*@-usedef -compdef -nullpass -nullderef@*/
1062 if (i < fsm->ldnlen &&
1063 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1064 !strncmp(fsm->path, fsm->ldn, i))
1067 /* Move pre-existing path marker forward. */
1068 fsm->dnlx[dc] = (te - dn);
1071 /*@=usedef =compdef =nullpass =nullderef@*/
1073 /* Validate next component of path. */
1074 rc = fsmStage(fsm, FSM_LSTAT);
1077 /* Directory already exists? */
1078 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1079 /* Move pre-existing path marker forward. */
1080 fsm->dnlx[dc] = (te - dn);
1081 } else if (rc == CPIOERR_LSTAT_FAILED) {
1082 TFI_t fi = fsmGetFi(fsm);
1084 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1085 rc = fsmStage(fsm, FSM_MKDIR);
1087 rpmMessage(RPMMESS_WARNING,
1088 _("%s directory created with perms %04o.\n"),
1089 fsm->path, (unsigned)(st->st_mode & 07777));
1096 /* Save last validated path. */
1097 if (fsm->ldnalloc < (dnlen + 1)) {
1098 fsm->ldnalloc = dnlen + 100;
1099 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1101 /*@-nullpass@*/ /* FIX: fsm->ldn NULL. */
1102 strcpy(fsm->ldn, fsm->path);
1104 fsm->ldnlen = dnlen;
1106 dnli = dnlFreeIterator(dnli);
1107 /*@=observertrans =dependenttrans@*/
1110 st->st_mode = st_mode; /* XXX restore st->st_mode */
1116 int fsmStage(FSM_t fsm, fileStage stage)
1119 fileStage prevStage = fsm->stage;
1120 const char * const prev = fileStageString(prevStage);
1122 static int modulo = 4;
1123 const char * const cur = fileStageString(stage);
1124 struct stat * st = &fsm->sb;
1125 struct stat * ost = &fsm->osb;
1126 int saveerrno = errno;
1131 #define _fafilter(_a) \
1132 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1133 ? fileActionString(_a) : "")
1135 if (stage & FSM_DEAD) {
1137 } else if (stage & FSM_INTERNAL) {
1138 if (_fsm_debug && !(stage & FSM_SYSCALL))
1139 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1141 (unsigned)st->st_mode, (int)st->st_nlink,
1142 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1143 (fsm->path ? fsm->path : ""),
1144 _fafilter(fsm->action));
1147 if (_fsm_debug || !(stage & FSM_VERBOSE))
1148 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1150 (unsigned)st->st_mode, (int)st->st_nlink,
1151 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1152 (fsm->path ? fsm->path + fsm->astriplen : ""),
1153 _fafilter(fsm->action));
1160 case FSM_PKGINSTALL:
1162 /* Clean fsm, free'ing memory. Read next archive header. */
1163 rc = fsmStage(fsm, FSM_INIT);
1165 /* Exit on end-of-payload. */
1166 if (rc == CPIOERR_HDR_TRAILER) {
1171 /* Exit on error. */
1174 (void) fsmStage(fsm, FSM_UNDO);
1178 /* Extract file from archive. */
1179 rc = fsmStage(fsm, FSM_PROCESS);
1181 (void) fsmStage(fsm, FSM_UNDO);
1185 /* Notify on success. */
1186 (void) fsmStage(fsm, FSM_NOTIFY);
1188 if (fsmStage(fsm, FSM_FINI))
1195 /* Clean fsm, free'ing memory. */
1196 rc = fsmStage(fsm, FSM_INIT);
1198 /* Exit on end-of-payload. */
1199 if (rc == CPIOERR_HDR_TRAILER) {
1204 /* Rename/erase next item. */
1205 if (fsmStage(fsm, FSM_FINI))
1212 rc = fsmStage(fsm, FSM_INIT);
1214 /* Exit on end-of-payload. */
1215 if (rc == CPIOERR_HDR_TRAILER) {
1220 /* Exit on error. */
1223 (void) fsmStage(fsm, FSM_UNDO);
1227 /* Copy file into archive. */
1228 rc = fsmStage(fsm, FSM_PROCESS);
1230 (void) fsmStage(fsm, FSM_UNDO);
1234 if (fsmStage(fsm, FSM_FINI))
1239 rc = fsmStage(fsm, FSM_TRAILER);
1242 { rpmTransactionSet ts = fsmGetTs(fsm);
1243 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1244 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1245 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1248 fsm->path = _free(fsm->path);
1249 fsm->opath = _free(fsm->opath);
1250 fsm->dnlx = _free(fsm->dnlx);
1252 fsm->ldn = _free(fsm->ldn);
1253 fsm->ldnalloc = fsm->ldnlen = 0;
1255 fsm->rdsize = fsm->wrsize = 0;
1256 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1257 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1258 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1259 fsm->rdsize = 8 * BUFSIZ;
1260 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1261 fsm->wrsize = 8 * BUFSIZ;
1262 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1265 fsm->mkdirsdone = 0;
1269 errno = 0; /* XXX get rid of EBADF */
1271 /* Detect and create directories not explicitly in package. */
1272 if (fsm->goal == FSM_PKGINSTALL) {
1273 rc = fsmStage(fsm, FSM_MKDIRS);
1274 if (!rc) fsm->mkdirsdone = 1;
1279 fsm->path = _free(fsm->path);
1281 fsm->diskchecked = fsm->exists = 0;
1283 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1284 fsm->action = FA_UNKNOWN;
1285 fsm->osuffix = NULL;
1286 fsm->nsuffix = NULL;
1288 if (fsm->goal == FSM_PKGINSTALL) {
1289 /* Read next header from payload, checking for end-of-payload. */
1290 rc = fsmStage(fsm, FSM_NEXT);
1294 /* Identify mapping index. */
1295 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1296 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1298 /* On non-install, detect end-of-loop. */
1299 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1300 rc = CPIOERR_HDR_TRAILER;
1304 /* On non-install, mode must be known so that dirs don't get suffix. */
1305 if (fsm->goal != FSM_PKGINSTALL) {
1306 TFI_t fi = fsmGetFi(fsm);
1307 st->st_mode = fi->fmodes[fsm->ix];
1310 /* Generate file path. */
1311 rc = fsmStage(fsm, FSM_MAP);
1314 /* Perform lstat/stat for disk file. */
1315 if (fsm->path != NULL) {
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) {
1326 /* Skip %ghost files on build. */
1329 fsm->diskchecked = 1;
1332 /* On non-install, the disk file stat is what's remapped. */
1333 if (fsm->goal != FSM_PKGINSTALL)
1334 *st = *ost; /* structure assignment */
1336 /* Remap file perms, owner, and group. */
1337 rc = fsmMapAttrs(fsm);
1340 fsm->postpone = XFA_SKIPPING(fsm->action);
1341 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1342 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1343 fsm->postpone = saveHardLink(fsm);
1349 rc = fsmMapPath(fsm);
1352 rc = fsmMkdirs(fsm);
1356 rc = fsmRmdirs(fsm);
1359 if (fsm->postpone) {
1360 if (fsm->goal == FSM_PKGINSTALL)
1361 rc = fsmStage(fsm, FSM_EAT);
1365 if (fsm->goal == FSM_PKGBUILD) {
1366 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1367 struct hardLink * li, * prev;
1368 rc = writeLinkedFile(fsm);
1369 if (rc) break; /* W2DO? */
1371 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1372 if (li == fsm->li) break;
1375 fsm->links = fsm->li->next;
1377 prev->next = fsm->li->next;
1378 fsm->li->next = NULL;
1379 fsm->li = freeHardLink(fsm->li);
1381 rc = writeFile(fsm, 1);
1386 if (fsm->goal != FSM_PKGINSTALL)
1389 if (S_ISREG(st->st_mode)) {
1390 const char * path = fsm->path;
1392 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1393 rc = fsmStage(fsm, FSM_VERIFY);
1395 if (rc == 0 && fsm->osuffix) {
1396 const char * opath = fsm->opath;
1397 fsm->opath = fsm->path;
1398 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1399 rc = fsmStage(fsm, FSM_RENAME);
1401 rpmMessage(RPMMESS_WARNING,
1402 _("%s saved as %s\n"), fsm->opath, fsm->path);
1403 fsm->path = _free(fsm->path);
1407 /*@-dependenttrans@*/
1409 /*@=dependenttrans@*/
1410 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1411 rc = expandRegular(fsm);
1412 } else if (S_ISDIR(st->st_mode)) {
1413 mode_t st_mode = st->st_mode;
1414 rc = fsmStage(fsm, FSM_VERIFY);
1415 if (rc == CPIOERR_LSTAT_FAILED) {
1416 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1417 st->st_mode |= 00700;
1418 rc = fsmStage(fsm, FSM_MKDIR);
1419 st->st_mode = st_mode; /* XXX restore st->st_mode */
1421 } else if (S_ISLNK(st->st_mode)) {
1422 const char * opath = fsm->opath;
1424 if ((st->st_size + 1) > fsm->rdsize) {
1425 rc = CPIOERR_HDR_SIZE;
1429 fsm->wrlen = st->st_size;
1430 rc = fsmStage(fsm, FSM_DREAD);
1431 if (!rc && fsm->rdnb != fsm->wrlen)
1432 rc = CPIOERR_READ_FAILED;
1435 fsm->wrbuf[st->st_size] = '\0';
1436 /* XXX symlink(fsm->opath, fsm->path) */
1437 /*@-dependenttrans@*/
1438 fsm->opath = fsm->wrbuf;
1439 /*@=dependenttrans@*/
1440 rc = fsmStage(fsm, FSM_VERIFY);
1441 if (rc == CPIOERR_LSTAT_FAILED)
1442 rc = fsmStage(fsm, FSM_SYMLINK);
1443 fsm->opath = opath; /* XXX restore fsm->path */
1444 } else if (S_ISFIFO(st->st_mode)) {
1445 mode_t st_mode = st->st_mode;
1446 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1447 rc = fsmStage(fsm, FSM_VERIFY);
1448 if (rc == CPIOERR_LSTAT_FAILED) {
1449 st->st_mode = 0000; /* XXX abuse st->st_mode */
1450 rc = fsmStage(fsm, FSM_MKFIFO);
1451 st->st_mode = st_mode; /* XXX restore st->st_mode */
1453 } else if (S_ISCHR(st->st_mode) ||
1454 S_ISBLK(st->st_mode) ||
1455 S_ISSOCK(st->st_mode))
1457 rc = fsmStage(fsm, FSM_VERIFY);
1458 if (rc == CPIOERR_LSTAT_FAILED)
1459 rc = fsmStage(fsm, FSM_MKNOD);
1461 rc = CPIOERR_UNKNOWN_FILETYPE;
1463 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1464 fsm->li->createdPath = fsm->li->linkIndex;
1465 rc = fsmMakeLinks(fsm);
1471 rc = fsmMakeLinks(fsm);
1473 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1474 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1475 rpmTransactionSet ts = fsmGetTs(fsm);
1476 TFI_t fi = fsmGetFi(fsm);
1477 if (ts && ts->notify && fi)
1478 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1479 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1480 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1486 if (fsm->goal == FSM_PKGINSTALL) {
1487 (void) fsmStage(fsm,
1488 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1490 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1492 (void) fsmStage(fsm, FSM_RMDIRS);
1496 if (fsm->failedFile && *fsm->failedFile == NULL)
1497 *fsm->failedFile = xstrdup(fsm->path);
1500 if (!fsm->postpone && fsm->commit) {
1501 if (fsm->goal == FSM_PKGINSTALL)
1502 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1503 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1504 if (fsm->goal == FSM_PKGCOMMIT)
1505 rc = fsmStage(fsm, FSM_COMMIT);
1506 if (fsm->goal == FSM_PKGERASE)
1507 rc = fsmStage(fsm, FSM_COMMIT);
1509 fsm->path = _free(fsm->path);
1510 fsm->opath = _free(fsm->opath);
1511 memset(st, 0, sizeof(*st));
1512 memset(ost, 0, sizeof(*ost));
1515 /* Rename pre-existing modified or unmanaged file. */
1516 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1517 const char * opath = fsm->opath;
1518 const char * path = fsm->path;
1519 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1520 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1522 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1523 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1525 rc = fsmStage(fsm, FSM_RENAME);
1527 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1528 fsm->opath, fsm->path);
1530 fsm->path = _free(fsm->path);
1532 fsm->opath = _free(fsm->opath);
1536 /* Remove erased files. */
1537 if (fsm->goal == FSM_PKGERASE) {
1538 if (fsm->action == FA_ERASE) {
1539 TFI_t fi = fsmGetFi(fsm);
1540 if (S_ISDIR(st->st_mode)) {
1541 rc = fsmStage(fsm, FSM_RMDIR);
1544 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1546 /* XXX make sure that build side permits %missingok on directories. */
1547 if (fsm->fflags & RPMFILE_MISSINGOK)
1550 /* XXX common error message. */
1552 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1553 _("%s rmdir of %s failed: Directory not empty\n"),
1554 fiTypeString(fi), fsm->path);
1558 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1559 _("%s rmdir of %s failed: %s\n"),
1560 fiTypeString(fi), fsm->path, strerror(errno));
1564 rc = fsmStage(fsm, FSM_UNLINK);
1566 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1568 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1569 _("%s unlink of %s failed: %s\n"),
1570 fiTypeString(fi), fsm->path, strerror(errno));
1573 /* XXX Failure to remove is not (yet) cause for failure. */
1574 if (!strict_erasures) rc = 0;
1578 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1579 /* Rename temporary to final file name. */
1580 if (!S_ISDIR(st->st_mode) &&
1581 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1583 fsm->opath = fsm->path;
1584 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1585 rc = fsmStage(fsm, FSM_RENAME);
1586 if (!rc && fsm->nsuffix) {
1587 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1588 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1589 (opath ? opath : ""), fsm->path);
1590 opath = _free(opath);
1592 fsm->opath = _free(fsm->opath);
1594 if (S_ISLNK(st->st_mode)) {
1595 if (!rc && !getuid())
1596 rc = fsmStage(fsm, FSM_LCHOWN);
1598 if (!rc && !getuid())
1599 rc = fsmStage(fsm, FSM_CHOWN);
1601 rc = fsmStage(fsm, FSM_CHMOD);
1603 time_t st_mtime = st->st_mtime;
1604 TFI_t fi = fsmGetFi(fsm);
1606 st->st_mtime = fi->fmtimes[fsm->ix];
1607 rc = fsmStage(fsm, FSM_UTIME);
1608 st->st_mtime = st_mtime;
1613 /* Notify on success. */
1614 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1617 fsm->path = _free(fsm->path);
1619 /* Create any remaining links (if no error), and clean up. */
1620 while ((fsm->li = fsm->links) != NULL) {
1621 fsm->links = fsm->li->next;
1622 fsm->li->next = NULL;
1623 if (fsm->goal == FSM_PKGINSTALL && fsm->commit && fsm->li->linksLeft)
1625 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1626 if (fsm->li->filex[i] < 0) continue;
1627 rc = CPIOERR_MISSING_HARDLINK;
1628 if (fsm->failedFile && *fsm->failedFile == NULL) {
1629 fsm->ix = fsm->li->filex[i];
1630 if (!fsmStage(fsm, FSM_MAP)) {
1631 *fsm->failedFile = fsm->path;
1638 if (fsm->goal == FSM_PKGBUILD) {
1639 rc = CPIOERR_MISSING_HARDLINK;
1641 fsm->li = freeHardLink(fsm->li);
1643 fsm->ldn = _free(fsm->ldn);
1644 fsm->ldnalloc = fsm->ldnlen = 0;
1645 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1646 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1649 if (fsm->diskchecked && !fsm->exists) {
1650 rc = CPIOERR_LSTAT_FAILED;
1653 if (S_ISREG(st->st_mode)) {
1654 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1655 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1657 * XXX HP-UX (and other os'es) don't permit unlink on busy
1660 fsm->opath = fsm->path;
1662 rc = fsmStage(fsm, FSM_RENAME);
1664 (void) fsmStage(fsm, FSM_UNLINK);
1666 rc = CPIOERR_UNLINK_FAILED;
1667 fsm->path = fsm->opath;
1669 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1670 /*@notreached@*/ break;
1671 } else if (S_ISDIR(st->st_mode)) {
1672 if (S_ISDIR(ost->st_mode)) return 0;
1673 if (S_ISLNK(ost->st_mode)) {
1674 rc = fsmStage(fsm, FSM_STAT);
1675 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1678 if (S_ISDIR(ost->st_mode)) return 0;
1680 } else if (S_ISLNK(st->st_mode)) {
1681 if (S_ISLNK(ost->st_mode)) {
1682 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1683 rc = fsmStage(fsm, FSM_READLINK);
1686 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1688 } else if (S_ISFIFO(st->st_mode)) {
1689 if (S_ISFIFO(ost->st_mode)) return 0;
1690 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1691 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1692 (ost->st_rdev == st->st_rdev)) return 0;
1693 } else if (S_ISSOCK(st->st_mode)) {
1694 if (S_ISSOCK(ost->st_mode)) return 0;
1696 /* XXX shouldn't do this with commit/undo. */
1698 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1699 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1700 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1701 /*@notreached@*/ break;
1704 rc = Unlink(fsm->path);
1705 if (_fsm_debug && (stage & FSM_SYSCALL))
1706 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1707 fsm->path, (rc < 0 ? strerror(errno) : ""));
1708 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1711 rc = Rename(fsm->opath, fsm->path);
1712 if (_fsm_debug && (stage & FSM_SYSCALL))
1713 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1714 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1715 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1718 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1719 if (_fsm_debug && (stage & FSM_SYSCALL))
1720 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1721 fsm->path, (unsigned)(st->st_mode & 07777),
1722 (rc < 0 ? strerror(errno) : ""));
1723 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1726 rc = Rmdir(fsm->path);
1727 if (_fsm_debug && (stage & FSM_SYSCALL))
1728 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1729 fsm->path, (rc < 0 ? strerror(errno) : ""));
1730 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1733 rc = chown(fsm->path, st->st_uid, st->st_gid);
1734 if (_fsm_debug && (stage & FSM_SYSCALL))
1735 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1736 fsm->path, (int)st->st_uid, (int)st->st_gid,
1737 (rc < 0 ? strerror(errno) : ""));
1738 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1741 #if ! CHOWN_FOLLOWS_SYMLINK
1742 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1743 if (_fsm_debug && (stage & FSM_SYSCALL))
1744 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1745 fsm->path, (int)st->st_uid, (int)st->st_gid,
1746 (rc < 0 ? strerror(errno) : ""));
1747 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1751 rc = chmod(fsm->path, (st->st_mode & 07777));
1752 if (_fsm_debug && (stage & FSM_SYSCALL))
1753 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1754 fsm->path, (unsigned)(st->st_mode & 07777),
1755 (rc < 0 ? strerror(errno) : ""));
1756 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1759 { struct utimbuf stamp;
1760 stamp.actime = st->st_mtime;
1761 stamp.modtime = st->st_mtime;
1762 rc = utime(fsm->path, &stamp);
1763 if (_fsm_debug && (stage & FSM_SYSCALL))
1764 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1765 fsm->path, (unsigned)st->st_mtime,
1766 (rc < 0 ? strerror(errno) : ""));
1767 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1771 rc = symlink(fsm->opath, fsm->path);
1772 if (_fsm_debug && (stage & FSM_SYSCALL))
1773 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1774 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1775 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1778 rc = Link(fsm->opath, fsm->path);
1779 if (_fsm_debug && (stage & FSM_SYSCALL))
1780 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1781 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1782 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1785 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1786 if (_fsm_debug && (stage & FSM_SYSCALL))
1787 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1788 fsm->path, (unsigned)(st->st_mode & 07777),
1789 (rc < 0 ? strerror(errno) : ""));
1790 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1794 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1795 if (_fsm_debug && (stage & FSM_SYSCALL))
1796 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1797 fsm->path, (unsigned)(st->st_mode & ~07777),
1798 (unsigned)st->st_rdev,
1799 (rc < 0 ? strerror(errno) : ""));
1800 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1804 rc = Lstat(fsm->path, ost);
1805 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1806 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1807 fsm->path, (rc < 0 ? strerror(errno) : ""));
1808 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1811 rc = Stat(fsm->path, ost);
1812 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1813 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1814 fsm->path, (rc < 0 ? strerror(errno) : ""));
1815 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1818 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1819 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1820 if (_fsm_debug && (stage & FSM_SYSCALL))
1821 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1822 fsm->path, (int)fsm->rdlen, (rc < 0 ? strerror(errno) : ""));
1823 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1826 fsm->rdbuf[fsm->rdnb] = '\0';
1834 rc = fsmStage(fsm, FSM_HREAD);
1836 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1837 fsm->path = _free(fsm->path);
1838 rc = CPIOERR_HDR_TRAILER;
1841 rc = fsmStage(fsm, FSM_POS);
1844 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1845 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1846 rc = fsmStage(fsm, FSM_DREAD);
1851 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1854 (void) fsmStage(fsm, FSM_DREAD);
1858 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1860 memset(fsm->rdbuf, 0, left);
1861 /* XXX DWRITE uses rdnb for I/O length. */
1863 (void) fsmStage(fsm, FSM_DWRITE);
1867 rc = cpioTrailerWrite(fsm);
1870 rc = fsmStage(fsm, FSM_POS);
1872 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1875 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1878 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1879 if (_fsm_debug && (stage & FSM_SYSCALL))
1880 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1881 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1882 (int)fsm->wrlen, (int)fsm->rdnb);
1883 if (fsm->rdnb != fsm->wrlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrlen);
1885 if (Ferror(fsm->rfd))
1886 rc = CPIOERR_READ_FAILED;
1889 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1892 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1893 if (_fsm_debug && (stage & FSM_SYSCALL))
1894 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1895 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
1896 (int)fsm->rdnb, (int)fsm->wrnb);
1897 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1899 if (Ferror(fsm->wfd))
1900 rc = CPIOERR_WRITE_FAILED;
1903 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
1907 fsm->rfd = Fopen(fsm->path, "r.ufdio");
1908 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
1909 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
1911 rc = CPIOERR_OPEN_FAILED;
1914 if (_fsm_debug && (stage & FSM_SYSCALL))
1915 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
1916 fsm->path, fsm->rfd, fsm->rdbuf);
1919 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
1920 if (_fsm_debug && (stage & FSM_SYSCALL))
1921 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
1922 cur, (int)fsm->rdlen, (int)fsm->rdnb);
1923 if (fsm->rdnb != fsm->rdlen) fprintf(stderr, "*** short read, had %d, got %d\n", (int)fsm->rdnb, (int)fsm->rdlen);
1925 if (Ferror(fsm->rfd))
1926 rc = CPIOERR_READ_FAILED;
1931 if (_fsm_debug && (stage & FSM_SYSCALL))
1932 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
1933 (void) Fclose(fsm->rfd);
1939 fsm->wfd = Fopen(fsm->path, "w.ufdio");
1940 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
1941 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
1943 rc = CPIOERR_OPEN_FAILED;
1945 if (_fsm_debug && (stage & FSM_SYSCALL))
1946 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
1947 fsm->path, fsm->wfd, fsm->wrbuf);
1950 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
1951 if (_fsm_debug && (stage & FSM_SYSCALL))
1952 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
1953 cur, (int)fsm->rdnb, (int)fsm->wrnb);
1954 if (fsm->rdnb != fsm->wrnb) fprintf(stderr, "*** short write: had %d, got %d\n", (int)fsm->rdnb, (int)fsm->wrnb);
1956 if (Ferror(fsm->wfd))
1957 rc = CPIOERR_WRITE_FAILED;
1962 if (_fsm_debug && (stage & FSM_SYSCALL))
1963 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
1964 (void) Fclose(fsm->wfd);
1974 if (!(stage & FSM_INTERNAL)) {
1975 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
1981 /*@obserever@*/ const char *const fileActionString(fileAction a)
1984 case FA_UNKNOWN: return "unknown";
1985 case FA_CREATE: return "create";
1986 case FA_COPYOUT: return "copyout";
1987 case FA_COPYIN: return "copyin";
1988 case FA_BACKUP: return "backup";
1989 case FA_SAVE: return "save";
1990 case FA_SKIP: return "skip";
1991 case FA_ALTNAME: return "altname";
1992 case FA_ERASE: return "erase";
1993 case FA_SKIPNSTATE: return "skipnstate";
1994 case FA_SKIPNETSHARED: return "skipnetshared";
1995 case FA_SKIPMULTILIB: return "skipmultilib";
1996 default: return "???";
2001 /*@observer@*/ const char *const fileStageString(fileStage a) {
2003 case FSM_UNKNOWN: return "unknown";
2005 case FSM_PKGINSTALL:return "pkginstall";
2006 case FSM_PKGERASE: return "pkgerase";
2007 case FSM_PKGBUILD: return "pkgbuild";
2008 case FSM_PKGCOMMIT: return "pkgcommit";
2009 case FSM_PKGUNDO: return "pkgundo";
2011 case FSM_CREATE: return "create";
2012 case FSM_INIT: return "init";
2013 case FSM_MAP: return "map";
2014 case FSM_MKDIRS: return "mkdirs";
2015 case FSM_RMDIRS: return "rmdirs";
2016 case FSM_PRE: return "pre";
2017 case FSM_PROCESS: return "process";
2018 case FSM_POST: return "post";
2019 case FSM_MKLINKS: return "mklinks";
2020 case FSM_NOTIFY: return "notify";
2021 case FSM_UNDO: return "undo";
2022 case FSM_FINI: return "fini";
2023 case FSM_COMMIT: return "commit";
2024 case FSM_DESTROY: return "destroy";
2025 case FSM_VERIFY: return "verify";
2027 case FSM_UNLINK: return "Unlink";
2028 case FSM_RENAME: return "Rename";
2029 case FSM_MKDIR: return "Mkdir";
2030 case FSM_RMDIR: return "rmdir";
2031 case FSM_CHOWN: return "chown";
2032 case FSM_LCHOWN: return "lchown";
2033 case FSM_CHMOD: return "chmod";
2034 case FSM_UTIME: return "utime";
2035 case FSM_SYMLINK: return "symlink";
2036 case FSM_LINK: return "Link";
2037 case FSM_MKFIFO: return "mkfifo";
2038 case FSM_MKNOD: return "mknod";
2039 case FSM_LSTAT: return "Lstat";
2040 case FSM_STAT: return "Stat";
2041 case FSM_READLINK: return "Readlink";
2042 case FSM_CHROOT: return "chroot";
2044 case FSM_NEXT: return "next";
2045 case FSM_EAT: return "eat";
2046 case FSM_POS: return "pos";
2047 case FSM_PAD: return "pad";
2048 case FSM_TRAILER: return "trailer";
2049 case FSM_HREAD: return "hread";
2050 case FSM_HWRITE: return "hwrite";
2051 case FSM_DREAD: return "Fread";
2052 case FSM_DWRITE: return "Fwrite";
2054 case FSM_ROPEN: return "Fopen";
2055 case FSM_READ: return "Fread";
2056 case FSM_RCLOSE: return "Fclose";
2057 case FSM_WOPEN: return "Fopen";
2058 case FSM_WRITE: return "Fwrite";
2059 case FSM_WCLOSE: return "Fclose";
2061 default: return "???";