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;
634 if (fsm->mapFlags & CPIO_MAP_UID)
635 st->st_uid = finalUid;
636 if (fsm->mapFlags & CPIO_MAP_GID)
637 st->st_gid = finalGid;
639 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
646 * Create file from payload stream.
647 * @todo Legacy: support brokenEndian MD5 checks?
648 * @param fsm file state machine data
649 * @return 0 on success
651 static int expandRegular(/*@special@*/ FSM_t fsm)
653 /*@modifies fsm, fileSystem @*/
655 const char * fmd5sum;
656 const struct stat * st = &fsm->sb;
657 int left = st->st_size;
660 rc = fsmStage(fsm, FSM_WOPEN);
664 /* XXX md5sum's will break on repackaging that includes modified files. */
665 fmd5sum = fsm->fmd5sum;
667 /* XXX This doesn't support brokenEndian checks. */
668 if (st->st_size > 0 && fmd5sum)
669 fdInitMD5(fsm->wfd, 0);
673 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
674 rc = fsmStage(fsm, FSM_DREAD);
678 rc = fsmStage(fsm, FSM_WRITE);
684 /* don't call this with fileSize == fileComplete */
686 (void) fsmStage(fsm, FSM_NOTIFY);
689 if (st->st_size > 0 && fmd5sum) {
690 const char * md5sum = NULL;
692 (void) Fflush(fsm->wfd);
693 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
695 if (md5sum == NULL) {
696 rc = CPIOERR_MD5SUM_MISMATCH;
698 if (strcmp(md5sum, fmd5sum))
699 rc = CPIOERR_MD5SUM_MISMATCH;
700 md5sum = _free(md5sum);
705 (void) fsmStage(fsm, FSM_WCLOSE);
710 * Write next item to payload stream.
711 * @param fsm file state machine data
712 * @param writeData should data be written?
713 * @return 0 on success
715 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
716 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
717 /*@modifies fsm, fileSystem @*/
719 const char * path = fsm->path;
720 const char * opath = fsm->opath;
721 struct stat * st = &fsm->sb;
722 struct stat * ost = &fsm->osb;
723 size_t pos = fdGetCpioPos(fsm->cfd);
724 char * symbuf = NULL;
728 st->st_size = (writeData ? ost->st_size : 0);
730 if (S_ISDIR(st->st_mode)) {
732 } else if (S_ISLNK(st->st_mode)) {
734 * While linux puts the size of a symlink in the st_size field,
735 * I don't think that's a specified standard.
737 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
738 rc = fsmStage(fsm, FSM_READLINK);
740 st->st_size = fsm->rdnb;
741 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
744 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
745 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
746 char * t = alloca(nb);
749 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
751 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
752 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
753 TFI_t fi = fsmGetFi(fsm);
755 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
758 rc = fsmStage(fsm, FSM_HWRITE);
762 if (writeData && S_ISREG(st->st_mode)) {
765 void * mapped = (void *)-1;
769 rc = fsmStage(fsm, FSM_ROPEN);
772 /* XXX unbuffered mmap generates *lots* of fdio debugging */
775 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
776 if (mapped != (void *)-1) {
778 fsm->rdbuf = (char *) mapped;
779 fsm->rdlen = nmapped = st->st_size;
787 if (mapped != (void *)-1) {
792 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
793 rc = fsmStage(fsm, FSM_READ);
797 /* XXX DWRITE uses rdnb for I/O length. */
798 rc = fsmStage(fsm, FSM_DWRITE);
805 if (mapped != (void *)-1) {
806 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
811 } else if (writeData && S_ISLNK(st->st_mode)) {
812 /* XXX DWRITE uses rdnb for I/O length. */
813 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
814 fsm->rdnb = strlen(symbuf);
815 rc = fsmStage(fsm, FSM_DWRITE);
819 rc = fsmStage(fsm, FSM_PAD);
822 { const rpmTransactionSet ts = fsmGetTs(fsm);
823 TFI_t fi = fsmGetFi(fsm);
824 if (ts && fi && ts->notify) {
825 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
826 /*@-modunconnomods@*/
827 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
828 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
829 /*@=modunconnomods@*/
837 (void) fsmStage(fsm, FSM_RCLOSE);
838 /*@-dependenttrans@*/
841 /*@=dependenttrans@*/
846 * Write set of linked files to payload stream.
847 * @param fsm file state machine data
848 * @return 0 on success
850 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
851 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
852 /*@modifies fsm, fileSystem @*/
854 const char * path = fsm->path;
855 const char * nsuffix = fsm->nsuffix;
856 int iterIndex = fsm->ix;
865 for (i = fsm->li->nlink - 1; i >= 0; i--) {
867 if (fsm->li->filex[i] < 0) continue;
869 fsm->ix = fsm->li->filex[i];
870 rc = fsmStage(fsm, FSM_MAP);
872 /* Write data after last link. */
873 rc = writeFile(fsm, (i == 0));
874 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
876 *fsm->failedFile = xstrdup(fsm->path);
879 fsm->path = _free(fsm->path);
880 fsm->li->filex[i] = -1;
884 fsm->nsuffix = nsuffix;
890 * Create pending hard links to existing file.
891 * @param fsm file state machine data
892 * @return 0 on success
894 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
895 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
896 /*@modifies fsm, fileSystem @*/
898 const char * path = fsm->path;
899 const char * opath = fsm->opath;
900 const char * nsuffix = fsm->nsuffix;
901 int iterIndex = fsm->ix;
911 fsm->ix = fsm->li->filex[fsm->li->createdPath];
912 rc = fsmStage(fsm, FSM_MAP);
913 fsm->opath = fsm->path;
915 for (i = 0; i < fsm->li->nlink; i++) {
916 if (fsm->li->filex[i] < 0) continue;
917 if (i == fsm->li->createdPath) continue;
919 fsm->ix = fsm->li->filex[i];
920 rc = fsmStage(fsm, FSM_MAP);
921 rc = fsmStage(fsm, FSM_VERIFY);
923 if (rc != CPIOERR_LSTAT_FAILED) break;
925 /* XXX link(fsm->opath, fsm->path) */
926 rc = fsmStage(fsm, FSM_LINK);
927 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
929 *fsm->failedFile = xstrdup(fsm->path);
932 fsm->li->linksLeft--;
934 fsm->opath = _free(fsm->opath);
937 fsm->nsuffix = nsuffix;
944 * Commit hard linked file set atomically.
945 * @param fsm file state machine data
946 * @return 0 on success
948 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
949 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
950 fsm->li, fsm->links @*/
951 /*@modifies fsm, fileSystem @*/
953 const char * path = fsm->path;
954 const char * nsuffix = fsm->nsuffix;
955 int iterIndex = fsm->ix;
956 struct stat * st = &fsm->sb;
964 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
965 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
969 for (i = 0; i < fsm->li->nlink; i++) {
970 if (fsm->li->filex[i] < 0) continue;
971 fsm->ix = fsm->li->filex[i];
972 rc = fsmStage(fsm, FSM_MAP);
973 rc = fsmStage(fsm, FSM_COMMIT);
974 fsm->path = _free(fsm->path);
975 fsm->li->filex[i] = -1;
979 fsm->nsuffix = nsuffix;
985 * Remove (if created) directories not explicitly included in package.
986 * @param fsm file state machine data
987 * @return 0 on success
990 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
991 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
992 /*@modifies fsm, fileSystem @*/
994 const char * path = fsm->path;
995 void * dnli = dnlInitIterator(fsm, 1);
996 char * dn = fsm->rdbuf;
997 int dc = dnlCount(dnli);
1002 /*@-observertrans -dependenttrans@*/
1003 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1004 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1005 int dnlen = strlen(fsm->path);
1008 dc = dnlIndex(dnli);
1009 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1012 /* Copy to avoid const on fsm->path. */
1013 te = stpcpy(dn, fsm->path) - 1;
1016 /* Remove generated directories. */
1020 rc = fsmStage(fsm, FSM_RMDIR);
1024 /*@innerbreak@*/ break;
1026 } while ((te - dn) > fsm->dnlx[dc]);
1028 dnli = dnlFreeIterator(dnli);
1029 /*@=observertrans =dependenttrans@*/
1037 * Create (if necessary) directories not explicitly included in package.
1038 * @param fsm file state machine data
1039 * @return 0 on success
1041 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1042 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1043 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1044 /*@defines fsm->dnlx, fsm->ldn @*/
1045 /*@modifies fsm, fileSystem @*/
1047 struct stat * st = &fsm->sb;
1048 struct stat * ost = &fsm->osb;
1049 const char * path = fsm->path;
1050 mode_t st_mode = st->st_mode;
1051 void * dnli = dnlInitIterator(fsm, 0);
1052 char * dn = fsm->rdbuf;
1053 int dc = dnlCount(dnli);
1060 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1061 /*@-observertrans -dependenttrans@*/
1062 if (fsm->dnlx != NULL)
1063 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1064 int dnlen = strlen(fsm->path);
1067 dc = dnlIndex(dnli);
1068 if (dc < 0) continue;
1069 fsm->dnlx[dc] = dnlen;
1073 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1074 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1076 /*@=compdef =nullpass@*/
1078 /* Copy to avoid const on fsm->path. */
1079 (void) stpcpy(dn, fsm->path);
1082 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1083 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1084 if (*te != '/') continue;
1088 /* Already validated? */
1089 /*@-usedef -compdef -nullpass -nullderef@*/
1090 if (i < fsm->ldnlen &&
1091 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1092 !strncmp(fsm->path, fsm->ldn, i))
1095 /* Move pre-existing path marker forward. */
1096 fsm->dnlx[dc] = (te - dn);
1099 /*@=usedef =compdef =nullpass =nullderef@*/
1101 /* Validate next component of path. */
1102 rc = fsmStage(fsm, FSM_LSTAT);
1105 /* Directory already exists? */
1106 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1107 /* Move pre-existing path marker forward. */
1108 fsm->dnlx[dc] = (te - dn);
1109 } else if (rc == CPIOERR_LSTAT_FAILED) {
1110 TFI_t fi = fsmGetFi(fsm);
1112 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1113 rc = fsmStage(fsm, FSM_MKDIR);
1115 rpmMessage(RPMMESS_WARNING,
1116 _("%s directory created with perms %04o.\n"),
1117 fsm->path, (unsigned)(st->st_mode & 07777));
1121 /*@innerbreak@*/ break;
1125 /* Save last validated path. */
1126 if (fsm->ldnalloc < (dnlen + 1)) {
1127 fsm->ldnalloc = dnlen + 100;
1128 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1130 if (fsm->ldn != NULL) { /* XXX can't happen */
1131 strcpy(fsm->ldn, fsm->path);
1132 fsm->ldnlen = dnlen;
1135 dnli = dnlFreeIterator(dnli);
1136 /*@=observertrans =dependenttrans@*/
1139 st->st_mode = st_mode; /* XXX restore st->st_mode */
1145 * Check for file on disk.
1146 * @param fsm file state machine data
1147 * @return 0 on success
1149 static int fsmStat(FSM_t fsm)
1151 int saveerrno = errno;
1154 if (fsm->path != NULL) {
1155 int saveernno = errno;
1156 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1157 ? FSM_LSTAT : FSM_STAT));
1158 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1162 } else if (rc == 0) {
1166 /* Skip %ghost files on build. */
1174 int fsmStage(FSM_t fsm, fileStage stage)
1177 fileStage prevStage = fsm->stage;
1178 const char * const prev = fileStageString(prevStage);
1180 static int modulo = 4;
1181 const char * const cur = fileStageString(stage);
1182 struct stat * st = &fsm->sb;
1183 struct stat * ost = &fsm->osb;
1184 int saveerrno = errno;
1189 #define _fafilter(_a) \
1190 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1191 ? fileActionString(_a) : "")
1193 if (stage & FSM_DEAD) {
1195 } else if (stage & FSM_INTERNAL) {
1196 if (_fsm_debug && !(stage & FSM_SYSCALL))
1197 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1199 (unsigned)st->st_mode, (int)st->st_nlink,
1200 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1201 (fsm->path ? fsm->path : ""),
1202 _fafilter(fsm->action));
1205 if (_fsm_debug || !(stage & FSM_VERBOSE))
1206 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1208 (unsigned)st->st_mode, (int)st->st_nlink,
1209 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1210 (fsm->path ? fsm->path + fsm->astriplen : ""),
1211 _fafilter(fsm->action));
1218 case FSM_PKGINSTALL:
1220 /* Clean fsm, free'ing memory. Read next archive header. */
1221 rc = fsmStage(fsm, FSM_INIT);
1223 /* Exit on end-of-payload. */
1224 if (rc == CPIOERR_HDR_TRAILER) {
1226 /*@loopbreak@*/ break;
1229 /* Exit on error. */
1232 (void) fsmStage(fsm, FSM_UNDO);
1233 /*@loopbreak@*/ break;
1236 /* Extract file from archive. */
1237 rc = fsmStage(fsm, FSM_PROCESS);
1239 (void) fsmStage(fsm, FSM_UNDO);
1240 /*@loopbreak@*/ break;
1243 /* Notify on success. */
1244 (void) fsmStage(fsm, FSM_NOTIFY);
1246 if (fsmStage(fsm, FSM_FINI))
1247 /*@loopbreak@*/ break;
1253 /* Clean fsm, free'ing memory. */
1254 rc = fsmStage(fsm, FSM_INIT);
1256 /* Exit on end-of-payload. */
1257 if (rc == CPIOERR_HDR_TRAILER) {
1259 /*@loopbreak@*/ break;
1262 /* Rename/erase next item. */
1263 if (fsmStage(fsm, FSM_FINI))
1264 /*@loopbreak@*/ break;
1270 rc = fsmStage(fsm, FSM_INIT);
1272 /* Exit on end-of-payload. */
1273 if (rc == CPIOERR_HDR_TRAILER) {
1275 /*@loopbreak@*/ break;
1278 /* Exit on error. */
1281 (void) fsmStage(fsm, FSM_UNDO);
1282 /*@loopbreak@*/ break;
1285 /* Copy file into archive. */
1286 rc = fsmStage(fsm, FSM_PROCESS);
1288 (void) fsmStage(fsm, FSM_UNDO);
1289 /*@loopbreak@*/ break;
1292 if (fsmStage(fsm, FSM_FINI))
1293 /*@loopbreak@*/ break;
1296 /* Flush partial sets of hard linked files. */
1297 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1299 while ((fsm->li = fsm->links) != NULL) {
1300 fsm->links = fsm->li->next;
1301 fsm->li->next = NULL;
1303 /* Re-calculate link count for archive header. */
1304 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1305 if (fsm->li->filex[i] < 0) continue;
1309 /* XXX force the contents out as well. */
1311 fsm->li->filex[0] = fsm->li->filex[j];
1312 fsm->li->filex[j] = -1;
1314 fsm->li->sb.st_nlink = nlink;
1316 fsm->sb = fsm->li->sb; /* structure assignment */
1317 fsm->osb = fsm->sb; /* structure assignment */
1319 if (!rc) rc = writeLinkedFile(fsm);
1321 fsm->li = freeHardLink(fsm->li);
1326 rc = fsmStage(fsm, FSM_TRAILER);
1330 { rpmTransactionSet ts = fsmGetTs(fsm);
1331 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1332 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1333 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1336 fsm->path = _free(fsm->path);
1337 fsm->opath = _free(fsm->opath);
1338 fsm->dnlx = _free(fsm->dnlx);
1340 fsm->ldn = _free(fsm->ldn);
1341 fsm->ldnalloc = fsm->ldnlen = 0;
1343 fsm->rdsize = fsm->wrsize = 0;
1344 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1345 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1346 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1347 fsm->rdsize = 8 * BUFSIZ;
1348 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1349 fsm->wrsize = 8 * BUFSIZ;
1350 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1353 fsm->mkdirsdone = 0;
1357 errno = 0; /* XXX get rid of EBADF */
1359 /* Detect and create directories not explicitly in package. */
1360 if (fsm->goal == FSM_PKGINSTALL) {
1361 rc = fsmStage(fsm, FSM_MKDIRS);
1362 if (!rc) fsm->mkdirsdone = 1;
1367 fsm->path = _free(fsm->path);
1369 fsm->diskchecked = fsm->exists = 0;
1371 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1372 fsm->action = FA_UNKNOWN;
1373 fsm->osuffix = NULL;
1374 fsm->nsuffix = NULL;
1376 if (fsm->goal == FSM_PKGINSTALL) {
1377 /* Read next header from payload, checking for end-of-payload. */
1378 rc = fsmStage(fsm, FSM_NEXT);
1382 /* Identify mapping index. */
1383 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1384 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1386 /* On non-install, detect end-of-loop. */
1387 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1388 rc = CPIOERR_HDR_TRAILER;
1392 /* On non-install, mode must be known so that dirs don't get suffix. */
1393 if (fsm->goal != FSM_PKGINSTALL) {
1394 TFI_t fi = fsmGetFi(fsm);
1395 st->st_mode = fi->fmodes[fsm->ix];
1398 /* Generate file path. */
1399 rc = fsmStage(fsm, FSM_MAP);
1402 /* Perform lstat/stat for disk file. */
1406 if (fsm->path != NULL) {
1407 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1408 ? FSM_LSTAT : FSM_STAT));
1409 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1413 } else if (rc == 0) {
1417 /* Skip %ghost files on build. */
1421 fsm->diskchecked = 1;
1424 /* On non-install, the disk file stat is what's remapped. */
1425 if (fsm->goal != FSM_PKGINSTALL)
1426 *st = *ost; /* structure assignment */
1428 /* Remap file perms, owner, and group. */
1429 rc = fsmMapAttrs(fsm);
1432 fsm->postpone = XFA_SKIPPING(fsm->action);
1433 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1435 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1436 fsm->postpone = saveHardLink(fsm);
1443 rc = fsmMapPath(fsm);
1446 rc = fsmMkdirs(fsm);
1450 rc = fsmRmdirs(fsm);
1453 if (fsm->postpone) {
1454 if (fsm->goal == FSM_PKGINSTALL)
1455 rc = fsmStage(fsm, FSM_EAT);
1459 if (fsm->goal == FSM_PKGBUILD) {
1460 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1461 struct hardLink * li, * prev;
1463 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1464 rc = writeLinkedFile(fsm);
1465 if (rc) break; /* W2DO? */
1467 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1469 /*@loopbreak@*/ break;
1472 fsm->links = fsm->li->next;
1474 prev->next = fsm->li->next;
1475 fsm->li->next = NULL;
1476 fsm->li = freeHardLink(fsm->li);
1478 rc = writeFile(fsm, 1);
1483 if (fsm->goal != FSM_PKGINSTALL)
1486 if (S_ISREG(st->st_mode)) {
1487 const char * path = fsm->path;
1489 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1490 rc = fsmStage(fsm, FSM_VERIFY);
1492 if (rc == 0 && fsm->osuffix) {
1493 const char * opath = fsm->opath;
1494 fsm->opath = fsm->path;
1495 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1496 rc = fsmStage(fsm, FSM_RENAME);
1498 rpmMessage(RPMMESS_WARNING,
1499 _("%s saved as %s\n"), fsm->opath, fsm->path);
1500 fsm->path = _free(fsm->path);
1504 /*@-dependenttrans@*/
1506 /*@=dependenttrans@*/
1507 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1508 rc = expandRegular(fsm);
1509 } else if (S_ISDIR(st->st_mode)) {
1510 mode_t st_mode = st->st_mode;
1511 rc = fsmStage(fsm, FSM_VERIFY);
1512 if (rc == CPIOERR_LSTAT_FAILED) {
1513 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1514 st->st_mode |= 00700;
1515 rc = fsmStage(fsm, FSM_MKDIR);
1516 st->st_mode = st_mode; /* XXX restore st->st_mode */
1518 } else if (S_ISLNK(st->st_mode)) {
1519 const char * opath = fsm->opath;
1521 if ((st->st_size + 1) > fsm->rdsize) {
1522 rc = CPIOERR_HDR_SIZE;
1526 fsm->wrlen = st->st_size;
1527 rc = fsmStage(fsm, FSM_DREAD);
1528 if (!rc && fsm->rdnb != fsm->wrlen)
1529 rc = CPIOERR_READ_FAILED;
1532 fsm->wrbuf[st->st_size] = '\0';
1533 /* XXX symlink(fsm->opath, fsm->path) */
1534 /*@-dependenttrans@*/
1535 fsm->opath = fsm->wrbuf;
1536 /*@=dependenttrans@*/
1537 rc = fsmStage(fsm, FSM_VERIFY);
1538 if (rc == CPIOERR_LSTAT_FAILED)
1539 rc = fsmStage(fsm, FSM_SYMLINK);
1540 fsm->opath = opath; /* XXX restore fsm->path */
1541 } else if (S_ISFIFO(st->st_mode)) {
1542 mode_t st_mode = st->st_mode;
1543 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1544 rc = fsmStage(fsm, FSM_VERIFY);
1545 if (rc == CPIOERR_LSTAT_FAILED) {
1546 st->st_mode = 0000; /* XXX abuse st->st_mode */
1547 rc = fsmStage(fsm, FSM_MKFIFO);
1548 st->st_mode = st_mode; /* XXX restore st->st_mode */
1550 } else if (S_ISCHR(st->st_mode) ||
1551 S_ISBLK(st->st_mode) ||
1552 S_ISSOCK(st->st_mode))
1554 rc = fsmStage(fsm, FSM_VERIFY);
1555 if (rc == CPIOERR_LSTAT_FAILED)
1556 rc = fsmStage(fsm, FSM_MKNOD);
1558 rc = CPIOERR_UNKNOWN_FILETYPE;
1560 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1561 fsm->li->createdPath = fsm->li->linkIndex;
1562 rc = fsmMakeLinks(fsm);
1568 rc = fsmMakeLinks(fsm);
1570 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1571 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1572 rpmTransactionSet ts = fsmGetTs(fsm);
1573 TFI_t fi = fsmGetFi(fsm);
1574 if (ts && ts->notify && fi)
1575 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1576 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1577 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1583 if (fsm->goal == FSM_PKGINSTALL) {
1584 (void) fsmStage(fsm,
1585 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1587 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1589 (void) fsmStage(fsm, FSM_RMDIRS);
1593 if (fsm->failedFile && *fsm->failedFile == NULL)
1594 *fsm->failedFile = xstrdup(fsm->path);
1597 if (!fsm->postpone && fsm->commit) {
1598 if (fsm->goal == FSM_PKGINSTALL)
1599 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1600 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1601 if (fsm->goal == FSM_PKGCOMMIT)
1602 rc = fsmStage(fsm, FSM_COMMIT);
1603 if (fsm->goal == FSM_PKGERASE)
1604 rc = fsmStage(fsm, FSM_COMMIT);
1606 fsm->path = _free(fsm->path);
1607 fsm->opath = _free(fsm->opath);
1608 memset(st, 0, sizeof(*st));
1609 memset(ost, 0, sizeof(*ost));
1612 /* Rename pre-existing modified or unmanaged file. */
1613 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1614 const char * opath = fsm->opath;
1615 const char * path = fsm->path;
1616 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1617 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1619 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1620 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1622 rc = fsmStage(fsm, FSM_RENAME);
1624 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1625 fsm->opath, fsm->path);
1627 fsm->path = _free(fsm->path);
1629 fsm->opath = _free(fsm->opath);
1633 /* Remove erased files. */
1634 if (fsm->goal == FSM_PKGERASE) {
1635 if (fsm->action == FA_ERASE) {
1636 TFI_t fi = fsmGetFi(fsm);
1637 if (S_ISDIR(st->st_mode)) {
1638 rc = fsmStage(fsm, FSM_RMDIR);
1641 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1643 /* XXX make sure that build side permits %missingok on directories. */
1644 if (fsm->fflags & RPMFILE_MISSINGOK)
1647 /* XXX common error message. */
1649 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1650 _("%s rmdir of %s failed: Directory not empty\n"),
1651 fiTypeString(fi), fsm->path);
1655 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1656 _("%s rmdir of %s failed: %s\n"),
1657 fiTypeString(fi), fsm->path, strerror(errno));
1661 rc = fsmStage(fsm, FSM_UNLINK);
1663 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1665 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1666 _("%s unlink of %s failed: %s\n"),
1667 fiTypeString(fi), fsm->path, strerror(errno));
1670 /* XXX Failure to remove is not (yet) cause for failure. */
1671 if (!strict_erasures) rc = 0;
1675 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1676 /* Rename temporary to final file name. */
1677 if (!S_ISDIR(st->st_mode) &&
1678 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1680 fsm->opath = fsm->path;
1681 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1682 rc = fsmStage(fsm, FSM_RENAME);
1683 if (!rc && fsm->nsuffix) {
1684 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1685 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1686 (opath ? opath : ""), fsm->path);
1687 opath = _free(opath);
1689 fsm->opath = _free(fsm->opath);
1691 if (S_ISLNK(st->st_mode)) {
1692 if (!rc && !getuid())
1693 rc = fsmStage(fsm, FSM_LCHOWN);
1695 if (!rc && !getuid())
1696 rc = fsmStage(fsm, FSM_CHOWN);
1698 rc = fsmStage(fsm, FSM_CHMOD);
1700 time_t st_mtime = st->st_mtime;
1701 TFI_t fi = fsmGetFi(fsm);
1703 st->st_mtime = fi->fmtimes[fsm->ix];
1704 rc = fsmStage(fsm, FSM_UTIME);
1705 st->st_mtime = st_mtime;
1710 /* Notify on success. */
1711 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1714 fsm->path = _free(fsm->path);
1716 /* Check for hard links missing from payload. */
1717 while ((fsm->li = fsm->links) != NULL) {
1718 fsm->links = fsm->li->next;
1719 fsm->li->next = NULL;
1720 if (fsm->goal == FSM_PKGINSTALL &&
1721 fsm->commit && fsm->li->linksLeft)
1723 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1724 if (fsm->li->filex[i] < 0) continue;
1725 rc = CPIOERR_MISSING_HARDLINK;
1726 if (fsm->failedFile && *fsm->failedFile == NULL) {
1727 fsm->ix = fsm->li->filex[i];
1728 if (!fsmStage(fsm, FSM_MAP)) {
1729 *fsm->failedFile = fsm->path;
1733 /*@loopbreak@*/ break;
1736 if (fsm->goal == FSM_PKGBUILD &&
1737 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1739 rc = CPIOERR_MISSING_HARDLINK;
1741 fsm->li = freeHardLink(fsm->li);
1743 fsm->ldn = _free(fsm->ldn);
1744 fsm->ldnalloc = fsm->ldnlen = 0;
1745 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1746 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1749 if (fsm->diskchecked && !fsm->exists) {
1750 rc = CPIOERR_LSTAT_FAILED;
1753 if (S_ISREG(st->st_mode)) {
1754 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1755 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1757 * XXX HP-UX (and other os'es) don't permit unlink on busy
1760 fsm->opath = fsm->path;
1762 rc = fsmStage(fsm, FSM_RENAME);
1764 (void) fsmStage(fsm, FSM_UNLINK);
1766 rc = CPIOERR_UNLINK_FAILED;
1767 fsm->path = fsm->opath;
1769 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1770 /*@notreached@*/ break;
1771 } else if (S_ISDIR(st->st_mode)) {
1772 if (S_ISDIR(ost->st_mode)) return 0;
1773 if (S_ISLNK(ost->st_mode)) {
1774 rc = fsmStage(fsm, FSM_STAT);
1775 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1778 if (S_ISDIR(ost->st_mode)) return 0;
1780 } else if (S_ISLNK(st->st_mode)) {
1781 if (S_ISLNK(ost->st_mode)) {
1782 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1783 rc = fsmStage(fsm, FSM_READLINK);
1786 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1788 } else if (S_ISFIFO(st->st_mode)) {
1789 if (S_ISFIFO(ost->st_mode)) return 0;
1790 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1791 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1792 (ost->st_rdev == st->st_rdev)) return 0;
1793 } else if (S_ISSOCK(st->st_mode)) {
1794 if (S_ISSOCK(ost->st_mode)) return 0;
1796 /* XXX shouldn't do this with commit/undo. */
1798 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1799 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1800 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1801 /*@notreached@*/ break;
1804 rc = Unlink(fsm->path);
1805 if (_fsm_debug && (stage & FSM_SYSCALL))
1806 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1807 fsm->path, (rc < 0 ? strerror(errno) : ""));
1808 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1811 rc = Rename(fsm->opath, fsm->path);
1812 if (_fsm_debug && (stage & FSM_SYSCALL))
1813 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1814 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1815 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1818 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1819 if (_fsm_debug && (stage & FSM_SYSCALL))
1820 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1821 fsm->path, (unsigned)(st->st_mode & 07777),
1822 (rc < 0 ? strerror(errno) : ""));
1823 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1826 rc = Rmdir(fsm->path);
1827 if (_fsm_debug && (stage & FSM_SYSCALL))
1828 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1829 fsm->path, (rc < 0 ? strerror(errno) : ""));
1830 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1833 rc = chown(fsm->path, st->st_uid, st->st_gid);
1834 if (_fsm_debug && (stage & FSM_SYSCALL))
1835 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1836 fsm->path, (int)st->st_uid, (int)st->st_gid,
1837 (rc < 0 ? strerror(errno) : ""));
1838 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1841 #if ! CHOWN_FOLLOWS_SYMLINK
1842 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1843 if (_fsm_debug && (stage & FSM_SYSCALL))
1844 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1845 fsm->path, (int)st->st_uid, (int)st->st_gid,
1846 (rc < 0 ? strerror(errno) : ""));
1847 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1851 rc = chmod(fsm->path, (st->st_mode & 07777));
1852 if (_fsm_debug && (stage & FSM_SYSCALL))
1853 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1854 fsm->path, (unsigned)(st->st_mode & 07777),
1855 (rc < 0 ? strerror(errno) : ""));
1856 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1859 { struct utimbuf stamp;
1860 stamp.actime = st->st_mtime;
1861 stamp.modtime = st->st_mtime;
1862 rc = utime(fsm->path, &stamp);
1863 if (_fsm_debug && (stage & FSM_SYSCALL))
1864 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1865 fsm->path, (unsigned)st->st_mtime,
1866 (rc < 0 ? strerror(errno) : ""));
1867 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1871 rc = symlink(fsm->opath, fsm->path);
1872 if (_fsm_debug && (stage & FSM_SYSCALL))
1873 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1874 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1875 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1878 rc = Link(fsm->opath, fsm->path);
1879 if (_fsm_debug && (stage & FSM_SYSCALL))
1880 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1881 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1882 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1885 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1886 if (_fsm_debug && (stage & FSM_SYSCALL))
1887 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1888 fsm->path, (unsigned)(st->st_mode & 07777),
1889 (rc < 0 ? strerror(errno) : ""));
1890 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1894 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1896 if (_fsm_debug && (stage & FSM_SYSCALL))
1897 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1898 fsm->path, (unsigned)(st->st_mode & ~07777),
1899 (unsigned)st->st_rdev,
1900 (rc < 0 ? strerror(errno) : ""));
1901 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1904 rc = Lstat(fsm->path, ost);
1905 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1906 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1907 fsm->path, (rc < 0 ? strerror(errno) : ""));
1908 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1911 rc = Stat(fsm->path, ost);
1912 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1913 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1914 fsm->path, (rc < 0 ? strerror(errno) : ""));
1915 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1918 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1919 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1920 if (_fsm_debug && (stage & FSM_SYSCALL))
1921 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1922 fsm->path, (int)fsm->rdlen, (rc < 0 ? strerror(errno) : ""));
1923 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1926 fsm->rdbuf[fsm->rdnb] = '\0';
1934 rc = fsmStage(fsm, FSM_HREAD);
1936 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1937 fsm->path = _free(fsm->path);
1938 rc = CPIOERR_HDR_TRAILER;
1941 rc = fsmStage(fsm, FSM_POS);
1944 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1945 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1946 rc = fsmStage(fsm, FSM_DREAD);
1948 /*@loopbreak@*/ break;
1952 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1955 (void) fsmStage(fsm, FSM_DREAD);
1959 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1961 memset(fsm->rdbuf, 0, left);
1962 /* XXX DWRITE uses rdnb for I/O length. */
1964 (void) fsmStage(fsm, FSM_DWRITE);
1968 rc = cpioTrailerWrite(fsm);
1971 rc = fsmStage(fsm, FSM_POS);
1973 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1976 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1979 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1980 if (_fsm_debug && (stage & FSM_SYSCALL))
1981 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1982 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1983 (int)fsm->wrlen, (int)fsm->rdnb);
1984 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
1985 rc = CPIOERR_READ_FAILED;
1987 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1990 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1991 if (_fsm_debug && (stage & FSM_SYSCALL))
1992 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1993 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
1994 (int)fsm->rdnb, (int)fsm->wrnb);
1995 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
1996 rc = CPIOERR_WRITE_FAILED;
1998 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2002 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2003 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2004 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2006 rc = CPIOERR_OPEN_FAILED;
2009 if (_fsm_debug && (stage & FSM_SYSCALL))
2010 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2011 fsm->path, fsm->rfd, fsm->rdbuf);
2014 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2015 if (_fsm_debug && (stage & FSM_SYSCALL))
2016 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2017 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2018 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2019 rc = CPIOERR_READ_FAILED;
2023 if (_fsm_debug && (stage & FSM_SYSCALL))
2024 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2025 (void) Fclose(fsm->rfd);
2031 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2032 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2033 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2035 rc = CPIOERR_OPEN_FAILED;
2037 if (_fsm_debug && (stage & FSM_SYSCALL))
2038 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2039 fsm->path, fsm->wfd, fsm->wrbuf);
2042 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2043 if (_fsm_debug && (stage & FSM_SYSCALL))
2044 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2045 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2046 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2047 rc = CPIOERR_WRITE_FAILED;
2051 if (_fsm_debug && (stage & FSM_SYSCALL))
2052 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2053 (void) Fclose(fsm->wfd);
2063 if (!(stage & FSM_INTERNAL)) {
2064 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2070 /*@obserever@*/ const char *const fileActionString(fileAction a)
2073 case FA_UNKNOWN: return "unknown";
2074 case FA_CREATE: return "create";
2075 case FA_COPYOUT: return "copyout";
2076 case FA_COPYIN: return "copyin";
2077 case FA_BACKUP: return "backup";
2078 case FA_SAVE: return "save";
2079 case FA_SKIP: return "skip";
2080 case FA_ALTNAME: return "altname";
2081 case FA_ERASE: return "erase";
2082 case FA_SKIPNSTATE: return "skipnstate";
2083 case FA_SKIPNETSHARED: return "skipnetshared";
2084 case FA_SKIPMULTILIB: return "skipmultilib";
2085 default: return "???";
2090 /*@observer@*/ const char *const fileStageString(fileStage a) {
2092 case FSM_UNKNOWN: return "unknown";
2094 case FSM_PKGINSTALL:return "pkginstall";
2095 case FSM_PKGERASE: return "pkgerase";
2096 case FSM_PKGBUILD: return "pkgbuild";
2097 case FSM_PKGCOMMIT: return "pkgcommit";
2098 case FSM_PKGUNDO: return "pkgundo";
2100 case FSM_CREATE: return "create";
2101 case FSM_INIT: return "init";
2102 case FSM_MAP: return "map";
2103 case FSM_MKDIRS: return "mkdirs";
2104 case FSM_RMDIRS: return "rmdirs";
2105 case FSM_PRE: return "pre";
2106 case FSM_PROCESS: return "process";
2107 case FSM_POST: return "post";
2108 case FSM_MKLINKS: return "mklinks";
2109 case FSM_NOTIFY: return "notify";
2110 case FSM_UNDO: return "undo";
2111 case FSM_FINI: return "fini";
2112 case FSM_COMMIT: return "commit";
2113 case FSM_DESTROY: return "destroy";
2114 case FSM_VERIFY: return "verify";
2116 case FSM_UNLINK: return "Unlink";
2117 case FSM_RENAME: return "Rename";
2118 case FSM_MKDIR: return "Mkdir";
2119 case FSM_RMDIR: return "rmdir";
2120 case FSM_CHOWN: return "chown";
2121 case FSM_LCHOWN: return "lchown";
2122 case FSM_CHMOD: return "chmod";
2123 case FSM_UTIME: return "utime";
2124 case FSM_SYMLINK: return "symlink";
2125 case FSM_LINK: return "Link";
2126 case FSM_MKFIFO: return "mkfifo";
2127 case FSM_MKNOD: return "mknod";
2128 case FSM_LSTAT: return "Lstat";
2129 case FSM_STAT: return "Stat";
2130 case FSM_READLINK: return "Readlink";
2131 case FSM_CHROOT: return "chroot";
2133 case FSM_NEXT: return "next";
2134 case FSM_EAT: return "eat";
2135 case FSM_POS: return "pos";
2136 case FSM_PAD: return "pad";
2137 case FSM_TRAILER: return "trailer";
2138 case FSM_HREAD: return "hread";
2139 case FSM_HWRITE: return "hwrite";
2140 case FSM_DREAD: return "Fread";
2141 case FSM_DWRITE: return "Fwrite";
2143 case FSM_ROPEN: return "Fopen";
2144 case FSM_READ: return "Fread";
2145 case FSM_RCLOSE: return "Fclose";
2146 case FSM_WOPEN: return "Fopen";
2147 case FSM_WRITE: return "Fwrite";
2148 case FSM_WCLOSE: return "Fclose";
2150 default: return "???";