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. */
23 /*@-exportlocal -exportheadervar@*/
24 int strict_erasures = 0;
25 /*@=exportlocal =exportheadervar@*/
27 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
28 const FSMI_t iter = fsm->iter;
30 return (iter ? iter->ts : NULL);
34 TFI_t fsmGetFi(const FSM_t fsm)
36 const FSMI_t iter = fsm->iter;
38 return (iter ? iter->fi : NULL);
42 #define SUFFIX_RPMORIG ".rpmorig"
43 #define SUFFIX_RPMSAVE ".rpmsave"
44 #define SUFFIX_RPMNEW ".rpmnew"
47 * Build path to file from file info, ornamented with subdir and suffix.
48 * @param fsm file state machine data
49 * @param st file stat info
50 * @param subdir subdir to use (NULL disables)
51 * @param suffix suffix to use (NULL disables)
52 * @retval path to file
54 static /*@only@*//*@null@*/
55 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
56 /*@null@*/ const struct stat * st,
57 /*@null@*/ const char * subdir,
58 /*@null@*/ const char * suffix)
59 /*@uses fsm->dirName, fsm->baseName */
61 const char * s = NULL;
66 /*@-nullpass@*/ /* LCL: subdir/suffix != NULL */
67 nb = strlen(fsm->dirName) +
68 (st && subdir && !S_ISDIR(st->st_mode) ? strlen(subdir) : 0) +
69 (st && suffix && !S_ISDIR(st->st_mode) ? strlen(suffix) : 0) +
70 strlen(fsm->baseName) + 1;
72 t = stpcpy(t, fsm->dirName);
73 if (st && subdir && !S_ISDIR(st->st_mode))
74 t = stpcpy(t, subdir);
75 t = stpcpy(t, fsm->baseName);
76 if (st && suffix && !S_ISDIR(st->st_mode))
77 t = stpcpy(t, suffix);
84 * Destroy file info iterator.
85 * @param p file info iterator
88 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
90 return _free((void *)p);
94 * Create file info iterator.
95 * @param a transaction set
96 * @param b transaction element file info
97 * @return file info iterator
100 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
102 rpmTransactionSet ts = (void *)a;
103 TFI_t fi = (void *)b;
106 iter = xcalloc(1, sizeof(*iter));
111 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
112 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
113 iter->isave = iter->i;
118 * Return next index into file info.
119 * @param a file info iterator
120 * @return next index, -1 on termination
122 static int mapNextIterator(void * a) {
124 const TFI_t fi = iter->fi;
128 if (iter->i >= 0) i = iter->i--;
130 if (iter->i < fi->fc) i = iter->i++;
138 static int cpioStrCmp(const void * a, const void * b)
141 const char * afn = *(const char **)a;
142 const char * bfn = *(const char **)b;
144 /* Match rpm-4.0 payloads with ./ prefixes. */
145 if (afn[0] == '.' && afn[1] == '/') afn += 2;
146 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
148 /* If either path is absolute, make it relative. */
149 if (afn[0] == '/') afn += 1;
150 if (bfn[0] == '/') bfn += 1;
152 return strcmp(afn, bfn);
156 * Locate archive path in file info.
157 * @param a file info iterator
158 * @param fsmPath archive path
159 * @return index into file info, -1 if archive path was not found
161 static int mapFind(void * a, const char * fsmPath)
165 const TFI_t fi = iter->fi;
168 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
169 const char ** p = NULL;
171 if (fi->apath != NULL)
172 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
175 fprintf(stderr, "*** not mapped %s\n", fsmPath);
177 iter->i = p - fi->apath;
178 ix = mapNextIterator(iter);
185 * Directory name iterator.
187 typedef struct dnli_s {
188 /*@dependent@*/ TFI_t fi;
189 /*@only@*/ /*@null@*/ char * active;
196 * Destroy directory name iterator.
197 * @param a directory name iterator
198 * @retval NULL always
200 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
204 DNLI_t dnli = (void *)a;
205 if (dnli->active) free(dnli->active);
212 static inline int dnlCount(const DNLI_t dnli)
215 return (dnli ? dnli->fi->dc : 0);
220 static inline int dnlIndex(const DNLI_t dnli)
223 return (dnli ? dnli->isave : -1);
227 * Create directory name iterator.
228 * @param fsm file state machine data
229 * @param reverse traverse directory names in reverse order?
230 * @return directory name iterator
232 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
234 /*@uses fsm->iter @*/
237 TFI_t fi = fsmGetFi(fsm);
243 dnli = xcalloc(1, sizeof(*dnli));
245 dnli->reverse = reverse;
246 dnli->i = (reverse ? fi->dc : 0);
249 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
251 /* Identify parent directories not skipped. */
252 for (i = 0; i < fi->fc; i++)
253 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
255 /* Exclude parent directories that are explicitly included. */
256 for (i = 0; i < fi->fc; i++) {
257 int dil, dnlen, bnlen;
259 if (!S_ISDIR(fi->fmodes[i]))
263 dnlen = strlen(fi->dnl[dil]);
264 bnlen = strlen(fi->bnl[i]);
266 for (j = 0; j < fi->dc; j++) {
270 if (!dnli->active[j] || j == dil) continue;
273 if (jlen != (dnlen+bnlen+1)) continue;
274 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
275 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
276 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
278 /* This directory is included in the package. */
280 /*@innerbreak@*/ break;
284 /* Print only once per package. */
287 for (i = 0; i < fi->dc; i++) {
288 if (!dnli->active[i]) continue;
291 rpmMessage(RPMMESS_DEBUG,
292 _("========= Directories not explictly included in package:\n"));
294 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
297 rpmMessage(RPMMESS_DEBUG, "=========\n");
304 * Return next directory name (from file info).
305 * @param dnli directory name iterator
306 * @return next directory name
308 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
311 const char * dn = NULL;
319 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
320 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
322 if (i >= 0 && i < fi->dc)
332 * Save hard link in chain.
333 * @param fsm file state machine data
334 * @return Is chain only partially filled?
337 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
338 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
339 /*@defines fsm->li @*/
340 /*@releases fsm->path @*/
343 struct stat * st = &fsm->sb;
348 /* Find hard link set. */
349 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
350 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
354 /* New hard link encountered, add new link to set. */
355 if (fsm->li == NULL) {
356 fsm->li = xcalloc(1, sizeof(*fsm->li));
357 fsm->li->next = NULL;
358 fsm->li->sb = *st; /* structure assignment */
359 fsm->li->nlink = st->st_nlink;
360 fsm->li->linkIndex = fsm->ix;
361 fsm->li->createdPath = -1;
363 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
364 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
365 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
367 if (fsm->goal == FSM_PKGBUILD)
368 fsm->li->linksLeft = st->st_nlink;
369 if (fsm->goal == FSM_PKGINSTALL)
370 fsm->li->linksLeft = 0;
373 fsm->li->next = fsm->links;
375 fsm->links = fsm->li;
378 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
379 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
380 /*@-observertrans -dependenttrans@*/
381 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
382 /*@=observertrans =dependenttrans@*/
383 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
385 if (fsm->goal == FSM_PKGBUILD)
386 return (fsm->li->linksLeft > 0);
388 if (fsm->goal != FSM_PKGINSTALL)
391 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
394 /* Here come the bits, time to choose a non-skipped file name. */
395 { TFI_t fi = fsmGetFi(fsm);
397 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
398 ix = fsm->li->filex[j];
399 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
405 /* Are all links skipped or not encountered yet? */
407 return 1; /* XXX W2DO? */
409 /* Save the non-skipped file name and map index. */
410 fsm->li->linkIndex = j;
411 fsm->path = _free(fsm->path);
413 rc = fsmStage(fsm, FSM_MAP);
414 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
421 * Destroy set of hard links.
422 * @param li set of hard links
424 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
428 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
429 li->filex = _free(li->filex);
436 FSM_t fsm = xcalloc(1, sizeof(*fsm));
440 FSM_t freeFSM(FSM_t fsm)
443 fsm->path = _free(fsm->path);
444 while ((fsm->li = fsm->links) != NULL) {
445 fsm->links = fsm->li->next;
446 fsm->li->next = NULL;
447 fsm->li = freeHardLink(fsm->li);
449 fsm->dnlx = _free(fsm->dnlx);
450 fsm->ldn = _free(fsm->ldn);
451 fsm->iter = mapFreeIterator(fsm->iter);
456 int fsmSetup(FSM_t fsm, fileStage goal,
457 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
458 unsigned int * archiveSize, const char ** failedFile)
465 fsm->cfd = fdLink(cfd, "persist (fsm)");
466 pos = fdGetCpioPos(fsm->cfd);
467 fdSetCpioPos(fsm->cfd, 0);
469 fsm->iter = mapInitIterator(ts, fi);
471 if (fsm->goal == FSM_PKGINSTALL) {
472 if (ts && ts->notify) {
473 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
474 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
479 fsm->archiveSize = archiveSize;
480 if (fsm->archiveSize)
481 *fsm->archiveSize = 0;
482 fsm->failedFile = failedFile;
484 *fsm->failedFile = NULL;
487 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
488 if (fsm->goal == FSM_PKGINSTALL) {
489 if (ts && ts->id > 0)
490 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
494 rc = fsmStage(fsm, FSM_CREATE);
496 rc = fsmStage(fsm, fsm->goal);
498 if (fsm->archiveSize && rc == 0)
499 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
504 int fsmTeardown(FSM_t fsm) {
508 rc = fsmStage(fsm, FSM_DESTROY);
510 fsm->iter = mapFreeIterator(fsm->iter);
512 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
515 fsm->failedFile = NULL;
516 /*@-nullstate@*/ /* FIX: fsm->iter null annotation? */
521 int fsmMapPath(FSM_t fsm)
523 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
530 fsm->action = FA_UNKNOWN;
534 if (fi && i >= 0 && i < fi->fc) {
536 fsm->astriplen = fi->astriplen;
537 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
538 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
539 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
541 /* src rpms have simple base name in payload. */
542 fsm->dirName = fi->dnl[fi->dil[i]];
543 fsm->baseName = fi->bnl[i];
545 switch (fsm->action) {
548 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
557 assert(fi->type == TR_ADDED);
561 if (fi->fstates && fi->type == TR_ADDED)
562 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
565 case FA_SKIPNETSHARED:
566 if (fi->fstates && fi->type == TR_ADDED)
567 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
571 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
574 fsm->osuffix = SUFFIX_RPMORIG;
577 fsm->osuffix = SUFFIX_RPMSAVE;
583 assert(fi->type == TR_ADDED);
584 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
585 fsm->nsuffix = SUFFIX_RPMNEW;
589 assert(fi->type == TR_ADDED);
590 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
591 fsm->osuffix = SUFFIX_RPMSAVE;
594 assert(fi->type == TR_REMOVED);
596 * XXX TODO: %ghost probably shouldn't be removed, but that changes
597 * legacy rpm behavior.
604 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
605 const struct stat * st = &fsm->sb;
606 fsm->path = _free(fsm->path);
607 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
608 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
609 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
616 int fsmMapAttrs(FSM_t fsm)
618 struct stat * st = &fsm->sb;
619 TFI_t fi = fsmGetFi(fsm);
622 if (fi && i >= 0 && i < fi->fc) {
624 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
626 (fi->fmodes ? fi->fmodes[i] : perms);
628 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
630 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
632 (fi->frdevs ? fi->frdevs[i] : 0);
634 (fi->fmtimes ? fi->fmtimes[i] : 0);
636 if (fsm->mapFlags & CPIO_MAP_MODE)
637 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
638 if (fsm->mapFlags & CPIO_MAP_TYPE) {
639 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
640 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
641 && st->st_nlink == 0)
643 st->st_rdev = finalRdev;
644 st->st_mtime = finalMtime;
646 if (fsm->mapFlags & CPIO_MAP_UID)
647 st->st_uid = finalUid;
648 if (fsm->mapFlags & CPIO_MAP_GID)
649 st->st_gid = finalGid;
651 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
658 * Create file from payload stream.
659 * @todo Legacy: support brokenEndian MD5 checks?
660 * @param fsm file state machine data
661 * @return 0 on success
663 static int expandRegular(/*@special@*/ FSM_t fsm)
665 /*@modifies fsm, fileSystem @*/
667 const char * fmd5sum;
668 const struct stat * st = &fsm->sb;
669 int left = st->st_size;
672 rc = fsmStage(fsm, FSM_WOPEN);
676 /* XXX md5sum's will break on repackaging that includes modified files. */
677 fmd5sum = fsm->fmd5sum;
679 /* XXX This doesn't support brokenEndian checks. */
680 if (st->st_size > 0 && fmd5sum)
681 fdInitMD5(fsm->wfd, 0);
685 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
686 rc = fsmStage(fsm, FSM_DREAD);
690 rc = fsmStage(fsm, FSM_WRITE);
696 /* don't call this with fileSize == fileComplete */
698 (void) fsmStage(fsm, FSM_NOTIFY);
701 if (st->st_size > 0 && fmd5sum) {
702 const char * md5sum = NULL;
704 (void) Fflush(fsm->wfd);
705 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
707 if (md5sum == NULL) {
708 rc = CPIOERR_MD5SUM_MISMATCH;
710 if (strcmp(md5sum, fmd5sum))
711 rc = CPIOERR_MD5SUM_MISMATCH;
712 md5sum = _free(md5sum);
717 (void) fsmStage(fsm, FSM_WCLOSE);
722 * Write next item to payload stream.
723 * @param fsm file state machine data
724 * @param writeData should data be written?
725 * @return 0 on success
727 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
728 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
729 /*@modifies fsm, fileSystem @*/
731 const char * path = fsm->path;
732 const char * opath = fsm->opath;
733 struct stat * st = &fsm->sb;
734 struct stat * ost = &fsm->osb;
735 size_t pos = fdGetCpioPos(fsm->cfd);
736 char * symbuf = NULL;
740 st->st_size = (writeData ? ost->st_size : 0);
742 if (S_ISDIR(st->st_mode)) {
744 } else if (S_ISLNK(st->st_mode)) {
746 * While linux puts the size of a symlink in the st_size field,
747 * I don't think that's a specified standard.
749 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
750 rc = fsmStage(fsm, FSM_READLINK);
752 st->st_size = fsm->rdnb;
753 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
756 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
757 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
758 char * t = alloca(nb);
761 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
763 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
764 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
765 TFI_t fi = fsmGetFi(fsm);
767 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
770 rc = fsmStage(fsm, FSM_HWRITE);
774 if (writeData && S_ISREG(st->st_mode)) {
777 void * mapped = (void *)-1;
781 rc = fsmStage(fsm, FSM_ROPEN);
784 /* XXX unbuffered mmap generates *lots* of fdio debugging */
787 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
788 if (mapped != (void *)-1) {
790 fsm->rdbuf = (char *) mapped;
791 fsm->rdlen = nmapped = st->st_size;
799 if (mapped != (void *)-1) {
804 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
805 rc = fsmStage(fsm, FSM_READ);
809 /* XXX DWRITE uses rdnb for I/O length. */
810 rc = fsmStage(fsm, FSM_DWRITE);
817 if (mapped != (void *)-1) {
818 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
823 } else if (writeData && S_ISLNK(st->st_mode)) {
824 /* XXX DWRITE uses rdnb for I/O length. */
825 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
826 fsm->rdnb = strlen(symbuf);
827 rc = fsmStage(fsm, FSM_DWRITE);
831 rc = fsmStage(fsm, FSM_PAD);
834 { const rpmTransactionSet ts = fsmGetTs(fsm);
835 TFI_t fi = fsmGetFi(fsm);
836 if (ts && fi && ts->notify) {
837 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
838 /*@-modunconnomods@*/
839 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
840 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
841 /*@=modunconnomods@*/
849 (void) fsmStage(fsm, FSM_RCLOSE);
850 /*@-dependenttrans@*/
853 /*@=dependenttrans@*/
858 * Write set of linked files to payload stream.
859 * @param fsm file state machine data
860 * @return 0 on success
862 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
863 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
864 /*@modifies fsm, fileSystem @*/
866 const char * path = fsm->path;
867 const char * nsuffix = fsm->nsuffix;
868 int iterIndex = fsm->ix;
877 for (i = fsm->li->nlink - 1; i >= 0; i--) {
879 if (fsm->li->filex[i] < 0) continue;
881 fsm->ix = fsm->li->filex[i];
882 rc = fsmStage(fsm, FSM_MAP);
884 /* Write data after last link. */
885 rc = writeFile(fsm, (i == 0));
886 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
888 *fsm->failedFile = xstrdup(fsm->path);
891 fsm->path = _free(fsm->path);
892 fsm->li->filex[i] = -1;
896 fsm->nsuffix = nsuffix;
902 * Create pending hard links to existing file.
903 * @param fsm file state machine data
904 * @return 0 on success
906 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
907 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
908 /*@modifies fsm, fileSystem @*/
910 const char * path = fsm->path;
911 const char * opath = fsm->opath;
912 const char * nsuffix = fsm->nsuffix;
913 int iterIndex = fsm->ix;
923 fsm->ix = fsm->li->filex[fsm->li->createdPath];
924 rc = fsmStage(fsm, FSM_MAP);
925 fsm->opath = fsm->path;
927 for (i = 0; i < fsm->li->nlink; i++) {
928 if (fsm->li->filex[i] < 0) continue;
929 if (i == fsm->li->createdPath) continue;
931 fsm->ix = fsm->li->filex[i];
932 fsm->path = _free(fsm->path);
933 rc = fsmStage(fsm, FSM_MAP);
934 rc = fsmStage(fsm, FSM_VERIFY);
936 if (rc != CPIOERR_LSTAT_FAILED) break;
938 /* XXX link(fsm->opath, fsm->path) */
939 rc = fsmStage(fsm, FSM_LINK);
940 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
942 *fsm->failedFile = xstrdup(fsm->path);
945 fsm->li->linksLeft--;
947 fsm->path = _free(fsm->path);
948 fsm->opath = _free(fsm->opath);
951 fsm->nsuffix = nsuffix;
958 * Commit hard linked file set atomically.
959 * @param fsm file state machine data
960 * @return 0 on success
962 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
963 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
964 fsm->li, fsm->links @*/
965 /*@modifies fsm, fileSystem @*/
967 const char * path = fsm->path;
968 const char * nsuffix = fsm->nsuffix;
969 int iterIndex = fsm->ix;
970 struct stat * st = &fsm->sb;
978 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
979 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
983 for (i = 0; i < fsm->li->nlink; i++) {
984 if (fsm->li->filex[i] < 0) continue;
985 fsm->ix = fsm->li->filex[i];
986 rc = fsmStage(fsm, FSM_MAP);
987 rc = fsmStage(fsm, FSM_COMMIT);
988 fsm->path = _free(fsm->path);
989 fsm->li->filex[i] = -1;
993 fsm->nsuffix = nsuffix;
999 * Remove (if created) directories not explicitly included in package.
1000 * @param fsm file state machine data
1001 * @return 0 on success
1004 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1005 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1006 /*@modifies fsm, fileSystem @*/
1008 const char * path = fsm->path;
1009 void * dnli = dnlInitIterator(fsm, 1);
1010 char * dn = fsm->rdbuf;
1011 int dc = dnlCount(dnli);
1016 /*@-observertrans -dependenttrans@*/
1017 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1018 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1019 int dnlen = strlen(fsm->path);
1022 dc = dnlIndex(dnli);
1023 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1026 /* Copy to avoid const on fsm->path. */
1027 te = stpcpy(dn, fsm->path) - 1;
1030 /* Remove generated directories. */
1034 rc = fsmStage(fsm, FSM_RMDIR);
1038 /*@innerbreak@*/ break;
1040 } while ((te - dn) > fsm->dnlx[dc]);
1042 dnli = dnlFreeIterator(dnli);
1043 /*@=observertrans =dependenttrans@*/
1051 * Create (if necessary) directories not explicitly included in package.
1052 * @param fsm file state machine data
1053 * @return 0 on success
1055 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1056 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1057 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1058 /*@defines fsm->dnlx, fsm->ldn @*/
1059 /*@modifies fsm, fileSystem @*/
1061 struct stat * st = &fsm->sb;
1062 struct stat * ost = &fsm->osb;
1063 const char * path = fsm->path;
1064 mode_t st_mode = st->st_mode;
1065 void * dnli = dnlInitIterator(fsm, 0);
1066 char * dn = fsm->rdbuf;
1067 int dc = dnlCount(dnli);
1074 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1075 /*@-observertrans -dependenttrans@*/
1076 if (fsm->dnlx != NULL)
1077 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1078 int dnlen = strlen(fsm->path);
1081 dc = dnlIndex(dnli);
1082 if (dc < 0) continue;
1083 fsm->dnlx[dc] = dnlen;
1087 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1088 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1090 /*@=compdef =nullpass@*/
1092 /* Copy to avoid const on fsm->path. */
1093 (void) stpcpy(dn, fsm->path);
1096 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1097 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1098 if (*te != '/') continue;
1102 /* Already validated? */
1103 /*@-usedef -compdef -nullpass -nullderef@*/
1104 if (i < fsm->ldnlen &&
1105 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1106 !strncmp(fsm->path, fsm->ldn, i))
1109 /* Move pre-existing path marker forward. */
1110 fsm->dnlx[dc] = (te - dn);
1113 /*@=usedef =compdef =nullpass =nullderef@*/
1115 /* Validate next component of path. */
1116 rc = fsmStage(fsm, FSM_LSTAT);
1119 /* Directory already exists? */
1120 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1121 /* Move pre-existing path marker forward. */
1122 fsm->dnlx[dc] = (te - dn);
1123 } else if (rc == CPIOERR_LSTAT_FAILED) {
1124 TFI_t fi = fsmGetFi(fsm);
1126 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1127 rc = fsmStage(fsm, FSM_MKDIR);
1129 rpmMessage(RPMMESS_WARNING,
1130 _("%s directory created with perms %04o.\n"),
1131 fsm->path, (unsigned)(st->st_mode & 07777));
1135 /*@innerbreak@*/ break;
1139 /* Save last validated path. */
1140 if (fsm->ldnalloc < (dnlen + 1)) {
1141 fsm->ldnalloc = dnlen + 100;
1142 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1144 if (fsm->ldn != NULL) { /* XXX can't happen */
1145 strcpy(fsm->ldn, fsm->path);
1146 fsm->ldnlen = dnlen;
1149 dnli = dnlFreeIterator(dnli);
1150 /*@=observertrans =dependenttrans@*/
1153 st->st_mode = st_mode; /* XXX restore st->st_mode */
1159 * Check for file on disk.
1160 * @param fsm file state machine data
1161 * @return 0 on success
1163 static int fsmStat(FSM_t fsm)
1165 int saveerrno = errno;
1168 if (fsm->path != NULL) {
1169 int saveernno = errno;
1170 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1171 ? FSM_LSTAT : FSM_STAT));
1172 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1176 } else if (rc == 0) {
1180 /* Skip %ghost files on build. */
1188 int fsmStage(FSM_t fsm, fileStage stage)
1191 fileStage prevStage = fsm->stage;
1192 const char * const prev = fileStageString(prevStage);
1194 static int modulo = 4;
1195 const char * const cur = fileStageString(stage);
1196 struct stat * st = &fsm->sb;
1197 struct stat * ost = &fsm->osb;
1198 int saveerrno = errno;
1203 #define _fafilter(_a) \
1204 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1205 ? fileActionString(_a) : "")
1207 if (stage & FSM_DEAD) {
1209 } else if (stage & FSM_INTERNAL) {
1210 if (_fsm_debug && !(stage & FSM_SYSCALL))
1211 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1213 (unsigned)st->st_mode, (int)st->st_nlink,
1214 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1215 (fsm->path ? fsm->path : ""),
1216 _fafilter(fsm->action));
1219 if (_fsm_debug || !(stage & FSM_VERBOSE))
1220 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1222 (unsigned)st->st_mode, (int)st->st_nlink,
1223 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1224 (fsm->path ? fsm->path + fsm->astriplen : ""),
1225 _fafilter(fsm->action));
1232 case FSM_PKGINSTALL:
1234 /* Clean fsm, free'ing memory. Read next archive header. */
1235 rc = fsmStage(fsm, FSM_INIT);
1237 /* Exit on end-of-payload. */
1238 if (rc == CPIOERR_HDR_TRAILER) {
1240 /*@loopbreak@*/ break;
1243 /* Exit on error. */
1246 (void) fsmStage(fsm, FSM_UNDO);
1247 /*@loopbreak@*/ break;
1250 /* Extract file from archive. */
1251 rc = fsmStage(fsm, FSM_PROCESS);
1253 (void) fsmStage(fsm, FSM_UNDO);
1254 /*@loopbreak@*/ break;
1257 /* Notify on success. */
1258 (void) fsmStage(fsm, FSM_NOTIFY);
1260 if (fsmStage(fsm, FSM_FINI))
1261 /*@loopbreak@*/ break;
1267 /* Clean fsm, free'ing memory. */
1268 rc = fsmStage(fsm, FSM_INIT);
1270 /* Exit on end-of-payload. */
1271 if (rc == CPIOERR_HDR_TRAILER) {
1273 /*@loopbreak@*/ break;
1276 /* Rename/erase next item. */
1277 if (fsmStage(fsm, FSM_FINI))
1278 /*@loopbreak@*/ break;
1284 rc = fsmStage(fsm, FSM_INIT);
1286 /* Exit on end-of-payload. */
1287 if (rc == CPIOERR_HDR_TRAILER) {
1289 /*@loopbreak@*/ break;
1292 /* Exit on error. */
1295 (void) fsmStage(fsm, FSM_UNDO);
1296 /*@loopbreak@*/ break;
1299 /* Copy file into archive. */
1300 rc = fsmStage(fsm, FSM_PROCESS);
1302 (void) fsmStage(fsm, FSM_UNDO);
1303 /*@loopbreak@*/ break;
1306 if (fsmStage(fsm, FSM_FINI))
1307 /*@loopbreak@*/ break;
1310 /* Flush partial sets of hard linked files. */
1311 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1313 while ((fsm->li = fsm->links) != NULL) {
1314 fsm->links = fsm->li->next;
1315 fsm->li->next = NULL;
1317 /* Re-calculate link count for archive header. */
1318 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1319 if (fsm->li->filex[i] < 0) continue;
1323 /* XXX force the contents out as well. */
1325 fsm->li->filex[0] = fsm->li->filex[j];
1326 fsm->li->filex[j] = -1;
1328 fsm->li->sb.st_nlink = nlink;
1330 fsm->sb = fsm->li->sb; /* structure assignment */
1331 fsm->osb = fsm->sb; /* structure assignment */
1333 if (!rc) rc = writeLinkedFile(fsm);
1335 fsm->li = freeHardLink(fsm->li);
1340 rc = fsmStage(fsm, FSM_TRAILER);
1344 { rpmTransactionSet ts = fsmGetTs(fsm);
1345 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1346 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1347 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1350 fsm->path = _free(fsm->path);
1351 fsm->opath = _free(fsm->opath);
1352 fsm->dnlx = _free(fsm->dnlx);
1354 fsm->ldn = _free(fsm->ldn);
1355 fsm->ldnalloc = fsm->ldnlen = 0;
1357 fsm->rdsize = fsm->wrsize = 0;
1358 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1359 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1360 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1361 fsm->rdsize = 8 * BUFSIZ;
1362 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1363 fsm->wrsize = 8 * BUFSIZ;
1364 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1367 fsm->mkdirsdone = 0;
1371 errno = 0; /* XXX get rid of EBADF */
1373 /* Detect and create directories not explicitly in package. */
1374 if (fsm->goal == FSM_PKGINSTALL) {
1375 rc = fsmStage(fsm, FSM_MKDIRS);
1376 if (!rc) fsm->mkdirsdone = 1;
1381 fsm->path = _free(fsm->path);
1383 fsm->diskchecked = fsm->exists = 0;
1385 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1386 fsm->action = FA_UNKNOWN;
1387 fsm->osuffix = NULL;
1388 fsm->nsuffix = NULL;
1390 if (fsm->goal == FSM_PKGINSTALL) {
1391 /* Read next header from payload, checking for end-of-payload. */
1392 rc = fsmStage(fsm, FSM_NEXT);
1396 /* Identify mapping index. */
1397 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1398 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1400 /* On non-install, detect end-of-loop. */
1401 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1402 rc = CPIOERR_HDR_TRAILER;
1406 /* On non-install, mode must be known so that dirs don't get suffix. */
1407 if (fsm->goal != FSM_PKGINSTALL) {
1408 TFI_t fi = fsmGetFi(fsm);
1409 st->st_mode = fi->fmodes[fsm->ix];
1412 /* Generate file path. */
1413 rc = fsmStage(fsm, FSM_MAP);
1416 /* Perform lstat/stat for disk file. */
1420 if (fsm->path != NULL) {
1421 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1422 ? FSM_LSTAT : FSM_STAT));
1423 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1427 } else if (rc == 0) {
1431 /* Skip %ghost files on build. */
1435 fsm->diskchecked = 1;
1438 /* On non-install, the disk file stat is what's remapped. */
1439 if (fsm->goal != FSM_PKGINSTALL)
1440 *st = *ost; /* structure assignment */
1442 /* Remap file perms, owner, and group. */
1443 rc = fsmMapAttrs(fsm);
1446 fsm->postpone = XFA_SKIPPING(fsm->action);
1447 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1449 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1450 fsm->postpone = saveHardLink(fsm);
1457 rc = fsmMapPath(fsm);
1460 rc = fsmMkdirs(fsm);
1464 rc = fsmRmdirs(fsm);
1467 if (fsm->postpone) {
1468 if (fsm->goal == FSM_PKGINSTALL)
1469 rc = fsmStage(fsm, FSM_EAT);
1473 if (fsm->goal == FSM_PKGBUILD) {
1474 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1475 struct hardLink * li, * prev;
1477 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1478 rc = writeLinkedFile(fsm);
1479 if (rc) break; /* W2DO? */
1481 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1483 /*@loopbreak@*/ break;
1486 fsm->links = fsm->li->next;
1488 prev->next = fsm->li->next;
1489 fsm->li->next = NULL;
1490 fsm->li = freeHardLink(fsm->li);
1492 rc = writeFile(fsm, 1);
1497 if (fsm->goal != FSM_PKGINSTALL)
1500 if (S_ISREG(st->st_mode)) {
1501 const char * path = fsm->path;
1503 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1504 rc = fsmStage(fsm, FSM_VERIFY);
1506 if (rc == 0 && fsm->osuffix) {
1507 const char * opath = fsm->opath;
1508 fsm->opath = fsm->path;
1509 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1510 rc = fsmStage(fsm, FSM_RENAME);
1512 rpmMessage(RPMMESS_WARNING,
1513 _("%s saved as %s\n"), fsm->opath, fsm->path);
1514 fsm->path = _free(fsm->path);
1518 /*@-dependenttrans@*/
1520 /*@=dependenttrans@*/
1521 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1522 rc = expandRegular(fsm);
1523 } else if (S_ISDIR(st->st_mode)) {
1524 mode_t st_mode = st->st_mode;
1525 rc = fsmStage(fsm, FSM_VERIFY);
1526 if (rc == CPIOERR_LSTAT_FAILED) {
1527 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1528 st->st_mode |= 00700;
1529 rc = fsmStage(fsm, FSM_MKDIR);
1530 st->st_mode = st_mode; /* XXX restore st->st_mode */
1532 } else if (S_ISLNK(st->st_mode)) {
1533 const char * opath = fsm->opath;
1535 if ((st->st_size + 1) > fsm->rdsize) {
1536 rc = CPIOERR_HDR_SIZE;
1540 fsm->wrlen = st->st_size;
1541 rc = fsmStage(fsm, FSM_DREAD);
1542 if (!rc && fsm->rdnb != fsm->wrlen)
1543 rc = CPIOERR_READ_FAILED;
1546 fsm->wrbuf[st->st_size] = '\0';
1547 /* XXX symlink(fsm->opath, fsm->path) */
1548 /*@-dependenttrans@*/
1549 fsm->opath = fsm->wrbuf;
1550 /*@=dependenttrans@*/
1551 rc = fsmStage(fsm, FSM_VERIFY);
1552 if (rc == CPIOERR_LSTAT_FAILED)
1553 rc = fsmStage(fsm, FSM_SYMLINK);
1554 fsm->opath = opath; /* XXX restore fsm->path */
1555 } else if (S_ISFIFO(st->st_mode)) {
1556 mode_t st_mode = st->st_mode;
1557 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1558 rc = fsmStage(fsm, FSM_VERIFY);
1559 if (rc == CPIOERR_LSTAT_FAILED) {
1560 st->st_mode = 0000; /* XXX abuse st->st_mode */
1561 rc = fsmStage(fsm, FSM_MKFIFO);
1562 st->st_mode = st_mode; /* XXX restore st->st_mode */
1564 } else if (S_ISCHR(st->st_mode) ||
1565 S_ISBLK(st->st_mode) ||
1566 S_ISSOCK(st->st_mode))
1568 rc = fsmStage(fsm, FSM_VERIFY);
1569 if (rc == CPIOERR_LSTAT_FAILED)
1570 rc = fsmStage(fsm, FSM_MKNOD);
1572 rc = CPIOERR_UNKNOWN_FILETYPE;
1574 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1575 fsm->li->createdPath = fsm->li->linkIndex;
1576 rc = fsmMakeLinks(fsm);
1582 rc = fsmMakeLinks(fsm);
1584 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1585 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1586 rpmTransactionSet ts = fsmGetTs(fsm);
1587 TFI_t fi = fsmGetFi(fsm);
1588 if (ts && ts->notify && fi)
1589 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1590 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1591 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1597 if (fsm->goal == FSM_PKGINSTALL) {
1598 (void) fsmStage(fsm,
1599 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1601 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1603 (void) fsmStage(fsm, FSM_RMDIRS);
1607 if (fsm->failedFile && *fsm->failedFile == NULL)
1608 *fsm->failedFile = xstrdup(fsm->path);
1611 if (!fsm->postpone && fsm->commit) {
1612 if (fsm->goal == FSM_PKGINSTALL)
1613 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1614 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1615 if (fsm->goal == FSM_PKGCOMMIT)
1616 rc = fsmStage(fsm, FSM_COMMIT);
1617 if (fsm->goal == FSM_PKGERASE)
1618 rc = fsmStage(fsm, FSM_COMMIT);
1620 fsm->path = _free(fsm->path);
1621 fsm->opath = _free(fsm->opath);
1622 memset(st, 0, sizeof(*st));
1623 memset(ost, 0, sizeof(*ost));
1626 /* Rename pre-existing modified or unmanaged file. */
1627 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1628 const char * opath = fsm->opath;
1629 const char * path = fsm->path;
1630 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1631 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1633 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1634 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1636 rc = fsmStage(fsm, FSM_RENAME);
1638 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1639 fsm->opath, fsm->path);
1641 fsm->path = _free(fsm->path);
1643 fsm->opath = _free(fsm->opath);
1647 /* Remove erased files. */
1648 if (fsm->goal == FSM_PKGERASE) {
1649 if (fsm->action == FA_ERASE) {
1650 TFI_t fi = fsmGetFi(fsm);
1651 if (S_ISDIR(st->st_mode)) {
1652 rc = fsmStage(fsm, FSM_RMDIR);
1655 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1657 /* XXX make sure that build side permits %missingok on directories. */
1658 if (fsm->fflags & RPMFILE_MISSINGOK)
1661 /* XXX common error message. */
1663 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1664 _("%s rmdir of %s failed: Directory not empty\n"),
1665 fiTypeString(fi), fsm->path);
1669 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1670 _("%s rmdir of %s failed: %s\n"),
1671 fiTypeString(fi), fsm->path, strerror(errno));
1675 rc = fsmStage(fsm, FSM_UNLINK);
1677 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1679 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1680 _("%s unlink of %s failed: %s\n"),
1681 fiTypeString(fi), fsm->path, strerror(errno));
1684 /* XXX Failure to remove is not (yet) cause for failure. */
1685 if (!strict_erasures) rc = 0;
1689 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1690 /* Rename temporary to final file name. */
1691 if (!S_ISDIR(st->st_mode) &&
1692 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1694 fsm->opath = fsm->path;
1695 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1696 rc = fsmStage(fsm, FSM_RENAME);
1697 if (!rc && fsm->nsuffix) {
1698 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1699 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1700 (opath ? opath : ""), fsm->path);
1701 opath = _free(opath);
1703 fsm->opath = _free(fsm->opath);
1705 if (S_ISLNK(st->st_mode)) {
1706 if (!rc && !getuid())
1707 rc = fsmStage(fsm, FSM_LCHOWN);
1709 if (!rc && !getuid())
1710 rc = fsmStage(fsm, FSM_CHOWN);
1712 rc = fsmStage(fsm, FSM_CHMOD);
1714 time_t mtime = st->st_mtime;
1715 TFI_t fi = fsmGetFi(fsm);
1717 st->st_mtime = fi->fmtimes[fsm->ix];
1718 rc = fsmStage(fsm, FSM_UTIME);
1719 st->st_mtime = mtime;
1724 /* Notify on success. */
1725 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1728 fsm->path = _free(fsm->path);
1730 /* Check for hard links missing from payload. */
1731 while ((fsm->li = fsm->links) != NULL) {
1732 fsm->links = fsm->li->next;
1733 fsm->li->next = NULL;
1734 if (fsm->goal == FSM_PKGINSTALL &&
1735 fsm->commit && fsm->li->linksLeft)
1737 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1738 if (fsm->li->filex[i] < 0) continue;
1739 rc = CPIOERR_MISSING_HARDLINK;
1740 if (fsm->failedFile && *fsm->failedFile == NULL) {
1741 fsm->ix = fsm->li->filex[i];
1742 if (!fsmStage(fsm, FSM_MAP)) {
1743 *fsm->failedFile = fsm->path;
1747 /*@loopbreak@*/ break;
1750 if (fsm->goal == FSM_PKGBUILD &&
1751 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1753 rc = CPIOERR_MISSING_HARDLINK;
1755 fsm->li = freeHardLink(fsm->li);
1757 fsm->ldn = _free(fsm->ldn);
1758 fsm->ldnalloc = fsm->ldnlen = 0;
1759 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1760 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1763 if (fsm->diskchecked && !fsm->exists) {
1764 rc = CPIOERR_LSTAT_FAILED;
1767 if (S_ISREG(st->st_mode)) {
1768 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1769 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1771 * XXX HP-UX (and other os'es) don't permit unlink on busy
1774 fsm->opath = fsm->path;
1776 rc = fsmStage(fsm, FSM_RENAME);
1778 (void) fsmStage(fsm, FSM_UNLINK);
1780 rc = CPIOERR_UNLINK_FAILED;
1781 fsm->path = fsm->opath;
1783 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1784 /*@notreached@*/ break;
1785 } else if (S_ISDIR(st->st_mode)) {
1786 if (S_ISDIR(ost->st_mode)) return 0;
1787 if (S_ISLNK(ost->st_mode)) {
1788 rc = fsmStage(fsm, FSM_STAT);
1789 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1792 if (S_ISDIR(ost->st_mode)) return 0;
1794 } else if (S_ISLNK(st->st_mode)) {
1795 if (S_ISLNK(ost->st_mode)) {
1796 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1797 rc = fsmStage(fsm, FSM_READLINK);
1800 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1802 } else if (S_ISFIFO(st->st_mode)) {
1803 if (S_ISFIFO(ost->st_mode)) return 0;
1804 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1805 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1806 (ost->st_rdev == st->st_rdev)) return 0;
1807 } else if (S_ISSOCK(st->st_mode)) {
1808 if (S_ISSOCK(ost->st_mode)) return 0;
1810 /* XXX shouldn't do this with commit/undo. */
1812 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1813 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1814 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1815 /*@notreached@*/ break;
1818 rc = Unlink(fsm->path);
1819 if (_fsm_debug && (stage & FSM_SYSCALL))
1820 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1821 fsm->path, (rc < 0 ? strerror(errno) : ""));
1822 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1825 rc = Rename(fsm->opath, fsm->path);
1826 if (_fsm_debug && (stage & FSM_SYSCALL))
1827 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1828 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1829 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1832 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1833 if (_fsm_debug && (stage & FSM_SYSCALL))
1834 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1835 fsm->path, (unsigned)(st->st_mode & 07777),
1836 (rc < 0 ? strerror(errno) : ""));
1837 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1840 rc = Rmdir(fsm->path);
1841 if (_fsm_debug && (stage & FSM_SYSCALL))
1842 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1843 fsm->path, (rc < 0 ? strerror(errno) : ""));
1844 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1847 rc = chown(fsm->path, st->st_uid, st->st_gid);
1848 if (_fsm_debug && (stage & FSM_SYSCALL))
1849 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1850 fsm->path, (int)st->st_uid, (int)st->st_gid,
1851 (rc < 0 ? strerror(errno) : ""));
1852 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1855 #if ! CHOWN_FOLLOWS_SYMLINK
1856 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1857 if (_fsm_debug && (stage & FSM_SYSCALL))
1858 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1859 fsm->path, (int)st->st_uid, (int)st->st_gid,
1860 (rc < 0 ? strerror(errno) : ""));
1861 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1865 rc = chmod(fsm->path, (st->st_mode & 07777));
1866 if (_fsm_debug && (stage & FSM_SYSCALL))
1867 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1868 fsm->path, (unsigned)(st->st_mode & 07777),
1869 (rc < 0 ? strerror(errno) : ""));
1870 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1873 { struct utimbuf stamp;
1874 stamp.actime = st->st_mtime;
1875 stamp.modtime = st->st_mtime;
1876 rc = utime(fsm->path, &stamp);
1877 if (_fsm_debug && (stage & FSM_SYSCALL))
1878 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1879 fsm->path, (unsigned)st->st_mtime,
1880 (rc < 0 ? strerror(errno) : ""));
1881 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1885 rc = symlink(fsm->opath, fsm->path);
1886 if (_fsm_debug && (stage & FSM_SYSCALL))
1887 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1888 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1889 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1892 rc = Link(fsm->opath, fsm->path);
1893 if (_fsm_debug && (stage & FSM_SYSCALL))
1894 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1895 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1896 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1899 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1900 if (_fsm_debug && (stage & FSM_SYSCALL))
1901 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1902 fsm->path, (unsigned)(st->st_mode & 07777),
1903 (rc < 0 ? strerror(errno) : ""));
1904 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1908 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1910 if (_fsm_debug && (stage & FSM_SYSCALL))
1911 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1912 fsm->path, (unsigned)(st->st_mode & ~07777),
1913 (unsigned)st->st_rdev,
1914 (rc < 0 ? strerror(errno) : ""));
1915 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1918 rc = Lstat(fsm->path, ost);
1919 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1920 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1921 fsm->path, (rc < 0 ? strerror(errno) : ""));
1922 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1925 rc = Stat(fsm->path, ost);
1926 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1927 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1928 fsm->path, (rc < 0 ? strerror(errno) : ""));
1929 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1932 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1933 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1934 if (_fsm_debug && (stage & FSM_SYSCALL))
1935 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1936 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
1937 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1940 fsm->rdbuf[fsm->rdnb] = '\0';
1948 rc = fsmStage(fsm, FSM_HREAD);
1950 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1951 fsm->path = _free(fsm->path);
1952 rc = CPIOERR_HDR_TRAILER;
1955 rc = fsmStage(fsm, FSM_POS);
1958 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1959 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1960 rc = fsmStage(fsm, FSM_DREAD);
1962 /*@loopbreak@*/ break;
1966 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1969 (void) fsmStage(fsm, FSM_DREAD);
1973 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1975 memset(fsm->rdbuf, 0, left);
1976 /* XXX DWRITE uses rdnb for I/O length. */
1978 (void) fsmStage(fsm, FSM_DWRITE);
1982 rc = cpioTrailerWrite(fsm);
1985 rc = fsmStage(fsm, FSM_POS);
1987 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1990 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1993 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1994 if (_fsm_debug && (stage & FSM_SYSCALL))
1995 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1996 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1997 (int)fsm->wrlen, (int)fsm->rdnb);
1998 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
1999 rc = CPIOERR_READ_FAILED;
2001 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2004 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2005 if (_fsm_debug && (stage & FSM_SYSCALL))
2006 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2007 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2008 (int)fsm->rdnb, (int)fsm->wrnb);
2009 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2010 rc = CPIOERR_WRITE_FAILED;
2012 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2016 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2017 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2018 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2020 rc = CPIOERR_OPEN_FAILED;
2023 if (_fsm_debug && (stage & FSM_SYSCALL))
2024 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2025 fsm->path, fsm->rfd, fsm->rdbuf);
2028 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2029 if (_fsm_debug && (stage & FSM_SYSCALL))
2030 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2031 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2032 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2033 rc = CPIOERR_READ_FAILED;
2037 if (_fsm_debug && (stage & FSM_SYSCALL))
2038 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2039 (void) Fclose(fsm->rfd);
2045 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2046 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2047 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2049 rc = CPIOERR_OPEN_FAILED;
2051 if (_fsm_debug && (stage & FSM_SYSCALL))
2052 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2053 fsm->path, fsm->wfd, fsm->wrbuf);
2056 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2057 if (_fsm_debug && (stage & FSM_SYSCALL))
2058 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2059 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2060 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2061 rc = CPIOERR_WRITE_FAILED;
2065 if (_fsm_debug && (stage & FSM_SYSCALL))
2066 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2067 (void) Fclose(fsm->wfd);
2077 if (!(stage & FSM_INTERNAL)) {
2078 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2084 /*@obserever@*/ const char *const fileActionString(fileAction a)
2087 case FA_UNKNOWN: return "unknown";
2088 case FA_CREATE: return "create";
2089 case FA_COPYOUT: return "copyout";
2090 case FA_COPYIN: return "copyin";
2091 case FA_BACKUP: return "backup";
2092 case FA_SAVE: return "save";
2093 case FA_SKIP: return "skip";
2094 case FA_ALTNAME: return "altname";
2095 case FA_ERASE: return "erase";
2096 case FA_SKIPNSTATE: return "skipnstate";
2097 case FA_SKIPNETSHARED: return "skipnetshared";
2098 case FA_SKIPMULTILIB: return "skipmultilib";
2099 default: return "???";
2104 /*@observer@*/ const char *const fileStageString(fileStage a) {
2106 case FSM_UNKNOWN: return "unknown";
2108 case FSM_PKGINSTALL:return "pkginstall";
2109 case FSM_PKGERASE: return "pkgerase";
2110 case FSM_PKGBUILD: return "pkgbuild";
2111 case FSM_PKGCOMMIT: return "pkgcommit";
2112 case FSM_PKGUNDO: return "pkgundo";
2114 case FSM_CREATE: return "create";
2115 case FSM_INIT: return "init";
2116 case FSM_MAP: return "map";
2117 case FSM_MKDIRS: return "mkdirs";
2118 case FSM_RMDIRS: return "rmdirs";
2119 case FSM_PRE: return "pre";
2120 case FSM_PROCESS: return "process";
2121 case FSM_POST: return "post";
2122 case FSM_MKLINKS: return "mklinks";
2123 case FSM_NOTIFY: return "notify";
2124 case FSM_UNDO: return "undo";
2125 case FSM_FINI: return "fini";
2126 case FSM_COMMIT: return "commit";
2127 case FSM_DESTROY: return "destroy";
2128 case FSM_VERIFY: return "verify";
2130 case FSM_UNLINK: return "Unlink";
2131 case FSM_RENAME: return "Rename";
2132 case FSM_MKDIR: return "Mkdir";
2133 case FSM_RMDIR: return "rmdir";
2134 case FSM_CHOWN: return "chown";
2135 case FSM_LCHOWN: return "lchown";
2136 case FSM_CHMOD: return "chmod";
2137 case FSM_UTIME: return "utime";
2138 case FSM_SYMLINK: return "symlink";
2139 case FSM_LINK: return "Link";
2140 case FSM_MKFIFO: return "mkfifo";
2141 case FSM_MKNOD: return "mknod";
2142 case FSM_LSTAT: return "Lstat";
2143 case FSM_STAT: return "Stat";
2144 case FSM_READLINK: return "Readlink";
2145 case FSM_CHROOT: return "chroot";
2147 case FSM_NEXT: return "next";
2148 case FSM_EAT: return "eat";
2149 case FSM_POS: return "pos";
2150 case FSM_PAD: return "pad";
2151 case FSM_TRAILER: return "trailer";
2152 case FSM_HREAD: return "hread";
2153 case FSM_HWRITE: return "hwrite";
2154 case FSM_DREAD: return "Fread";
2155 case FSM_DWRITE: return "Fwrite";
2157 case FSM_ROPEN: return "Fopen";
2158 case FSM_READ: return "Fread";
2159 case FSM_RCLOSE: return "Fclose";
2160 case FSM_WOPEN: return "Fopen";
2161 case FSM_WRITE: return "Fwrite";
2162 case FSM_WCLOSE: return "Fclose";
2164 default: return "???";