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))
23 /* XXX Failure to remove is not (yet) cause for failure. */
24 /*@-exportlocal -exportheadervar@*/
26 int strict_erasures = 0;
27 /*@=exportlocal =exportheadervar@*/
29 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
30 const FSMI_t iter = fsm->iter;
31 /*@-compdef -refcounttrans -retexpose -usereleased @*/
32 return (iter ? iter->ts : NULL);
33 /*@=compdef =refcounttrans =retexpose =usereleased @*/
36 TFI_t fsmGetFi(const FSM_t fsm)
38 const FSMI_t iter = fsm->iter;
39 /*@-compdef -refcounttrans -retexpose -usereleased @*/
40 return (iter ? iter->fi : NULL);
41 /*@=compdef =refcounttrans =retexpose =usereleased @*/
44 #define SUFFIX_RPMORIG ".rpmorig"
45 #define SUFFIX_RPMSAVE ".rpmsave"
46 #define SUFFIX_RPMNEW ".rpmnew"
49 * Build path to file from file info, ornamented with subdir and suffix.
50 * @param fsm file state machine data
51 * @param st file stat info
52 * @param subdir subdir to use (NULL disables)
53 * @param suffix suffix to use (NULL disables)
54 * @retval path to file
56 static /*@only@*//*@null@*/
57 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
58 /*@null@*/ const struct stat * st,
59 /*@null@*/ const char * subdir,
60 /*@null@*/ const char * suffix)
61 /*@uses fsm->dirName, fsm->baseName */
64 const char * s = NULL;
69 nb = strlen(fsm->dirName) +
70 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
71 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
72 strlen(fsm->baseName) + 1;
74 t = stpcpy(t, fsm->dirName);
75 if (st && !S_ISDIR(st->st_mode))
76 if (subdir) t = stpcpy(t, subdir);
77 t = stpcpy(t, fsm->baseName);
78 if (st && !S_ISDIR(st->st_mode))
79 if (suffix) t = stpcpy(t, suffix);
85 * Destroy file info iterator.
86 * @param p file info iterator
89 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/ void * p)
94 iter->ts = rpmtsUnlink(iter->ts, "mapIterator");
95 iter->fi = rpmfiUnlink(iter->fi, "mapIterator");
101 * Create file info iterator.
102 * @param a transaction set
103 * @param b transaction element file info
104 * @return file info iterator
107 mapInitIterator(rpmTransactionSet ts, TFI_t fi)
108 /*@modifies ts, fi @*/
112 iter = xcalloc(1, sizeof(*iter));
113 iter->ts = rpmtsLink(ts, "mapIterator");
114 iter->fi = rpmfiLink(fi, "mapIterator");
115 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
116 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
117 iter->isave = iter->i;
122 * Return next index into file info.
123 * @param a file info iterator
124 * @return next index, -1 on termination
126 static int mapNextIterator(/*@null@*/ void * a)
133 const TFI_t fi = iter->fi;
135 if (iter->i >= 0) i = iter->i--;
137 if (iter->i < fi->fc) i = iter->i++;
146 static int cpioStrCmp(const void * a, const void * b)
149 const char * afn = *(const char **)a;
150 const char * bfn = *(const char **)b;
152 /* Match rpm-4.0 payloads with ./ prefixes. */
153 if (afn[0] == '.' && afn[1] == '/') afn += 2;
154 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
156 /* If either path is absolute, make it relative. */
157 if (afn[0] == '/') afn += 1;
158 if (bfn[0] == '/') bfn += 1;
160 return strcmp(afn, bfn);
164 * Locate archive path in file info.
165 * @param iter file info iterator
166 * @param fsmPath archive path
167 * @return index into file info, -1 if archive path was not found
169 static int mapFind(/*@null@*/ FSMI_t iter, const char * fsmPath)
175 const TFI_t fi = iter->fi;
176 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
177 const char ** p = NULL;
179 if (fi->apath != NULL)
180 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
183 iter->i = p - fi->apath;
184 ix = mapNextIterator(iter);
192 * Directory name iterator.
194 typedef struct dnli_s {
196 /*@only@*/ /*@null@*/ char * active;
203 * Destroy directory name iterator.
204 * @param a directory name iterator
205 * @retval NULL always
207 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
211 DNLI_t dnli = (void *)a;
212 if (dnli->active) free(dnli->active);
219 static inline int dnlCount(const DNLI_t dnli)
222 return (dnli ? dnli->fi->dc : 0);
227 static inline int dnlIndex(const DNLI_t dnli)
230 return (dnli ? dnli->isave : -1);
234 * Create directory name iterator.
235 * @param fsm file state machine data
236 * @param reverse traverse directory names in reverse order?
237 * @return directory name iterator
240 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
242 /*@uses fsm->iter @*/
245 TFI_t fi = fsmGetFi(fsm);
251 dnli = xcalloc(1, sizeof(*dnli));
253 dnli->reverse = reverse;
255 dnli->i = (reverse ? fi->dc : 0);
259 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
261 /* Identify parent directories not skipped. */
262 for (i = 0; i < fi->fc; i++)
263 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
265 /* Exclude parent directories that are explicitly included. */
266 for (i = 0; i < fi->fc; i++) {
267 int dil, dnlen, bnlen;
269 if (!S_ISDIR(fi->fmodes[i]))
273 dnlen = strlen(fi->dnl[dil]);
274 bnlen = strlen(fi->bnl[i]);
276 for (j = 0; j < fi->dc; j++) {
280 if (!dnli->active[j] || j == dil)
281 /*@innercontinue@*/ continue;
284 if (jlen != (dnlen+bnlen+1))
285 /*@innercontinue@*/ continue;
286 if (strncmp(dnl, fi->dnl[dil], dnlen))
287 /*@innercontinue@*/ continue;
288 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
289 /*@innercontinue@*/ continue;
290 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
291 /*@innercontinue@*/ continue;
292 /* This directory is included in the package. */
294 /*@innerbreak@*/ break;
298 /* Print only once per package. */
301 for (i = 0; i < fi->dc; i++) {
302 if (!dnli->active[i]) continue;
305 rpmMessage(RPMMESS_DEBUG,
306 _("========== Directories not explictly included in package:\n"));
308 rpmMessage(RPMMESS_DEBUG, _("%10d %s\n"), i, fi->dnl[i]);
311 rpmMessage(RPMMESS_DEBUG, "==========\n");
319 * Return next directory name (from file info).
320 * @param dnli directory name iterator
321 * @return next directory name
323 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
326 const char * dn = NULL;
334 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
335 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
337 if (i >= 0 && i < fi->dc)
347 * Save hard link in chain.
348 * @param fsm file state machine data
349 * @return Is chain only partially filled?
351 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
352 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
353 /*@defines fsm->li @*/
354 /*@releases fsm->path @*/
355 /*@globals fileSystem@*/
356 /*@modifies fsm, fileSystem @*/
358 struct stat * st = &fsm->sb;
363 /* Find hard link set. */
365 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
366 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
371 /* New hard link encountered, add new link to set. */
373 if (fsm->li == NULL) {
374 fsm->li = xcalloc(1, sizeof(*fsm->li));
375 fsm->li->next = NULL;
376 fsm->li->sb = *st; /* structure assignment */
377 fsm->li->nlink = st->st_nlink;
378 fsm->li->linkIndex = fsm->ix;
379 fsm->li->createdPath = -1;
381 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
382 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
383 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
385 if (fsm->goal == FSM_PKGBUILD)
386 fsm->li->linksLeft = st->st_nlink;
387 if (fsm->goal == FSM_PKGINSTALL)
388 fsm->li->linksLeft = 0;
391 fsm->li->next = fsm->links;
393 fsm->links = fsm->li;
397 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
398 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
399 /*@-observertrans -dependenttrans@*/
400 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
401 /*@=observertrans =dependenttrans@*/
402 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
404 if (fsm->goal == FSM_PKGBUILD)
405 return (fsm->li->linksLeft > 0);
407 if (fsm->goal != FSM_PKGINSTALL)
410 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
413 /* Here come the bits, time to choose a non-skipped file name. */
414 { TFI_t fi = fsmGetFi(fsm);
416 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
417 ix = fsm->li->filex[j];
418 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
424 /* Are all links skipped or not encountered yet? */
426 return 1; /* XXX W2DO? */
428 /* Save the non-skipped file name and map index. */
429 fsm->li->linkIndex = j;
430 fsm->path = _free(fsm->path);
432 rc = fsmStage(fsm, FSM_MAP);
437 * Destroy set of hard links.
438 * @param li set of hard links
440 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
444 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
445 li->filex = _free(li->filex);
452 FSM_t fsm = xcalloc(1, sizeof(*fsm));
456 FSM_t freeFSM(FSM_t fsm)
459 fsm->path = _free(fsm->path);
461 while ((fsm->li = fsm->links) != NULL) {
462 fsm->links = fsm->li->next;
463 fsm->li->next = NULL;
464 fsm->li = freeHardLink(fsm->li);
467 fsm->dnlx = _free(fsm->dnlx);
468 fsm->ldn = _free(fsm->ldn);
469 fsm->iter = mapFreeIterator(fsm->iter);
474 int fsmSetup(FSM_t fsm, fileStage goal,
475 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
476 unsigned int * archiveSize, const char ** failedFile)
483 /*@-type@*/ /* FIX: cast? */
484 fsm->cfd = fdLink(cfd, "persist (fsm)");
486 pos = fdGetCpioPos(fsm->cfd);
487 fdSetCpioPos(fsm->cfd, 0);
489 fsm->iter = mapInitIterator(ts, fi);
491 if (fsm->goal == FSM_PKGINSTALL) {
492 if (ts && ts->notify) {
493 /*@-type@*/ /* FIX: cast? */
494 /*@-noeffectuncon @*/ /* FIX: check rc */
495 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
496 rpmfiGetKey(fi), ts->notifyData);
497 /*@=noeffectuncon @*/
503 fsm->archiveSize = archiveSize;
504 if (fsm->archiveSize)
505 *fsm->archiveSize = 0;
506 fsm->failedFile = failedFile;
508 *fsm->failedFile = NULL;
511 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
512 if (fsm->goal == FSM_PKGINSTALL) {
513 if (ts && ts->id > 0)
514 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
518 rc = fsmStage(fsm, FSM_CREATE);
519 if (rc && !ec) ec = rc;
521 rc = fsmStage(fsm, fsm->goal);
522 if (rc && !ec) ec = rc;
524 if (fsm->archiveSize && ec == 0)
525 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
530 int fsmTeardown(FSM_t fsm)
535 rc = fsmStage(fsm, FSM_DESTROY);
537 fsm->iter = mapFreeIterator(fsm->iter);
539 /*@-type@*/ /* FIX: cast? */
540 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
544 fsm->failedFile = NULL;
548 int fsmMapPath(FSM_t fsm)
550 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
557 fsm->action = FA_UNKNOWN;
561 if (fi && i >= 0 && i < fi->fc) {
563 fsm->astriplen = fi->astriplen;
564 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
565 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
566 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
568 /* src rpms have simple base name in payload. */
569 fsm->dirName = fi->dnl[fi->dil[i]];
570 fsm->baseName = fi->bnl[i];
572 switch (fsm->action) {
575 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
584 assert(fi->type == TR_ADDED);
588 if (fi->fstates && fi->type == TR_ADDED)
589 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
592 case FA_SKIPNETSHARED:
593 if (fi->fstates && fi->type == TR_ADDED)
594 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
598 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
601 fsm->osuffix = SUFFIX_RPMORIG;
602 /*@innerbreak@*/ break;
604 fsm->osuffix = SUFFIX_RPMSAVE;
605 /*@innerbreak@*/ break;
610 assert(fi->type == TR_ADDED);
611 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
612 fsm->nsuffix = SUFFIX_RPMNEW;
616 assert(fi->type == TR_ADDED);
617 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
618 fsm->osuffix = SUFFIX_RPMSAVE;
621 assert(fi->type == TR_REMOVED);
623 * XXX TODO: %ghost probably shouldn't be removed, but that changes
624 * legacy rpm behavior.
631 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
632 const struct stat * st = &fsm->sb;
633 fsm->path = _free(fsm->path);
634 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
635 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
641 int fsmMapAttrs(FSM_t fsm)
643 struct stat * st = &fsm->sb;
644 TFI_t fi = fsmGetFi(fsm);
647 if (fi && i >= 0 && i < fi->fc) {
649 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
651 (fi->fmodes ? fi->fmodes[i] : perms);
653 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
655 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
657 (fi->frdevs ? fi->frdevs[i] : 0);
659 (fi->fmtimes ? fi->fmtimes[i] : 0);
661 if (fsm->mapFlags & CPIO_MAP_MODE)
662 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
663 if (fsm->mapFlags & CPIO_MAP_TYPE) {
664 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
665 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
666 && st->st_nlink == 0)
668 st->st_rdev = finalRdev;
669 st->st_mtime = finalMtime;
671 if (fsm->mapFlags & CPIO_MAP_UID)
672 st->st_uid = finalUid;
673 if (fsm->mapFlags & CPIO_MAP_GID)
674 st->st_gid = finalGid;
676 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
683 * Create file from payload stream.
684 * @param fsm file state machine data
685 * @return 0 on success
687 static int expandRegular(/*@special@*/ FSM_t fsm)
689 /*@globals fileSystem@*/
690 /*@modifies fsm, fileSystem @*/
692 const char * fmd5sum;
693 const struct stat * st = &fsm->sb;
694 int left = st->st_size;
697 rc = fsmStage(fsm, FSM_WOPEN);
701 /* XXX md5sum's will break on repackaging that includes modified files. */
702 fmd5sum = fsm->fmd5sum;
704 if (st->st_size > 0 && fmd5sum)
705 fdInitDigest(fsm->wfd, PGPHASHALGO_MD5, 0);
709 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
710 rc = fsmStage(fsm, FSM_DREAD);
714 rc = fsmStage(fsm, FSM_WRITE);
720 /* don't call this with fileSize == fileComplete */
722 (void) fsmStage(fsm, FSM_NOTIFY);
725 if (st->st_size > 0 && fmd5sum) {
726 const char * md5sum = NULL;
728 (void) Fflush(fsm->wfd);
729 fdFiniDigest(fsm->wfd, PGPHASHALGO_MD5, (void **)&md5sum, NULL, 1);
731 if (md5sum == NULL) {
732 rc = CPIOERR_MD5SUM_MISMATCH;
734 if (strcmp(md5sum, fmd5sum))
735 rc = CPIOERR_MD5SUM_MISMATCH;
736 md5sum = _free(md5sum);
741 (void) fsmStage(fsm, FSM_WCLOSE);
746 * Write next item to payload stream.
747 * @param fsm file state machine data
748 * @param writeData should data be written?
749 * @return 0 on success
751 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
752 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
753 /*@globals fileSystem@*/
754 /*@modifies fsm, fileSystem @*/
756 const char * path = fsm->path;
757 const char * opath = fsm->opath;
758 struct stat * st = &fsm->sb;
759 struct stat * ost = &fsm->osb;
760 size_t pos = fdGetCpioPos(fsm->cfd);
761 char * symbuf = NULL;
765 st->st_size = (writeData ? ost->st_size : 0);
768 if (S_ISDIR(st->st_mode)) {
770 } else if (S_ISLNK(st->st_mode)) {
772 * While linux puts the size of a symlink in the st_size field,
773 * I don't think that's a specified standard.
775 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
776 rc = fsmStage(fsm, FSM_READLINK);
778 st->st_size = fsm->rdnb;
779 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
783 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
784 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
785 char * t = alloca(nb);
788 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
790 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
791 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
792 TFI_t fi = fsmGetFi(fsm);
794 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
797 rc = fsmStage(fsm, FSM_HWRITE);
801 if (writeData && S_ISREG(st->st_mode)) {
804 void * mapped = (void *)-1;
808 rc = fsmStage(fsm, FSM_ROPEN);
811 /* XXX unbuffered mmap generates *lots* of fdio debugging */
814 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
815 if (mapped != (void *)-1) {
817 fsm->rdbuf = (char *) mapped;
818 fsm->rdlen = nmapped = st->st_size;
826 if (mapped != (void *)-1) {
831 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
832 rc = fsmStage(fsm, FSM_READ);
836 /* XXX DWRITE uses rdnb for I/O length. */
837 rc = fsmStage(fsm, FSM_DWRITE);
844 if (mapped != (void *)-1) {
845 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
850 } else if (writeData && S_ISLNK(st->st_mode)) {
851 /* XXX DWRITE uses rdnb for I/O length. */
852 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
853 fsm->rdnb = strlen(symbuf);
854 rc = fsmStage(fsm, FSM_DWRITE);
858 rc = fsmStage(fsm, FSM_PAD);
861 { const rpmTransactionSet ts = fsmGetTs(fsm);
862 TFI_t fi = fsmGetFi(fsm);
863 if (ts && ts->notify && fi) {
864 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
865 /*@-type@*/ /* FIX: cast? */
866 /*@-noeffectuncon @*/ /* FIX: check rc */
867 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
868 rpmfiGetKey(fi), ts->notifyData);
869 /*@=noeffectuncon @*/
878 (void) fsmStage(fsm, FSM_RCLOSE);
879 /*@-dependenttrans@*/
882 /*@=dependenttrans@*/
887 * Write set of linked files to payload stream.
888 * @param fsm file state machine data
889 * @return 0 on success
891 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
892 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
893 /*@globals fileSystem@*/
894 /*@modifies fsm, fileSystem @*/
896 const char * path = fsm->path;
897 const char * nsuffix = fsm->nsuffix;
898 int iterIndex = fsm->ix;
907 for (i = fsm->li->nlink - 1; i >= 0; i--) {
909 if (fsm->li->filex[i] < 0) continue;
911 fsm->ix = fsm->li->filex[i];
912 rc = fsmStage(fsm, FSM_MAP);
914 /* Write data after last link. */
915 rc = writeFile(fsm, (i == 0));
916 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
918 *fsm->failedFile = xstrdup(fsm->path);
921 fsm->path = _free(fsm->path);
922 fsm->li->filex[i] = -1;
926 fsm->nsuffix = nsuffix;
932 * Create pending hard links to existing file.
933 * @param fsm file state machine data
934 * @return 0 on success
936 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
937 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
938 /*@globals fileSystem@*/
939 /*@modifies fsm, fileSystem @*/
941 const char * path = fsm->path;
942 const char * opath = fsm->opath;
943 const char * nsuffix = fsm->nsuffix;
944 int iterIndex = fsm->ix;
954 fsm->ix = fsm->li->filex[fsm->li->createdPath];
955 rc = fsmStage(fsm, FSM_MAP);
956 fsm->opath = fsm->path;
959 for (i = 0; i < fsm->li->nlink; i++) {
960 if (fsm->li->filex[i] < 0) continue;
961 if (i == fsm->li->createdPath) continue;
963 fsm->ix = fsm->li->filex[i];
964 fsm->path = _free(fsm->path);
965 rc = fsmStage(fsm, FSM_MAP);
966 rc = fsmStage(fsm, FSM_VERIFY);
968 if (rc != CPIOERR_LSTAT_FAILED) break;
970 /* XXX link(fsm->opath, fsm->path) */
971 rc = fsmStage(fsm, FSM_LINK);
972 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
974 *fsm->failedFile = xstrdup(fsm->path);
977 fsm->li->linksLeft--;
980 fsm->path = _free(fsm->path);
981 fsm->opath = _free(fsm->opath);
984 fsm->nsuffix = nsuffix;
991 * Commit hard linked file set atomically.
992 * @param fsm file state machine data
993 * @return 0 on success
995 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
996 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
997 fsm->li, fsm->links @*/
998 /*@globals fileSystem@*/
999 /*@modifies fsm, fileSystem @*/
1001 const char * path = fsm->path;
1002 const char * nsuffix = fsm->nsuffix;
1003 int iterIndex = fsm->ix;
1004 struct stat * st = &fsm->sb;
1009 fsm->nsuffix = NULL;
1013 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1014 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1019 for (i = 0; i < fsm->li->nlink; i++) {
1020 if (fsm->li->filex[i] < 0) continue;
1021 fsm->ix = fsm->li->filex[i];
1022 rc = fsmStage(fsm, FSM_MAP);
1023 rc = fsmStage(fsm, FSM_COMMIT);
1024 fsm->path = _free(fsm->path);
1025 fsm->li->filex[i] = -1;
1028 fsm->ix = iterIndex;
1029 fsm->nsuffix = nsuffix;
1035 * Remove (if created) directories not explicitly included in package.
1036 * @param fsm file state machine data
1037 * @return 0 on success
1039 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1040 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1041 /*@globals fileSystem@*/
1042 /*@modifies fsm, fileSystem @*/
1044 const char * path = fsm->path;
1045 void * dnli = dnlInitIterator(fsm, 1);
1046 char * dn = fsm->rdbuf;
1047 int dc = dnlCount(dnli);
1052 /*@-observertrans -dependenttrans@*/
1053 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1054 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1055 int dnlen = strlen(fsm->path);
1058 dc = dnlIndex(dnli);
1059 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1062 /* Copy to avoid const on fsm->path. */
1063 te = stpcpy(dn, fsm->path) - 1;
1066 /* Remove generated directories. */
1067 /*@-usereleased@*/ /* LCL: te used after release? */
1071 rc = fsmStage(fsm, FSM_RMDIR);
1075 /*@innerbreak@*/ break;
1077 } while ((te - fsm->path) > fsm->dnlx[dc]);
1080 dnli = dnlFreeIterator(dnli);
1081 /*@=observertrans =dependenttrans@*/
1088 * Create (if necessary) directories not explicitly included in package.
1089 * @param fsm file state machine data
1090 * @return 0 on success
1092 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1093 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1094 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1095 /*@defines fsm->dnlx, fsm->ldn @*/
1096 /*@globals fileSystem@*/
1097 /*@modifies fsm, fileSystem @*/
1099 struct stat * st = &fsm->sb;
1100 struct stat * ost = &fsm->osb;
1101 const char * path = fsm->path;
1102 mode_t st_mode = st->st_mode;
1103 void * dnli = dnlInitIterator(fsm, 0);
1104 char * dn = fsm->rdbuf;
1105 int dc = dnlCount(dnli);
1112 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1113 /*@-observertrans -dependenttrans@*/
1114 if (fsm->dnlx != NULL)
1115 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1116 int dnlen = strlen(fsm->path);
1119 dc = dnlIndex(dnli);
1120 if (dc < 0) continue;
1121 fsm->dnlx[dc] = dnlen;
1125 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1126 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1128 /*@=compdef =nullpass@*/
1130 /* Copy to avoid const on fsm->path. */
1131 (void) stpcpy(dn, fsm->path);
1134 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1135 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1137 /*@innercontinue@*/ continue;
1141 /* Already validated? */
1142 /*@-usedef -compdef -nullpass -nullderef@*/
1143 if (i < fsm->ldnlen &&
1144 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1145 !strncmp(fsm->path, fsm->ldn, i))
1148 /* Move pre-existing path marker forward. */
1149 fsm->dnlx[dc] = (te - dn);
1150 /*@innercontinue@*/ continue;
1152 /*@=usedef =compdef =nullpass =nullderef@*/
1154 /* Validate next component of path. */
1155 rc = fsmStage(fsm, FSM_LSTAT);
1158 /* Directory already exists? */
1159 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1160 /* Move pre-existing path marker forward. */
1161 fsm->dnlx[dc] = (te - dn);
1162 } else if (rc == CPIOERR_LSTAT_FAILED) {
1163 TFI_t fi = fsmGetFi(fsm);
1165 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1166 rc = fsmStage(fsm, FSM_MKDIR);
1168 rpmMessage(RPMMESS_DEBUG,
1169 _("%s directory created with perms %04o.\n"),
1170 fsm->path, (unsigned)(st->st_mode & 07777));
1174 /*@innerbreak@*/ break;
1178 /* Save last validated path. */
1179 if (fsm->ldnalloc < (dnlen + 1)) {
1180 fsm->ldnalloc = dnlen + 100;
1181 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1183 if (fsm->ldn != NULL) { /* XXX can't happen */
1184 strcpy(fsm->ldn, fsm->path);
1185 fsm->ldnlen = dnlen;
1188 dnli = dnlFreeIterator(dnli);
1189 /*@=observertrans =dependenttrans@*/
1192 st->st_mode = st_mode; /* XXX restore st->st_mode */
1198 * Check for file on disk.
1199 * @param fsm file state machine data
1200 * @return 0 on success
1202 static int fsmStat(FSM_t fsm)
1204 int saveerrno = errno;
1207 if (fsm->path != NULL) {
1208 int saveernno = errno;
1209 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1210 ? FSM_LSTAT : FSM_STAT));
1211 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1215 } else if (rc == 0) {
1219 /* Skip %ghost files on build. */
1227 int fsmStage(FSM_t fsm, fileStage stage)
1230 fileStage prevStage = fsm->stage;
1231 const char * const prev = fileStageString(prevStage);
1233 static int modulo = 4;
1234 const char * const cur = fileStageString(stage);
1235 struct stat * st = &fsm->sb;
1236 struct stat * ost = &fsm->osb;
1237 int saveerrno = errno;
1242 #define _fafilter(_a) \
1243 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1244 ? fileActionString(_a) : "")
1246 if (stage & FSM_DEAD) {
1248 } else if (stage & FSM_INTERNAL) {
1249 if (_fsm_debug && !(stage & FSM_SYSCALL))
1250 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1252 (unsigned)st->st_mode, (int)st->st_nlink,
1253 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1254 (fsm->path ? fsm->path : ""),
1255 _fafilter(fsm->action));
1258 if (_fsm_debug || !(stage & FSM_VERBOSE))
1259 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1261 (unsigned)st->st_mode, (int)st->st_nlink,
1262 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1263 (fsm->path ? fsm->path + fsm->astriplen : ""),
1264 _fafilter(fsm->action));
1272 case FSM_PKGINSTALL:
1274 /* Clean fsm, free'ing memory. Read next archive header. */
1275 rc = fsmStage(fsm, FSM_INIT);
1277 /* Exit on end-of-payload. */
1278 if (rc == CPIOERR_HDR_TRAILER) {
1280 /*@loopbreak@*/ break;
1283 /* Exit on error. */
1286 (void) fsmStage(fsm, FSM_UNDO);
1287 /*@loopbreak@*/ break;
1290 /* Extract file from archive. */
1291 rc = fsmStage(fsm, FSM_PROCESS);
1293 (void) fsmStage(fsm, FSM_UNDO);
1294 /*@loopbreak@*/ break;
1297 /* Notify on success. */
1298 (void) fsmStage(fsm, FSM_NOTIFY);
1300 rc = fsmStage(fsm, FSM_FINI);
1302 /*@loopbreak@*/ break;
1309 /* Clean fsm, free'ing memory. */
1310 rc = fsmStage(fsm, FSM_INIT);
1312 /* Exit on end-of-payload. */
1313 if (rc == CPIOERR_HDR_TRAILER) {
1315 /*@loopbreak@*/ break;
1318 /* Rename/erase next item. */
1319 if (fsmStage(fsm, FSM_FINI))
1320 /*@loopbreak@*/ break;
1326 rc = fsmStage(fsm, FSM_INIT);
1328 /* Exit on end-of-payload. */
1329 if (rc == CPIOERR_HDR_TRAILER) {
1331 /*@loopbreak@*/ break;
1334 /* Exit on error. */
1337 (void) fsmStage(fsm, FSM_UNDO);
1338 /*@loopbreak@*/ break;
1341 /* Copy file into archive. */
1342 rc = fsmStage(fsm, FSM_PROCESS);
1344 (void) fsmStage(fsm, FSM_UNDO);
1345 /*@loopbreak@*/ break;
1348 if (fsmStage(fsm, FSM_FINI))
1349 /*@loopbreak@*/ break;
1352 /* Flush partial sets of hard linked files. */
1353 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1355 while ((fsm->li = fsm->links) != NULL) {
1356 fsm->links = fsm->li->next;
1357 fsm->li->next = NULL;
1359 /* Re-calculate link count for archive header. */
1360 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1361 if (fsm->li->filex[i] < 0)
1362 /*@innercontinue@*/ continue;
1366 /* XXX force the contents out as well. */
1368 fsm->li->filex[0] = fsm->li->filex[j];
1369 fsm->li->filex[j] = -1;
1371 fsm->li->sb.st_nlink = nlink;
1373 fsm->sb = fsm->li->sb; /* structure assignment */
1374 fsm->osb = fsm->sb; /* structure assignment */
1376 if (!rc) rc = writeLinkedFile(fsm);
1378 fsm->li = freeHardLink(fsm->li);
1383 rc = fsmStage(fsm, FSM_TRAILER);
1387 { rpmTransactionSet ts = fsmGetTs(fsm);
1388 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1389 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1390 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1393 fsm->path = _free(fsm->path);
1394 fsm->opath = _free(fsm->opath);
1395 fsm->dnlx = _free(fsm->dnlx);
1397 fsm->ldn = _free(fsm->ldn);
1398 fsm->ldnalloc = fsm->ldnlen = 0;
1400 fsm->rdsize = fsm->wrsize = 0;
1401 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1402 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1403 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1404 fsm->rdsize = 8 * BUFSIZ;
1405 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1406 fsm->wrsize = 8 * BUFSIZ;
1407 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1410 fsm->mkdirsdone = 0;
1415 errno = 0; /* XXX get rid of EBADF */
1418 /* Detect and create directories not explicitly in package. */
1419 if (fsm->goal == FSM_PKGINSTALL) {
1420 rc = fsmStage(fsm, FSM_MKDIRS);
1421 if (!rc) fsm->mkdirsdone = 1;
1426 fsm->path = _free(fsm->path);
1428 fsm->diskchecked = fsm->exists = 0;
1430 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1431 fsm->action = FA_UNKNOWN;
1432 fsm->osuffix = NULL;
1433 fsm->nsuffix = NULL;
1435 if (fsm->goal == FSM_PKGINSTALL) {
1436 /* Read next header from payload, checking for end-of-payload. */
1437 rc = fsmStage(fsm, FSM_NEXT);
1441 /* Identify mapping index. */
1442 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1443 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1445 /* Detect end-of-loop and/or mapping error. */
1447 if (fsm->goal == FSM_PKGINSTALL) {
1449 rpmMessage(RPMMESS_WARNING,
1450 _("archive file %s was not found in header file list\n"),
1453 if (fsm->failedFile && *fsm->failedFile == NULL)
1454 *fsm->failedFile = xstrdup(fsm->path);
1455 rc = CPIOERR_UNMAPPED_FILE;
1457 rc = CPIOERR_HDR_TRAILER;
1462 /* On non-install, mode must be known so that dirs don't get suffix. */
1463 if (fsm->goal != FSM_PKGINSTALL) {
1464 TFI_t fi = fsmGetFi(fsm);
1465 st->st_mode = fi->fmodes[fsm->ix];
1468 /* Generate file path. */
1469 rc = fsmStage(fsm, FSM_MAP);
1472 /* Perform lstat/stat for disk file. */
1476 if (fsm->path != NULL) {
1477 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1478 ? FSM_LSTAT : FSM_STAT));
1479 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1485 } else if (rc == 0) {
1489 /* Skip %ghost files on build. */
1493 fsm->diskchecked = 1;
1496 /* On non-install, the disk file stat is what's remapped. */
1497 if (fsm->goal != FSM_PKGINSTALL)
1498 *st = *ost; /* structure assignment */
1500 /* Remap file perms, owner, and group. */
1501 rc = fsmMapAttrs(fsm);
1504 fsm->postpone = XFA_SKIPPING(fsm->action);
1505 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1506 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1507 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1508 fsm->postpone = saveHardLink(fsm);
1515 rc = fsmMapPath(fsm);
1518 rc = fsmMkdirs(fsm);
1522 rc = fsmRmdirs(fsm);
1525 if (fsm->postpone) {
1526 if (fsm->goal == FSM_PKGINSTALL)
1527 rc = fsmStage(fsm, FSM_EAT);
1531 if (fsm->goal == FSM_PKGBUILD) {
1532 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1533 struct hardLink * li, * prev;
1535 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1536 rc = writeLinkedFile(fsm);
1537 if (rc) break; /* W2DO? */
1539 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1541 /*@loopbreak@*/ break;
1544 fsm->links = fsm->li->next;
1546 prev->next = fsm->li->next;
1547 fsm->li->next = NULL;
1548 fsm->li = freeHardLink(fsm->li);
1550 rc = writeFile(fsm, 1);
1555 if (fsm->goal != FSM_PKGINSTALL)
1558 if (S_ISREG(st->st_mode)) {
1559 const char * path = fsm->path;
1561 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1562 rc = fsmStage(fsm, FSM_VERIFY);
1564 if (rc == 0 && fsm->osuffix) {
1565 const char * opath = fsm->opath;
1566 fsm->opath = fsm->path;
1567 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1568 rc = fsmStage(fsm, FSM_RENAME);
1570 rpmMessage(RPMMESS_WARNING,
1571 _("%s saved as %s\n"), fsm->opath, fsm->path);
1572 fsm->path = _free(fsm->path);
1576 /*@-dependenttrans@*/
1578 /*@=dependenttrans@*/
1579 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1580 rc = expandRegular(fsm);
1581 } else if (S_ISDIR(st->st_mode)) {
1582 mode_t st_mode = st->st_mode;
1583 rc = fsmStage(fsm, FSM_VERIFY);
1584 if (rc == CPIOERR_LSTAT_FAILED) {
1585 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1586 st->st_mode |= 00700;
1587 rc = fsmStage(fsm, FSM_MKDIR);
1588 st->st_mode = st_mode; /* XXX restore st->st_mode */
1590 } else if (S_ISLNK(st->st_mode)) {
1591 const char * opath = fsm->opath;
1593 if ((st->st_size + 1) > fsm->rdsize) {
1594 rc = CPIOERR_HDR_SIZE;
1598 fsm->wrlen = st->st_size;
1599 rc = fsmStage(fsm, FSM_DREAD);
1600 if (!rc && fsm->rdnb != fsm->wrlen)
1601 rc = CPIOERR_READ_FAILED;
1604 fsm->wrbuf[st->st_size] = '\0';
1605 /* XXX symlink(fsm->opath, fsm->path) */
1606 /*@-dependenttrans@*/
1607 fsm->opath = fsm->wrbuf;
1608 /*@=dependenttrans@*/
1609 rc = fsmStage(fsm, FSM_VERIFY);
1610 if (rc == CPIOERR_LSTAT_FAILED)
1611 rc = fsmStage(fsm, FSM_SYMLINK);
1612 fsm->opath = opath; /* XXX restore fsm->path */
1613 } else if (S_ISFIFO(st->st_mode)) {
1614 mode_t st_mode = st->st_mode;
1615 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1616 rc = fsmStage(fsm, FSM_VERIFY);
1617 if (rc == CPIOERR_LSTAT_FAILED) {
1618 st->st_mode = 0000; /* XXX abuse st->st_mode */
1619 rc = fsmStage(fsm, FSM_MKFIFO);
1620 st->st_mode = st_mode; /* XXX restore st->st_mode */
1622 } else if (S_ISCHR(st->st_mode) ||
1623 S_ISBLK(st->st_mode) ||
1624 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1626 rc = fsmStage(fsm, FSM_VERIFY);
1627 if (rc == CPIOERR_LSTAT_FAILED)
1628 rc = fsmStage(fsm, FSM_MKNOD);
1630 rc = CPIOERR_UNKNOWN_FILETYPE;
1632 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1633 fsm->li->createdPath = fsm->li->linkIndex;
1634 rc = fsmMakeLinks(fsm);
1640 rc = fsmMakeLinks(fsm);
1642 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1643 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1644 rpmTransactionSet ts = fsmGetTs(fsm);
1645 TFI_t fi = fsmGetFi(fsm);
1646 if (ts && ts->notify && fi) {
1647 /*@-type@*/ /* FIX: cast? */
1648 /*@-noeffectuncon @*/ /* FIX: check rc */
1649 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1650 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1651 rpmfiGetKey(fi), ts->notifyData);
1652 /*@=noeffectuncon @*/
1660 if (fsm->goal == FSM_PKGINSTALL) {
1661 (void) fsmStage(fsm,
1662 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1664 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1666 (void) fsmStage(fsm, FSM_RMDIRS);
1672 if (fsm->failedFile && *fsm->failedFile == NULL)
1673 *fsm->failedFile = xstrdup(fsm->path);
1676 if (!fsm->postpone && fsm->commit) {
1677 if (fsm->goal == FSM_PKGINSTALL)
1678 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1679 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1680 if (fsm->goal == FSM_PKGCOMMIT)
1681 rc = fsmStage(fsm, FSM_COMMIT);
1682 if (fsm->goal == FSM_PKGERASE)
1683 rc = fsmStage(fsm, FSM_COMMIT);
1685 fsm->path = _free(fsm->path);
1686 fsm->opath = _free(fsm->opath);
1687 memset(st, 0, sizeof(*st));
1688 memset(ost, 0, sizeof(*ost));
1691 /* Rename pre-existing modified or unmanaged file. */
1692 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1693 const char * opath = fsm->opath;
1694 const char * path = fsm->path;
1695 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1696 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1697 rc = fsmStage(fsm, FSM_RENAME);
1699 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1700 fsm->opath, fsm->path);
1702 fsm->path = _free(fsm->path);
1704 fsm->opath = _free(fsm->opath);
1708 /* Remove erased files. */
1709 if (fsm->goal == FSM_PKGERASE) {
1710 if (fsm->action == FA_ERASE) {
1711 TFI_t fi = fsmGetFi(fsm);
1712 if (S_ISDIR(st->st_mode)) {
1713 rc = fsmStage(fsm, FSM_RMDIR);
1716 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1718 /* XXX make sure that build side permits %missingok on directories. */
1719 if (fsm->fflags & RPMFILE_MISSINGOK)
1720 /*@innerbreak@*/ break;
1722 /* XXX common error message. */
1724 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1725 _("%s rmdir of %s failed: Directory not empty\n"),
1726 fiTypeString(fi), fsm->path);
1727 /*@innerbreak@*/ break;
1730 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1731 _("%s rmdir of %s failed: %s\n"),
1732 fiTypeString(fi), fsm->path, strerror(errno));
1733 /*@innerbreak@*/ break;
1736 rc = fsmStage(fsm, FSM_UNLINK);
1738 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1740 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1741 _("%s unlink of %s failed: %s\n"),
1742 fiTypeString(fi), fsm->path, strerror(errno));
1745 /* XXX Failure to remove is not (yet) cause for failure. */
1746 if (!strict_erasures) rc = 0;
1750 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1751 /* Rename temporary to final file name. */
1752 if (!S_ISDIR(st->st_mode) &&
1753 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1755 fsm->opath = fsm->path;
1756 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1757 rc = fsmStage(fsm, FSM_RENAME);
1758 if (!rc && fsm->nsuffix) {
1759 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1760 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1761 (opath ? opath : ""), fsm->path);
1762 opath = _free(opath);
1764 fsm->opath = _free(fsm->opath);
1766 if (S_ISLNK(st->st_mode)) {
1767 if (!rc && !getuid())
1768 rc = fsmStage(fsm, FSM_LCHOWN);
1770 if (!rc && !getuid())
1771 rc = fsmStage(fsm, FSM_CHOWN);
1773 rc = fsmStage(fsm, FSM_CHMOD);
1775 time_t mtime = st->st_mtime;
1776 TFI_t fi = fsmGetFi(fsm);
1778 st->st_mtime = fi->fmtimes[fsm->ix];
1779 rc = fsmStage(fsm, FSM_UTIME);
1780 st->st_mtime = mtime;
1785 /* Notify on success. */
1786 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1787 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1788 *fsm->failedFile = fsm->path;
1793 fsm->path = _free(fsm->path);
1795 /* Check for hard links missing from payload. */
1796 while ((fsm->li = fsm->links) != NULL) {
1797 fsm->links = fsm->li->next;
1798 fsm->li->next = NULL;
1799 if (fsm->goal == FSM_PKGINSTALL &&
1800 fsm->commit && fsm->li->linksLeft)
1802 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1803 if (fsm->li->filex[i] < 0)
1804 /*@innercontinue@*/ continue;
1805 rc = CPIOERR_MISSING_HARDLINK;
1806 if (fsm->failedFile && *fsm->failedFile == NULL) {
1807 fsm->ix = fsm->li->filex[i];
1808 if (!fsmStage(fsm, FSM_MAP)) {
1809 *fsm->failedFile = fsm->path;
1813 /*@loopbreak@*/ break;
1816 if (fsm->goal == FSM_PKGBUILD &&
1817 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1819 rc = CPIOERR_MISSING_HARDLINK;
1821 fsm->li = freeHardLink(fsm->li);
1823 fsm->ldn = _free(fsm->ldn);
1824 fsm->ldnalloc = fsm->ldnlen = 0;
1825 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1826 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1829 if (fsm->diskchecked && !fsm->exists) {
1830 rc = CPIOERR_LSTAT_FAILED;
1833 if (S_ISREG(st->st_mode)) {
1834 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1835 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1837 * XXX HP-UX (and other os'es) don't permit unlink on busy
1840 fsm->opath = fsm->path;
1842 rc = fsmStage(fsm, FSM_RENAME);
1844 (void) fsmStage(fsm, FSM_UNLINK);
1846 rc = CPIOERR_UNLINK_FAILED;
1847 fsm->path = fsm->opath;
1849 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1850 /*@notreached@*/ break;
1851 } else if (S_ISDIR(st->st_mode)) {
1852 if (S_ISDIR(ost->st_mode)) return 0;
1853 if (S_ISLNK(ost->st_mode)) {
1854 rc = fsmStage(fsm, FSM_STAT);
1855 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1860 if (S_ISDIR(ost->st_mode)) return 0;
1862 } else if (S_ISLNK(st->st_mode)) {
1863 if (S_ISLNK(ost->st_mode)) {
1864 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1865 rc = fsmStage(fsm, FSM_READLINK);
1870 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1872 } else if (S_ISFIFO(st->st_mode)) {
1873 if (S_ISFIFO(ost->st_mode)) return 0;
1874 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1875 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1876 (ost->st_rdev == st->st_rdev)) return 0;
1877 } else if (S_ISSOCK(st->st_mode)) {
1878 if (S_ISSOCK(ost->st_mode)) return 0;
1880 /* XXX shouldn't do this with commit/undo. */
1882 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1883 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1884 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1885 /*@notreached@*/ break;
1888 rc = Unlink(fsm->path);
1889 if (_fsm_debug && (stage & FSM_SYSCALL))
1890 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1891 fsm->path, (rc < 0 ? strerror(errno) : ""));
1892 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1895 rc = Rename(fsm->opath, fsm->path);
1896 if (_fsm_debug && (stage & FSM_SYSCALL))
1897 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1898 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1899 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1902 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1903 if (_fsm_debug && (stage & FSM_SYSCALL))
1904 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1905 fsm->path, (unsigned)(st->st_mode & 07777),
1906 (rc < 0 ? strerror(errno) : ""));
1907 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1910 rc = Rmdir(fsm->path);
1911 if (_fsm_debug && (stage & FSM_SYSCALL))
1912 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1913 fsm->path, (rc < 0 ? strerror(errno) : ""));
1914 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1917 rc = chown(fsm->path, st->st_uid, st->st_gid);
1918 if (_fsm_debug && (stage & FSM_SYSCALL))
1919 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1920 fsm->path, (int)st->st_uid, (int)st->st_gid,
1921 (rc < 0 ? strerror(errno) : ""));
1922 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1925 #if ! CHOWN_FOLLOWS_SYMLINK
1926 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1927 if (_fsm_debug && (stage & FSM_SYSCALL))
1928 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1929 fsm->path, (int)st->st_uid, (int)st->st_gid,
1930 (rc < 0 ? strerror(errno) : ""));
1931 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1935 rc = chmod(fsm->path, (st->st_mode & 07777));
1936 if (_fsm_debug && (stage & FSM_SYSCALL))
1937 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1938 fsm->path, (unsigned)(st->st_mode & 07777),
1939 (rc < 0 ? strerror(errno) : ""));
1940 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1943 { struct utimbuf stamp;
1944 stamp.actime = st->st_mtime;
1945 stamp.modtime = st->st_mtime;
1946 rc = utime(fsm->path, &stamp);
1947 if (_fsm_debug && (stage & FSM_SYSCALL))
1948 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1949 fsm->path, (unsigned)st->st_mtime,
1950 (rc < 0 ? strerror(errno) : ""));
1951 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1955 rc = symlink(fsm->opath, fsm->path);
1956 if (_fsm_debug && (stage & FSM_SYSCALL))
1957 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1958 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1959 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1962 rc = Link(fsm->opath, fsm->path);
1963 if (_fsm_debug && (stage & FSM_SYSCALL))
1964 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1965 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1966 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1969 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1970 if (_fsm_debug && (stage & FSM_SYSCALL))
1971 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1972 fsm->path, (unsigned)(st->st_mode & 07777),
1973 (rc < 0 ? strerror(errno) : ""));
1974 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1977 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
1978 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1979 /*@=unrecog =portability @*/
1980 if (_fsm_debug && (stage & FSM_SYSCALL))
1981 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1982 fsm->path, (unsigned)(st->st_mode & ~07777),
1983 (unsigned)st->st_rdev,
1984 (rc < 0 ? strerror(errno) : ""));
1985 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1988 rc = Lstat(fsm->path, ost);
1989 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1990 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1991 fsm->path, (rc < 0 ? strerror(errno) : ""));
1992 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1995 rc = Stat(fsm->path, ost);
1996 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1997 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1998 fsm->path, (rc < 0 ? strerror(errno) : ""));
1999 if (rc < 0) rc = CPIOERR_STAT_FAILED;
2002 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
2003 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
2004 if (_fsm_debug && (stage & FSM_SYSCALL))
2005 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
2006 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2007 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
2010 fsm->rdbuf[fsm->rdnb] = '\0';
2018 rc = fsmStage(fsm, FSM_HREAD);
2020 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2021 fsm->path = _free(fsm->path);
2022 rc = CPIOERR_HDR_TRAILER;
2025 rc = fsmStage(fsm, FSM_POS);
2028 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2029 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2030 rc = fsmStage(fsm, FSM_DREAD);
2032 /*@loopbreak@*/ break;
2036 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2039 (void) fsmStage(fsm, FSM_DREAD);
2043 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2045 memset(fsm->rdbuf, 0, left);
2046 /* XXX DWRITE uses rdnb for I/O length. */
2048 (void) fsmStage(fsm, FSM_DWRITE);
2052 rc = cpioTrailerWrite(fsm);
2055 rc = fsmStage(fsm, FSM_POS);
2057 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2060 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2063 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2064 if (_fsm_debug && (stage & FSM_SYSCALL))
2065 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2066 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2067 (int)fsm->wrlen, (int)fsm->rdnb);
2068 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2069 rc = CPIOERR_READ_FAILED;
2071 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2074 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2075 if (_fsm_debug && (stage & FSM_SYSCALL))
2076 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2077 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2078 (int)fsm->rdnb, (int)fsm->wrnb);
2079 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2080 rc = CPIOERR_WRITE_FAILED;
2082 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2086 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2087 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2088 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2090 rc = CPIOERR_OPEN_FAILED;
2093 if (_fsm_debug && (stage & FSM_SYSCALL))
2094 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2095 fsm->path, fsm->rfd, fsm->rdbuf);
2098 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2099 if (_fsm_debug && (stage & FSM_SYSCALL))
2100 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2101 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2102 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2103 rc = CPIOERR_READ_FAILED;
2107 if (_fsm_debug && (stage & FSM_SYSCALL))
2108 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2109 (void) Fclose(fsm->rfd);
2117 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2118 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2119 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2121 rc = CPIOERR_OPEN_FAILED;
2123 if (_fsm_debug && (stage & FSM_SYSCALL))
2124 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2125 fsm->path, fsm->wfd, fsm->wrbuf);
2128 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2129 if (_fsm_debug && (stage & FSM_SYSCALL))
2130 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2131 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2132 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2133 rc = CPIOERR_WRITE_FAILED;
2137 if (_fsm_debug && (stage & FSM_SYSCALL))
2138 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2139 (void) Fclose(fsm->wfd);
2152 if (!(stage & FSM_INTERNAL)) {
2153 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2159 /*@obserever@*/ const char *const fileActionString(fileAction a)
2162 case FA_UNKNOWN: return "unknown";
2163 case FA_CREATE: return "create";
2164 case FA_COPYOUT: return "copyout";
2165 case FA_COPYIN: return "copyin";
2166 case FA_BACKUP: return "backup";
2167 case FA_SAVE: return "save";
2168 case FA_SKIP: return "skip";
2169 case FA_ALTNAME: return "altname";
2170 case FA_ERASE: return "erase";
2171 case FA_SKIPNSTATE: return "skipnstate";
2172 case FA_SKIPNETSHARED: return "skipnetshared";
2173 case FA_SKIPMULTILIB: return "skipmultilib";
2174 default: return "???";
2179 /*@observer@*/ const char *const fileStageString(fileStage a) {
2181 case FSM_UNKNOWN: return "unknown";
2183 case FSM_PKGINSTALL:return "pkginstall";
2184 case FSM_PKGERASE: return "pkgerase";
2185 case FSM_PKGBUILD: return "pkgbuild";
2186 case FSM_PKGCOMMIT: return "pkgcommit";
2187 case FSM_PKGUNDO: return "pkgundo";
2189 case FSM_CREATE: return "create";
2190 case FSM_INIT: return "init";
2191 case FSM_MAP: return "map";
2192 case FSM_MKDIRS: return "mkdirs";
2193 case FSM_RMDIRS: return "rmdirs";
2194 case FSM_PRE: return "pre";
2195 case FSM_PROCESS: return "process";
2196 case FSM_POST: return "post";
2197 case FSM_MKLINKS: return "mklinks";
2198 case FSM_NOTIFY: return "notify";
2199 case FSM_UNDO: return "undo";
2200 case FSM_FINI: return "fini";
2201 case FSM_COMMIT: return "commit";
2202 case FSM_DESTROY: return "destroy";
2203 case FSM_VERIFY: return "verify";
2205 case FSM_UNLINK: return "Unlink";
2206 case FSM_RENAME: return "Rename";
2207 case FSM_MKDIR: return "Mkdir";
2208 case FSM_RMDIR: return "rmdir";
2209 case FSM_CHOWN: return "chown";
2210 case FSM_LCHOWN: return "lchown";
2211 case FSM_CHMOD: return "chmod";
2212 case FSM_UTIME: return "utime";
2213 case FSM_SYMLINK: return "symlink";
2214 case FSM_LINK: return "Link";
2215 case FSM_MKFIFO: return "mkfifo";
2216 case FSM_MKNOD: return "mknod";
2217 case FSM_LSTAT: return "Lstat";
2218 case FSM_STAT: return "Stat";
2219 case FSM_READLINK: return "Readlink";
2220 case FSM_CHROOT: return "chroot";
2222 case FSM_NEXT: return "next";
2223 case FSM_EAT: return "eat";
2224 case FSM_POS: return "pos";
2225 case FSM_PAD: return "pad";
2226 case FSM_TRAILER: return "trailer";
2227 case FSM_HREAD: return "hread";
2228 case FSM_HWRITE: return "hwrite";
2229 case FSM_DREAD: return "Fread";
2230 case FSM_DWRITE: return "Fwrite";
2232 case FSM_ROPEN: return "Fopen";
2233 case FSM_READ: return "Fread";
2234 case FSM_RCLOSE: return "Fclose";
2235 case FSM_WOPEN: return "Fopen";
2236 case FSM_WRITE: return "Fwrite";
2237 case FSM_WCLOSE: return "Fclose";
2239 default: return "???";