3 * File state machine to handle a payload from a package.
17 /*@access rpmFNSet @*/
18 /*@access transactionElement @*/
19 /*@access rpmTransactionSet @*/
21 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
26 /* XXX Failure to remove is not (yet) cause for failure. */
27 /*@-exportlocal -exportheadervar@*/
29 int strict_erasures = 0;
30 /*@=exportlocal =exportheadervar@*/
32 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
33 const FSMI_t iter = fsm->iter;
34 /*@-compdef -refcounttrans -retexpose -usereleased @*/
35 return (iter ? iter->ts : NULL);
36 /*@=compdef =refcounttrans =retexpose =usereleased @*/
39 TFI_t fsmGetFi(const FSM_t fsm)
41 const FSMI_t iter = fsm->iter;
42 /*@-compdef -refcounttrans -retexpose -usereleased @*/
43 return (iter ? iter->fi : NULL);
44 /*@=compdef =refcounttrans =retexpose =usereleased @*/
47 #define SUFFIX_RPMORIG ".rpmorig"
48 #define SUFFIX_RPMSAVE ".rpmsave"
49 #define SUFFIX_RPMNEW ".rpmnew"
52 * Build path to file from file info, ornamented with subdir and suffix.
53 * @param fsm file state machine data
54 * @param st file stat info
55 * @param subdir subdir to use (NULL disables)
56 * @param suffix suffix to use (NULL disables)
57 * @retval path to file
59 static /*@only@*//*@null@*/
60 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
61 /*@null@*/ const struct stat * st,
62 /*@null@*/ const char * subdir,
63 /*@null@*/ const char * suffix)
64 /*@uses fsm->dirName, fsm->baseName */
67 const char * s = NULL;
72 nb = strlen(fsm->dirName) +
73 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
74 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
75 strlen(fsm->baseName) + 1;
77 t = stpcpy(t, fsm->dirName);
78 if (st && !S_ISDIR(st->st_mode))
79 if (subdir) t = stpcpy(t, subdir);
80 t = stpcpy(t, fsm->baseName);
81 if (st && !S_ISDIR(st->st_mode))
82 if (suffix) t = stpcpy(t, suffix);
88 * Destroy file info iterator.
89 * @param p file info iterator
92 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/ void * p)
97 iter->ts = rpmtsUnlink(iter->ts, "mapIterator");
98 iter->fi = rpmfiUnlink(iter->fi, "mapIterator");
104 * Create file info iterator.
105 * @param a transaction set
106 * @param b transaction element file info
107 * @return file info iterator
110 mapInitIterator(rpmTransactionSet ts, TFI_t fi)
111 /*@modifies ts, fi @*/
115 iter = xcalloc(1, sizeof(*iter));
116 iter->ts = rpmtsLink(ts, "mapIterator");
117 iter->fi = rpmfiLink(fi, "mapIterator");
118 iter->reverse = (fi->te->type == TR_REMOVED && fi->action != FA_COPYOUT);
119 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
120 iter->isave = iter->i;
125 * Return next index into file info.
126 * @param a file info iterator
127 * @return next index, -1 on termination
129 static int mapNextIterator(/*@null@*/ void * a)
136 const TFI_t fi = iter->fi;
138 if (iter->i >= 0) i = iter->i--;
140 if (iter->i < fi->fc) i = iter->i++;
149 static int cpioStrCmp(const void * a, const void * b)
152 const char * afn = *(const char **)a;
153 const char * bfn = *(const char **)b;
155 /* Match rpm-4.0 payloads with ./ prefixes. */
156 if (afn[0] == '.' && afn[1] == '/') afn += 2;
157 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
159 /* If either path is absolute, make it relative. */
160 if (afn[0] == '/') afn += 1;
161 if (bfn[0] == '/') bfn += 1;
163 return strcmp(afn, bfn);
167 * Locate archive path in file info.
168 * @param iter file info iterator
169 * @param fsmPath archive path
170 * @return index into file info, -1 if archive path was not found
172 static int mapFind(/*@null@*/ FSMI_t iter, const char * fsmPath)
178 const TFI_t fi = iter->fi;
179 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
180 const char ** p = NULL;
182 if (fi->apath != NULL)
183 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
186 iter->i = p - fi->apath;
187 ix = mapNextIterator(iter);
195 * Directory name iterator.
197 typedef struct dnli_s {
199 /*@only@*/ /*@null@*/ char * active;
206 * Destroy directory name iterator.
207 * @param a directory name iterator
208 * @retval NULL always
210 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
214 DNLI_t dnli = (void *)a;
215 if (dnli->active) free(dnli->active);
222 static inline int dnlCount(const DNLI_t dnli)
225 return (dnli ? dnli->fi->dc : 0);
230 static inline int dnlIndex(const DNLI_t dnli)
233 return (dnli ? dnli->isave : -1);
237 * Create directory name iterator.
238 * @param fsm file state machine data
239 * @param reverse traverse directory names in reverse order?
240 * @return directory name iterator
243 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
245 /*@uses fsm->iter @*/
248 TFI_t fi = fsmGetFi(fsm);
254 dnli = xcalloc(1, sizeof(*dnli));
256 dnli->reverse = reverse;
258 dnli->i = (reverse ? fi->dc : 0);
262 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
264 /* Identify parent directories not skipped. */
265 for (i = 0; i < fi->fc; i++)
266 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
268 /* Exclude parent directories that are explicitly included. */
269 for (i = 0; i < fi->fc; i++) {
270 int dil, dnlen, bnlen;
272 if (!S_ISDIR(fi->fmodes[i]))
276 dnlen = strlen(fi->dnl[dil]);
277 bnlen = strlen(fi->bnl[i]);
279 for (j = 0; j < fi->dc; j++) {
283 if (!dnli->active[j] || j == dil)
284 /*@innercontinue@*/ continue;
287 if (jlen != (dnlen+bnlen+1))
288 /*@innercontinue@*/ continue;
289 if (strncmp(dnl, fi->dnl[dil], dnlen))
290 /*@innercontinue@*/ continue;
291 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen))
292 /*@innercontinue@*/ continue;
293 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
294 /*@innercontinue@*/ continue;
295 /* This directory is included in the package. */
297 /*@innerbreak@*/ break;
301 /* Print only once per package. */
304 for (i = 0; i < fi->dc; i++) {
305 if (!dnli->active[i]) continue;
308 rpmMessage(RPMMESS_DEBUG,
309 _("========== Directories not explictly included in package:\n"));
311 rpmMessage(RPMMESS_DEBUG, _("%10d %s\n"), i, fi->dnl[i]);
314 rpmMessage(RPMMESS_DEBUG, "==========\n");
322 * Return next directory name (from file info).
323 * @param dnli directory name iterator
324 * @return next directory name
326 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
329 const char * dn = NULL;
337 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
338 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
340 if (i >= 0 && i < fi->dc)
350 * Save hard link in chain.
351 * @param fsm file state machine data
352 * @return Is chain only partially filled?
354 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
355 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
356 /*@defines fsm->li @*/
357 /*@releases fsm->path @*/
358 /*@globals fileSystem@*/
359 /*@modifies fsm, fileSystem @*/
361 struct stat * st = &fsm->sb;
366 /* Find hard link set. */
368 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
369 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
374 /* New hard link encountered, add new link to set. */
376 if (fsm->li == NULL) {
377 fsm->li = xcalloc(1, sizeof(*fsm->li));
378 fsm->li->next = NULL;
379 fsm->li->sb = *st; /* structure assignment */
380 fsm->li->nlink = st->st_nlink;
381 fsm->li->linkIndex = fsm->ix;
382 fsm->li->createdPath = -1;
384 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
385 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
386 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
388 if (fsm->goal == FSM_PKGBUILD)
389 fsm->li->linksLeft = st->st_nlink;
390 if (fsm->goal == FSM_PKGINSTALL)
391 fsm->li->linksLeft = 0;
394 fsm->li->next = fsm->links;
396 fsm->links = fsm->li;
400 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
401 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
402 /*@-observertrans -dependenttrans@*/
403 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
404 /*@=observertrans =dependenttrans@*/
405 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
407 if (fsm->goal == FSM_PKGBUILD)
408 return (fsm->li->linksLeft > 0);
410 if (fsm->goal != FSM_PKGINSTALL)
413 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
416 /* Here come the bits, time to choose a non-skipped file name. */
417 { TFI_t fi = fsmGetFi(fsm);
419 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
420 ix = fsm->li->filex[j];
421 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
427 /* Are all links skipped or not encountered yet? */
429 return 1; /* XXX W2DO? */
431 /* Save the non-skipped file name and map index. */
432 fsm->li->linkIndex = j;
433 fsm->path = _free(fsm->path);
435 rc = fsmStage(fsm, FSM_MAP);
440 * Destroy set of hard links.
441 * @param li set of hard links
443 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
447 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
448 li->filex = _free(li->filex);
455 FSM_t fsm = xcalloc(1, sizeof(*fsm));
459 FSM_t freeFSM(FSM_t fsm)
462 fsm->path = _free(fsm->path);
464 while ((fsm->li = fsm->links) != NULL) {
465 fsm->links = fsm->li->next;
466 fsm->li->next = NULL;
467 fsm->li = freeHardLink(fsm->li);
470 fsm->dnlx = _free(fsm->dnlx);
471 fsm->ldn = _free(fsm->ldn);
472 fsm->iter = mapFreeIterator(fsm->iter);
477 int fsmSetup(FSM_t fsm, fileStage goal,
478 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
479 unsigned int * archiveSize, const char ** failedFile)
486 /*@-type@*/ /* FIX: cast? */
487 fsm->cfd = fdLink(cfd, "persist (fsm)");
489 pos = fdGetCpioPos(fsm->cfd);
490 fdSetCpioPos(fsm->cfd, 0);
492 fsm->iter = mapInitIterator(ts, fi);
494 if (fsm->goal == FSM_PKGINSTALL) {
495 if (ts && ts->notify) {
496 /*@-type@*/ /* FIX: cast? */
497 /*@-noeffectuncon @*/ /* FIX: check rc */
498 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
499 rpmfiGetKey(fi), ts->notifyData);
500 /*@=noeffectuncon @*/
506 fsm->archiveSize = archiveSize;
507 if (fsm->archiveSize)
508 *fsm->archiveSize = 0;
509 fsm->failedFile = failedFile;
511 *fsm->failedFile = NULL;
514 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
515 if (fsm->goal == FSM_PKGINSTALL) {
516 if (ts && ts->id > 0)
517 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
521 rc = fsmStage(fsm, FSM_CREATE);
522 if (rc && !ec) ec = rc;
524 rc = fsmStage(fsm, fsm->goal);
525 if (rc && !ec) ec = rc;
527 if (fsm->archiveSize && ec == 0)
528 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
533 int fsmTeardown(FSM_t fsm)
538 rc = fsmStage(fsm, FSM_DESTROY);
540 fsm->iter = mapFreeIterator(fsm->iter);
542 /*@-type@*/ /* FIX: cast? */
543 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
547 fsm->failedFile = NULL;
551 int fsmMapPath(FSM_t fsm)
553 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
560 fsm->action = FA_UNKNOWN;
564 if (fi && i >= 0 && i < fi->fc) {
566 fsm->astriplen = fi->astriplen;
567 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
568 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
569 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
571 /* src rpms have simple base name in payload. */
572 fsm->dirName = fi->dnl[fi->dil[i]];
573 fsm->baseName = fi->bnl[i];
575 switch (fsm->action) {
578 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
587 assert(fi->te->type == TR_ADDED);
591 if (fi->fstates && fi->te->type == TR_ADDED)
592 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
595 case FA_SKIPNETSHARED:
596 if (fi->fstates && fi->te->type == TR_ADDED)
597 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
601 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
602 switch (fi->te->type) {
604 fsm->osuffix = SUFFIX_RPMORIG;
605 /*@innerbreak@*/ break;
607 fsm->osuffix = SUFFIX_RPMSAVE;
608 /*@innerbreak@*/ break;
613 assert(fi->te->type == TR_ADDED);
614 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
615 fsm->nsuffix = SUFFIX_RPMNEW;
619 assert(fi->te->type == TR_ADDED);
620 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
621 fsm->osuffix = SUFFIX_RPMSAVE;
624 assert(fi->te->type == TR_REMOVED);
626 * XXX TODO: %ghost probably shouldn't be removed, but that changes
627 * legacy rpm behavior.
634 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
635 const struct stat * st = &fsm->sb;
636 fsm->path = _free(fsm->path);
637 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
638 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
644 int fsmMapAttrs(FSM_t fsm)
646 struct stat * st = &fsm->sb;
647 TFI_t fi = fsmGetFi(fsm);
650 if (fi && i >= 0 && i < fi->fc) {
652 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
654 (fi->fmodes ? fi->fmodes[i] : perms);
656 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
658 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
660 (fi->frdevs ? fi->frdevs[i] : 0);
662 (fi->fmtimes ? fi->fmtimes[i] : 0);
664 if (fsm->mapFlags & CPIO_MAP_MODE)
665 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
666 if (fsm->mapFlags & CPIO_MAP_TYPE) {
667 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
668 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
669 && st->st_nlink == 0)
671 st->st_rdev = finalRdev;
672 st->st_mtime = finalMtime;
674 if (fsm->mapFlags & CPIO_MAP_UID)
675 st->st_uid = finalUid;
676 if (fsm->mapFlags & CPIO_MAP_GID)
677 st->st_gid = finalGid;
679 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
686 * Create file from payload stream.
687 * @param fsm file state machine data
688 * @return 0 on success
690 static int expandRegular(/*@special@*/ FSM_t fsm)
692 /*@globals fileSystem@*/
693 /*@modifies fsm, fileSystem @*/
695 const char * fmd5sum;
696 const struct stat * st = &fsm->sb;
697 int left = st->st_size;
700 rc = fsmStage(fsm, FSM_WOPEN);
704 /* XXX md5sum's will break on repackaging that includes modified files. */
705 fmd5sum = fsm->fmd5sum;
707 if (st->st_size > 0 && fmd5sum)
708 fdInitDigest(fsm->wfd, PGPHASHALGO_MD5, 0);
712 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
713 rc = fsmStage(fsm, FSM_DREAD);
717 rc = fsmStage(fsm, FSM_WRITE);
723 /* don't call this with fileSize == fileComplete */
725 (void) fsmStage(fsm, FSM_NOTIFY);
728 if (st->st_size > 0 && fmd5sum) {
729 const char * md5sum = NULL;
731 (void) Fflush(fsm->wfd);
732 fdFiniDigest(fsm->wfd, PGPHASHALGO_MD5, (void **)&md5sum, NULL, 1);
734 if (md5sum == NULL) {
735 rc = CPIOERR_MD5SUM_MISMATCH;
737 if (strcmp(md5sum, fmd5sum))
738 rc = CPIOERR_MD5SUM_MISMATCH;
739 md5sum = _free(md5sum);
744 (void) fsmStage(fsm, FSM_WCLOSE);
749 * Write next item to payload stream.
750 * @param fsm file state machine data
751 * @param writeData should data be written?
752 * @return 0 on success
754 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
755 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
756 /*@globals fileSystem@*/
757 /*@modifies fsm, fileSystem @*/
759 const char * path = fsm->path;
760 const char * opath = fsm->opath;
761 struct stat * st = &fsm->sb;
762 struct stat * ost = &fsm->osb;
763 size_t pos = fdGetCpioPos(fsm->cfd);
764 char * symbuf = NULL;
768 st->st_size = (writeData ? ost->st_size : 0);
771 if (S_ISDIR(st->st_mode)) {
773 } else if (S_ISLNK(st->st_mode)) {
775 * While linux puts the size of a symlink in the st_size field,
776 * I don't think that's a specified standard.
778 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
779 rc = fsmStage(fsm, FSM_READLINK);
781 st->st_size = fsm->rdnb;
782 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
786 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
787 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
788 char * t = alloca(nb);
791 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
793 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
794 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
795 TFI_t fi = fsmGetFi(fsm);
797 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
800 rc = fsmStage(fsm, FSM_HWRITE);
804 if (writeData && S_ISREG(st->st_mode)) {
807 void * mapped = (void *)-1;
811 rc = fsmStage(fsm, FSM_ROPEN);
814 /* XXX unbuffered mmap generates *lots* of fdio debugging */
817 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
818 if (mapped != (void *)-1) {
820 fsm->rdbuf = (char *) mapped;
821 fsm->rdlen = nmapped = st->st_size;
829 if (mapped != (void *)-1) {
834 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
835 rc = fsmStage(fsm, FSM_READ);
839 /* XXX DWRITE uses rdnb for I/O length. */
840 rc = fsmStage(fsm, FSM_DWRITE);
847 if (mapped != (void *)-1) {
848 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
853 } else if (writeData && S_ISLNK(st->st_mode)) {
854 /* XXX DWRITE uses rdnb for I/O length. */
855 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
856 fsm->rdnb = strlen(symbuf);
857 rc = fsmStage(fsm, FSM_DWRITE);
861 rc = fsmStage(fsm, FSM_PAD);
864 { const rpmTransactionSet ts = fsmGetTs(fsm);
865 TFI_t fi = fsmGetFi(fsm);
866 if (ts && ts->notify && fi) {
867 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
868 /*@-type@*/ /* FIX: cast? */
869 /*@-noeffectuncon @*/ /* FIX: check rc */
870 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
871 rpmfiGetKey(fi), ts->notifyData);
872 /*@=noeffectuncon @*/
881 (void) fsmStage(fsm, FSM_RCLOSE);
882 /*@-dependenttrans@*/
885 /*@=dependenttrans@*/
890 * Write set of linked files to payload stream.
891 * @param fsm file state machine data
892 * @return 0 on success
894 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
895 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
896 /*@globals fileSystem@*/
897 /*@modifies fsm, fileSystem @*/
899 const char * path = fsm->path;
900 const char * nsuffix = fsm->nsuffix;
901 int iterIndex = fsm->ix;
910 for (i = fsm->li->nlink - 1; i >= 0; i--) {
912 if (fsm->li->filex[i] < 0) continue;
914 fsm->ix = fsm->li->filex[i];
915 rc = fsmStage(fsm, FSM_MAP);
917 /* Write data after last link. */
918 rc = writeFile(fsm, (i == 0));
919 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
921 *fsm->failedFile = xstrdup(fsm->path);
924 fsm->path = _free(fsm->path);
925 fsm->li->filex[i] = -1;
929 fsm->nsuffix = nsuffix;
935 * Create pending hard links to existing file.
936 * @param fsm file state machine data
937 * @return 0 on success
939 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
940 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
941 /*@globals fileSystem@*/
942 /*@modifies fsm, fileSystem @*/
944 const char * path = fsm->path;
945 const char * opath = fsm->opath;
946 const char * nsuffix = fsm->nsuffix;
947 int iterIndex = fsm->ix;
957 fsm->ix = fsm->li->filex[fsm->li->createdPath];
958 rc = fsmStage(fsm, FSM_MAP);
959 fsm->opath = fsm->path;
962 for (i = 0; i < fsm->li->nlink; i++) {
963 if (fsm->li->filex[i] < 0) continue;
964 if (i == fsm->li->createdPath) continue;
966 fsm->ix = fsm->li->filex[i];
967 fsm->path = _free(fsm->path);
968 rc = fsmStage(fsm, FSM_MAP);
969 rc = fsmStage(fsm, FSM_VERIFY);
971 if (rc != CPIOERR_LSTAT_FAILED) break;
973 /* XXX link(fsm->opath, fsm->path) */
974 rc = fsmStage(fsm, FSM_LINK);
975 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
977 *fsm->failedFile = xstrdup(fsm->path);
980 fsm->li->linksLeft--;
983 fsm->path = _free(fsm->path);
984 fsm->opath = _free(fsm->opath);
987 fsm->nsuffix = nsuffix;
994 * Commit hard linked file set atomically.
995 * @param fsm file state machine data
996 * @return 0 on success
998 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
999 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
1000 fsm->li, fsm->links @*/
1001 /*@globals fileSystem@*/
1002 /*@modifies fsm, fileSystem @*/
1004 const char * path = fsm->path;
1005 const char * nsuffix = fsm->nsuffix;
1006 int iterIndex = fsm->ix;
1007 struct stat * st = &fsm->sb;
1012 fsm->nsuffix = NULL;
1016 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
1017 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
1022 for (i = 0; i < fsm->li->nlink; i++) {
1023 if (fsm->li->filex[i] < 0) continue;
1024 fsm->ix = fsm->li->filex[i];
1025 rc = fsmStage(fsm, FSM_MAP);
1026 rc = fsmStage(fsm, FSM_COMMIT);
1027 fsm->path = _free(fsm->path);
1028 fsm->li->filex[i] = -1;
1031 fsm->ix = iterIndex;
1032 fsm->nsuffix = nsuffix;
1038 * Remove (if created) directories not explicitly included in package.
1039 * @param fsm file state machine data
1040 * @return 0 on success
1042 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1043 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1044 /*@globals fileSystem@*/
1045 /*@modifies fsm, fileSystem @*/
1047 const char * path = fsm->path;
1048 void * dnli = dnlInitIterator(fsm, 1);
1049 char * dn = fsm->rdbuf;
1050 int dc = dnlCount(dnli);
1055 /*@-observertrans -dependenttrans@*/
1056 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1057 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1058 int dnlen = strlen(fsm->path);
1061 dc = dnlIndex(dnli);
1062 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1065 /* Copy to avoid const on fsm->path. */
1066 te = stpcpy(dn, fsm->path) - 1;
1069 /* Remove generated directories. */
1070 /*@-usereleased@*/ /* LCL: te used after release? */
1074 rc = fsmStage(fsm, FSM_RMDIR);
1078 /*@innerbreak@*/ break;
1080 } while ((te - fsm->path) > fsm->dnlx[dc]);
1083 dnli = dnlFreeIterator(dnli);
1084 /*@=observertrans =dependenttrans@*/
1091 * Create (if necessary) directories not explicitly included in package.
1092 * @param fsm file state machine data
1093 * @return 0 on success
1095 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1096 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1097 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1098 /*@defines fsm->dnlx, fsm->ldn @*/
1099 /*@globals fileSystem@*/
1100 /*@modifies fsm, fileSystem @*/
1102 struct stat * st = &fsm->sb;
1103 struct stat * ost = &fsm->osb;
1104 const char * path = fsm->path;
1105 mode_t st_mode = st->st_mode;
1106 void * dnli = dnlInitIterator(fsm, 0);
1107 char * dn = fsm->rdbuf;
1108 int dc = dnlCount(dnli);
1115 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1116 /*@-observertrans -dependenttrans@*/
1117 if (fsm->dnlx != NULL)
1118 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1119 int dnlen = strlen(fsm->path);
1122 dc = dnlIndex(dnli);
1123 if (dc < 0) continue;
1124 fsm->dnlx[dc] = dnlen;
1128 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1129 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1131 /*@=compdef =nullpass@*/
1133 /* Copy to avoid const on fsm->path. */
1134 (void) stpcpy(dn, fsm->path);
1137 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1138 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1140 /*@innercontinue@*/ continue;
1144 /* Already validated? */
1145 /*@-usedef -compdef -nullpass -nullderef@*/
1146 if (i < fsm->ldnlen &&
1147 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1148 !strncmp(fsm->path, fsm->ldn, i))
1151 /* Move pre-existing path marker forward. */
1152 fsm->dnlx[dc] = (te - dn);
1153 /*@innercontinue@*/ continue;
1155 /*@=usedef =compdef =nullpass =nullderef@*/
1157 /* Validate next component of path. */
1158 rc = fsmStage(fsm, FSM_LSTAT);
1161 /* Directory already exists? */
1162 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1163 /* Move pre-existing path marker forward. */
1164 fsm->dnlx[dc] = (te - dn);
1165 } else if (rc == CPIOERR_LSTAT_FAILED) {
1166 TFI_t fi = fsmGetFi(fsm);
1168 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1169 rc = fsmStage(fsm, FSM_MKDIR);
1171 rpmMessage(RPMMESS_DEBUG,
1172 _("%s directory created with perms %04o.\n"),
1173 fsm->path, (unsigned)(st->st_mode & 07777));
1177 /*@innerbreak@*/ break;
1181 /* Save last validated path. */
1182 if (fsm->ldnalloc < (dnlen + 1)) {
1183 fsm->ldnalloc = dnlen + 100;
1184 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1186 if (fsm->ldn != NULL) { /* XXX can't happen */
1187 strcpy(fsm->ldn, fsm->path);
1188 fsm->ldnlen = dnlen;
1191 dnli = dnlFreeIterator(dnli);
1192 /*@=observertrans =dependenttrans@*/
1195 st->st_mode = st_mode; /* XXX restore st->st_mode */
1201 * Check for file on disk.
1202 * @param fsm file state machine data
1203 * @return 0 on success
1205 static int fsmStat(FSM_t fsm)
1207 int saveerrno = errno;
1210 if (fsm->path != NULL) {
1211 int saveernno = errno;
1212 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1213 ? FSM_LSTAT : FSM_STAT));
1214 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1218 } else if (rc == 0) {
1222 /* Skip %ghost files on build. */
1230 int fsmStage(FSM_t fsm, fileStage stage)
1233 fileStage prevStage = fsm->stage;
1234 const char * const prev = fileStageString(prevStage);
1236 static int modulo = 4;
1237 const char * const cur = fileStageString(stage);
1238 struct stat * st = &fsm->sb;
1239 struct stat * ost = &fsm->osb;
1240 int saveerrno = errno;
1245 #define _fafilter(_a) \
1246 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1247 ? fileActionString(_a) : "")
1249 if (stage & FSM_DEAD) {
1251 } else if (stage & FSM_INTERNAL) {
1252 if (_fsm_debug && !(stage & FSM_SYSCALL))
1253 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1255 (unsigned)st->st_mode, (int)st->st_nlink,
1256 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1257 (fsm->path ? fsm->path : ""),
1258 _fafilter(fsm->action));
1261 if (_fsm_debug || !(stage & FSM_VERBOSE))
1262 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1264 (unsigned)st->st_mode, (int)st->st_nlink,
1265 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1266 (fsm->path ? fsm->path + fsm->astriplen : ""),
1267 _fafilter(fsm->action));
1275 case FSM_PKGINSTALL:
1277 /* Clean fsm, free'ing memory. Read next archive header. */
1278 rc = fsmStage(fsm, FSM_INIT);
1280 /* Exit on end-of-payload. */
1281 if (rc == CPIOERR_HDR_TRAILER) {
1283 /*@loopbreak@*/ break;
1286 /* Exit on error. */
1289 (void) fsmStage(fsm, FSM_UNDO);
1290 /*@loopbreak@*/ break;
1293 /* Extract file from archive. */
1294 rc = fsmStage(fsm, FSM_PROCESS);
1296 (void) fsmStage(fsm, FSM_UNDO);
1297 /*@loopbreak@*/ break;
1300 /* Notify on success. */
1301 (void) fsmStage(fsm, FSM_NOTIFY);
1303 rc = fsmStage(fsm, FSM_FINI);
1305 /*@loopbreak@*/ break;
1312 /* Clean fsm, free'ing memory. */
1313 rc = fsmStage(fsm, FSM_INIT);
1315 /* Exit on end-of-payload. */
1316 if (rc == CPIOERR_HDR_TRAILER) {
1318 /*@loopbreak@*/ break;
1321 /* Rename/erase next item. */
1322 if (fsmStage(fsm, FSM_FINI))
1323 /*@loopbreak@*/ break;
1329 rc = fsmStage(fsm, FSM_INIT);
1331 /* Exit on end-of-payload. */
1332 if (rc == CPIOERR_HDR_TRAILER) {
1334 /*@loopbreak@*/ break;
1337 /* Exit on error. */
1340 (void) fsmStage(fsm, FSM_UNDO);
1341 /*@loopbreak@*/ break;
1344 /* Copy file into archive. */
1345 rc = fsmStage(fsm, FSM_PROCESS);
1347 (void) fsmStage(fsm, FSM_UNDO);
1348 /*@loopbreak@*/ break;
1351 if (fsmStage(fsm, FSM_FINI))
1352 /*@loopbreak@*/ break;
1355 /* Flush partial sets of hard linked files. */
1356 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1358 while ((fsm->li = fsm->links) != NULL) {
1359 fsm->links = fsm->li->next;
1360 fsm->li->next = NULL;
1362 /* Re-calculate link count for archive header. */
1363 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1364 if (fsm->li->filex[i] < 0)
1365 /*@innercontinue@*/ continue;
1369 /* XXX force the contents out as well. */
1371 fsm->li->filex[0] = fsm->li->filex[j];
1372 fsm->li->filex[j] = -1;
1374 fsm->li->sb.st_nlink = nlink;
1376 fsm->sb = fsm->li->sb; /* structure assignment */
1377 fsm->osb = fsm->sb; /* structure assignment */
1379 if (!rc) rc = writeLinkedFile(fsm);
1381 fsm->li = freeHardLink(fsm->li);
1386 rc = fsmStage(fsm, FSM_TRAILER);
1390 { rpmTransactionSet ts = fsmGetTs(fsm);
1391 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1392 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1393 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1396 fsm->path = _free(fsm->path);
1397 fsm->opath = _free(fsm->opath);
1398 fsm->dnlx = _free(fsm->dnlx);
1400 fsm->ldn = _free(fsm->ldn);
1401 fsm->ldnalloc = fsm->ldnlen = 0;
1403 fsm->rdsize = fsm->wrsize = 0;
1404 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1405 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1406 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1407 fsm->rdsize = 8 * BUFSIZ;
1408 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1409 fsm->wrsize = 8 * BUFSIZ;
1410 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1413 fsm->mkdirsdone = 0;
1418 errno = 0; /* XXX get rid of EBADF */
1421 /* Detect and create directories not explicitly in package. */
1422 if (fsm->goal == FSM_PKGINSTALL) {
1423 rc = fsmStage(fsm, FSM_MKDIRS);
1424 if (!rc) fsm->mkdirsdone = 1;
1429 fsm->path = _free(fsm->path);
1431 fsm->diskchecked = fsm->exists = 0;
1433 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1434 fsm->action = FA_UNKNOWN;
1435 fsm->osuffix = NULL;
1436 fsm->nsuffix = NULL;
1438 if (fsm->goal == FSM_PKGINSTALL) {
1439 /* Read next header from payload, checking for end-of-payload. */
1440 rc = fsmStage(fsm, FSM_NEXT);
1444 /* Identify mapping index. */
1445 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1446 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1448 /* Detect end-of-loop and/or mapping error. */
1450 if (fsm->goal == FSM_PKGINSTALL) {
1452 rpmMessage(RPMMESS_WARNING,
1453 _("archive file %s was not found in header file list\n"),
1456 if (fsm->failedFile && *fsm->failedFile == NULL)
1457 *fsm->failedFile = xstrdup(fsm->path);
1458 rc = CPIOERR_UNMAPPED_FILE;
1460 rc = CPIOERR_HDR_TRAILER;
1465 /* On non-install, mode must be known so that dirs don't get suffix. */
1466 if (fsm->goal != FSM_PKGINSTALL) {
1467 TFI_t fi = fsmGetFi(fsm);
1468 st->st_mode = fi->fmodes[fsm->ix];
1471 /* Generate file path. */
1472 rc = fsmStage(fsm, FSM_MAP);
1475 /* Perform lstat/stat for disk file. */
1479 if (fsm->path != NULL) {
1480 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1481 ? FSM_LSTAT : FSM_STAT));
1482 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1488 } else if (rc == 0) {
1492 /* Skip %ghost files on build. */
1496 fsm->diskchecked = 1;
1499 /* On non-install, the disk file stat is what's remapped. */
1500 if (fsm->goal != FSM_PKGINSTALL)
1501 *st = *ost; /* structure assignment */
1503 /* Remap file perms, owner, and group. */
1504 rc = fsmMapAttrs(fsm);
1507 fsm->postpone = XFA_SKIPPING(fsm->action);
1508 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1509 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1510 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1511 fsm->postpone = saveHardLink(fsm);
1518 rc = fsmMapPath(fsm);
1521 rc = fsmMkdirs(fsm);
1525 rc = fsmRmdirs(fsm);
1528 if (fsm->postpone) {
1529 if (fsm->goal == FSM_PKGINSTALL)
1530 rc = fsmStage(fsm, FSM_EAT);
1534 if (fsm->goal == FSM_PKGBUILD) {
1535 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1536 struct hardLink * li, * prev;
1538 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1539 rc = writeLinkedFile(fsm);
1540 if (rc) break; /* W2DO? */
1542 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1544 /*@loopbreak@*/ break;
1547 fsm->links = fsm->li->next;
1549 prev->next = fsm->li->next;
1550 fsm->li->next = NULL;
1551 fsm->li = freeHardLink(fsm->li);
1553 rc = writeFile(fsm, 1);
1558 if (fsm->goal != FSM_PKGINSTALL)
1561 if (S_ISREG(st->st_mode)) {
1562 const char * path = fsm->path;
1564 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1565 rc = fsmStage(fsm, FSM_VERIFY);
1567 if (rc == 0 && fsm->osuffix) {
1568 const char * opath = fsm->opath;
1569 fsm->opath = fsm->path;
1570 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1571 rc = fsmStage(fsm, FSM_RENAME);
1573 rpmMessage(RPMMESS_WARNING,
1574 _("%s saved as %s\n"), fsm->opath, fsm->path);
1575 fsm->path = _free(fsm->path);
1579 /*@-dependenttrans@*/
1581 /*@=dependenttrans@*/
1582 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1583 rc = expandRegular(fsm);
1584 } else if (S_ISDIR(st->st_mode)) {
1585 mode_t st_mode = st->st_mode;
1586 rc = fsmStage(fsm, FSM_VERIFY);
1587 if (rc == CPIOERR_LSTAT_FAILED) {
1588 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1589 st->st_mode |= 00700;
1590 rc = fsmStage(fsm, FSM_MKDIR);
1591 st->st_mode = st_mode; /* XXX restore st->st_mode */
1593 } else if (S_ISLNK(st->st_mode)) {
1594 const char * opath = fsm->opath;
1596 if ((st->st_size + 1) > fsm->rdsize) {
1597 rc = CPIOERR_HDR_SIZE;
1601 fsm->wrlen = st->st_size;
1602 rc = fsmStage(fsm, FSM_DREAD);
1603 if (!rc && fsm->rdnb != fsm->wrlen)
1604 rc = CPIOERR_READ_FAILED;
1607 fsm->wrbuf[st->st_size] = '\0';
1608 /* XXX symlink(fsm->opath, fsm->path) */
1609 /*@-dependenttrans@*/
1610 fsm->opath = fsm->wrbuf;
1611 /*@=dependenttrans@*/
1612 rc = fsmStage(fsm, FSM_VERIFY);
1613 if (rc == CPIOERR_LSTAT_FAILED)
1614 rc = fsmStage(fsm, FSM_SYMLINK);
1615 fsm->opath = opath; /* XXX restore fsm->path */
1616 } else if (S_ISFIFO(st->st_mode)) {
1617 mode_t st_mode = st->st_mode;
1618 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1619 rc = fsmStage(fsm, FSM_VERIFY);
1620 if (rc == CPIOERR_LSTAT_FAILED) {
1621 st->st_mode = 0000; /* XXX abuse st->st_mode */
1622 rc = fsmStage(fsm, FSM_MKFIFO);
1623 st->st_mode = st_mode; /* XXX restore st->st_mode */
1625 } else if (S_ISCHR(st->st_mode) ||
1626 S_ISBLK(st->st_mode) ||
1627 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1629 rc = fsmStage(fsm, FSM_VERIFY);
1630 if (rc == CPIOERR_LSTAT_FAILED)
1631 rc = fsmStage(fsm, FSM_MKNOD);
1633 rc = CPIOERR_UNKNOWN_FILETYPE;
1635 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1636 fsm->li->createdPath = fsm->li->linkIndex;
1637 rc = fsmMakeLinks(fsm);
1643 rc = fsmMakeLinks(fsm);
1645 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1646 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1647 rpmTransactionSet ts = fsmGetTs(fsm);
1648 TFI_t fi = fsmGetFi(fsm);
1649 if (ts && ts->notify && fi) {
1650 /*@-type@*/ /* FIX: cast? */
1651 /*@-noeffectuncon @*/ /* FIX: check rc */
1652 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1653 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1654 rpmfiGetKey(fi), ts->notifyData);
1655 /*@=noeffectuncon @*/
1663 if (fsm->goal == FSM_PKGINSTALL) {
1664 (void) fsmStage(fsm,
1665 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1667 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1669 (void) fsmStage(fsm, FSM_RMDIRS);
1675 if (fsm->failedFile && *fsm->failedFile == NULL)
1676 *fsm->failedFile = xstrdup(fsm->path);
1679 if (!fsm->postpone && fsm->commit) {
1680 if (fsm->goal == FSM_PKGINSTALL)
1681 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1682 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1683 if (fsm->goal == FSM_PKGCOMMIT)
1684 rc = fsmStage(fsm, FSM_COMMIT);
1685 if (fsm->goal == FSM_PKGERASE)
1686 rc = fsmStage(fsm, FSM_COMMIT);
1688 fsm->path = _free(fsm->path);
1689 fsm->opath = _free(fsm->opath);
1690 memset(st, 0, sizeof(*st));
1691 memset(ost, 0, sizeof(*ost));
1694 /* Rename pre-existing modified or unmanaged file. */
1695 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1696 const char * opath = fsm->opath;
1697 const char * path = fsm->path;
1698 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1699 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1700 rc = fsmStage(fsm, FSM_RENAME);
1702 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1703 fsm->opath, fsm->path);
1705 fsm->path = _free(fsm->path);
1707 fsm->opath = _free(fsm->opath);
1711 /* Remove erased files. */
1712 if (fsm->goal == FSM_PKGERASE) {
1713 if (fsm->action == FA_ERASE) {
1714 TFI_t fi = fsmGetFi(fsm);
1715 if (S_ISDIR(st->st_mode)) {
1716 rc = fsmStage(fsm, FSM_RMDIR);
1719 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1721 /* XXX make sure that build side permits %missingok on directories. */
1722 if (fsm->fflags & RPMFILE_MISSINGOK)
1723 /*@innerbreak@*/ break;
1725 /* XXX common error message. */
1727 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1728 _("%s rmdir of %s failed: Directory not empty\n"),
1729 fiTypeString(fi), fsm->path);
1730 /*@innerbreak@*/ break;
1733 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1734 _("%s rmdir of %s failed: %s\n"),
1735 fiTypeString(fi), fsm->path, strerror(errno));
1736 /*@innerbreak@*/ break;
1739 rc = fsmStage(fsm, FSM_UNLINK);
1741 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1743 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1744 _("%s unlink of %s failed: %s\n"),
1745 fiTypeString(fi), fsm->path, strerror(errno));
1748 /* XXX Failure to remove is not (yet) cause for failure. */
1749 if (!strict_erasures) rc = 0;
1753 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1754 /* Rename temporary to final file name. */
1755 if (!S_ISDIR(st->st_mode) &&
1756 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1758 fsm->opath = fsm->path;
1759 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1760 rc = fsmStage(fsm, FSM_RENAME);
1761 if (!rc && fsm->nsuffix) {
1762 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1763 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1764 (opath ? opath : ""), fsm->path);
1765 opath = _free(opath);
1767 fsm->opath = _free(fsm->opath);
1769 if (S_ISLNK(st->st_mode)) {
1770 if (!rc && !getuid())
1771 rc = fsmStage(fsm, FSM_LCHOWN);
1773 if (!rc && !getuid())
1774 rc = fsmStage(fsm, FSM_CHOWN);
1776 rc = fsmStage(fsm, FSM_CHMOD);
1778 time_t mtime = st->st_mtime;
1779 TFI_t fi = fsmGetFi(fsm);
1781 st->st_mtime = fi->fmtimes[fsm->ix];
1782 rc = fsmStage(fsm, FSM_UTIME);
1783 st->st_mtime = mtime;
1788 /* Notify on success. */
1789 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1790 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1791 *fsm->failedFile = fsm->path;
1796 fsm->path = _free(fsm->path);
1798 /* Check for hard links missing from payload. */
1799 while ((fsm->li = fsm->links) != NULL) {
1800 fsm->links = fsm->li->next;
1801 fsm->li->next = NULL;
1802 if (fsm->goal == FSM_PKGINSTALL &&
1803 fsm->commit && fsm->li->linksLeft)
1805 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1806 if (fsm->li->filex[i] < 0)
1807 /*@innercontinue@*/ continue;
1808 rc = CPIOERR_MISSING_HARDLINK;
1809 if (fsm->failedFile && *fsm->failedFile == NULL) {
1810 fsm->ix = fsm->li->filex[i];
1811 if (!fsmStage(fsm, FSM_MAP)) {
1812 *fsm->failedFile = fsm->path;
1816 /*@loopbreak@*/ break;
1819 if (fsm->goal == FSM_PKGBUILD &&
1820 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1822 rc = CPIOERR_MISSING_HARDLINK;
1824 fsm->li = freeHardLink(fsm->li);
1826 fsm->ldn = _free(fsm->ldn);
1827 fsm->ldnalloc = fsm->ldnlen = 0;
1828 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1829 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1832 if (fsm->diskchecked && !fsm->exists) {
1833 rc = CPIOERR_LSTAT_FAILED;
1836 if (S_ISREG(st->st_mode)) {
1837 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1838 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1840 * XXX HP-UX (and other os'es) don't permit unlink on busy
1843 fsm->opath = fsm->path;
1845 rc = fsmStage(fsm, FSM_RENAME);
1847 (void) fsmStage(fsm, FSM_UNLINK);
1849 rc = CPIOERR_UNLINK_FAILED;
1850 fsm->path = fsm->opath;
1852 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1853 /*@notreached@*/ break;
1854 } else if (S_ISDIR(st->st_mode)) {
1855 if (S_ISDIR(ost->st_mode)) return 0;
1856 if (S_ISLNK(ost->st_mode)) {
1857 rc = fsmStage(fsm, FSM_STAT);
1858 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1863 if (S_ISDIR(ost->st_mode)) return 0;
1865 } else if (S_ISLNK(st->st_mode)) {
1866 if (S_ISLNK(ost->st_mode)) {
1867 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1868 rc = fsmStage(fsm, FSM_READLINK);
1873 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1875 } else if (S_ISFIFO(st->st_mode)) {
1876 if (S_ISFIFO(ost->st_mode)) return 0;
1877 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1878 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1879 (ost->st_rdev == st->st_rdev)) return 0;
1880 } else if (S_ISSOCK(st->st_mode)) {
1881 if (S_ISSOCK(ost->st_mode)) return 0;
1883 /* XXX shouldn't do this with commit/undo. */
1885 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1886 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1887 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1888 /*@notreached@*/ break;
1891 rc = Unlink(fsm->path);
1892 if (_fsm_debug && (stage & FSM_SYSCALL))
1893 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1894 fsm->path, (rc < 0 ? strerror(errno) : ""));
1895 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1898 rc = Rename(fsm->opath, fsm->path);
1899 if (_fsm_debug && (stage & FSM_SYSCALL))
1900 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1901 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1902 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1905 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1906 if (_fsm_debug && (stage & FSM_SYSCALL))
1907 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1908 fsm->path, (unsigned)(st->st_mode & 07777),
1909 (rc < 0 ? strerror(errno) : ""));
1910 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1913 rc = Rmdir(fsm->path);
1914 if (_fsm_debug && (stage & FSM_SYSCALL))
1915 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1916 fsm->path, (rc < 0 ? strerror(errno) : ""));
1917 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1920 rc = chown(fsm->path, st->st_uid, st->st_gid);
1921 if (_fsm_debug && (stage & FSM_SYSCALL))
1922 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1923 fsm->path, (int)st->st_uid, (int)st->st_gid,
1924 (rc < 0 ? strerror(errno) : ""));
1925 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1928 #if ! CHOWN_FOLLOWS_SYMLINK
1929 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1930 if (_fsm_debug && (stage & FSM_SYSCALL))
1931 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1932 fsm->path, (int)st->st_uid, (int)st->st_gid,
1933 (rc < 0 ? strerror(errno) : ""));
1934 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1938 rc = chmod(fsm->path, (st->st_mode & 07777));
1939 if (_fsm_debug && (stage & FSM_SYSCALL))
1940 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1941 fsm->path, (unsigned)(st->st_mode & 07777),
1942 (rc < 0 ? strerror(errno) : ""));
1943 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1946 { struct utimbuf stamp;
1947 stamp.actime = st->st_mtime;
1948 stamp.modtime = st->st_mtime;
1949 rc = utime(fsm->path, &stamp);
1950 if (_fsm_debug && (stage & FSM_SYSCALL))
1951 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1952 fsm->path, (unsigned)st->st_mtime,
1953 (rc < 0 ? strerror(errno) : ""));
1954 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1958 rc = symlink(fsm->opath, fsm->path);
1959 if (_fsm_debug && (stage & FSM_SYSCALL))
1960 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1961 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1962 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1965 rc = Link(fsm->opath, fsm->path);
1966 if (_fsm_debug && (stage & FSM_SYSCALL))
1967 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1968 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1969 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1972 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1973 if (_fsm_debug && (stage & FSM_SYSCALL))
1974 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1975 fsm->path, (unsigned)(st->st_mode & 07777),
1976 (rc < 0 ? strerror(errno) : ""));
1977 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1980 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
1981 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1982 /*@=unrecog =portability @*/
1983 if (_fsm_debug && (stage & FSM_SYSCALL))
1984 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1985 fsm->path, (unsigned)(st->st_mode & ~07777),
1986 (unsigned)st->st_rdev,
1987 (rc < 0 ? strerror(errno) : ""));
1988 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1991 rc = Lstat(fsm->path, ost);
1992 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1993 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1994 fsm->path, (rc < 0 ? strerror(errno) : ""));
1995 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1998 rc = Stat(fsm->path, ost);
1999 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
2000 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
2001 fsm->path, (rc < 0 ? strerror(errno) : ""));
2002 if (rc < 0) rc = CPIOERR_STAT_FAILED;
2005 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
2006 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
2007 if (_fsm_debug && (stage & FSM_SYSCALL))
2008 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
2009 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
2010 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
2013 fsm->rdbuf[fsm->rdnb] = '\0';
2021 rc = fsmStage(fsm, FSM_HREAD);
2023 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
2024 fsm->path = _free(fsm->path);
2025 rc = CPIOERR_HDR_TRAILER;
2028 rc = fsmStage(fsm, FSM_POS);
2031 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
2032 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
2033 rc = fsmStage(fsm, FSM_DREAD);
2035 /*@loopbreak@*/ break;
2039 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2042 (void) fsmStage(fsm, FSM_DREAD);
2046 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
2048 memset(fsm->rdbuf, 0, left);
2049 /* XXX DWRITE uses rdnb for I/O length. */
2051 (void) fsmStage(fsm, FSM_DWRITE);
2055 rc = cpioTrailerWrite(fsm);
2058 rc = fsmStage(fsm, FSM_POS);
2060 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2063 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2066 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2067 if (_fsm_debug && (stage & FSM_SYSCALL))
2068 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2069 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2070 (int)fsm->wrlen, (int)fsm->rdnb);
2071 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2072 rc = CPIOERR_READ_FAILED;
2074 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2077 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2078 if (_fsm_debug && (stage & FSM_SYSCALL))
2079 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2080 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2081 (int)fsm->rdnb, (int)fsm->wrnb);
2082 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2083 rc = CPIOERR_WRITE_FAILED;
2085 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2089 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2090 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2091 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2093 rc = CPIOERR_OPEN_FAILED;
2096 if (_fsm_debug && (stage & FSM_SYSCALL))
2097 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2098 fsm->path, fsm->rfd, fsm->rdbuf);
2101 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2102 if (_fsm_debug && (stage & FSM_SYSCALL))
2103 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2104 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2105 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2106 rc = CPIOERR_READ_FAILED;
2110 if (_fsm_debug && (stage & FSM_SYSCALL))
2111 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2112 (void) Fclose(fsm->rfd);
2120 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2121 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2122 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2124 rc = CPIOERR_OPEN_FAILED;
2126 if (_fsm_debug && (stage & FSM_SYSCALL))
2127 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2128 fsm->path, fsm->wfd, fsm->wrbuf);
2131 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2132 if (_fsm_debug && (stage & FSM_SYSCALL))
2133 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2134 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2135 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2136 rc = CPIOERR_WRITE_FAILED;
2140 if (_fsm_debug && (stage & FSM_SYSCALL))
2141 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2142 (void) Fclose(fsm->wfd);
2155 if (!(stage & FSM_INTERNAL)) {
2156 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2162 /*@obserever@*/ const char *const fileActionString(fileAction a)
2165 case FA_UNKNOWN: return "unknown";
2166 case FA_CREATE: return "create";
2167 case FA_COPYOUT: return "copyout";
2168 case FA_COPYIN: return "copyin";
2169 case FA_BACKUP: return "backup";
2170 case FA_SAVE: return "save";
2171 case FA_SKIP: return "skip";
2172 case FA_ALTNAME: return "altname";
2173 case FA_ERASE: return "erase";
2174 case FA_SKIPNSTATE: return "skipnstate";
2175 case FA_SKIPNETSHARED: return "skipnetshared";
2176 case FA_SKIPMULTILIB: return "skipmultilib";
2177 default: return "???";
2182 /*@observer@*/ const char *const fileStageString(fileStage a) {
2184 case FSM_UNKNOWN: return "unknown";
2186 case FSM_PKGINSTALL:return "pkginstall";
2187 case FSM_PKGERASE: return "pkgerase";
2188 case FSM_PKGBUILD: return "pkgbuild";
2189 case FSM_PKGCOMMIT: return "pkgcommit";
2190 case FSM_PKGUNDO: return "pkgundo";
2192 case FSM_CREATE: return "create";
2193 case FSM_INIT: return "init";
2194 case FSM_MAP: return "map";
2195 case FSM_MKDIRS: return "mkdirs";
2196 case FSM_RMDIRS: return "rmdirs";
2197 case FSM_PRE: return "pre";
2198 case FSM_PROCESS: return "process";
2199 case FSM_POST: return "post";
2200 case FSM_MKLINKS: return "mklinks";
2201 case FSM_NOTIFY: return "notify";
2202 case FSM_UNDO: return "undo";
2203 case FSM_FINI: return "fini";
2204 case FSM_COMMIT: return "commit";
2205 case FSM_DESTROY: return "destroy";
2206 case FSM_VERIFY: return "verify";
2208 case FSM_UNLINK: return "Unlink";
2209 case FSM_RENAME: return "Rename";
2210 case FSM_MKDIR: return "Mkdir";
2211 case FSM_RMDIR: return "rmdir";
2212 case FSM_CHOWN: return "chown";
2213 case FSM_LCHOWN: return "lchown";
2214 case FSM_CHMOD: return "chmod";
2215 case FSM_UTIME: return "utime";
2216 case FSM_SYMLINK: return "symlink";
2217 case FSM_LINK: return "Link";
2218 case FSM_MKFIFO: return "mkfifo";
2219 case FSM_MKNOD: return "mknod";
2220 case FSM_LSTAT: return "Lstat";
2221 case FSM_STAT: return "Stat";
2222 case FSM_READLINK: return "Readlink";
2223 case FSM_CHROOT: return "chroot";
2225 case FSM_NEXT: return "next";
2226 case FSM_EAT: return "eat";
2227 case FSM_POS: return "pos";
2228 case FSM_PAD: return "pad";
2229 case FSM_TRAILER: return "trailer";
2230 case FSM_HREAD: return "hread";
2231 case FSM_HWRITE: return "hwrite";
2232 case FSM_DREAD: return "Fread";
2233 case FSM_DWRITE: return "Fwrite";
2235 case FSM_ROPEN: return "Fopen";
2236 case FSM_READ: return "Fread";
2237 case FSM_RCLOSE: return "Fclose";
2238 case FSM_WOPEN: return "Fopen";
2239 case FSM_WRITE: return "Fwrite";
2240 case FSM_WCLOSE: return "Fclose";
2242 default: return "???";