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 if (fsm->mapFlags & CPIO_MAP_MODE)
633 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
634 if (fsm->mapFlags & CPIO_MAP_TYPE) {
635 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
636 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
637 && st->st_nlink == 0)
640 if (fsm->mapFlags & CPIO_MAP_UID)
641 st->st_uid = finalUid;
642 if (fsm->mapFlags & CPIO_MAP_GID)
643 st->st_gid = finalGid;
645 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
652 * Create file from payload stream.
653 * @todo Legacy: support brokenEndian MD5 checks?
654 * @param fsm file state machine data
655 * @return 0 on success
657 static int expandRegular(/*@special@*/ FSM_t fsm)
659 /*@modifies fsm, fileSystem @*/
661 const char * fmd5sum;
662 const struct stat * st = &fsm->sb;
663 int left = st->st_size;
666 rc = fsmStage(fsm, FSM_WOPEN);
670 /* XXX md5sum's will break on repackaging that includes modified files. */
671 fmd5sum = fsm->fmd5sum;
673 /* XXX This doesn't support brokenEndian checks. */
674 if (st->st_size > 0 && fmd5sum)
675 fdInitMD5(fsm->wfd, 0);
679 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
680 rc = fsmStage(fsm, FSM_DREAD);
684 rc = fsmStage(fsm, FSM_WRITE);
690 /* don't call this with fileSize == fileComplete */
692 (void) fsmStage(fsm, FSM_NOTIFY);
695 if (st->st_size > 0 && fmd5sum) {
696 const char * md5sum = NULL;
698 (void) Fflush(fsm->wfd);
699 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
701 if (md5sum == NULL) {
702 rc = CPIOERR_MD5SUM_MISMATCH;
704 if (strcmp(md5sum, fmd5sum))
705 rc = CPIOERR_MD5SUM_MISMATCH;
706 md5sum = _free(md5sum);
711 (void) fsmStage(fsm, FSM_WCLOSE);
716 * Write next item to payload stream.
717 * @param fsm file state machine data
718 * @param writeData should data be written?
719 * @return 0 on success
721 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
722 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
723 /*@modifies fsm, fileSystem @*/
725 const char * path = fsm->path;
726 const char * opath = fsm->opath;
727 struct stat * st = &fsm->sb;
728 struct stat * ost = &fsm->osb;
729 size_t pos = fdGetCpioPos(fsm->cfd);
730 char * symbuf = NULL;
734 st->st_size = (writeData ? ost->st_size : 0);
736 if (S_ISDIR(st->st_mode)) {
738 } else if (S_ISLNK(st->st_mode)) {
740 * While linux puts the size of a symlink in the st_size field,
741 * I don't think that's a specified standard.
743 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
744 rc = fsmStage(fsm, FSM_READLINK);
746 st->st_size = fsm->rdnb;
747 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
750 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
751 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
752 char * t = alloca(nb);
755 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
757 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
758 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
759 TFI_t fi = fsmGetFi(fsm);
761 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
764 rc = fsmStage(fsm, FSM_HWRITE);
768 if (writeData && S_ISREG(st->st_mode)) {
771 void * mapped = (void *)-1;
775 rc = fsmStage(fsm, FSM_ROPEN);
778 /* XXX unbuffered mmap generates *lots* of fdio debugging */
781 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
782 if (mapped != (void *)-1) {
784 fsm->rdbuf = (char *) mapped;
785 fsm->rdlen = nmapped = st->st_size;
793 if (mapped != (void *)-1) {
798 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
799 rc = fsmStage(fsm, FSM_READ);
803 /* XXX DWRITE uses rdnb for I/O length. */
804 rc = fsmStage(fsm, FSM_DWRITE);
811 if (mapped != (void *)-1) {
812 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
817 } else if (writeData && S_ISLNK(st->st_mode)) {
818 /* XXX DWRITE uses rdnb for I/O length. */
819 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
820 fsm->rdnb = strlen(symbuf);
821 rc = fsmStage(fsm, FSM_DWRITE);
825 rc = fsmStage(fsm, FSM_PAD);
828 { const rpmTransactionSet ts = fsmGetTs(fsm);
829 TFI_t fi = fsmGetFi(fsm);
830 if (ts && fi && ts->notify) {
831 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
832 /*@-modunconnomods@*/
833 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
834 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
835 /*@=modunconnomods@*/
843 (void) fsmStage(fsm, FSM_RCLOSE);
844 /*@-dependenttrans@*/
847 /*@=dependenttrans@*/
852 * Write set of linked files to payload stream.
853 * @param fsm file state machine data
854 * @return 0 on success
856 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
857 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
858 /*@modifies fsm, fileSystem @*/
860 const char * path = fsm->path;
861 const char * nsuffix = fsm->nsuffix;
862 int iterIndex = fsm->ix;
871 for (i = fsm->li->nlink - 1; i >= 0; i--) {
873 if (fsm->li->filex[i] < 0) continue;
875 fsm->ix = fsm->li->filex[i];
876 rc = fsmStage(fsm, FSM_MAP);
878 /* Write data after last link. */
879 rc = writeFile(fsm, (i == 0));
880 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
882 *fsm->failedFile = xstrdup(fsm->path);
885 fsm->path = _free(fsm->path);
886 fsm->li->filex[i] = -1;
890 fsm->nsuffix = nsuffix;
896 * Create pending hard links to existing file.
897 * @param fsm file state machine data
898 * @return 0 on success
900 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
901 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
902 /*@modifies fsm, fileSystem @*/
904 const char * path = fsm->path;
905 const char * opath = fsm->opath;
906 const char * nsuffix = fsm->nsuffix;
907 int iterIndex = fsm->ix;
917 fsm->ix = fsm->li->filex[fsm->li->createdPath];
918 rc = fsmStage(fsm, FSM_MAP);
919 fsm->opath = fsm->path;
921 for (i = 0; i < fsm->li->nlink; i++) {
922 if (fsm->li->filex[i] < 0) continue;
923 if (i == fsm->li->createdPath) continue;
925 fsm->ix = fsm->li->filex[i];
926 rc = fsmStage(fsm, FSM_MAP);
927 rc = fsmStage(fsm, FSM_VERIFY);
929 if (rc != CPIOERR_LSTAT_FAILED) break;
931 /* XXX link(fsm->opath, fsm->path) */
932 rc = fsmStage(fsm, FSM_LINK);
933 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
935 *fsm->failedFile = xstrdup(fsm->path);
938 fsm->li->linksLeft--;
940 fsm->opath = _free(fsm->opath);
943 fsm->nsuffix = nsuffix;
950 * Commit hard linked file set atomically.
951 * @param fsm file state machine data
952 * @return 0 on success
954 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
955 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
956 fsm->li, fsm->links @*/
957 /*@modifies fsm, fileSystem @*/
959 const char * path = fsm->path;
960 const char * nsuffix = fsm->nsuffix;
961 int iterIndex = fsm->ix;
962 struct stat * st = &fsm->sb;
970 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
971 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
975 for (i = 0; i < fsm->li->nlink; i++) {
976 if (fsm->li->filex[i] < 0) continue;
977 fsm->ix = fsm->li->filex[i];
978 rc = fsmStage(fsm, FSM_MAP);
979 rc = fsmStage(fsm, FSM_COMMIT);
980 fsm->path = _free(fsm->path);
981 fsm->li->filex[i] = -1;
985 fsm->nsuffix = nsuffix;
991 * Remove (if created) directories not explicitly included in package.
992 * @param fsm file state machine data
993 * @return 0 on success
996 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
997 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
998 /*@modifies fsm, fileSystem @*/
1000 const char * path = fsm->path;
1001 void * dnli = dnlInitIterator(fsm, 1);
1002 char * dn = fsm->rdbuf;
1003 int dc = dnlCount(dnli);
1008 /*@-observertrans -dependenttrans@*/
1009 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1010 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1011 int dnlen = strlen(fsm->path);
1014 dc = dnlIndex(dnli);
1015 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1018 /* Copy to avoid const on fsm->path. */
1019 te = stpcpy(dn, fsm->path) - 1;
1022 /* Remove generated directories. */
1026 rc = fsmStage(fsm, FSM_RMDIR);
1030 /*@innerbreak@*/ break;
1032 } while ((te - dn) > fsm->dnlx[dc]);
1034 dnli = dnlFreeIterator(dnli);
1035 /*@=observertrans =dependenttrans@*/
1043 * Create (if necessary) directories not explicitly included in package.
1044 * @param fsm file state machine data
1045 * @return 0 on success
1047 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1048 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1049 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1050 /*@defines fsm->dnlx, fsm->ldn @*/
1051 /*@modifies fsm, fileSystem @*/
1053 struct stat * st = &fsm->sb;
1054 struct stat * ost = &fsm->osb;
1055 const char * path = fsm->path;
1056 mode_t st_mode = st->st_mode;
1057 void * dnli = dnlInitIterator(fsm, 0);
1058 char * dn = fsm->rdbuf;
1059 int dc = dnlCount(dnli);
1066 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1067 /*@-observertrans -dependenttrans@*/
1068 if (fsm->dnlx != NULL)
1069 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1070 int dnlen = strlen(fsm->path);
1073 dc = dnlIndex(dnli);
1074 if (dc < 0) continue;
1075 fsm->dnlx[dc] = dnlen;
1079 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1080 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1082 /*@=compdef =nullpass@*/
1084 /* Copy to avoid const on fsm->path. */
1085 (void) stpcpy(dn, fsm->path);
1088 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1089 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1090 if (*te != '/') continue;
1094 /* Already validated? */
1095 /*@-usedef -compdef -nullpass -nullderef@*/
1096 if (i < fsm->ldnlen &&
1097 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1098 !strncmp(fsm->path, fsm->ldn, i))
1101 /* Move pre-existing path marker forward. */
1102 fsm->dnlx[dc] = (te - dn);
1105 /*@=usedef =compdef =nullpass =nullderef@*/
1107 /* Validate next component of path. */
1108 rc = fsmStage(fsm, FSM_LSTAT);
1111 /* Directory already exists? */
1112 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1113 /* Move pre-existing path marker forward. */
1114 fsm->dnlx[dc] = (te - dn);
1115 } else if (rc == CPIOERR_LSTAT_FAILED) {
1116 TFI_t fi = fsmGetFi(fsm);
1118 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1119 rc = fsmStage(fsm, FSM_MKDIR);
1121 rpmMessage(RPMMESS_WARNING,
1122 _("%s directory created with perms %04o.\n"),
1123 fsm->path, (unsigned)(st->st_mode & 07777));
1127 /*@innerbreak@*/ break;
1131 /* Save last validated path. */
1132 if (fsm->ldnalloc < (dnlen + 1)) {
1133 fsm->ldnalloc = dnlen + 100;
1134 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1136 if (fsm->ldn != NULL) { /* XXX can't happen */
1137 strcpy(fsm->ldn, fsm->path);
1138 fsm->ldnlen = dnlen;
1141 dnli = dnlFreeIterator(dnli);
1142 /*@=observertrans =dependenttrans@*/
1145 st->st_mode = st_mode; /* XXX restore st->st_mode */
1151 * Check for file on disk.
1152 * @param fsm file state machine data
1153 * @return 0 on success
1155 static int fsmStat(FSM_t fsm)
1157 int saveerrno = errno;
1160 if (fsm->path != NULL) {
1161 int saveernno = errno;
1162 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1163 ? FSM_LSTAT : FSM_STAT));
1164 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1168 } else if (rc == 0) {
1172 /* Skip %ghost files on build. */
1180 int fsmStage(FSM_t fsm, fileStage stage)
1183 fileStage prevStage = fsm->stage;
1184 const char * const prev = fileStageString(prevStage);
1186 static int modulo = 4;
1187 const char * const cur = fileStageString(stage);
1188 struct stat * st = &fsm->sb;
1189 struct stat * ost = &fsm->osb;
1190 int saveerrno = errno;
1195 #define _fafilter(_a) \
1196 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1197 ? fileActionString(_a) : "")
1199 if (stage & FSM_DEAD) {
1201 } else if (stage & FSM_INTERNAL) {
1202 if (_fsm_debug && !(stage & FSM_SYSCALL))
1203 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1205 (unsigned)st->st_mode, (int)st->st_nlink,
1206 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1207 (fsm->path ? fsm->path : ""),
1208 _fafilter(fsm->action));
1211 if (_fsm_debug || !(stage & FSM_VERBOSE))
1212 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1214 (unsigned)st->st_mode, (int)st->st_nlink,
1215 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1216 (fsm->path ? fsm->path + fsm->astriplen : ""),
1217 _fafilter(fsm->action));
1224 case FSM_PKGINSTALL:
1226 /* Clean fsm, free'ing memory. Read next archive header. */
1227 rc = fsmStage(fsm, FSM_INIT);
1229 /* Exit on end-of-payload. */
1230 if (rc == CPIOERR_HDR_TRAILER) {
1232 /*@loopbreak@*/ break;
1235 /* Exit on error. */
1238 (void) fsmStage(fsm, FSM_UNDO);
1239 /*@loopbreak@*/ break;
1242 /* Extract file from archive. */
1243 rc = fsmStage(fsm, FSM_PROCESS);
1245 (void) fsmStage(fsm, FSM_UNDO);
1246 /*@loopbreak@*/ break;
1249 /* Notify on success. */
1250 (void) fsmStage(fsm, FSM_NOTIFY);
1252 if (fsmStage(fsm, FSM_FINI))
1253 /*@loopbreak@*/ break;
1259 /* Clean fsm, free'ing memory. */
1260 rc = fsmStage(fsm, FSM_INIT);
1262 /* Exit on end-of-payload. */
1263 if (rc == CPIOERR_HDR_TRAILER) {
1265 /*@loopbreak@*/ break;
1268 /* Rename/erase next item. */
1269 if (fsmStage(fsm, FSM_FINI))
1270 /*@loopbreak@*/ break;
1276 rc = fsmStage(fsm, FSM_INIT);
1278 /* Exit on end-of-payload. */
1279 if (rc == CPIOERR_HDR_TRAILER) {
1281 /*@loopbreak@*/ break;
1284 /* Exit on error. */
1287 (void) fsmStage(fsm, FSM_UNDO);
1288 /*@loopbreak@*/ break;
1291 /* Copy file into archive. */
1292 rc = fsmStage(fsm, FSM_PROCESS);
1294 (void) fsmStage(fsm, FSM_UNDO);
1295 /*@loopbreak@*/ break;
1298 if (fsmStage(fsm, FSM_FINI))
1299 /*@loopbreak@*/ break;
1302 /* Flush partial sets of hard linked files. */
1303 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1305 while ((fsm->li = fsm->links) != NULL) {
1306 fsm->links = fsm->li->next;
1307 fsm->li->next = NULL;
1309 /* Re-calculate link count for archive header. */
1310 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1311 if (fsm->li->filex[i] < 0) continue;
1315 /* XXX force the contents out as well. */
1317 fsm->li->filex[0] = fsm->li->filex[j];
1318 fsm->li->filex[j] = -1;
1320 fsm->li->sb.st_nlink = nlink;
1322 fsm->sb = fsm->li->sb; /* structure assignment */
1323 fsm->osb = fsm->sb; /* structure assignment */
1325 if (!rc) rc = writeLinkedFile(fsm);
1327 fsm->li = freeHardLink(fsm->li);
1332 rc = fsmStage(fsm, FSM_TRAILER);
1336 { rpmTransactionSet ts = fsmGetTs(fsm);
1337 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1338 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1339 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1342 fsm->path = _free(fsm->path);
1343 fsm->opath = _free(fsm->opath);
1344 fsm->dnlx = _free(fsm->dnlx);
1346 fsm->ldn = _free(fsm->ldn);
1347 fsm->ldnalloc = fsm->ldnlen = 0;
1349 fsm->rdsize = fsm->wrsize = 0;
1350 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1351 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1352 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1353 fsm->rdsize = 8 * BUFSIZ;
1354 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1355 fsm->wrsize = 8 * BUFSIZ;
1356 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1359 fsm->mkdirsdone = 0;
1363 errno = 0; /* XXX get rid of EBADF */
1365 /* Detect and create directories not explicitly in package. */
1366 if (fsm->goal == FSM_PKGINSTALL) {
1367 rc = fsmStage(fsm, FSM_MKDIRS);
1368 if (!rc) fsm->mkdirsdone = 1;
1373 fsm->path = _free(fsm->path);
1375 fsm->diskchecked = fsm->exists = 0;
1377 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1378 fsm->action = FA_UNKNOWN;
1379 fsm->osuffix = NULL;
1380 fsm->nsuffix = NULL;
1382 if (fsm->goal == FSM_PKGINSTALL) {
1383 /* Read next header from payload, checking for end-of-payload. */
1384 rc = fsmStage(fsm, FSM_NEXT);
1388 /* Identify mapping index. */
1389 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1390 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1392 /* On non-install, detect end-of-loop. */
1393 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1394 rc = CPIOERR_HDR_TRAILER;
1398 /* On non-install, mode must be known so that dirs don't get suffix. */
1399 if (fsm->goal != FSM_PKGINSTALL) {
1400 TFI_t fi = fsmGetFi(fsm);
1401 st->st_mode = fi->fmodes[fsm->ix];
1404 /* Generate file path. */
1405 rc = fsmStage(fsm, FSM_MAP);
1408 /* Perform lstat/stat for disk file. */
1412 if (fsm->path != NULL) {
1413 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1414 ? FSM_LSTAT : FSM_STAT));
1415 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1419 } else if (rc == 0) {
1423 /* Skip %ghost files on build. */
1427 fsm->diskchecked = 1;
1430 /* On non-install, the disk file stat is what's remapped. */
1431 if (fsm->goal != FSM_PKGINSTALL)
1432 *st = *ost; /* structure assignment */
1434 /* Remap file perms, owner, and group. */
1435 rc = fsmMapAttrs(fsm);
1438 fsm->postpone = XFA_SKIPPING(fsm->action);
1439 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1441 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1442 fsm->postpone = saveHardLink(fsm);
1449 rc = fsmMapPath(fsm);
1452 rc = fsmMkdirs(fsm);
1456 rc = fsmRmdirs(fsm);
1459 if (fsm->postpone) {
1460 if (fsm->goal == FSM_PKGINSTALL)
1461 rc = fsmStage(fsm, FSM_EAT);
1465 if (fsm->goal == FSM_PKGBUILD) {
1466 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1467 struct hardLink * li, * prev;
1469 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1470 rc = writeLinkedFile(fsm);
1471 if (rc) break; /* W2DO? */
1473 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1475 /*@loopbreak@*/ break;
1478 fsm->links = fsm->li->next;
1480 prev->next = fsm->li->next;
1481 fsm->li->next = NULL;
1482 fsm->li = freeHardLink(fsm->li);
1484 rc = writeFile(fsm, 1);
1489 if (fsm->goal != FSM_PKGINSTALL)
1492 if (S_ISREG(st->st_mode)) {
1493 const char * path = fsm->path;
1495 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1496 rc = fsmStage(fsm, FSM_VERIFY);
1498 if (rc == 0 && fsm->osuffix) {
1499 const char * opath = fsm->opath;
1500 fsm->opath = fsm->path;
1501 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1502 rc = fsmStage(fsm, FSM_RENAME);
1504 rpmMessage(RPMMESS_WARNING,
1505 _("%s saved as %s\n"), fsm->opath, fsm->path);
1506 fsm->path = _free(fsm->path);
1510 /*@-dependenttrans@*/
1512 /*@=dependenttrans@*/
1513 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1514 rc = expandRegular(fsm);
1515 } else if (S_ISDIR(st->st_mode)) {
1516 mode_t st_mode = st->st_mode;
1517 rc = fsmStage(fsm, FSM_VERIFY);
1518 if (rc == CPIOERR_LSTAT_FAILED) {
1519 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1520 st->st_mode |= 00700;
1521 rc = fsmStage(fsm, FSM_MKDIR);
1522 st->st_mode = st_mode; /* XXX restore st->st_mode */
1524 } else if (S_ISLNK(st->st_mode)) {
1525 const char * opath = fsm->opath;
1527 if ((st->st_size + 1) > fsm->rdsize) {
1528 rc = CPIOERR_HDR_SIZE;
1532 fsm->wrlen = st->st_size;
1533 rc = fsmStage(fsm, FSM_DREAD);
1534 if (!rc && fsm->rdnb != fsm->wrlen)
1535 rc = CPIOERR_READ_FAILED;
1538 fsm->wrbuf[st->st_size] = '\0';
1539 /* XXX symlink(fsm->opath, fsm->path) */
1540 /*@-dependenttrans@*/
1541 fsm->opath = fsm->wrbuf;
1542 /*@=dependenttrans@*/
1543 rc = fsmStage(fsm, FSM_VERIFY);
1544 if (rc == CPIOERR_LSTAT_FAILED)
1545 rc = fsmStage(fsm, FSM_SYMLINK);
1546 fsm->opath = opath; /* XXX restore fsm->path */
1547 } else if (S_ISFIFO(st->st_mode)) {
1548 mode_t st_mode = st->st_mode;
1549 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1550 rc = fsmStage(fsm, FSM_VERIFY);
1551 if (rc == CPIOERR_LSTAT_FAILED) {
1552 st->st_mode = 0000; /* XXX abuse st->st_mode */
1553 rc = fsmStage(fsm, FSM_MKFIFO);
1554 st->st_mode = st_mode; /* XXX restore st->st_mode */
1556 } else if (S_ISCHR(st->st_mode) ||
1557 S_ISBLK(st->st_mode) ||
1558 S_ISSOCK(st->st_mode))
1560 rc = fsmStage(fsm, FSM_VERIFY);
1561 if (rc == CPIOERR_LSTAT_FAILED)
1562 rc = fsmStage(fsm, FSM_MKNOD);
1564 rc = CPIOERR_UNKNOWN_FILETYPE;
1566 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1567 fsm->li->createdPath = fsm->li->linkIndex;
1568 rc = fsmMakeLinks(fsm);
1574 rc = fsmMakeLinks(fsm);
1576 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1577 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1578 rpmTransactionSet ts = fsmGetTs(fsm);
1579 TFI_t fi = fsmGetFi(fsm);
1580 if (ts && ts->notify && fi)
1581 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1582 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1583 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1589 if (fsm->goal == FSM_PKGINSTALL) {
1590 (void) fsmStage(fsm,
1591 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1593 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1595 (void) fsmStage(fsm, FSM_RMDIRS);
1599 if (fsm->failedFile && *fsm->failedFile == NULL)
1600 *fsm->failedFile = xstrdup(fsm->path);
1603 if (!fsm->postpone && fsm->commit) {
1604 if (fsm->goal == FSM_PKGINSTALL)
1605 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1606 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1607 if (fsm->goal == FSM_PKGCOMMIT)
1608 rc = fsmStage(fsm, FSM_COMMIT);
1609 if (fsm->goal == FSM_PKGERASE)
1610 rc = fsmStage(fsm, FSM_COMMIT);
1612 fsm->path = _free(fsm->path);
1613 fsm->opath = _free(fsm->opath);
1614 memset(st, 0, sizeof(*st));
1615 memset(ost, 0, sizeof(*ost));
1618 /* Rename pre-existing modified or unmanaged file. */
1619 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1620 const char * opath = fsm->opath;
1621 const char * path = fsm->path;
1622 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1623 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1625 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1626 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1628 rc = fsmStage(fsm, FSM_RENAME);
1630 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1631 fsm->opath, fsm->path);
1633 fsm->path = _free(fsm->path);
1635 fsm->opath = _free(fsm->opath);
1639 /* Remove erased files. */
1640 if (fsm->goal == FSM_PKGERASE) {
1641 if (fsm->action == FA_ERASE) {
1642 TFI_t fi = fsmGetFi(fsm);
1643 if (S_ISDIR(st->st_mode)) {
1644 rc = fsmStage(fsm, FSM_RMDIR);
1647 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1649 /* XXX make sure that build side permits %missingok on directories. */
1650 if (fsm->fflags & RPMFILE_MISSINGOK)
1653 /* XXX common error message. */
1655 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1656 _("%s rmdir of %s failed: Directory not empty\n"),
1657 fiTypeString(fi), fsm->path);
1661 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1662 _("%s rmdir of %s failed: %s\n"),
1663 fiTypeString(fi), fsm->path, strerror(errno));
1667 rc = fsmStage(fsm, FSM_UNLINK);
1669 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1671 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1672 _("%s unlink of %s failed: %s\n"),
1673 fiTypeString(fi), fsm->path, strerror(errno));
1676 /* XXX Failure to remove is not (yet) cause for failure. */
1677 if (!strict_erasures) rc = 0;
1681 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1682 /* Rename temporary to final file name. */
1683 if (!S_ISDIR(st->st_mode) &&
1684 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1686 fsm->opath = fsm->path;
1687 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1688 rc = fsmStage(fsm, FSM_RENAME);
1689 if (!rc && fsm->nsuffix) {
1690 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1691 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1692 (opath ? opath : ""), fsm->path);
1693 opath = _free(opath);
1695 fsm->opath = _free(fsm->opath);
1697 if (S_ISLNK(st->st_mode)) {
1698 if (!rc && !getuid())
1699 rc = fsmStage(fsm, FSM_LCHOWN);
1701 if (!rc && !getuid())
1702 rc = fsmStage(fsm, FSM_CHOWN);
1704 rc = fsmStage(fsm, FSM_CHMOD);
1706 time_t mtime = st->st_mtime;
1707 TFI_t fi = fsmGetFi(fsm);
1709 st->st_mtime = fi->fmtimes[fsm->ix];
1710 rc = fsmStage(fsm, FSM_UTIME);
1711 st->st_mtime = mtime;
1716 /* Notify on success. */
1717 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1720 fsm->path = _free(fsm->path);
1722 /* Check for hard links missing from payload. */
1723 while ((fsm->li = fsm->links) != NULL) {
1724 fsm->links = fsm->li->next;
1725 fsm->li->next = NULL;
1726 if (fsm->goal == FSM_PKGINSTALL &&
1727 fsm->commit && fsm->li->linksLeft)
1729 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1730 if (fsm->li->filex[i] < 0) continue;
1731 rc = CPIOERR_MISSING_HARDLINK;
1732 if (fsm->failedFile && *fsm->failedFile == NULL) {
1733 fsm->ix = fsm->li->filex[i];
1734 if (!fsmStage(fsm, FSM_MAP)) {
1735 *fsm->failedFile = fsm->path;
1739 /*@loopbreak@*/ break;
1742 if (fsm->goal == FSM_PKGBUILD &&
1743 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1745 rc = CPIOERR_MISSING_HARDLINK;
1747 fsm->li = freeHardLink(fsm->li);
1749 fsm->ldn = _free(fsm->ldn);
1750 fsm->ldnalloc = fsm->ldnlen = 0;
1751 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1752 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1755 if (fsm->diskchecked && !fsm->exists) {
1756 rc = CPIOERR_LSTAT_FAILED;
1759 if (S_ISREG(st->st_mode)) {
1760 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1761 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1763 * XXX HP-UX (and other os'es) don't permit unlink on busy
1766 fsm->opath = fsm->path;
1768 rc = fsmStage(fsm, FSM_RENAME);
1770 (void) fsmStage(fsm, FSM_UNLINK);
1772 rc = CPIOERR_UNLINK_FAILED;
1773 fsm->path = fsm->opath;
1775 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1776 /*@notreached@*/ break;
1777 } else if (S_ISDIR(st->st_mode)) {
1778 if (S_ISDIR(ost->st_mode)) return 0;
1779 if (S_ISLNK(ost->st_mode)) {
1780 rc = fsmStage(fsm, FSM_STAT);
1781 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1784 if (S_ISDIR(ost->st_mode)) return 0;
1786 } else if (S_ISLNK(st->st_mode)) {
1787 if (S_ISLNK(ost->st_mode)) {
1788 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1789 rc = fsmStage(fsm, FSM_READLINK);
1792 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1794 } else if (S_ISFIFO(st->st_mode)) {
1795 if (S_ISFIFO(ost->st_mode)) return 0;
1796 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1797 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1798 (ost->st_rdev == st->st_rdev)) return 0;
1799 } else if (S_ISSOCK(st->st_mode)) {
1800 if (S_ISSOCK(ost->st_mode)) return 0;
1802 /* XXX shouldn't do this with commit/undo. */
1804 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1805 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1806 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1807 /*@notreached@*/ break;
1810 rc = Unlink(fsm->path);
1811 if (_fsm_debug && (stage & FSM_SYSCALL))
1812 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1813 fsm->path, (rc < 0 ? strerror(errno) : ""));
1814 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1817 rc = Rename(fsm->opath, fsm->path);
1818 if (_fsm_debug && (stage & FSM_SYSCALL))
1819 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1820 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1821 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1824 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1825 if (_fsm_debug && (stage & FSM_SYSCALL))
1826 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1827 fsm->path, (unsigned)(st->st_mode & 07777),
1828 (rc < 0 ? strerror(errno) : ""));
1829 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1832 rc = Rmdir(fsm->path);
1833 if (_fsm_debug && (stage & FSM_SYSCALL))
1834 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1835 fsm->path, (rc < 0 ? strerror(errno) : ""));
1836 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1839 rc = chown(fsm->path, st->st_uid, st->st_gid);
1840 if (_fsm_debug && (stage & FSM_SYSCALL))
1841 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1842 fsm->path, (int)st->st_uid, (int)st->st_gid,
1843 (rc < 0 ? strerror(errno) : ""));
1844 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1847 #if ! CHOWN_FOLLOWS_SYMLINK
1848 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1849 if (_fsm_debug && (stage & FSM_SYSCALL))
1850 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1851 fsm->path, (int)st->st_uid, (int)st->st_gid,
1852 (rc < 0 ? strerror(errno) : ""));
1853 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1857 rc = chmod(fsm->path, (st->st_mode & 07777));
1858 if (_fsm_debug && (stage & FSM_SYSCALL))
1859 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1860 fsm->path, (unsigned)(st->st_mode & 07777),
1861 (rc < 0 ? strerror(errno) : ""));
1862 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1865 { struct utimbuf stamp;
1866 stamp.actime = st->st_mtime;
1867 stamp.modtime = st->st_mtime;
1868 rc = utime(fsm->path, &stamp);
1869 if (_fsm_debug && (stage & FSM_SYSCALL))
1870 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1871 fsm->path, (unsigned)st->st_mtime,
1872 (rc < 0 ? strerror(errno) : ""));
1873 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1877 rc = symlink(fsm->opath, fsm->path);
1878 if (_fsm_debug && (stage & FSM_SYSCALL))
1879 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1880 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1881 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1884 rc = Link(fsm->opath, fsm->path);
1885 if (_fsm_debug && (stage & FSM_SYSCALL))
1886 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1887 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1888 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1891 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1892 if (_fsm_debug && (stage & FSM_SYSCALL))
1893 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1894 fsm->path, (unsigned)(st->st_mode & 07777),
1895 (rc < 0 ? strerror(errno) : ""));
1896 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1900 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1902 if (_fsm_debug && (stage & FSM_SYSCALL))
1903 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1904 fsm->path, (unsigned)(st->st_mode & ~07777),
1905 (unsigned)st->st_rdev,
1906 (rc < 0 ? strerror(errno) : ""));
1907 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1910 rc = Lstat(fsm->path, ost);
1911 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1912 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1913 fsm->path, (rc < 0 ? strerror(errno) : ""));
1914 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1917 rc = Stat(fsm->path, ost);
1918 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1919 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1920 fsm->path, (rc < 0 ? strerror(errno) : ""));
1921 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1924 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1925 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1926 if (_fsm_debug && (stage & FSM_SYSCALL))
1927 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1928 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
1929 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1932 fsm->rdbuf[fsm->rdnb] = '\0';
1940 rc = fsmStage(fsm, FSM_HREAD);
1942 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1943 fsm->path = _free(fsm->path);
1944 rc = CPIOERR_HDR_TRAILER;
1947 rc = fsmStage(fsm, FSM_POS);
1950 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1951 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1952 rc = fsmStage(fsm, FSM_DREAD);
1954 /*@loopbreak@*/ break;
1958 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1961 (void) fsmStage(fsm, FSM_DREAD);
1965 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1967 memset(fsm->rdbuf, 0, left);
1968 /* XXX DWRITE uses rdnb for I/O length. */
1970 (void) fsmStage(fsm, FSM_DWRITE);
1974 rc = cpioTrailerWrite(fsm);
1977 rc = fsmStage(fsm, FSM_POS);
1979 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1982 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1985 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1986 if (_fsm_debug && (stage & FSM_SYSCALL))
1987 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1988 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1989 (int)fsm->wrlen, (int)fsm->rdnb);
1990 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
1991 rc = CPIOERR_READ_FAILED;
1993 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1996 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1997 if (_fsm_debug && (stage & FSM_SYSCALL))
1998 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1999 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2000 (int)fsm->rdnb, (int)fsm->wrnb);
2001 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2002 rc = CPIOERR_WRITE_FAILED;
2004 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2008 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2009 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2010 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2012 rc = CPIOERR_OPEN_FAILED;
2015 if (_fsm_debug && (stage & FSM_SYSCALL))
2016 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2017 fsm->path, fsm->rfd, fsm->rdbuf);
2020 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2021 if (_fsm_debug && (stage & FSM_SYSCALL))
2022 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2023 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2024 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2025 rc = CPIOERR_READ_FAILED;
2029 if (_fsm_debug && (stage & FSM_SYSCALL))
2030 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2031 (void) Fclose(fsm->rfd);
2037 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2038 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2039 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2041 rc = CPIOERR_OPEN_FAILED;
2043 if (_fsm_debug && (stage & FSM_SYSCALL))
2044 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2045 fsm->path, fsm->wfd, fsm->wrbuf);
2048 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2049 if (_fsm_debug && (stage & FSM_SYSCALL))
2050 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2051 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2052 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2053 rc = CPIOERR_WRITE_FAILED;
2057 if (_fsm_debug && (stage & FSM_SYSCALL))
2058 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2059 (void) Fclose(fsm->wfd);
2069 if (!(stage & FSM_INTERNAL)) {
2070 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2076 /*@obserever@*/ const char *const fileActionString(fileAction a)
2079 case FA_UNKNOWN: return "unknown";
2080 case FA_CREATE: return "create";
2081 case FA_COPYOUT: return "copyout";
2082 case FA_COPYIN: return "copyin";
2083 case FA_BACKUP: return "backup";
2084 case FA_SAVE: return "save";
2085 case FA_SKIP: return "skip";
2086 case FA_ALTNAME: return "altname";
2087 case FA_ERASE: return "erase";
2088 case FA_SKIPNSTATE: return "skipnstate";
2089 case FA_SKIPNETSHARED: return "skipnetshared";
2090 case FA_SKIPMULTILIB: return "skipmultilib";
2091 default: return "???";
2096 /*@observer@*/ const char *const fileStageString(fileStage a) {
2098 case FSM_UNKNOWN: return "unknown";
2100 case FSM_PKGINSTALL:return "pkginstall";
2101 case FSM_PKGERASE: return "pkgerase";
2102 case FSM_PKGBUILD: return "pkgbuild";
2103 case FSM_PKGCOMMIT: return "pkgcommit";
2104 case FSM_PKGUNDO: return "pkgundo";
2106 case FSM_CREATE: return "create";
2107 case FSM_INIT: return "init";
2108 case FSM_MAP: return "map";
2109 case FSM_MKDIRS: return "mkdirs";
2110 case FSM_RMDIRS: return "rmdirs";
2111 case FSM_PRE: return "pre";
2112 case FSM_PROCESS: return "process";
2113 case FSM_POST: return "post";
2114 case FSM_MKLINKS: return "mklinks";
2115 case FSM_NOTIFY: return "notify";
2116 case FSM_UNDO: return "undo";
2117 case FSM_FINI: return "fini";
2118 case FSM_COMMIT: return "commit";
2119 case FSM_DESTROY: return "destroy";
2120 case FSM_VERIFY: return "verify";
2122 case FSM_UNLINK: return "Unlink";
2123 case FSM_RENAME: return "Rename";
2124 case FSM_MKDIR: return "Mkdir";
2125 case FSM_RMDIR: return "rmdir";
2126 case FSM_CHOWN: return "chown";
2127 case FSM_LCHOWN: return "lchown";
2128 case FSM_CHMOD: return "chmod";
2129 case FSM_UTIME: return "utime";
2130 case FSM_SYMLINK: return "symlink";
2131 case FSM_LINK: return "Link";
2132 case FSM_MKFIFO: return "mkfifo";
2133 case FSM_MKNOD: return "mknod";
2134 case FSM_LSTAT: return "Lstat";
2135 case FSM_STAT: return "Stat";
2136 case FSM_READLINK: return "Readlink";
2137 case FSM_CHROOT: return "chroot";
2139 case FSM_NEXT: return "next";
2140 case FSM_EAT: return "eat";
2141 case FSM_POS: return "pos";
2142 case FSM_PAD: return "pad";
2143 case FSM_TRAILER: return "trailer";
2144 case FSM_HREAD: return "hread";
2145 case FSM_HWRITE: return "hwrite";
2146 case FSM_DREAD: return "Fread";
2147 case FSM_DWRITE: return "Fwrite";
2149 case FSM_ROPEN: return "Fopen";
2150 case FSM_READ: return "Fread";
2151 case FSM_RCLOSE: return "Fclose";
2152 case FSM_WOPEN: return "Fopen";
2153 case FSM_WRITE: return "Fwrite";
2154 case FSM_WCLOSE: return "Fclose";
2156 default: return "???";