3 * File state machine to handle a payload from a package.
13 /*@access rpmTransactionSet @*/
18 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
22 /* XXX Failure to remove is not (yet) cause for failure. */
23 /*@-exportlocal -exportheadervar@*/
24 int strict_erasures = 0;
25 /*@=exportlocal =exportheadervar@*/
27 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
28 const FSMI_t iter = fsm->iter;
30 return (iter ? iter->ts : NULL);
34 TFI_t fsmGetFi(const FSM_t fsm)
36 const FSMI_t iter = fsm->iter;
38 return (iter ? iter->fi : NULL);
42 #define SUFFIX_RPMORIG ".rpmorig"
43 #define SUFFIX_RPMSAVE ".rpmsave"
44 #define SUFFIX_RPMNEW ".rpmnew"
47 * Build path to file from file info, ornamented with subdir and suffix.
48 * @param fsm file state machine data
49 * @param st file stat info
50 * @param subdir subdir to use (NULL disables)
51 * @param suffix suffix to use (NULL disables)
52 * @retval path to file
54 static /*@only@*//*@null@*/
55 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
56 /*@null@*/ const struct stat * st,
57 /*@null@*/ const char * subdir,
58 /*@null@*/ const char * suffix)
59 /*@uses fsm->dirName, fsm->baseName */
62 const char * s = NULL;
67 nb = strlen(fsm->dirName) +
68 (st && !S_ISDIR(st->st_mode) ? (subdir ? strlen(subdir) : 0) : 0) +
69 (st && !S_ISDIR(st->st_mode) ? (suffix ? strlen(suffix) : 0) : 0) +
70 strlen(fsm->baseName) + 1;
72 t = stpcpy(t, fsm->dirName);
73 if (st && !S_ISDIR(st->st_mode))
74 if (subdir) t = stpcpy(t, subdir);
75 t = stpcpy(t, fsm->baseName);
76 if (st && !S_ISDIR(st->st_mode))
77 if (suffix) t = stpcpy(t, suffix);
83 * Destroy file info iterator.
84 * @param p file info iterator
87 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
90 return _free((void *)p);
94 * Create file info iterator.
95 * @param a transaction set
96 * @param b transaction element file info
97 * @return file info iterator
100 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
103 rpmTransactionSet ts = (void *)a;
104 TFI_t fi = (void *)b;
107 iter = xcalloc(1, sizeof(*iter));
112 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
113 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
114 iter->isave = iter->i;
119 * Return next index into file info.
120 * @param a file info iterator
121 * @return next index, -1 on termination
123 static int mapNextIterator(void * a)
127 const TFI_t fi = iter->fi;
131 if (iter->i >= 0) i = iter->i--;
133 if (iter->i < fi->fc) i = iter->i++;
141 static int cpioStrCmp(const void * a, const void * b)
144 const char * afn = *(const char **)a;
145 const char * bfn = *(const char **)b;
147 /* Match rpm-4.0 payloads with ./ prefixes. */
148 if (afn[0] == '.' && afn[1] == '/') afn += 2;
149 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
151 /* If either path is absolute, make it relative. */
152 if (afn[0] == '/') afn += 1;
153 if (bfn[0] == '/') bfn += 1;
155 return strcmp(afn, bfn);
159 * Locate archive path in file info.
160 * @param a file info iterator
161 * @param fsmPath archive path
162 * @return index into file info, -1 if archive path was not found
164 static int mapFind(void * a, const char * fsmPath)
168 const TFI_t fi = iter->fi;
171 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
172 const char ** p = NULL;
174 if (fi->apath != NULL)
175 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
178 iter->i = p - fi->apath;
179 ix = mapNextIterator(iter);
186 * Directory name iterator.
188 typedef struct dnli_s {
189 /*@dependent@*/ TFI_t fi;
190 /*@only@*/ /*@null@*/ char * active;
197 * Destroy directory name iterator.
198 * @param a directory name iterator
199 * @retval NULL always
201 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
205 DNLI_t dnli = (void *)a;
206 if (dnli->active) free(dnli->active);
213 static inline int dnlCount(const DNLI_t dnli)
216 return (dnli ? dnli->fi->dc : 0);
221 static inline int dnlIndex(const DNLI_t dnli)
224 return (dnli ? dnli->isave : -1);
228 * Create directory name iterator.
229 * @param fsm file state machine data
230 * @param reverse traverse directory names in reverse order?
231 * @return directory name iterator
233 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
235 /*@uses fsm->iter @*/
238 TFI_t fi = fsmGetFi(fsm);
244 dnli = xcalloc(1, sizeof(*dnli));
246 dnli->reverse = reverse;
247 dnli->i = (reverse ? fi->dc : 0);
250 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
252 /* Identify parent directories not skipped. */
253 for (i = 0; i < fi->fc; i++)
254 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
256 /* Exclude parent directories that are explicitly included. */
257 for (i = 0; i < fi->fc; i++) {
258 int dil, dnlen, bnlen;
260 if (!S_ISDIR(fi->fmodes[i]))
264 dnlen = strlen(fi->dnl[dil]);
265 bnlen = strlen(fi->bnl[i]);
267 for (j = 0; j < fi->dc; j++) {
271 if (!dnli->active[j] || j == dil) continue;
274 if (jlen != (dnlen+bnlen+1)) continue;
275 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
276 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
277 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
279 /* This directory is included in the package. */
281 /*@innerbreak@*/ break;
285 /* Print only once per package. */
288 for (i = 0; i < fi->dc; i++) {
289 if (!dnli->active[i]) continue;
292 rpmMessage(RPMMESS_DEBUG,
293 _("========= Directories not explictly included in package:\n"));
295 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
298 rpmMessage(RPMMESS_DEBUG, "=========\n");
305 * Return next directory name (from file info).
306 * @param dnli directory name iterator
307 * @return next directory name
309 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
312 const char * dn = NULL;
320 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
321 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
323 if (i >= 0 && i < fi->dc)
333 * Save hard link in chain.
334 * @param fsm file state machine data
335 * @return Is chain only partially filled?
337 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
338 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
339 /*@defines fsm->li @*/
340 /*@releases fsm->path @*/
343 struct stat * st = &fsm->sb;
348 /* Find hard link set. */
349 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
350 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
354 /* New hard link encountered, add new link to set. */
355 if (fsm->li == NULL) {
356 fsm->li = xcalloc(1, sizeof(*fsm->li));
357 fsm->li->next = NULL;
358 fsm->li->sb = *st; /* structure assignment */
359 fsm->li->nlink = st->st_nlink;
360 fsm->li->linkIndex = fsm->ix;
361 fsm->li->createdPath = -1;
363 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
364 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
365 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
367 if (fsm->goal == FSM_PKGBUILD)
368 fsm->li->linksLeft = st->st_nlink;
369 if (fsm->goal == FSM_PKGINSTALL)
370 fsm->li->linksLeft = 0;
373 fsm->li->next = fsm->links;
375 fsm->links = fsm->li;
378 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
379 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
380 /*@-observertrans -dependenttrans@*/
381 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
382 /*@=observertrans =dependenttrans@*/
383 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
385 if (fsm->goal == FSM_PKGBUILD)
386 return (fsm->li->linksLeft > 0);
388 if (fsm->goal != FSM_PKGINSTALL)
391 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
394 /* Here come the bits, time to choose a non-skipped file name. */
395 { TFI_t fi = fsmGetFi(fsm);
397 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
398 ix = fsm->li->filex[j];
399 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
405 /* Are all links skipped or not encountered yet? */
407 return 1; /* XXX W2DO? */
409 /* Save the non-skipped file name and map index. */
410 fsm->li->linkIndex = j;
411 fsm->path = _free(fsm->path);
413 rc = fsmStage(fsm, FSM_MAP);
414 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
420 * Destroy set of hard links.
421 * @param li set of hard links
423 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
427 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
428 li->filex = _free(li->filex);
435 FSM_t fsm = xcalloc(1, sizeof(*fsm));
439 FSM_t freeFSM(FSM_t fsm)
442 fsm->path = _free(fsm->path);
443 while ((fsm->li = fsm->links) != NULL) {
444 fsm->links = fsm->li->next;
445 fsm->li->next = NULL;
446 fsm->li = freeHardLink(fsm->li);
448 fsm->dnlx = _free(fsm->dnlx);
449 fsm->ldn = _free(fsm->ldn);
450 fsm->iter = mapFreeIterator(fsm->iter);
455 int fsmSetup(FSM_t fsm, fileStage goal,
456 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
457 unsigned int * archiveSize, const char ** failedFile)
464 fsm->cfd = fdLink(cfd, "persist (fsm)");
465 pos = fdGetCpioPos(fsm->cfd);
466 fdSetCpioPos(fsm->cfd, 0);
468 fsm->iter = mapInitIterator(ts, fi);
470 if (fsm->goal == FSM_PKGINSTALL) {
471 if (ts && ts->notify) {
472 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
473 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
478 fsm->archiveSize = archiveSize;
479 if (fsm->archiveSize)
480 *fsm->archiveSize = 0;
481 fsm->failedFile = failedFile;
483 *fsm->failedFile = NULL;
486 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
487 if (fsm->goal == FSM_PKGINSTALL) {
488 if (ts && ts->id > 0)
489 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
493 rc = fsmStage(fsm, FSM_CREATE);
494 if (rc && !ec) ec = rc;
496 rc = fsmStage(fsm, fsm->goal);
497 if (rc && !ec) ec = rc;
499 if (fsm->archiveSize && ec == 0)
500 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
505 int fsmTeardown(FSM_t fsm) {
509 rc = fsmStage(fsm, FSM_DESTROY);
511 fsm->iter = mapFreeIterator(fsm->iter);
513 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
516 fsm->failedFile = NULL;
517 /*@-nullstate@*/ /* FIX: fsm->iter null annotation? */
522 int fsmMapPath(FSM_t fsm)
524 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
531 fsm->action = FA_UNKNOWN;
535 if (fi && i >= 0 && i < fi->fc) {
537 fsm->astriplen = fi->astriplen;
538 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
539 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
540 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
542 /* src rpms have simple base name in payload. */
543 fsm->dirName = fi->dnl[fi->dil[i]];
544 fsm->baseName = fi->bnl[i];
546 switch (fsm->action) {
549 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
558 assert(fi->type == TR_ADDED);
562 if (fi->fstates && fi->type == TR_ADDED)
563 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
566 case FA_SKIPNETSHARED:
567 if (fi->fstates && fi->type == TR_ADDED)
568 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
572 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
575 fsm->osuffix = SUFFIX_RPMORIG;
576 /*@innerbreak@*/ break;
578 fsm->osuffix = SUFFIX_RPMSAVE;
579 /*@innerbreak@*/ break;
584 assert(fi->type == TR_ADDED);
585 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
586 fsm->nsuffix = SUFFIX_RPMNEW;
590 assert(fi->type == TR_ADDED);
591 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
592 fsm->osuffix = SUFFIX_RPMSAVE;
595 assert(fi->type == TR_REMOVED);
597 * XXX TODO: %ghost probably shouldn't be removed, but that changes
598 * legacy rpm behavior.
605 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
606 const struct stat * st = &fsm->sb;
607 fsm->path = _free(fsm->path);
608 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
609 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
610 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
617 int fsmMapAttrs(FSM_t fsm)
619 struct stat * st = &fsm->sb;
620 TFI_t fi = fsmGetFi(fsm);
623 if (fi && i >= 0 && i < fi->fc) {
625 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
627 (fi->fmodes ? fi->fmodes[i] : perms);
629 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
631 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
633 (fi->frdevs ? fi->frdevs[i] : 0);
635 (fi->fmtimes ? fi->fmtimes[i] : 0);
637 if (fsm->mapFlags & CPIO_MAP_MODE)
638 st->st_mode = (st->st_mode & S_IFMT) | (finalMode & ~S_IFMT);
639 if (fsm->mapFlags & CPIO_MAP_TYPE) {
640 st->st_mode = (st->st_mode & ~S_IFMT) | (finalMode & S_IFMT);
641 if ((S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
642 && st->st_nlink == 0)
644 st->st_rdev = finalRdev;
645 st->st_mtime = finalMtime;
647 if (fsm->mapFlags & CPIO_MAP_UID)
648 st->st_uid = finalUid;
649 if (fsm->mapFlags & CPIO_MAP_GID)
650 st->st_gid = finalGid;
652 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
659 * Create file from payload stream.
660 * @param fsm file state machine data
661 * @return 0 on success
663 static int expandRegular(/*@special@*/ FSM_t fsm)
665 /*@modifies fsm, fileSystem @*/
667 const char * fmd5sum;
668 const struct stat * st = &fsm->sb;
669 int left = st->st_size;
672 rc = fsmStage(fsm, FSM_WOPEN);
676 /* XXX md5sum's will break on repackaging that includes modified files. */
677 fmd5sum = fsm->fmd5sum;
679 if (st->st_size > 0 && fmd5sum)
680 fdInitMD5(fsm->wfd, 0);
684 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
685 rc = fsmStage(fsm, FSM_DREAD);
689 rc = fsmStage(fsm, FSM_WRITE);
695 /* don't call this with fileSize == fileComplete */
697 (void) fsmStage(fsm, FSM_NOTIFY);
700 if (st->st_size > 0 && fmd5sum) {
701 const char * md5sum = NULL;
703 (void) Fflush(fsm->wfd);
704 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
706 if (md5sum == NULL) {
707 rc = CPIOERR_MD5SUM_MISMATCH;
709 if (strcmp(md5sum, fmd5sum))
710 rc = CPIOERR_MD5SUM_MISMATCH;
711 md5sum = _free(md5sum);
716 (void) fsmStage(fsm, FSM_WCLOSE);
721 * Write next item to payload stream.
722 * @param fsm file state machine data
723 * @param writeData should data be written?
724 * @return 0 on success
726 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
727 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
728 /*@modifies fsm, fileSystem @*/
730 const char * path = fsm->path;
731 const char * opath = fsm->opath;
732 struct stat * st = &fsm->sb;
733 struct stat * ost = &fsm->osb;
734 size_t pos = fdGetCpioPos(fsm->cfd);
735 char * symbuf = NULL;
739 st->st_size = (writeData ? ost->st_size : 0);
741 if (S_ISDIR(st->st_mode)) {
743 } else if (S_ISLNK(st->st_mode)) {
745 * While linux puts the size of a symlink in the st_size field,
746 * I don't think that's a specified standard.
748 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
749 rc = fsmStage(fsm, FSM_READLINK);
751 st->st_size = fsm->rdnb;
752 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
755 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
756 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
757 char * t = alloca(nb);
760 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
762 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
763 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
764 TFI_t fi = fsmGetFi(fsm);
766 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
769 rc = fsmStage(fsm, FSM_HWRITE);
773 if (writeData && S_ISREG(st->st_mode)) {
776 void * mapped = (void *)-1;
780 rc = fsmStage(fsm, FSM_ROPEN);
783 /* XXX unbuffered mmap generates *lots* of fdio debugging */
786 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
787 if (mapped != (void *)-1) {
789 fsm->rdbuf = (char *) mapped;
790 fsm->rdlen = nmapped = st->st_size;
798 if (mapped != (void *)-1) {
803 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
804 rc = fsmStage(fsm, FSM_READ);
808 /* XXX DWRITE uses rdnb for I/O length. */
809 rc = fsmStage(fsm, FSM_DWRITE);
816 if (mapped != (void *)-1) {
817 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
822 } else if (writeData && S_ISLNK(st->st_mode)) {
823 /* XXX DWRITE uses rdnb for I/O length. */
824 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
825 fsm->rdnb = strlen(symbuf);
826 rc = fsmStage(fsm, FSM_DWRITE);
830 rc = fsmStage(fsm, FSM_PAD);
833 { const rpmTransactionSet ts = fsmGetTs(fsm);
834 TFI_t fi = fsmGetFi(fsm);
835 if (ts && fi && ts->notify) {
836 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
837 /*@-modunconnomods@*/
838 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
839 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
840 /*@=modunconnomods@*/
848 (void) fsmStage(fsm, FSM_RCLOSE);
849 /*@-dependenttrans@*/
852 /*@=dependenttrans@*/
857 * Write set of linked files to payload stream.
858 * @param fsm file state machine data
859 * @return 0 on success
861 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
862 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
863 /*@modifies fsm, fileSystem @*/
865 const char * path = fsm->path;
866 const char * nsuffix = fsm->nsuffix;
867 int iterIndex = fsm->ix;
876 for (i = fsm->li->nlink - 1; i >= 0; i--) {
878 if (fsm->li->filex[i] < 0) continue;
880 fsm->ix = fsm->li->filex[i];
881 rc = fsmStage(fsm, FSM_MAP);
883 /* Write data after last link. */
884 rc = writeFile(fsm, (i == 0));
885 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
887 *fsm->failedFile = xstrdup(fsm->path);
890 fsm->path = _free(fsm->path);
891 fsm->li->filex[i] = -1;
895 fsm->nsuffix = nsuffix;
901 * Create pending hard links to existing file.
902 * @param fsm file state machine data
903 * @return 0 on success
905 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
906 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
907 /*@modifies fsm, fileSystem @*/
909 const char * path = fsm->path;
910 const char * opath = fsm->opath;
911 const char * nsuffix = fsm->nsuffix;
912 int iterIndex = fsm->ix;
922 fsm->ix = fsm->li->filex[fsm->li->createdPath];
923 rc = fsmStage(fsm, FSM_MAP);
924 fsm->opath = fsm->path;
926 for (i = 0; i < fsm->li->nlink; i++) {
927 if (fsm->li->filex[i] < 0) continue;
928 if (i == fsm->li->createdPath) continue;
930 fsm->ix = fsm->li->filex[i];
931 fsm->path = _free(fsm->path);
932 rc = fsmStage(fsm, FSM_MAP);
933 rc = fsmStage(fsm, FSM_VERIFY);
935 if (rc != CPIOERR_LSTAT_FAILED) break;
937 /* XXX link(fsm->opath, fsm->path) */
938 rc = fsmStage(fsm, FSM_LINK);
939 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
941 *fsm->failedFile = xstrdup(fsm->path);
944 fsm->li->linksLeft--;
946 fsm->path = _free(fsm->path);
947 fsm->opath = _free(fsm->opath);
950 fsm->nsuffix = nsuffix;
957 * Commit hard linked file set atomically.
958 * @param fsm file state machine data
959 * @return 0 on success
961 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
962 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
963 fsm->li, fsm->links @*/
964 /*@modifies fsm, fileSystem @*/
966 const char * path = fsm->path;
967 const char * nsuffix = fsm->nsuffix;
968 int iterIndex = fsm->ix;
969 struct stat * st = &fsm->sb;
977 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
978 if (fsm->li->sb.st_ino == st->st_ino && fsm->li->sb.st_dev == st->st_dev)
982 for (i = 0; i < fsm->li->nlink; i++) {
983 if (fsm->li->filex[i] < 0) continue;
984 fsm->ix = fsm->li->filex[i];
985 rc = fsmStage(fsm, FSM_MAP);
986 rc = fsmStage(fsm, FSM_COMMIT);
987 fsm->path = _free(fsm->path);
988 fsm->li->filex[i] = -1;
992 fsm->nsuffix = nsuffix;
998 * Remove (if created) directories not explicitly included in package.
999 * @param fsm file state machine data
1000 * @return 0 on success
1002 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
1003 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
1004 /*@modifies fsm, fileSystem @*/
1006 const char * path = fsm->path;
1007 void * dnli = dnlInitIterator(fsm, 1);
1008 char * dn = fsm->rdbuf;
1009 int dc = dnlCount(dnli);
1014 /*@-observertrans -dependenttrans@*/
1015 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1016 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1017 int dnlen = strlen(fsm->path);
1020 dc = dnlIndex(dnli);
1021 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1024 /* Copy to avoid const on fsm->path. */
1025 te = stpcpy(dn, fsm->path) - 1;
1028 /* Remove generated directories. */
1029 /*@-usereleased@*/ /* LCL: te used after release? */
1033 rc = fsmStage(fsm, FSM_RMDIR);
1037 /*@innerbreak@*/ break;
1039 } while ((te - fsm->path) > fsm->dnlx[dc]);
1042 dnli = dnlFreeIterator(dnli);
1043 /*@=observertrans =dependenttrans@*/
1050 * Create (if necessary) directories not explicitly included in package.
1051 * @param fsm file state machine data
1052 * @return 0 on success
1054 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1055 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1056 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1057 /*@defines fsm->dnlx, fsm->ldn @*/
1058 /*@modifies fsm, fileSystem @*/
1060 struct stat * st = &fsm->sb;
1061 struct stat * ost = &fsm->osb;
1062 const char * path = fsm->path;
1063 mode_t st_mode = st->st_mode;
1064 void * dnli = dnlInitIterator(fsm, 0);
1065 char * dn = fsm->rdbuf;
1066 int dc = dnlCount(dnli);
1073 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1074 /*@-observertrans -dependenttrans@*/
1075 if (fsm->dnlx != NULL)
1076 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1077 int dnlen = strlen(fsm->path);
1080 dc = dnlIndex(dnli);
1081 if (dc < 0) continue;
1082 fsm->dnlx[dc] = dnlen;
1086 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1087 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1089 /*@=compdef =nullpass@*/
1091 /* Copy to avoid const on fsm->path. */
1092 (void) stpcpy(dn, fsm->path);
1095 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1096 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1097 if (*te != '/') continue;
1101 /* Already validated? */
1102 /*@-usedef -compdef -nullpass -nullderef@*/
1103 if (i < fsm->ldnlen &&
1104 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1105 !strncmp(fsm->path, fsm->ldn, i))
1108 /* Move pre-existing path marker forward. */
1109 fsm->dnlx[dc] = (te - dn);
1112 /*@=usedef =compdef =nullpass =nullderef@*/
1114 /* Validate next component of path. */
1115 rc = fsmStage(fsm, FSM_LSTAT);
1118 /* Directory already exists? */
1119 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1120 /* Move pre-existing path marker forward. */
1121 fsm->dnlx[dc] = (te - dn);
1122 } else if (rc == CPIOERR_LSTAT_FAILED) {
1123 TFI_t fi = fsmGetFi(fsm);
1125 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1126 rc = fsmStage(fsm, FSM_MKDIR);
1128 rpmMessage(RPMMESS_DEBUG,
1129 _("%s directory created with perms %04o.\n"),
1130 fsm->path, (unsigned)(st->st_mode & 07777));
1134 /*@innerbreak@*/ break;
1138 /* Save last validated path. */
1139 if (fsm->ldnalloc < (dnlen + 1)) {
1140 fsm->ldnalloc = dnlen + 100;
1141 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1143 if (fsm->ldn != NULL) { /* XXX can't happen */
1144 strcpy(fsm->ldn, fsm->path);
1145 fsm->ldnlen = dnlen;
1148 dnli = dnlFreeIterator(dnli);
1149 /*@=observertrans =dependenttrans@*/
1152 st->st_mode = st_mode; /* XXX restore st->st_mode */
1158 * Check for file on disk.
1159 * @param fsm file state machine data
1160 * @return 0 on success
1162 static int fsmStat(FSM_t fsm)
1164 int saveerrno = errno;
1167 if (fsm->path != NULL) {
1168 int saveernno = errno;
1169 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1170 ? FSM_LSTAT : FSM_STAT));
1171 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1175 } else if (rc == 0) {
1179 /* Skip %ghost files on build. */
1187 int fsmStage(FSM_t fsm, fileStage stage)
1190 fileStage prevStage = fsm->stage;
1191 const char * const prev = fileStageString(prevStage);
1193 static int modulo = 4;
1194 const char * const cur = fileStageString(stage);
1195 struct stat * st = &fsm->sb;
1196 struct stat * ost = &fsm->osb;
1197 int saveerrno = errno;
1202 #define _fafilter(_a) \
1203 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1204 ? fileActionString(_a) : "")
1206 if (stage & FSM_DEAD) {
1208 } else if (stage & FSM_INTERNAL) {
1209 if (_fsm_debug && !(stage & FSM_SYSCALL))
1210 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1212 (unsigned)st->st_mode, (int)st->st_nlink,
1213 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1214 (fsm->path ? fsm->path : ""),
1215 _fafilter(fsm->action));
1218 if (_fsm_debug || !(stage & FSM_VERBOSE))
1219 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1221 (unsigned)st->st_mode, (int)st->st_nlink,
1222 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1223 (fsm->path ? fsm->path + fsm->astriplen : ""),
1224 _fafilter(fsm->action));
1231 case FSM_PKGINSTALL:
1233 /* Clean fsm, free'ing memory. Read next archive header. */
1234 rc = fsmStage(fsm, FSM_INIT);
1236 /* Exit on end-of-payload. */
1237 if (rc == CPIOERR_HDR_TRAILER) {
1239 /*@loopbreak@*/ break;
1242 /* Exit on error. */
1245 (void) fsmStage(fsm, FSM_UNDO);
1246 /*@loopbreak@*/ break;
1249 /* Extract file from archive. */
1250 rc = fsmStage(fsm, FSM_PROCESS);
1252 (void) fsmStage(fsm, FSM_UNDO);
1253 /*@loopbreak@*/ break;
1256 /* Notify on success. */
1257 (void) fsmStage(fsm, FSM_NOTIFY);
1259 rc = fsmStage(fsm, FSM_FINI);
1261 /*@loopbreak@*/ break;
1268 /* Clean fsm, free'ing memory. */
1269 rc = fsmStage(fsm, FSM_INIT);
1271 /* Exit on end-of-payload. */
1272 if (rc == CPIOERR_HDR_TRAILER) {
1274 /*@loopbreak@*/ break;
1277 /* Rename/erase next item. */
1278 if (fsmStage(fsm, FSM_FINI))
1279 /*@loopbreak@*/ break;
1285 rc = fsmStage(fsm, FSM_INIT);
1287 /* Exit on end-of-payload. */
1288 if (rc == CPIOERR_HDR_TRAILER) {
1290 /*@loopbreak@*/ break;
1293 /* Exit on error. */
1296 (void) fsmStage(fsm, FSM_UNDO);
1297 /*@loopbreak@*/ break;
1300 /* Copy file into archive. */
1301 rc = fsmStage(fsm, FSM_PROCESS);
1303 (void) fsmStage(fsm, FSM_UNDO);
1304 /*@loopbreak@*/ break;
1307 if (fsmStage(fsm, FSM_FINI))
1308 /*@loopbreak@*/ break;
1311 /* Flush partial sets of hard linked files. */
1312 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) {
1314 while ((fsm->li = fsm->links) != NULL) {
1315 fsm->links = fsm->li->next;
1316 fsm->li->next = NULL;
1318 /* Re-calculate link count for archive header. */
1319 for (j = -1, nlink = 0, i = 0; i < fsm->li->nlink; i++) {
1320 if (fsm->li->filex[i] < 0) continue;
1324 /* XXX force the contents out as well. */
1326 fsm->li->filex[0] = fsm->li->filex[j];
1327 fsm->li->filex[j] = -1;
1329 fsm->li->sb.st_nlink = nlink;
1331 fsm->sb = fsm->li->sb; /* structure assignment */
1332 fsm->osb = fsm->sb; /* structure assignment */
1334 if (!rc) rc = writeLinkedFile(fsm);
1336 fsm->li = freeHardLink(fsm->li);
1341 rc = fsmStage(fsm, FSM_TRAILER);
1345 { rpmTransactionSet ts = fsmGetTs(fsm);
1346 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1347 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1348 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1351 fsm->path = _free(fsm->path);
1352 fsm->opath = _free(fsm->opath);
1353 fsm->dnlx = _free(fsm->dnlx);
1355 fsm->ldn = _free(fsm->ldn);
1356 fsm->ldnalloc = fsm->ldnlen = 0;
1358 fsm->rdsize = fsm->wrsize = 0;
1359 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1360 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1361 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1362 fsm->rdsize = 8 * BUFSIZ;
1363 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1364 fsm->wrsize = 8 * BUFSIZ;
1365 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1368 fsm->mkdirsdone = 0;
1372 errno = 0; /* XXX get rid of EBADF */
1374 /* Detect and create directories not explicitly in package. */
1375 if (fsm->goal == FSM_PKGINSTALL) {
1376 rc = fsmStage(fsm, FSM_MKDIRS);
1377 if (!rc) fsm->mkdirsdone = 1;
1382 fsm->path = _free(fsm->path);
1384 fsm->diskchecked = fsm->exists = 0;
1386 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1387 fsm->action = FA_UNKNOWN;
1388 fsm->osuffix = NULL;
1389 fsm->nsuffix = NULL;
1391 if (fsm->goal == FSM_PKGINSTALL) {
1392 /* Read next header from payload, checking for end-of-payload. */
1393 rc = fsmStage(fsm, FSM_NEXT);
1397 /* Identify mapping index. */
1398 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1399 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1401 /* Detect end-of-loop and/or mapping error. */
1403 if (fsm->goal == FSM_PKGINSTALL) {
1405 rpmMessage(RPMMESS_WARNING,
1406 _("archive file %s was not found in header file list\n"),
1409 if (fsm->failedFile && *fsm->failedFile == NULL)
1410 *fsm->failedFile = xstrdup(fsm->path);
1411 rc = CPIOERR_UNMAPPED_FILE;
1413 rc = CPIOERR_HDR_TRAILER;
1418 /* On non-install, mode must be known so that dirs don't get suffix. */
1419 if (fsm->goal != FSM_PKGINSTALL) {
1420 TFI_t fi = fsmGetFi(fsm);
1421 st->st_mode = fi->fmodes[fsm->ix];
1424 /* Generate file path. */
1425 rc = fsmStage(fsm, FSM_MAP);
1428 /* Perform lstat/stat for disk file. */
1432 if (fsm->path != NULL) {
1433 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1434 ? FSM_LSTAT : FSM_STAT));
1435 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1439 } else if (rc == 0) {
1443 /* Skip %ghost files on build. */
1447 fsm->diskchecked = 1;
1450 /* On non-install, the disk file stat is what's remapped. */
1451 if (fsm->goal != FSM_PKGINSTALL)
1452 *st = *ost; /* structure assignment */
1454 /* Remap file perms, owner, and group. */
1455 rc = fsmMapAttrs(fsm);
1458 fsm->postpone = XFA_SKIPPING(fsm->action);
1459 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1460 /*@-evalorder@*/ /* FIX: saveHardLink can modify fsm */
1461 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1462 fsm->postpone = saveHardLink(fsm);
1469 rc = fsmMapPath(fsm);
1472 rc = fsmMkdirs(fsm);
1476 rc = fsmRmdirs(fsm);
1479 if (fsm->postpone) {
1480 if (fsm->goal == FSM_PKGINSTALL)
1481 rc = fsmStage(fsm, FSM_EAT);
1485 if (fsm->goal == FSM_PKGBUILD) {
1486 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1487 struct hardLink * li, * prev;
1489 if (!(fsm->mapFlags & CPIO_ALL_HARDLINKS)) break;
1490 rc = writeLinkedFile(fsm);
1491 if (rc) break; /* W2DO? */
1493 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1495 /*@loopbreak@*/ break;
1498 fsm->links = fsm->li->next;
1500 prev->next = fsm->li->next;
1501 fsm->li->next = NULL;
1502 fsm->li = freeHardLink(fsm->li);
1504 rc = writeFile(fsm, 1);
1509 if (fsm->goal != FSM_PKGINSTALL)
1512 if (S_ISREG(st->st_mode)) {
1513 const char * path = fsm->path;
1515 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1516 rc = fsmStage(fsm, FSM_VERIFY);
1518 if (rc == 0 && fsm->osuffix) {
1519 const char * opath = fsm->opath;
1520 fsm->opath = fsm->path;
1521 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1522 rc = fsmStage(fsm, FSM_RENAME);
1524 rpmMessage(RPMMESS_WARNING,
1525 _("%s saved as %s\n"), fsm->opath, fsm->path);
1526 fsm->path = _free(fsm->path);
1530 /*@-dependenttrans@*/
1532 /*@=dependenttrans@*/
1533 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1534 rc = expandRegular(fsm);
1535 } else if (S_ISDIR(st->st_mode)) {
1536 mode_t st_mode = st->st_mode;
1537 rc = fsmStage(fsm, FSM_VERIFY);
1538 if (rc == CPIOERR_LSTAT_FAILED) {
1539 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1540 st->st_mode |= 00700;
1541 rc = fsmStage(fsm, FSM_MKDIR);
1542 st->st_mode = st_mode; /* XXX restore st->st_mode */
1544 } else if (S_ISLNK(st->st_mode)) {
1545 const char * opath = fsm->opath;
1547 if ((st->st_size + 1) > fsm->rdsize) {
1548 rc = CPIOERR_HDR_SIZE;
1552 fsm->wrlen = st->st_size;
1553 rc = fsmStage(fsm, FSM_DREAD);
1554 if (!rc && fsm->rdnb != fsm->wrlen)
1555 rc = CPIOERR_READ_FAILED;
1558 fsm->wrbuf[st->st_size] = '\0';
1559 /* XXX symlink(fsm->opath, fsm->path) */
1560 /*@-dependenttrans@*/
1561 fsm->opath = fsm->wrbuf;
1562 /*@=dependenttrans@*/
1563 rc = fsmStage(fsm, FSM_VERIFY);
1564 if (rc == CPIOERR_LSTAT_FAILED)
1565 rc = fsmStage(fsm, FSM_SYMLINK);
1566 fsm->opath = opath; /* XXX restore fsm->path */
1567 } else if (S_ISFIFO(st->st_mode)) {
1568 mode_t st_mode = st->st_mode;
1569 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1570 rc = fsmStage(fsm, FSM_VERIFY);
1571 if (rc == CPIOERR_LSTAT_FAILED) {
1572 st->st_mode = 0000; /* XXX abuse st->st_mode */
1573 rc = fsmStage(fsm, FSM_MKFIFO);
1574 st->st_mode = st_mode; /* XXX restore st->st_mode */
1576 } else if (S_ISCHR(st->st_mode) ||
1577 S_ISBLK(st->st_mode) ||
1578 /*@-unrecog@*/ S_ISSOCK(st->st_mode) /*@=unrecog@*/)
1580 rc = fsmStage(fsm, FSM_VERIFY);
1581 if (rc == CPIOERR_LSTAT_FAILED)
1582 rc = fsmStage(fsm, FSM_MKNOD);
1584 rc = CPIOERR_UNKNOWN_FILETYPE;
1586 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1587 fsm->li->createdPath = fsm->li->linkIndex;
1588 rc = fsmMakeLinks(fsm);
1594 rc = fsmMakeLinks(fsm);
1596 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1597 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1598 rpmTransactionSet ts = fsmGetTs(fsm);
1599 TFI_t fi = fsmGetFi(fsm);
1600 if (ts && ts->notify && fi)
1601 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1602 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1603 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1609 if (fsm->goal == FSM_PKGINSTALL) {
1610 (void) fsmStage(fsm,
1611 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1613 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1615 (void) fsmStage(fsm, FSM_RMDIRS);
1619 if (fsm->failedFile && *fsm->failedFile == NULL)
1620 *fsm->failedFile = xstrdup(fsm->path);
1623 if (!fsm->postpone && fsm->commit) {
1624 if (fsm->goal == FSM_PKGINSTALL)
1625 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1626 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1627 if (fsm->goal == FSM_PKGCOMMIT)
1628 rc = fsmStage(fsm, FSM_COMMIT);
1629 if (fsm->goal == FSM_PKGERASE)
1630 rc = fsmStage(fsm, FSM_COMMIT);
1632 fsm->path = _free(fsm->path);
1633 fsm->opath = _free(fsm->opath);
1634 memset(st, 0, sizeof(*st));
1635 memset(ost, 0, sizeof(*ost));
1638 /* Rename pre-existing modified or unmanaged file. */
1639 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1640 const char * opath = fsm->opath;
1641 const char * path = fsm->path;
1642 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1643 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1645 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1646 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1648 rc = fsmStage(fsm, FSM_RENAME);
1650 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1651 fsm->opath, fsm->path);
1653 fsm->path = _free(fsm->path);
1655 fsm->opath = _free(fsm->opath);
1659 /* Remove erased files. */
1660 if (fsm->goal == FSM_PKGERASE) {
1661 if (fsm->action == FA_ERASE) {
1662 TFI_t fi = fsmGetFi(fsm);
1663 if (S_ISDIR(st->st_mode)) {
1664 rc = fsmStage(fsm, FSM_RMDIR);
1667 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1669 /* XXX make sure that build side permits %missingok on directories. */
1670 if (fsm->fflags & RPMFILE_MISSINGOK)
1671 /*@innerbreak@*/ break;
1673 /* XXX common error message. */
1675 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1676 _("%s rmdir of %s failed: Directory not empty\n"),
1677 fiTypeString(fi), fsm->path);
1678 /*@innerbreak@*/ break;
1681 (strict_erasures ? RPMERR_RMDIR : RPMDEBUG_RMDIR),
1682 _("%s rmdir of %s failed: %s\n"),
1683 fiTypeString(fi), fsm->path, strerror(errno));
1684 /*@innerbreak@*/ break;
1687 rc = fsmStage(fsm, FSM_UNLINK);
1689 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1691 (strict_erasures ? RPMERR_UNLINK : RPMDEBUG_UNLINK),
1692 _("%s unlink of %s failed: %s\n"),
1693 fiTypeString(fi), fsm->path, strerror(errno));
1696 /* XXX Failure to remove is not (yet) cause for failure. */
1697 if (!strict_erasures) rc = 0;
1701 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1702 /* Rename temporary to final file name. */
1703 if (!S_ISDIR(st->st_mode) &&
1704 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1706 fsm->opath = fsm->path;
1707 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1708 rc = fsmStage(fsm, FSM_RENAME);
1709 if (!rc && fsm->nsuffix) {
1710 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1711 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1712 (opath ? opath : ""), fsm->path);
1713 opath = _free(opath);
1715 fsm->opath = _free(fsm->opath);
1717 if (S_ISLNK(st->st_mode)) {
1718 if (!rc && !getuid())
1719 rc = fsmStage(fsm, FSM_LCHOWN);
1721 if (!rc && !getuid())
1722 rc = fsmStage(fsm, FSM_CHOWN);
1724 rc = fsmStage(fsm, FSM_CHMOD);
1726 time_t mtime = st->st_mtime;
1727 TFI_t fi = fsmGetFi(fsm);
1729 st->st_mtime = fi->fmtimes[fsm->ix];
1730 rc = fsmStage(fsm, FSM_UTIME);
1731 st->st_mtime = mtime;
1736 /* Notify on success. */
1737 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1738 else if (fsm->failedFile && *fsm->failedFile == NULL) {
1739 *fsm->failedFile = fsm->path;
1744 fsm->path = _free(fsm->path);
1746 /* Check for hard links missing from payload. */
1747 while ((fsm->li = fsm->links) != NULL) {
1748 fsm->links = fsm->li->next;
1749 fsm->li->next = NULL;
1750 if (fsm->goal == FSM_PKGINSTALL &&
1751 fsm->commit && fsm->li->linksLeft)
1753 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1754 if (fsm->li->filex[i] < 0) continue;
1755 rc = CPIOERR_MISSING_HARDLINK;
1756 if (fsm->failedFile && *fsm->failedFile == NULL) {
1757 fsm->ix = fsm->li->filex[i];
1758 if (!fsmStage(fsm, FSM_MAP)) {
1759 *fsm->failedFile = fsm->path;
1763 /*@loopbreak@*/ break;
1766 if (fsm->goal == FSM_PKGBUILD &&
1767 (fsm->mapFlags & CPIO_ALL_HARDLINKS))
1769 rc = CPIOERR_MISSING_HARDLINK;
1771 fsm->li = freeHardLink(fsm->li);
1773 fsm->ldn = _free(fsm->ldn);
1774 fsm->ldnalloc = fsm->ldnlen = 0;
1775 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1776 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1779 if (fsm->diskchecked && !fsm->exists) {
1780 rc = CPIOERR_LSTAT_FAILED;
1783 if (S_ISREG(st->st_mode)) {
1784 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1785 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1787 * XXX HP-UX (and other os'es) don't permit unlink on busy
1790 fsm->opath = fsm->path;
1792 rc = fsmStage(fsm, FSM_RENAME);
1794 (void) fsmStage(fsm, FSM_UNLINK);
1796 rc = CPIOERR_UNLINK_FAILED;
1797 fsm->path = fsm->opath;
1799 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1800 /*@notreached@*/ break;
1801 } else if (S_ISDIR(st->st_mode)) {
1802 if (S_ISDIR(ost->st_mode)) return 0;
1803 if (S_ISLNK(ost->st_mode)) {
1804 rc = fsmStage(fsm, FSM_STAT);
1805 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1808 if (S_ISDIR(ost->st_mode)) return 0;
1810 } else if (S_ISLNK(st->st_mode)) {
1811 if (S_ISLNK(ost->st_mode)) {
1812 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1813 rc = fsmStage(fsm, FSM_READLINK);
1816 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1818 } else if (S_ISFIFO(st->st_mode)) {
1819 if (S_ISFIFO(ost->st_mode)) return 0;
1820 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1821 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1822 (ost->st_rdev == st->st_rdev)) return 0;
1823 } else if (S_ISSOCK(st->st_mode)) {
1824 if (S_ISSOCK(ost->st_mode)) return 0;
1826 /* XXX shouldn't do this with commit/undo. */
1828 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1829 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1830 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1831 /*@notreached@*/ break;
1834 rc = Unlink(fsm->path);
1835 if (_fsm_debug && (stage & FSM_SYSCALL))
1836 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1837 fsm->path, (rc < 0 ? strerror(errno) : ""));
1838 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1841 rc = Rename(fsm->opath, fsm->path);
1842 if (_fsm_debug && (stage & FSM_SYSCALL))
1843 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1844 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1845 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1848 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1849 if (_fsm_debug && (stage & FSM_SYSCALL))
1850 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1851 fsm->path, (unsigned)(st->st_mode & 07777),
1852 (rc < 0 ? strerror(errno) : ""));
1853 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1856 rc = Rmdir(fsm->path);
1857 if (_fsm_debug && (stage & FSM_SYSCALL))
1858 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1859 fsm->path, (rc < 0 ? strerror(errno) : ""));
1860 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1863 rc = chown(fsm->path, st->st_uid, st->st_gid);
1864 if (_fsm_debug && (stage & FSM_SYSCALL))
1865 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1866 fsm->path, (int)st->st_uid, (int)st->st_gid,
1867 (rc < 0 ? strerror(errno) : ""));
1868 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1871 #if ! CHOWN_FOLLOWS_SYMLINK
1872 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1873 if (_fsm_debug && (stage & FSM_SYSCALL))
1874 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1875 fsm->path, (int)st->st_uid, (int)st->st_gid,
1876 (rc < 0 ? strerror(errno) : ""));
1877 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1881 rc = chmod(fsm->path, (st->st_mode & 07777));
1882 if (_fsm_debug && (stage & FSM_SYSCALL))
1883 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1884 fsm->path, (unsigned)(st->st_mode & 07777),
1885 (rc < 0 ? strerror(errno) : ""));
1886 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1889 { struct utimbuf stamp;
1890 stamp.actime = st->st_mtime;
1891 stamp.modtime = st->st_mtime;
1892 rc = utime(fsm->path, &stamp);
1893 if (_fsm_debug && (stage & FSM_SYSCALL))
1894 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1895 fsm->path, (unsigned)st->st_mtime,
1896 (rc < 0 ? strerror(errno) : ""));
1897 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1901 rc = symlink(fsm->opath, fsm->path);
1902 if (_fsm_debug && (stage & FSM_SYSCALL))
1903 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1904 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1905 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1908 rc = Link(fsm->opath, fsm->path);
1909 if (_fsm_debug && (stage & FSM_SYSCALL))
1910 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1911 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1912 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1915 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1916 if (_fsm_debug && (stage & FSM_SYSCALL))
1917 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1918 fsm->path, (unsigned)(st->st_mode & 07777),
1919 (rc < 0 ? strerror(errno) : ""));
1920 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1923 /*@-unrecog -portability @*/ /* FIX: check S_IFIFO or dev != 0 */
1924 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1925 /*@=unrecog =portability @*/
1926 if (_fsm_debug && (stage & FSM_SYSCALL))
1927 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1928 fsm->path, (unsigned)(st->st_mode & ~07777),
1929 (unsigned)st->st_rdev,
1930 (rc < 0 ? strerror(errno) : ""));
1931 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1934 rc = Lstat(fsm->path, ost);
1935 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1936 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1937 fsm->path, (rc < 0 ? strerror(errno) : ""));
1938 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1941 rc = Stat(fsm->path, ost);
1942 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1943 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1944 fsm->path, (rc < 0 ? strerror(errno) : ""));
1945 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1948 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1949 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1950 if (_fsm_debug && (stage & FSM_SYSCALL))
1951 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1952 fsm->path, (int)(fsm->rdsize -1), (rc < 0 ? strerror(errno) : ""));
1953 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1956 fsm->rdbuf[fsm->rdnb] = '\0';
1964 rc = fsmStage(fsm, FSM_HREAD);
1966 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1967 fsm->path = _free(fsm->path);
1968 rc = CPIOERR_HDR_TRAILER;
1971 rc = fsmStage(fsm, FSM_POS);
1974 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1975 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1976 rc = fsmStage(fsm, FSM_DREAD);
1978 /*@loopbreak@*/ break;
1982 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1985 (void) fsmStage(fsm, FSM_DREAD);
1989 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1991 memset(fsm->rdbuf, 0, left);
1992 /* XXX DWRITE uses rdnb for I/O length. */
1994 (void) fsmStage(fsm, FSM_DWRITE);
1998 rc = cpioTrailerWrite(fsm);
2001 rc = fsmStage(fsm, FSM_POS);
2003 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
2006 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
2009 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
2010 if (_fsm_debug && (stage & FSM_SYSCALL))
2011 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
2012 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
2013 (int)fsm->wrlen, (int)fsm->rdnb);
2014 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
2015 rc = CPIOERR_READ_FAILED;
2017 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
2020 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
2021 if (_fsm_debug && (stage & FSM_SYSCALL))
2022 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
2023 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
2024 (int)fsm->rdnb, (int)fsm->wrnb);
2025 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
2026 rc = CPIOERR_WRITE_FAILED;
2028 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
2032 fsm->rfd = Fopen(fsm->path, "r.ufdio");
2033 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
2034 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
2036 rc = CPIOERR_OPEN_FAILED;
2039 if (_fsm_debug && (stage & FSM_SYSCALL))
2040 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
2041 fsm->path, fsm->rfd, fsm->rdbuf);
2044 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
2045 if (_fsm_debug && (stage & FSM_SYSCALL))
2046 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
2047 cur, (int)fsm->rdlen, (int)fsm->rdnb);
2048 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
2049 rc = CPIOERR_READ_FAILED;
2053 if (_fsm_debug && (stage & FSM_SYSCALL))
2054 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
2055 (void) Fclose(fsm->rfd);
2061 fsm->wfd = Fopen(fsm->path, "w.ufdio");
2062 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
2063 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
2065 rc = CPIOERR_OPEN_FAILED;
2067 if (_fsm_debug && (stage & FSM_SYSCALL))
2068 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
2069 fsm->path, fsm->wfd, fsm->wrbuf);
2072 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
2073 if (_fsm_debug && (stage & FSM_SYSCALL))
2074 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
2075 cur, (int)fsm->rdnb, (int)fsm->wrnb);
2076 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
2077 rc = CPIOERR_WRITE_FAILED;
2081 if (_fsm_debug && (stage & FSM_SYSCALL))
2082 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
2083 (void) Fclose(fsm->wfd);
2093 if (!(stage & FSM_INTERNAL)) {
2094 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2100 /*@obserever@*/ const char *const fileActionString(fileAction a)
2103 case FA_UNKNOWN: return "unknown";
2104 case FA_CREATE: return "create";
2105 case FA_COPYOUT: return "copyout";
2106 case FA_COPYIN: return "copyin";
2107 case FA_BACKUP: return "backup";
2108 case FA_SAVE: return "save";
2109 case FA_SKIP: return "skip";
2110 case FA_ALTNAME: return "altname";
2111 case FA_ERASE: return "erase";
2112 case FA_SKIPNSTATE: return "skipnstate";
2113 case FA_SKIPNETSHARED: return "skipnetshared";
2114 case FA_SKIPMULTILIB: return "skipmultilib";
2115 default: return "???";
2120 /*@observer@*/ const char *const fileStageString(fileStage a) {
2122 case FSM_UNKNOWN: return "unknown";
2124 case FSM_PKGINSTALL:return "pkginstall";
2125 case FSM_PKGERASE: return "pkgerase";
2126 case FSM_PKGBUILD: return "pkgbuild";
2127 case FSM_PKGCOMMIT: return "pkgcommit";
2128 case FSM_PKGUNDO: return "pkgundo";
2130 case FSM_CREATE: return "create";
2131 case FSM_INIT: return "init";
2132 case FSM_MAP: return "map";
2133 case FSM_MKDIRS: return "mkdirs";
2134 case FSM_RMDIRS: return "rmdirs";
2135 case FSM_PRE: return "pre";
2136 case FSM_PROCESS: return "process";
2137 case FSM_POST: return "post";
2138 case FSM_MKLINKS: return "mklinks";
2139 case FSM_NOTIFY: return "notify";
2140 case FSM_UNDO: return "undo";
2141 case FSM_FINI: return "fini";
2142 case FSM_COMMIT: return "commit";
2143 case FSM_DESTROY: return "destroy";
2144 case FSM_VERIFY: return "verify";
2146 case FSM_UNLINK: return "Unlink";
2147 case FSM_RENAME: return "Rename";
2148 case FSM_MKDIR: return "Mkdir";
2149 case FSM_RMDIR: return "rmdir";
2150 case FSM_CHOWN: return "chown";
2151 case FSM_LCHOWN: return "lchown";
2152 case FSM_CHMOD: return "chmod";
2153 case FSM_UTIME: return "utime";
2154 case FSM_SYMLINK: return "symlink";
2155 case FSM_LINK: return "Link";
2156 case FSM_MKFIFO: return "mkfifo";
2157 case FSM_MKNOD: return "mknod";
2158 case FSM_LSTAT: return "Lstat";
2159 case FSM_STAT: return "Stat";
2160 case FSM_READLINK: return "Readlink";
2161 case FSM_CHROOT: return "chroot";
2163 case FSM_NEXT: return "next";
2164 case FSM_EAT: return "eat";
2165 case FSM_POS: return "pos";
2166 case FSM_PAD: return "pad";
2167 case FSM_TRAILER: return "trailer";
2168 case FSM_HREAD: return "hread";
2169 case FSM_HWRITE: return "hwrite";
2170 case FSM_DREAD: return "Fread";
2171 case FSM_DWRITE: return "Fwrite";
2173 case FSM_ROPEN: return "Fopen";
2174 case FSM_READ: return "Fread";
2175 case FSM_RCLOSE: return "Fclose";
2176 case FSM_WOPEN: return "Fopen";
2177 case FSM_WRITE: return "Fwrite";
2178 case FSM_WCLOSE: return "Fclose";
2180 default: return "???";