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 static int all_hardlinks_in_package = 0;
24 /* XXX Failure to remove is not (yet) cause for failure. */
25 /*@-exportlocal -exportheadervar@*/
26 int strict_erasures = 0;
27 /*@=exportlocal =exportheadervar@*/
29 rpmTransactionSet fsmGetTs(const FSM_t fsm) {
30 const FSMI_t iter = fsm->iter;
32 return (iter ? iter->ts : NULL);
36 TFI_t fsmGetFi(const FSM_t fsm)
38 const FSMI_t iter = fsm->iter;
40 return (iter ? iter->fi : NULL);
44 #define SUFFIX_RPMORIG ".rpmorig"
45 #define SUFFIX_RPMSAVE ".rpmsave"
46 #define SUFFIX_RPMNEW ".rpmnew"
49 * Build path to file from file info, ornamented with subdir and suffix.
50 * @param fsm file state machine data
51 * @param st file stat info
52 * @param subdir subdir to use (NULL disables)
53 * @param suffix suffix to use (NULL disables)
54 * @retval path to file
56 static /*@only@*//*@null@*/
57 const char * fsmFsPath(/*@special@*/ /*@null@*/ const FSM_t fsm,
58 /*@null@*/ const struct stat * st,
59 /*@null@*/ const char * subdir,
60 /*@null@*/ const char * suffix)
61 /*@uses fsm->dirName, fsm->baseName */
63 const char * s = NULL;
68 /*@-nullpass@*/ /* LCL: subdir/suffix != NULL */
69 nb = strlen(fsm->dirName) +
70 (st && subdir && !S_ISDIR(st->st_mode) ? strlen(subdir) : 0) +
71 (st && suffix && !S_ISDIR(st->st_mode) ? strlen(suffix) : 0) +
72 strlen(fsm->baseName) + 1;
74 t = stpcpy(t, fsm->dirName);
75 if (st && subdir && !S_ISDIR(st->st_mode))
76 t = stpcpy(t, subdir);
77 t = stpcpy(t, fsm->baseName);
78 if (st && suffix && !S_ISDIR(st->st_mode))
79 t = stpcpy(t, suffix);
86 * Destroy file info iterator.
87 * @param p file info iterator
90 static /*@null@*/ void * mapFreeIterator(/*@only@*//*@null@*/const void * p)
92 return _free((void *)p);
96 * Create file info iterator.
97 * @param a transaction set
98 * @param b transaction element file info
99 * @return file info iterator
102 mapInitIterator(/*@kept@*/ const void * a, /*@kept@*/ const void * b)
104 rpmTransactionSet ts = (void *)a;
105 TFI_t fi = (void *)b;
108 iter = xcalloc(1, sizeof(*iter));
113 iter->reverse = (fi->type == TR_REMOVED && fi->action != FA_COPYOUT);
114 iter->i = (iter->reverse ? (fi->fc - 1) : 0);
115 iter->isave = iter->i;
120 * Return next index into file info.
121 * @param a file info iterator
122 * @return next index, -1 on termination
124 static int mapNextIterator(void * a) {
126 const TFI_t fi = iter->fi;
130 if (iter->i >= 0) i = iter->i--;
132 if (iter->i < fi->fc) i = iter->i++;
140 static int cpioStrCmp(const void * a, const void * b)
143 const char * afn = *(const char **)a;
144 const char * bfn = *(const char **)b;
146 /* Match rpm-4.0 payloads with ./ prefixes. */
147 if (afn[0] == '.' && afn[1] == '/') afn += 2;
148 if (bfn[0] == '.' && bfn[1] == '/') bfn += 2;
150 /* If either path is absolute, make it relative. */
151 if (afn[0] == '/') afn += 1;
152 if (bfn[0] == '/') bfn += 1;
154 return strcmp(afn, bfn);
158 * Locate archive path in file info.
159 * @param a file info iterator
160 * @param fsmPath archive path
161 * @return index into file info, -1 if archive path was not found
163 static int mapFind(void * a, const char * fsmPath)
167 const TFI_t fi = iter->fi;
170 if (fi && fi->fc > 0 && fi->apath && fsmPath && *fsmPath) {
171 const char ** p = NULL;
173 if (fi->apath != NULL)
174 p = bsearch(&fsmPath, fi->apath, fi->fc, sizeof(fsmPath),
177 fprintf(stderr, "*** not mapped %s\n", fsmPath);
179 iter->i = p - fi->apath;
180 ix = mapNextIterator(iter);
187 * Directory name iterator.
189 typedef struct dnli_s {
190 /*@dependent@*/ TFI_t fi;
191 /*@only@*/ /*@null@*/ char * active;
198 * Destroy directory name iterator.
199 * @param a directory name iterator
200 * @retval NULL always
202 static /*@null@*/ void * dnlFreeIterator(/*@only@*//*@null@*/ const void * a)
206 DNLI_t dnli = (void *)a;
207 if (dnli->active) free(dnli->active);
214 static inline int dnlCount(const DNLI_t dnli)
217 return (dnli ? dnli->fi->dc : 0);
222 static inline int dnlIndex(const DNLI_t dnli)
225 return (dnli ? dnli->isave : -1);
229 * Create directory name iterator.
230 * @param fsm file state machine data
231 * @param reverse traverse directory names in reverse order?
232 * @return directory name iterator
234 static /*@only@*/ void * dnlInitIterator(/*@special@*/ const FSM_t fsm,
236 /*@uses fsm->iter @*/
239 TFI_t fi = fsmGetFi(fsm);
245 dnli = xcalloc(1, sizeof(*dnli));
247 dnli->reverse = reverse;
248 dnli->i = (reverse ? fi->dc : 0);
251 dnli->active = xcalloc(fi->dc, sizeof(*dnli->active));
253 /* Identify parent directories not skipped. */
254 for (i = 0; i < fi->fc; i++)
255 if (!XFA_SKIPPING(fi->actions[i])) dnli->active[fi->dil[i]] = 1;
257 /* Exclude parent directories that are explicitly included. */
258 for (i = 0; i < fi->fc; i++) {
259 int dil, dnlen, bnlen;
261 if (!S_ISDIR(fi->fmodes[i]))
265 dnlen = strlen(fi->dnl[dil]);
266 bnlen = strlen(fi->bnl[i]);
268 for (j = 0; j < fi->dc; j++) {
272 if (!dnli->active[j] || j == dil) continue;
275 if (jlen != (dnlen+bnlen+1)) continue;
276 if (strncmp(dnl, fi->dnl[dil], dnlen)) continue;
277 if (strncmp(dnl+dnlen, fi->bnl[i], bnlen)) continue;
278 if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
280 /* This directory is included in the package. */
282 /*@innerbreak@*/ break;
286 /* Print only once per package. */
289 for (i = 0; i < fi->dc; i++) {
290 if (!dnli->active[i]) continue;
293 rpmMessage(RPMMESS_DEBUG,
294 _("========= Directories not explictly included in package:\n"));
296 rpmMessage(RPMMESS_DEBUG, _("%9d %s\n"), i, fi->dnl[i]);
299 rpmMessage(RPMMESS_DEBUG, "=========\n");
306 * Return next directory name (from file info).
307 * @param dnli directory name iterator
308 * @return next directory name
310 static /*@observer@*/ const char * dnlNextIterator(/*@null@*/ DNLI_t dnli)
313 const char * dn = NULL;
321 i = (!dnli->reverse ? dnli->i++ : --dnli->i);
322 } while (i >= 0 && i < fi->dc && !dnli->active[i]);
324 if (i >= 0 && i < fi->dc)
334 * Save hard link in chain.
335 * @param fsm file state machine data
336 * @return Is chain only partially filled?
339 static int saveHardLink(/*@special@*/ /*@partial@*/ FSM_t fsm)
340 /*@uses fsm->links, fsm->ix, fsm->sb, fsm->goal, fsm->nsuffix @*/
341 /*@defines fsm->li @*/
342 /*@releases fsm->path @*/
345 struct stat * st = &fsm->sb;
350 /* Find hard link set. */
351 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
352 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
356 /* New hard link encountered, add new link to set. */
357 if (fsm->li == NULL) {
358 fsm->li = xcalloc(1, sizeof(*fsm->li));
359 fsm->li->next = NULL;
360 fsm->li->nlink = st->st_nlink;
361 fsm->li->dev = st->st_dev;
362 fsm->li->inode = st->st_ino;
363 fsm->li->linkIndex = -1;
364 fsm->li->createdPath = -1;
366 fsm->li->filex = xcalloc(st->st_nlink, sizeof(fsm->li->filex[0]));
367 memset(fsm->li->filex, -1, (st->st_nlink * sizeof(fsm->li->filex[0])));
368 fsm->li->nsuffix = xcalloc(st->st_nlink, sizeof(*fsm->li->nsuffix));
370 if (fsm->goal == FSM_PKGBUILD)
371 fsm->li->linksLeft = st->st_nlink;
372 if (fsm->goal == FSM_PKGINSTALL)
373 fsm->li->linksLeft = 0;
376 fsm->li->next = fsm->links;
378 fsm->links = fsm->li;
381 if (fsm->goal == FSM_PKGBUILD) --fsm->li->linksLeft;
382 fsm->li->filex[fsm->li->linksLeft] = fsm->ix;
383 /*@-observertrans -dependenttrans@*/
384 fsm->li->nsuffix[fsm->li->linksLeft] = fsm->nsuffix;
385 /*@=observertrans =dependenttrans@*/
386 if (fsm->goal == FSM_PKGINSTALL) fsm->li->linksLeft++;
388 if (fsm->goal == FSM_PKGBUILD)
389 return (fsm->li->linksLeft > 0);
391 if (fsm->goal != FSM_PKGINSTALL)
394 if (!(st->st_size || fsm->li->linksLeft == st->st_nlink))
397 /* Here come the bits, time to choose a non-skipped file name. */
398 { TFI_t fi = fsmGetFi(fsm);
400 for (j = fsm->li->linksLeft - 1; j >= 0; j--) {
401 ix = fsm->li->filex[j];
402 if (ix < 0 || XFA_SKIPPING(fi->actions[ix]))
408 /* Are all links skipped or not encountered yet? */
410 return 1; /* XXX W2DO? */
412 /* Save the non-skipped file name and map index. */
413 fsm->li->linkIndex = j;
414 fsm->path = _free(fsm->path);
416 rc = fsmStage(fsm, FSM_MAP);
417 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
424 * Destroy set of hard links.
425 * @param li set of hard links
427 static /*@null@*/ void * freeHardLink(/*@only@*/ /*@null@*/ struct hardLink * li)
431 li->nsuffix = _free(li->nsuffix); /* XXX elements are shared */
432 li->filex = _free(li->filex);
439 FSM_t fsm = xcalloc(1, sizeof(*fsm));
443 FSM_t freeFSM(FSM_t fsm)
446 fsm->path = _free(fsm->path);
447 while ((fsm->li = fsm->links) != NULL) {
448 fsm->links = fsm->li->next;
449 fsm->li->next = NULL;
450 fsm->li = freeHardLink(fsm->li);
452 fsm->dnlx = _free(fsm->dnlx);
453 fsm->ldn = _free(fsm->ldn);
454 fsm->iter = mapFreeIterator(fsm->iter);
459 int fsmSetup(FSM_t fsm, fileStage goal,
460 const rpmTransactionSet ts, const TFI_t fi, FD_t cfd,
461 unsigned int * archiveSize, const char ** failedFile)
468 fsm->cfd = fdLink(cfd, "persist (fsm)");
469 pos = fdGetCpioPos(fsm->cfd);
470 fdSetCpioPos(fsm->cfd, 0);
472 fsm->iter = mapInitIterator(ts, fi);
474 if (fsm->goal == FSM_PKGINSTALL) {
475 if (ts && ts->notify) {
476 (void)ts->notify(fi->h, RPMCALLBACK_INST_START, 0, fi->archiveSize,
477 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
482 fsm->archiveSize = archiveSize;
483 if (fsm->archiveSize)
484 *fsm->archiveSize = 0;
485 fsm->failedFile = failedFile;
487 *fsm->failedFile = NULL;
490 memset(fsm->sufbuf, 0, sizeof(fsm->sufbuf));
491 if (fsm->goal == FSM_PKGINSTALL) {
492 if (ts && ts->id > 0)
493 sprintf(fsm->sufbuf, ";%08x", (unsigned)ts->id);
497 rc = fsmStage(fsm, FSM_CREATE);
499 rc = fsmStage(fsm, fsm->goal);
501 if (fsm->archiveSize && rc == 0)
502 *fsm->archiveSize = (fdGetCpioPos(fsm->cfd) - pos);
507 int fsmTeardown(FSM_t fsm) {
511 rc = fsmStage(fsm, FSM_DESTROY);
513 fsm->iter = mapFreeIterator(fsm->iter);
515 fsm->cfd = fdFree(fsm->cfd, "persist (fsm)");
518 fsm->failedFile = NULL;
519 /*@-nullstate@*/ /* FIX: fsm->iter null annotation? */
524 int fsmMapPath(FSM_t fsm)
526 TFI_t fi = fsmGetFi(fsm); /* XXX const except for fstates */
533 fsm->action = FA_UNKNOWN;
537 if (fi && i >= 0 && i < fi->fc) {
539 fsm->astriplen = fi->astriplen;
540 fsm->action = (fi->actions ? fi->actions[i] : fi->action);
541 fsm->fflags = (fi->fflags ? fi->fflags[i] : fi->flags);
542 fsm->mapFlags = (fi->fmapflags ? fi->fmapflags[i] : fi->mapflags);
544 /* src rpms have simple base name in payload. */
545 fsm->dirName = fi->dnl[fi->dil[i]];
546 fsm->baseName = fi->bnl[i];
548 switch (fsm->action) {
551 case FA_SKIPMULTILIB: /* XXX RPMFILE_STATE_MULTILIB? */
560 assert(fi->type == TR_ADDED);
564 if (fi->fstates && fi->type == TR_ADDED)
565 fi->fstates[i] = RPMFILE_STATE_NOTINSTALLED;
568 case FA_SKIPNETSHARED:
569 if (fi->fstates && fi->type == TR_ADDED)
570 fi->fstates[i] = RPMFILE_STATE_NETSHARED;
574 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
577 fsm->osuffix = SUFFIX_RPMORIG;
580 fsm->osuffix = SUFFIX_RPMSAVE;
586 assert(fi->type == TR_ADDED);
587 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
588 fsm->nsuffix = SUFFIX_RPMNEW;
592 assert(fi->type == TR_ADDED);
593 if (!(fsm->fflags & RPMFILE_GHOST)) /* XXX Don't if %ghost file. */
594 fsm->osuffix = SUFFIX_RPMSAVE;
597 assert(fi->type == TR_REMOVED);
599 * XXX TODO: %ghost probably shouldn't be removed, but that changes
600 * legacy rpm behavior.
607 if ((fsm->mapFlags & CPIO_MAP_PATH) || fsm->nsuffix) {
608 const struct stat * st = &fsm->sb;
609 fsm->path = _free(fsm->path);
610 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
611 fsm->path = fsmFsPath(fsm, st, fsm->subdir,
612 (fsm->suffix ? fsm->suffix : fsm->nsuffix));
619 int fsmMapAttrs(FSM_t fsm)
621 struct stat * st = &fsm->sb;
622 TFI_t fi = fsmGetFi(fsm);
625 if (fi && i >= 0 && i < fi->fc) {
627 (S_ISDIR(st->st_mode) ? fi->dperms : fi->fperms);
629 (fi->fmodes ? fi->fmodes[i] : perms);
631 (fi->fuids ? fi->fuids[i] : fi->uid); /* XXX chmod u-s */
633 (fi->fgids ? fi->fgids[i] : fi->gid); /* XXX chmod g-s */
635 if (fsm->mapFlags & CPIO_MAP_MODE)
636 st->st_mode = (st->st_mode & S_IFMT) | finalMode;
637 if (fsm->mapFlags & CPIO_MAP_UID)
638 st->st_uid = finalUid;
639 if (fsm->mapFlags & CPIO_MAP_GID)
640 st->st_gid = finalGid;
642 fsm->fmd5sum = (fi->fmd5s ? fi->fmd5s[i] : NULL);
649 * Create file from payload stream.
650 * @todo Legacy: support brokenEndian MD5 checks?
651 * @param fsm file state machine data
652 * @return 0 on success
654 static int expandRegular(/*@special@*/ FSM_t fsm)
656 /*@modifies fsm, fileSystem @*/
658 const char * fmd5sum;
659 const struct stat * st = &fsm->sb;
660 int left = st->st_size;
663 rc = fsmStage(fsm, FSM_WOPEN);
667 /* XXX md5sum's will break on repackaging that includes modified files. */
668 fmd5sum = fsm->fmd5sum;
670 /* XXX This doesn't support brokenEndian checks. */
671 if (st->st_size > 0 && fmd5sum)
672 fdInitMD5(fsm->wfd, 0);
676 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
677 rc = fsmStage(fsm, FSM_DREAD);
681 rc = fsmStage(fsm, FSM_WRITE);
687 /* don't call this with fileSize == fileComplete */
689 (void) fsmStage(fsm, FSM_NOTIFY);
692 if (st->st_size > 0 && fmd5sum) {
693 const char * md5sum = NULL;
695 (void) Fflush(fsm->wfd);
696 fdFiniMD5(fsm->wfd, (void **)&md5sum, NULL, 1);
698 if (md5sum == NULL) {
699 rc = CPIOERR_MD5SUM_MISMATCH;
701 if (strcmp(md5sum, fmd5sum))
702 rc = CPIOERR_MD5SUM_MISMATCH;
703 md5sum = _free(md5sum);
708 (void) fsmStage(fsm, FSM_WCLOSE);
713 * Write next item to payload stream.
714 * @param fsm file state machine data
715 * @param writeData should data be written?
716 * @return 0 on success
718 static int writeFile(/*@special@*/ FSM_t fsm, int writeData)
719 /*@uses fsm->path, fsm->opath, fsm->sb, fsm->osb, fsm->cfd @*/
720 /*@modifies fsm, fileSystem @*/
722 const char * path = fsm->path;
723 const char * opath = fsm->opath;
724 struct stat * st = &fsm->sb;
725 struct stat * ost = &fsm->osb;
726 size_t pos = fdGetCpioPos(fsm->cfd);
727 char * symbuf = NULL;
731 st->st_size = (writeData ? ost->st_size : 0);
732 if (S_ISDIR(st->st_mode)) {
734 } else if (S_ISLNK(st->st_mode)) {
736 * While linux puts the size of a symlink in the st_size field,
737 * I don't think that's a specified standard.
739 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
740 rc = fsmStage(fsm, FSM_READLINK);
742 st->st_size = fsm->rdnb;
743 symbuf = alloca_strdup(fsm->rdbuf); /* XXX save readlink return. */
746 if (fsm->mapFlags & CPIO_MAP_ABSOLUTE) {
747 int nb = strlen(fsm->dirName) + strlen(fsm->baseName) + sizeof(".");
748 char * t = alloca(nb);
751 if (fsm->mapFlags & CPIO_MAP_ADDDOT)
753 t = stpcpy( stpcpy(t, fsm->dirName), fsm->baseName);
754 } else if (fsm->mapFlags & CPIO_MAP_PATH) {
755 TFI_t fi = fsmGetFi(fsm);
757 (fi->apath ? fi->apath[fsm->ix] + fi->striplen : fi->bnl[fsm->ix]);
760 rc = fsmStage(fsm, FSM_HWRITE);
764 if (writeData && S_ISREG(st->st_mode)) {
767 void * mapped = (void *)-1;
771 rc = fsmStage(fsm, FSM_ROPEN);
774 /* XXX unbuffered mmap generates *lots* of fdio debugging */
777 mapped = mmap(NULL, st->st_size, PROT_READ, MAP_SHARED, Fileno(fsm->rfd), 0);
778 if (mapped != (void *)-1) {
780 fsm->rdbuf = (char *) mapped;
781 fsm->rdlen = nmapped = st->st_size;
789 if (mapped != (void *)-1) {
794 fsm->rdlen = (left > fsm->rdsize ? fsm->rdsize : left),
795 rc = fsmStage(fsm, FSM_READ);
799 /* XXX DWRITE uses rdnb for I/O length. */
800 rc = fsmStage(fsm, FSM_DWRITE);
807 if (mapped != (void *)-1) {
808 /*@-noeffect@*/ (void) munmap(mapped, nmapped) /*@=noeffect@*/;
813 } else if (writeData && S_ISLNK(st->st_mode)) {
814 /* XXX DWRITE uses rdnb for I/O length. */
815 strcpy(fsm->rdbuf, symbuf); /* XXX restore readlink buffer. */
816 fsm->rdnb = strlen(symbuf);
817 rc = fsmStage(fsm, FSM_DWRITE);
821 rc = fsmStage(fsm, FSM_PAD);
824 { const rpmTransactionSet ts = fsmGetTs(fsm);
825 TFI_t fi = fsmGetFi(fsm);
826 if (ts && fi && ts->notify) {
827 size_t size = (fdGetCpioPos(fsm->cfd) - pos);
828 /*@-modunconnomods@*/
829 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS, size, size,
830 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
831 /*@=modunconnomods@*/
839 (void) fsmStage(fsm, FSM_RCLOSE);
840 /*@-dependenttrans@*/
843 /*@=dependenttrans@*/
848 * Write set of linked files to payload stream.
849 * @param fsm file state machine data
850 * @return 0 on success
852 static int writeLinkedFile(/*@special@*/ FSM_t fsm)
853 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->li, fsm->failedFile @*/
854 /*@modifies fsm, fileSystem @*/
856 const char * path = fsm->path;
857 const char * nsuffix = fsm->nsuffix;
858 int iterIndex = fsm->ix;
867 for (i = fsm->li->nlink - 1; i >= 0; i--) {
868 if (fsm->li->filex[i] < 0) continue;
870 fsm->ix = fsm->li->filex[i];
871 rc = fsmStage(fsm, FSM_MAP);
873 /* Write data after last link. */
874 rc = writeFile(fsm, (i == 0));
875 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
877 *fsm->failedFile = xstrdup(fsm->path);
880 fsm->path = _free(fsm->path);
881 fsm->li->filex[i] = -1;
885 fsm->nsuffix = nsuffix;
891 * Create pending hard links to existing file.
892 * @param fsm file state machine data
893 * @return 0 on success
895 static int fsmMakeLinks(/*@special@*/ FSM_t fsm)
896 /*@uses fsm->path, fsm->opath, fsm->nsuffix, fsm->ix, fsm->li @*/
897 /*@modifies fsm, fileSystem @*/
899 const char * path = fsm->path;
900 const char * opath = fsm->opath;
901 const char * nsuffix = fsm->nsuffix;
902 int iterIndex = fsm->ix;
912 fsm->ix = fsm->li->filex[fsm->li->createdPath];
913 rc = fsmStage(fsm, FSM_MAP);
914 fsm->opath = fsm->path;
916 for (i = 0; i < fsm->li->nlink; i++) {
917 if (fsm->li->filex[i] < 0) continue;
918 if (i == fsm->li->createdPath) continue;
920 fsm->ix = fsm->li->filex[i];
921 rc = fsmStage(fsm, FSM_MAP);
922 rc = fsmStage(fsm, FSM_VERIFY);
924 if (rc != CPIOERR_LSTAT_FAILED) break;
926 /* XXX link(fsm->opath, fsm->path) */
927 rc = fsmStage(fsm, FSM_LINK);
928 if (fsm->failedFile && rc != 0 && *fsm->failedFile == NULL) {
930 *fsm->failedFile = xstrdup(fsm->path);
933 fsm->li->linksLeft--;
935 fsm->opath = _free(fsm->opath);
938 fsm->nsuffix = nsuffix;
945 * Commit hard linked file set atomically.
946 * @param fsm file state machine data
947 * @return 0 on success
949 static int fsmCommitLinks(/*@special@*/ FSM_t fsm)
950 /*@uses fsm->path, fsm->nsuffix, fsm->ix, fsm->sb,
951 fsm->li, fsm->links @*/
952 /*@modifies fsm, fileSystem @*/
954 const char * path = fsm->path;
955 const char * nsuffix = fsm->nsuffix;
956 int iterIndex = fsm->ix;
957 struct stat * st = &fsm->sb;
965 for (fsm->li = fsm->links; fsm->li; fsm->li = fsm->li->next) {
966 if (fsm->li->inode == st->st_ino && fsm->li->dev == st->st_dev)
970 for (i = 0; i < fsm->li->nlink; i++) {
971 if (fsm->li->filex[i] < 0) continue;
972 fsm->ix = fsm->li->filex[i];
973 rc = fsmStage(fsm, FSM_MAP);
974 rc = fsmStage(fsm, FSM_COMMIT);
975 fsm->path = _free(fsm->path);
976 fsm->li->filex[i] = -1;
980 fsm->nsuffix = nsuffix;
986 * Remove (if created) directories not explicitly included in package.
987 * @param fsm file state machine data
988 * @return 0 on success
991 static int fsmRmdirs(/*@special@*/ FSM_t fsm)
992 /*@uses fsm->path, fsm->dnlx, fsm->ldn, fsm->rdbuf, fsm->iter @*/
993 /*@modifies fsm, fileSystem @*/
995 const char * path = fsm->path;
996 void * dnli = dnlInitIterator(fsm, 1);
997 char * dn = fsm->rdbuf;
998 int dc = dnlCount(dnli);
1003 /*@-observertrans -dependenttrans@*/
1004 if (fsm->ldn != NULL && fsm->dnlx != NULL)
1005 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1006 int dnlen = strlen(fsm->path);
1009 dc = dnlIndex(dnli);
1010 if (fsm->dnlx[dc] < 1 || fsm->dnlx[dc] >= dnlen)
1013 /* Copy to avoid const on fsm->path. */
1014 te = stpcpy(dn, fsm->path) - 1;
1017 /* Remove generated directories. */
1021 rc = fsmStage(fsm, FSM_RMDIR);
1025 /*@innerbreak@*/ break;
1027 } while ((te - dn) > fsm->dnlx[dc]);
1029 dnli = dnlFreeIterator(dnli);
1030 /*@=observertrans =dependenttrans@*/
1038 * Create (if necessary) directories not explicitly included in package.
1039 * @param fsm file state machine data
1040 * @return 0 on success
1042 static int fsmMkdirs(/*@special@*/ FSM_t fsm)
1043 /*@uses fsm->path, fsm->sb, fsm->osb, fsm->rdbuf, fsm->iter,
1044 fsm->ldn, fsm->ldnlen, fsm->ldnalloc @*/
1045 /*@defines fsm->dnlx, fsm->ldn @*/
1046 /*@modifies fsm, fileSystem @*/
1048 struct stat * st = &fsm->sb;
1049 struct stat * ost = &fsm->osb;
1050 const char * path = fsm->path;
1051 mode_t st_mode = st->st_mode;
1052 void * dnli = dnlInitIterator(fsm, 0);
1053 char * dn = fsm->rdbuf;
1054 int dc = dnlCount(dnli);
1061 fsm->dnlx = (dc ? xcalloc(dc, sizeof(*fsm->dnlx)) : NULL);
1062 /*@-observertrans -dependenttrans@*/
1063 if (fsm->dnlx != NULL)
1064 while ((fsm->path = dnlNextIterator(dnli)) != NULL) {
1065 int dnlen = strlen(fsm->path);
1068 dc = dnlIndex(dnli);
1069 if (dc < 0) continue;
1070 fsm->dnlx[dc] = dnlen;
1074 /*@-compdef -nullpass@*/ /* FIX: fsm->ldn not defined ??? */
1075 if (dnlen <= fsm->ldnlen && !strcmp(fsm->path, fsm->ldn))
1077 /*@=compdef =nullpass@*/
1079 /* Copy to avoid const on fsm->path. */
1080 (void) stpcpy(dn, fsm->path);
1083 /* Assume '/' directory exists, "mkdir -p" for others if non-existent */
1084 for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
1085 if (*te != '/') continue;
1089 /* Already validated? */
1090 /*@-usedef -compdef -nullpass -nullderef@*/
1091 if (i < fsm->ldnlen &&
1092 (fsm->ldn[i] == '/' || fsm->ldn[i] == '\0') &&
1093 !strncmp(fsm->path, fsm->ldn, i))
1096 /* Move pre-existing path marker forward. */
1097 fsm->dnlx[dc] = (te - dn);
1100 /*@=usedef =compdef =nullpass =nullderef@*/
1102 /* Validate next component of path. */
1103 rc = fsmStage(fsm, FSM_LSTAT);
1106 /* Directory already exists? */
1107 if (rc == 0 && S_ISDIR(ost->st_mode)) {
1108 /* Move pre-existing path marker forward. */
1109 fsm->dnlx[dc] = (te - dn);
1110 } else if (rc == CPIOERR_LSTAT_FAILED) {
1111 TFI_t fi = fsmGetFi(fsm);
1113 st->st_mode = S_IFDIR | (fi->dperms & 07777);
1114 rc = fsmStage(fsm, FSM_MKDIR);
1116 rpmMessage(RPMMESS_WARNING,
1117 _("%s directory created with perms %04o.\n"),
1118 fsm->path, (unsigned)(st->st_mode & 07777));
1122 /*@innerbreak@*/ break;
1126 /* Save last validated path. */
1127 if (fsm->ldnalloc < (dnlen + 1)) {
1128 fsm->ldnalloc = dnlen + 100;
1129 fsm->ldn = xrealloc(fsm->ldn, fsm->ldnalloc);
1131 if (fsm->ldn != NULL) { /* XXX can't happen */
1132 strcpy(fsm->ldn, fsm->path);
1133 fsm->ldnlen = dnlen;
1136 dnli = dnlFreeIterator(dnli);
1137 /*@=observertrans =dependenttrans@*/
1140 st->st_mode = st_mode; /* XXX restore st->st_mode */
1146 int fsmStage(FSM_t fsm, fileStage stage)
1149 fileStage prevStage = fsm->stage;
1150 const char * const prev = fileStageString(prevStage);
1152 static int modulo = 4;
1153 const char * const cur = fileStageString(stage);
1154 struct stat * st = &fsm->sb;
1155 struct stat * ost = &fsm->osb;
1156 int saveerrno = errno;
1161 #define _fafilter(_a) \
1162 (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
1163 ? fileActionString(_a) : "")
1165 if (stage & FSM_DEAD) {
1167 } else if (stage & FSM_INTERNAL) {
1168 if (_fsm_debug && !(stage & FSM_SYSCALL))
1169 rpmMessage(RPMMESS_DEBUG, " %8s %06o%3d (%4d,%4d)%10d %s %s\n",
1171 (unsigned)st->st_mode, (int)st->st_nlink,
1172 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1173 (fsm->path ? fsm->path : ""),
1174 _fafilter(fsm->action));
1177 if (_fsm_debug || !(stage & FSM_VERBOSE))
1178 rpmMessage(RPMMESS_DEBUG, "%-8s %06o%3d (%4d,%4d)%10d %s %s\n",
1180 (unsigned)st->st_mode, (int)st->st_nlink,
1181 (int)st->st_uid, (int)st->st_gid, (int)st->st_size,
1182 (fsm->path ? fsm->path + fsm->astriplen : ""),
1183 _fafilter(fsm->action));
1190 case FSM_PKGINSTALL:
1192 /* Clean fsm, free'ing memory. Read next archive header. */
1193 rc = fsmStage(fsm, FSM_INIT);
1195 /* Exit on end-of-payload. */
1196 if (rc == CPIOERR_HDR_TRAILER) {
1198 /*@loopbreak@*/ break;
1201 /* Exit on error. */
1204 (void) fsmStage(fsm, FSM_UNDO);
1205 /*@loopbreak@*/ break;
1208 /* Extract file from archive. */
1209 rc = fsmStage(fsm, FSM_PROCESS);
1211 (void) fsmStage(fsm, FSM_UNDO);
1212 /*@loopbreak@*/ break;
1215 /* Notify on success. */
1216 (void) fsmStage(fsm, FSM_NOTIFY);
1218 if (fsmStage(fsm, FSM_FINI))
1219 /*@loopbreak@*/ break;
1225 /* Clean fsm, free'ing memory. */
1226 rc = fsmStage(fsm, FSM_INIT);
1228 /* Exit on end-of-payload. */
1229 if (rc == CPIOERR_HDR_TRAILER) {
1231 /*@loopbreak@*/ break;
1234 /* Rename/erase next item. */
1235 if (fsmStage(fsm, FSM_FINI))
1236 /*@loopbreak@*/ break;
1242 rc = fsmStage(fsm, FSM_INIT);
1244 /* Exit on end-of-payload. */
1245 if (rc == CPIOERR_HDR_TRAILER) {
1247 /*@loopbreak@*/ break;
1250 /* Exit on error. */
1253 (void) fsmStage(fsm, FSM_UNDO);
1254 /*@loopbreak@*/ break;
1257 /* Copy file into archive. */
1258 rc = fsmStage(fsm, FSM_PROCESS);
1260 (void) fsmStage(fsm, FSM_UNDO);
1261 /*@loopbreak@*/ break;
1264 if (fsmStage(fsm, FSM_FINI))
1265 /*@loopbreak@*/ break;
1268 /* Flush partial sets of hard linked files. */
1269 if (!all_hardlinks_in_package) {
1270 while ((fsm->li = fsm->links) != NULL) {
1271 fsm->links = fsm->li->next;
1272 fsm->li->next = NULL;
1274 rc = writeLinkedFile(fsm);
1275 fsm->li = freeHardLink(fsm->li);
1280 rc = fsmStage(fsm, FSM_TRAILER);
1284 { rpmTransactionSet ts = fsmGetTs(fsm);
1285 #define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
1286 fsm->commit = ((ts && (ts->transFlags & _tsmask) &&
1287 fsm->goal != FSM_PKGCOMMIT) ? 0 : 1);
1290 fsm->path = _free(fsm->path);
1291 fsm->opath = _free(fsm->opath);
1292 fsm->dnlx = _free(fsm->dnlx);
1294 fsm->ldn = _free(fsm->ldn);
1295 fsm->ldnalloc = fsm->ldnlen = 0;
1297 fsm->rdsize = fsm->wrsize = 0;
1298 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1299 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1300 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1301 fsm->rdsize = 8 * BUFSIZ;
1302 fsm->rdbuf = fsm->rdb = xmalloc(fsm->rdsize);
1303 fsm->wrsize = 8 * BUFSIZ;
1304 fsm->wrbuf = fsm->wrb = xmalloc(fsm->wrsize);
1307 fsm->mkdirsdone = 0;
1311 errno = 0; /* XXX get rid of EBADF */
1313 /* Detect and create directories not explicitly in package. */
1314 if (fsm->goal == FSM_PKGINSTALL) {
1315 rc = fsmStage(fsm, FSM_MKDIRS);
1316 if (!rc) fsm->mkdirsdone = 1;
1321 fsm->path = _free(fsm->path);
1323 fsm->diskchecked = fsm->exists = 0;
1325 fsm->suffix = (fsm->sufbuf[0] != '\0' ? fsm->sufbuf : NULL);
1326 fsm->action = FA_UNKNOWN;
1327 fsm->osuffix = NULL;
1328 fsm->nsuffix = NULL;
1330 if (fsm->goal == FSM_PKGINSTALL) {
1331 /* Read next header from payload, checking for end-of-payload. */
1332 rc = fsmStage(fsm, FSM_NEXT);
1336 /* Identify mapping index. */
1337 fsm->ix = ((fsm->goal == FSM_PKGINSTALL)
1338 ? mapFind(fsm->iter, fsm->path) : mapNextIterator(fsm->iter));
1340 /* On non-install, detect end-of-loop. */
1341 if (fsm->goal != FSM_PKGINSTALL && fsm->ix < 0) {
1342 rc = CPIOERR_HDR_TRAILER;
1346 /* On non-install, mode must be known so that dirs don't get suffix. */
1347 if (fsm->goal != FSM_PKGINSTALL) {
1348 TFI_t fi = fsmGetFi(fsm);
1349 st->st_mode = fi->fmodes[fsm->ix];
1352 /* Generate file path. */
1353 rc = fsmStage(fsm, FSM_MAP);
1356 /* Perform lstat/stat for disk file. */
1357 if (fsm->path != NULL) {
1358 rc = fsmStage(fsm, (!(fsm->mapFlags & CPIO_FOLLOW_SYMLINKS)
1359 ? FSM_LSTAT : FSM_STAT));
1360 if (rc == CPIOERR_LSTAT_FAILED && errno == ENOENT) {
1364 } else if (rc == 0) {
1368 /* Skip %ghost files on build. */
1371 fsm->diskchecked = 1;
1374 /* On non-install, the disk file stat is what's remapped. */
1375 if (fsm->goal != FSM_PKGINSTALL)
1376 *st = *ost; /* structure assignment */
1378 /* Remap file perms, owner, and group. */
1379 rc = fsmMapAttrs(fsm);
1382 fsm->postpone = XFA_SKIPPING(fsm->action);
1383 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1385 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1386 fsm->postpone = saveHardLink(fsm);
1393 rc = fsmMapPath(fsm);
1396 rc = fsmMkdirs(fsm);
1400 rc = fsmRmdirs(fsm);
1403 if (fsm->postpone) {
1404 if (fsm->goal == FSM_PKGINSTALL)
1405 rc = fsmStage(fsm, FSM_EAT);
1409 if (fsm->goal == FSM_PKGBUILD) {
1410 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1411 struct hardLink * li, * prev;
1412 rc = writeLinkedFile(fsm);
1413 if (rc) break; /* W2DO? */
1415 for (li = fsm->links, prev = NULL; li; prev = li, li = li->next)
1417 /*@loopbreak@*/ break;
1420 fsm->links = fsm->li->next;
1422 prev->next = fsm->li->next;
1423 fsm->li->next = NULL;
1424 fsm->li = freeHardLink(fsm->li);
1426 rc = writeFile(fsm, 1);
1431 if (fsm->goal != FSM_PKGINSTALL)
1434 if (S_ISREG(st->st_mode)) {
1435 const char * path = fsm->path;
1437 fsm->path = fsmFsPath(fsm, st, NULL, NULL);
1438 rc = fsmStage(fsm, FSM_VERIFY);
1440 if (rc == 0 && fsm->osuffix) {
1441 const char * opath = fsm->opath;
1442 fsm->opath = fsm->path;
1443 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1444 rc = fsmStage(fsm, FSM_RENAME);
1446 rpmMessage(RPMMESS_WARNING,
1447 _("%s saved as %s\n"), fsm->opath, fsm->path);
1448 fsm->path = _free(fsm->path);
1452 /*@-dependenttrans@*/
1454 /*@=dependenttrans@*/
1455 if (rc != CPIOERR_LSTAT_FAILED) return rc;
1456 rc = expandRegular(fsm);
1457 } else if (S_ISDIR(st->st_mode)) {
1458 mode_t st_mode = st->st_mode;
1459 rc = fsmStage(fsm, FSM_VERIFY);
1460 if (rc == CPIOERR_LSTAT_FAILED) {
1461 st->st_mode &= ~07777; /* XXX abuse st->st_mode */
1462 st->st_mode |= 00700;
1463 rc = fsmStage(fsm, FSM_MKDIR);
1464 st->st_mode = st_mode; /* XXX restore st->st_mode */
1466 } else if (S_ISLNK(st->st_mode)) {
1467 const char * opath = fsm->opath;
1469 if ((st->st_size + 1) > fsm->rdsize) {
1470 rc = CPIOERR_HDR_SIZE;
1474 fsm->wrlen = st->st_size;
1475 rc = fsmStage(fsm, FSM_DREAD);
1476 if (!rc && fsm->rdnb != fsm->wrlen)
1477 rc = CPIOERR_READ_FAILED;
1480 fsm->wrbuf[st->st_size] = '\0';
1481 /* XXX symlink(fsm->opath, fsm->path) */
1482 /*@-dependenttrans@*/
1483 fsm->opath = fsm->wrbuf;
1484 /*@=dependenttrans@*/
1485 rc = fsmStage(fsm, FSM_VERIFY);
1486 if (rc == CPIOERR_LSTAT_FAILED)
1487 rc = fsmStage(fsm, FSM_SYMLINK);
1488 fsm->opath = opath; /* XXX restore fsm->path */
1489 } else if (S_ISFIFO(st->st_mode)) {
1490 mode_t st_mode = st->st_mode;
1491 /* This mimics cpio S_ISSOCK() behavior but probably isnt' right */
1492 rc = fsmStage(fsm, FSM_VERIFY);
1493 if (rc == CPIOERR_LSTAT_FAILED) {
1494 st->st_mode = 0000; /* XXX abuse st->st_mode */
1495 rc = fsmStage(fsm, FSM_MKFIFO);
1496 st->st_mode = st_mode; /* XXX restore st->st_mode */
1498 } else if (S_ISCHR(st->st_mode) ||
1499 S_ISBLK(st->st_mode) ||
1500 S_ISSOCK(st->st_mode))
1502 rc = fsmStage(fsm, FSM_VERIFY);
1503 if (rc == CPIOERR_LSTAT_FAILED)
1504 rc = fsmStage(fsm, FSM_MKNOD);
1506 rc = CPIOERR_UNKNOWN_FILETYPE;
1508 if (!S_ISDIR(st->st_mode) && st->st_nlink > 1) {
1509 fsm->li->createdPath = fsm->li->linkIndex;
1510 rc = fsmMakeLinks(fsm);
1516 rc = fsmMakeLinks(fsm);
1518 case FSM_NOTIFY: /* XXX move from fsm to psm -> tsm */
1519 if (fsm->goal == FSM_PKGINSTALL || fsm->goal == FSM_PKGBUILD) {
1520 rpmTransactionSet ts = fsmGetTs(fsm);
1521 TFI_t fi = fsmGetFi(fsm);
1522 if (ts && ts->notify && fi)
1523 (void)ts->notify(fi->h, RPMCALLBACK_INST_PROGRESS,
1524 fdGetCpioPos(fsm->cfd), fi->archiveSize,
1525 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
1531 if (fsm->goal == FSM_PKGINSTALL) {
1532 (void) fsmStage(fsm,
1533 (S_ISDIR(st->st_mode) ? FSM_RMDIR : FSM_UNLINK));
1535 #ifdef NOTYET /* XXX remove only dirs just created, not all. */
1537 (void) fsmStage(fsm, FSM_RMDIRS);
1541 if (fsm->failedFile && *fsm->failedFile == NULL)
1542 *fsm->failedFile = xstrdup(fsm->path);
1545 if (!fsm->postpone && fsm->commit) {
1546 if (fsm->goal == FSM_PKGINSTALL)
1547 rc = ((!S_ISDIR(st->st_mode) && st->st_nlink > 1)
1548 ? fsmCommitLinks(fsm) : fsmStage(fsm, FSM_COMMIT));
1549 if (fsm->goal == FSM_PKGCOMMIT)
1550 rc = fsmStage(fsm, FSM_COMMIT);
1551 if (fsm->goal == FSM_PKGERASE)
1552 rc = fsmStage(fsm, FSM_COMMIT);
1554 fsm->path = _free(fsm->path);
1555 fsm->opath = _free(fsm->opath);
1556 memset(st, 0, sizeof(*st));
1557 memset(ost, 0, sizeof(*ost));
1560 /* Rename pre-existing modified or unmanaged file. */
1561 if (fsm->diskchecked && fsm->exists && fsm->osuffix) {
1562 const char * opath = fsm->opath;
1563 const char * path = fsm->path;
1564 /*@-nullstate@*/ /* FIX: fsm->opath null annotation? */
1565 fsm->opath = fsmFsPath(fsm, st, NULL, NULL);
1567 /*@-nullstate@*/ /* FIX: fsm->path null annotation? */
1568 fsm->path = fsmFsPath(fsm, st, NULL, fsm->osuffix);
1570 rc = fsmStage(fsm, FSM_RENAME);
1572 rpmMessage(RPMMESS_WARNING, _("%s saved as %s\n"),
1573 fsm->opath, fsm->path);
1575 fsm->path = _free(fsm->path);
1577 fsm->opath = _free(fsm->opath);
1581 /* Remove erased files. */
1582 if (fsm->goal == FSM_PKGERASE) {
1583 if (fsm->action == FA_ERASE) {
1584 TFI_t fi = fsmGetFi(fsm);
1585 if (S_ISDIR(st->st_mode)) {
1586 rc = fsmStage(fsm, FSM_RMDIR);
1589 case ENOENT: /* XXX rmdir("/") linux 2.2.x kernel hack */
1591 /* XXX make sure that build side permits %missingok on directories. */
1592 if (fsm->fflags & RPMFILE_MISSINGOK)
1595 /* XXX common error message. */
1597 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1598 _("%s rmdir of %s failed: Directory not empty\n"),
1599 fiTypeString(fi), fsm->path);
1603 (strict_erasures ? RPMERR_RMDIR : RPMWARN_RMDIR),
1604 _("%s rmdir of %s failed: %s\n"),
1605 fiTypeString(fi), fsm->path, strerror(errno));
1609 rc = fsmStage(fsm, FSM_UNLINK);
1611 if (!(errno == ENOENT && (fsm->fflags & RPMFILE_MISSINGOK)))
1613 (strict_erasures ? RPMERR_UNLINK : RPMWARN_UNLINK),
1614 _("%s unlink of %s failed: %s\n"),
1615 fiTypeString(fi), fsm->path, strerror(errno));
1618 /* XXX Failure to remove is not (yet) cause for failure. */
1619 if (!strict_erasures) rc = 0;
1623 if (!S_ISSOCK(st->st_mode)) { /* XXX /dev/log et al are skipped */
1624 /* Rename temporary to final file name. */
1625 if (!S_ISDIR(st->st_mode) &&
1626 (fsm->subdir || fsm->suffix || fsm->nsuffix))
1628 fsm->opath = fsm->path;
1629 fsm->path = fsmFsPath(fsm, st, NULL, fsm->nsuffix);
1630 rc = fsmStage(fsm, FSM_RENAME);
1631 if (!rc && fsm->nsuffix) {
1632 const char * opath = fsmFsPath(fsm, st, NULL, NULL);
1633 rpmMessage(RPMMESS_WARNING, _("%s created as %s\n"),
1634 (opath ? opath : ""), fsm->path);
1635 opath = _free(opath);
1637 fsm->opath = _free(fsm->opath);
1639 if (S_ISLNK(st->st_mode)) {
1640 if (!rc && !getuid())
1641 rc = fsmStage(fsm, FSM_LCHOWN);
1643 if (!rc && !getuid())
1644 rc = fsmStage(fsm, FSM_CHOWN);
1646 rc = fsmStage(fsm, FSM_CHMOD);
1648 time_t st_mtime = st->st_mtime;
1649 TFI_t fi = fsmGetFi(fsm);
1651 st->st_mtime = fi->fmtimes[fsm->ix];
1652 rc = fsmStage(fsm, FSM_UTIME);
1653 st->st_mtime = st_mtime;
1658 /* Notify on success. */
1659 if (!rc) rc = fsmStage(fsm, FSM_NOTIFY);
1662 fsm->path = _free(fsm->path);
1664 /* Check for hard links missing from payload. */
1665 while ((fsm->li = fsm->links) != NULL) {
1666 fsm->links = fsm->li->next;
1667 fsm->li->next = NULL;
1668 if (fsm->goal == FSM_PKGINSTALL &&
1669 fsm->commit && fsm->li->linksLeft)
1671 for (i = 0 ; i < fsm->li->linksLeft; i++) {
1672 if (fsm->li->filex[i] < 0) continue;
1673 rc = CPIOERR_MISSING_HARDLINK;
1674 if (fsm->failedFile && *fsm->failedFile == NULL) {
1675 fsm->ix = fsm->li->filex[i];
1676 if (!fsmStage(fsm, FSM_MAP)) {
1677 *fsm->failedFile = fsm->path;
1681 /*@loopbreak@*/ break;
1684 if (fsm->goal == FSM_PKGBUILD && all_hardlinks_in_package) {
1685 rc = CPIOERR_MISSING_HARDLINK;
1687 fsm->li = freeHardLink(fsm->li);
1689 fsm->ldn = _free(fsm->ldn);
1690 fsm->ldnalloc = fsm->ldnlen = 0;
1691 fsm->rdbuf = fsm->rdb = _free(fsm->rdb);
1692 fsm->wrbuf = fsm->wrb = _free(fsm->wrb);
1695 if (fsm->diskchecked && !fsm->exists) {
1696 rc = CPIOERR_LSTAT_FAILED;
1699 if (S_ISREG(st->st_mode)) {
1700 char * path = alloca(strlen(fsm->path) + sizeof("-RPMDELETE"));
1701 (void) stpcpy( stpcpy(path, fsm->path), "-RPMDELETE");
1703 * XXX HP-UX (and other os'es) don't permit unlink on busy
1706 fsm->opath = fsm->path;
1708 rc = fsmStage(fsm, FSM_RENAME);
1710 (void) fsmStage(fsm, FSM_UNLINK);
1712 rc = CPIOERR_UNLINK_FAILED;
1713 fsm->path = fsm->opath;
1715 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1716 /*@notreached@*/ break;
1717 } else if (S_ISDIR(st->st_mode)) {
1718 if (S_ISDIR(ost->st_mode)) return 0;
1719 if (S_ISLNK(ost->st_mode)) {
1720 rc = fsmStage(fsm, FSM_STAT);
1721 if (rc == CPIOERR_STAT_FAILED && errno == ENOENT) rc = 0;
1724 if (S_ISDIR(ost->st_mode)) return 0;
1726 } else if (S_ISLNK(st->st_mode)) {
1727 if (S_ISLNK(ost->st_mode)) {
1728 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1729 rc = fsmStage(fsm, FSM_READLINK);
1732 if (!strcmp(fsm->opath, fsm->rdbuf)) return 0;
1734 } else if (S_ISFIFO(st->st_mode)) {
1735 if (S_ISFIFO(ost->st_mode)) return 0;
1736 } else if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) {
1737 if ((S_ISCHR(ost->st_mode) || S_ISBLK(ost->st_mode)) &&
1738 (ost->st_rdev == st->st_rdev)) return 0;
1739 } else if (S_ISSOCK(st->st_mode)) {
1740 if (S_ISSOCK(ost->st_mode)) return 0;
1742 /* XXX shouldn't do this with commit/undo. */
1744 if (fsm->stage == FSM_PROCESS) rc = fsmStage(fsm, FSM_UNLINK);
1745 if (rc == 0) rc = CPIOERR_LSTAT_FAILED;
1746 return (rc ? rc : CPIOERR_LSTAT_FAILED); /* XXX HACK */
1747 /*@notreached@*/ break;
1750 rc = Unlink(fsm->path);
1751 if (_fsm_debug && (stage & FSM_SYSCALL))
1752 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1753 fsm->path, (rc < 0 ? strerror(errno) : ""));
1754 if (rc < 0) rc = CPIOERR_UNLINK_FAILED;
1757 rc = Rename(fsm->opath, fsm->path);
1758 if (_fsm_debug && (stage & FSM_SYSCALL))
1759 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1760 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1761 if (rc < 0) rc = CPIOERR_RENAME_FAILED;
1764 rc = Mkdir(fsm->path, (st->st_mode & 07777));
1765 if (_fsm_debug && (stage & FSM_SYSCALL))
1766 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1767 fsm->path, (unsigned)(st->st_mode & 07777),
1768 (rc < 0 ? strerror(errno) : ""));
1769 if (rc < 0) rc = CPIOERR_MKDIR_FAILED;
1772 rc = Rmdir(fsm->path);
1773 if (_fsm_debug && (stage & FSM_SYSCALL))
1774 rpmMessage(RPMMESS_DEBUG, " %8s (%s) %s\n", cur,
1775 fsm->path, (rc < 0 ? strerror(errno) : ""));
1776 if (rc < 0) rc = CPIOERR_RMDIR_FAILED;
1779 rc = chown(fsm->path, st->st_uid, st->st_gid);
1780 if (_fsm_debug && (stage & FSM_SYSCALL))
1781 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1782 fsm->path, (int)st->st_uid, (int)st->st_gid,
1783 (rc < 0 ? strerror(errno) : ""));
1784 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1787 #if ! CHOWN_FOLLOWS_SYMLINK
1788 rc = lchown(fsm->path, st->st_uid, st->st_gid);
1789 if (_fsm_debug && (stage & FSM_SYSCALL))
1790 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, %d) %s\n", cur,
1791 fsm->path, (int)st->st_uid, (int)st->st_gid,
1792 (rc < 0 ? strerror(errno) : ""));
1793 if (rc < 0) rc = CPIOERR_CHOWN_FAILED;
1797 rc = chmod(fsm->path, (st->st_mode & 07777));
1798 if (_fsm_debug && (stage & FSM_SYSCALL))
1799 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1800 fsm->path, (unsigned)(st->st_mode & 07777),
1801 (rc < 0 ? strerror(errno) : ""));
1802 if (rc < 0) rc = CPIOERR_CHMOD_FAILED;
1805 { struct utimbuf stamp;
1806 stamp.actime = st->st_mtime;
1807 stamp.modtime = st->st_mtime;
1808 rc = utime(fsm->path, &stamp);
1809 if (_fsm_debug && (stage & FSM_SYSCALL))
1810 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0x%x) %s\n", cur,
1811 fsm->path, (unsigned)st->st_mtime,
1812 (rc < 0 ? strerror(errno) : ""));
1813 if (rc < 0) rc = CPIOERR_UTIME_FAILED;
1817 rc = symlink(fsm->opath, fsm->path);
1818 if (_fsm_debug && (stage & FSM_SYSCALL))
1819 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1820 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1821 if (rc < 0) rc = CPIOERR_SYMLINK_FAILED;
1824 rc = Link(fsm->opath, fsm->path);
1825 if (_fsm_debug && (stage & FSM_SYSCALL))
1826 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %s) %s\n", cur,
1827 fsm->opath, fsm->path, (rc < 0 ? strerror(errno) : ""));
1828 if (rc < 0) rc = CPIOERR_LINK_FAILED;
1831 rc = mkfifo(fsm->path, (st->st_mode & 07777));
1832 if (_fsm_debug && (stage & FSM_SYSCALL))
1833 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%04o) %s\n", cur,
1834 fsm->path, (unsigned)(st->st_mode & 07777),
1835 (rc < 0 ? strerror(errno) : ""));
1836 if (rc < 0) rc = CPIOERR_MKFIFO_FAILED;
1840 rc = mknod(fsm->path, (st->st_mode & ~07777), st->st_rdev);
1842 if (_fsm_debug && (stage & FSM_SYSCALL))
1843 rpmMessage(RPMMESS_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n", cur,
1844 fsm->path, (unsigned)(st->st_mode & ~07777),
1845 (unsigned)st->st_rdev,
1846 (rc < 0 ? strerror(errno) : ""));
1847 if (rc < 0) rc = CPIOERR_MKNOD_FAILED;
1850 rc = Lstat(fsm->path, ost);
1851 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1852 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1853 fsm->path, (rc < 0 ? strerror(errno) : ""));
1854 if (rc < 0) rc = CPIOERR_LSTAT_FAILED;
1857 rc = Stat(fsm->path, ost);
1858 if (_fsm_debug && (stage & FSM_SYSCALL) && rc && errno != ENOENT)
1859 rpmMessage(RPMMESS_DEBUG, " %8s (%s, ost) %s\n", cur,
1860 fsm->path, (rc < 0 ? strerror(errno) : ""));
1861 if (rc < 0) rc = CPIOERR_STAT_FAILED;
1864 /* XXX NUL terminated result in fsm->rdbuf, len in fsm->rdnb. */
1865 rc = Readlink(fsm->path, fsm->rdbuf, fsm->rdsize - 1);
1866 if (_fsm_debug && (stage & FSM_SYSCALL))
1867 rpmMessage(RPMMESS_DEBUG, " %8s (%s, rdbuf, %d) %s\n", cur,
1868 fsm->path, (int)fsm->rdlen, (rc < 0 ? strerror(errno) : ""));
1869 if (rc < 0) rc = CPIOERR_READLINK_FAILED;
1872 fsm->rdbuf[fsm->rdnb] = '\0';
1880 rc = fsmStage(fsm, FSM_HREAD);
1882 if (!strcmp(fsm->path, CPIO_TRAILER)) { /* Detect end-of-payload. */
1883 fsm->path = _free(fsm->path);
1884 rc = CPIOERR_HDR_TRAILER;
1887 rc = fsmStage(fsm, FSM_POS);
1890 for (left = st->st_size; left > 0; left -= fsm->rdnb) {
1891 fsm->wrlen = (left > fsm->wrsize ? fsm->wrsize : left);
1892 rc = fsmStage(fsm, FSM_DREAD);
1894 /*@loopbreak@*/ break;
1898 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1901 (void) fsmStage(fsm, FSM_DREAD);
1905 left = (modulo - (fdGetCpioPos(fsm->cfd) % modulo)) % modulo;
1907 memset(fsm->rdbuf, 0, left);
1908 /* XXX DWRITE uses rdnb for I/O length. */
1910 (void) fsmStage(fsm, FSM_DWRITE);
1914 rc = cpioTrailerWrite(fsm);
1917 rc = fsmStage(fsm, FSM_POS);
1919 rc = cpioHeaderRead(fsm, st); /* Read next payload header. */
1922 rc = cpioHeaderWrite(fsm, st); /* Write next payload header. */
1925 fsm->rdnb = Fread(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->wrlen, fsm->cfd);
1926 if (_fsm_debug && (stage & FSM_SYSCALL))
1927 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\trdnb %d\n",
1928 cur, (fsm->wrbuf == fsm->wrb ? "wrbuf" : "mmap"),
1929 (int)fsm->wrlen, (int)fsm->rdnb);
1930 if (fsm->rdnb != fsm->wrlen || Ferror(fsm->cfd))
1931 rc = CPIOERR_READ_FAILED;
1933 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->rdnb);
1936 fsm->wrnb = Fwrite(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdnb, fsm->cfd);
1937 if (_fsm_debug && (stage & FSM_SYSCALL))
1938 rpmMessage(RPMMESS_DEBUG, " %8s (%s, %d, cfd)\twrnb %d\n",
1939 cur, (fsm->rdbuf == fsm->rdb ? "rdbuf" : "mmap"),
1940 (int)fsm->rdnb, (int)fsm->wrnb);
1941 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->cfd))
1942 rc = CPIOERR_WRITE_FAILED;
1944 fdSetCpioPos(fsm->cfd, fdGetCpioPos(fsm->cfd) + fsm->wrnb);
1948 fsm->rfd = Fopen(fsm->path, "r.ufdio");
1949 if (fsm->rfd == NULL || Ferror(fsm->rfd)) {
1950 if (fsm->rfd) (void) fsmStage(fsm, FSM_RCLOSE);
1952 rc = CPIOERR_OPEN_FAILED;
1955 if (_fsm_debug && (stage & FSM_SYSCALL))
1956 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"r\") rfd %p rdbuf %p\n", cur,
1957 fsm->path, fsm->rfd, fsm->rdbuf);
1960 fsm->rdnb = Fread(fsm->rdbuf, sizeof(*fsm->rdbuf), fsm->rdlen, fsm->rfd);
1961 if (_fsm_debug && (stage & FSM_SYSCALL))
1962 rpmMessage(RPMMESS_DEBUG, " %8s (rdbuf, %d, rfd)\trdnb %d\n",
1963 cur, (int)fsm->rdlen, (int)fsm->rdnb);
1964 if (fsm->rdnb != fsm->rdlen || Ferror(fsm->rfd))
1965 rc = CPIOERR_READ_FAILED;
1969 if (_fsm_debug && (stage & FSM_SYSCALL))
1970 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->rfd);
1971 (void) Fclose(fsm->rfd);
1977 fsm->wfd = Fopen(fsm->path, "w.ufdio");
1978 if (fsm->wfd == NULL || Ferror(fsm->wfd)) {
1979 if (fsm->wfd) (void) fsmStage(fsm, FSM_WCLOSE);
1981 rc = CPIOERR_OPEN_FAILED;
1983 if (_fsm_debug && (stage & FSM_SYSCALL))
1984 rpmMessage(RPMMESS_DEBUG, " %8s (%s, \"w\") wfd %p wrbuf %p\n", cur,
1985 fsm->path, fsm->wfd, fsm->wrbuf);
1988 fsm->wrnb = Fwrite(fsm->wrbuf, sizeof(*fsm->wrbuf), fsm->rdnb, fsm->wfd);
1989 if (_fsm_debug && (stage & FSM_SYSCALL))
1990 rpmMessage(RPMMESS_DEBUG, " %8s (wrbuf, %d, wfd)\twrnb %d\n",
1991 cur, (int)fsm->rdnb, (int)fsm->wrnb);
1992 if (fsm->rdnb != fsm->wrnb || Ferror(fsm->wfd))
1993 rc = CPIOERR_WRITE_FAILED;
1997 if (_fsm_debug && (stage & FSM_SYSCALL))
1998 rpmMessage(RPMMESS_DEBUG, " %8s (%p)\n", cur, fsm->wfd);
1999 (void) Fclose(fsm->wfd);
2009 if (!(stage & FSM_INTERNAL)) {
2010 fsm->rc = (rc == CPIOERR_HDR_TRAILER ? 0 : rc);
2016 /*@obserever@*/ const char *const fileActionString(fileAction a)
2019 case FA_UNKNOWN: return "unknown";
2020 case FA_CREATE: return "create";
2021 case FA_COPYOUT: return "copyout";
2022 case FA_COPYIN: return "copyin";
2023 case FA_BACKUP: return "backup";
2024 case FA_SAVE: return "save";
2025 case FA_SKIP: return "skip";
2026 case FA_ALTNAME: return "altname";
2027 case FA_ERASE: return "erase";
2028 case FA_SKIPNSTATE: return "skipnstate";
2029 case FA_SKIPNETSHARED: return "skipnetshared";
2030 case FA_SKIPMULTILIB: return "skipmultilib";
2031 default: return "???";
2036 /*@observer@*/ const char *const fileStageString(fileStage a) {
2038 case FSM_UNKNOWN: return "unknown";
2040 case FSM_PKGINSTALL:return "pkginstall";
2041 case FSM_PKGERASE: return "pkgerase";
2042 case FSM_PKGBUILD: return "pkgbuild";
2043 case FSM_PKGCOMMIT: return "pkgcommit";
2044 case FSM_PKGUNDO: return "pkgundo";
2046 case FSM_CREATE: return "create";
2047 case FSM_INIT: return "init";
2048 case FSM_MAP: return "map";
2049 case FSM_MKDIRS: return "mkdirs";
2050 case FSM_RMDIRS: return "rmdirs";
2051 case FSM_PRE: return "pre";
2052 case FSM_PROCESS: return "process";
2053 case FSM_POST: return "post";
2054 case FSM_MKLINKS: return "mklinks";
2055 case FSM_NOTIFY: return "notify";
2056 case FSM_UNDO: return "undo";
2057 case FSM_FINI: return "fini";
2058 case FSM_COMMIT: return "commit";
2059 case FSM_DESTROY: return "destroy";
2060 case FSM_VERIFY: return "verify";
2062 case FSM_UNLINK: return "Unlink";
2063 case FSM_RENAME: return "Rename";
2064 case FSM_MKDIR: return "Mkdir";
2065 case FSM_RMDIR: return "rmdir";
2066 case FSM_CHOWN: return "chown";
2067 case FSM_LCHOWN: return "lchown";
2068 case FSM_CHMOD: return "chmod";
2069 case FSM_UTIME: return "utime";
2070 case FSM_SYMLINK: return "symlink";
2071 case FSM_LINK: return "Link";
2072 case FSM_MKFIFO: return "mkfifo";
2073 case FSM_MKNOD: return "mknod";
2074 case FSM_LSTAT: return "Lstat";
2075 case FSM_STAT: return "Stat";
2076 case FSM_READLINK: return "Readlink";
2077 case FSM_CHROOT: return "chroot";
2079 case FSM_NEXT: return "next";
2080 case FSM_EAT: return "eat";
2081 case FSM_POS: return "pos";
2082 case FSM_PAD: return "pad";
2083 case FSM_TRAILER: return "trailer";
2084 case FSM_HREAD: return "hread";
2085 case FSM_HWRITE: return "hwrite";
2086 case FSM_DREAD: return "Fread";
2087 case FSM_DWRITE: return "Fwrite";
2089 case FSM_ROPEN: return "Fopen";
2090 case FSM_READ: return "Fread";
2091 case FSM_RCLOSE: return "Fclose";
2092 case FSM_WOPEN: return "Fopen";
2093 case FSM_WRITE: return "Fwrite";
2094 case FSM_WCLOSE: return "Fclose";
2096 default: return "???";