Data Structures | Defines | Typedefs | Functions | Variables

# lib/fsm.c File Reference

File state machine to handle a payload from a package. More...

`#include "system.h"`
`#include <rpmio_internal.h>`
`#include <rpmcb.h>`
`#include <rpmtag.h>`
`#include <rpmlib.h>`
`#include "cpio.h"`
`#include "tar.h"`
`#include "fsm.h"`
`#include "rpmte.h"`
`#include "rpmts.h"`
`#include "rpmsq.h"`
`#include "ugid.h"`
`#include "debug.h"`
Include dependency graph for fsm.c:

Go to the source code of this file.

## Data Structures
struct | dnli_s
| Directory name iterator. More...
## Defines
#define | _RPMFI_INTERNAL
#define | fsmUNSAFE fsmStage
#define | alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
#define | _FSM_DEBUG 0
#define | SUFFIX_RPMORIG ".rpmorig"
#define | SUFFIX_RPMSAVE ".rpmsave"
#define | SUFFIX_RPMNEW ".rpmnew"
#define | IS_DEV_LOG(_x)
#define | _fafilter(_a)
#define | _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)
## Typedefs
typedef struct dnli_s * | DNLI_t
| Directory name iterator.
## Functions
rpmts | fsmGetTs (const FSM_t fsm)
| Retrieve transaction set from file state machine iterator.
rpmfi | fsmGetFi (const FSM_t fsm)
| Retrieve transaction element file info from file state machine iterator.
static const char * | fsmFsPath (const FSM_t fsm, const struct stat *st, const char *subdir, const char *suffix)
| Build path to file from file info, ornamented with subdir and suffix.
static void * | mapFreeIterator (void *p)
| Destroy file info iterator.
static void * | mapInitIterator (rpmts ts, rpmfi fi)
| Create file info iterator.
static int | mapNextIterator (void *a)
| Return next index into file info.
static int | cpioStrCmp (const void *a, const void *b)
static int | mapFind (FSMI_t iter, const char *fsmPath)
| Locate archive path in file info.
static void * | dnlFreeIterator (const void *a)
| Destroy directory name iterator.
static int | dnlCount (const DNLI_t dnli)
static int | dnlIndex (const DNLI_t dnli)
static void * | dnlInitIterator (const FSM_t fsm, int reverse)
| Create directory name iterator.
static const char * | dnlNextIterator (DNLI_t dnli)
| Return next directory name (from file info).
int | fsmNext (FSM_t fsm, fileStage nstage)
| File state machine driver.
static int | saveHardLink (FSM_t fsm)
| Save hard link in chain.
static void * | freeHardLink (struct hardLink_s *li)
| Destroy set of hard links.
FSM_t | newFSM (void)
| Create file state machine instance.
FSM_t | freeFSM (FSM_t fsm)
| Destroy file state machine instance.
int | fsmSetup (FSM_t fsm, fileStage goal, const char *afmt, const rpmts ts, const rpmfi fi, FD_t cfd, unsigned int *archiveSize, const char **failedFile)
| Load external data into file state machine.
int | fsmTeardown (FSM_t fsm)
| Clean file state machine.
static int | fsmMapFContext (FSM_t fsm)
int | fsmMapPath (FSM_t fsm)
| Map next file path and action.
int | fsmMapAttrs (FSM_t fsm)
| Map file stat(2) info.
static int | extractRegular (FSM_t fsm)
| Create file from payload stream.
static int | writeFile (FSM_t fsm, int writeData)
| Write next item to payload stream.
static int | writeLinkedFile (FSM_t fsm)
| Write set of linked files to payload stream.
static int | fsmMakeLinks (FSM_t fsm)
| Create pending hard links to existing file.
static int | fsmCommitLinks (FSM_t fsm)
| Commit hard linked file set atomically.
static int | fsmRmdirs (FSM_t fsm)
| Remove (if created) directories not explicitly included in package.
static int | fsmMkdirs (FSM_t fsm)
| Create (if necessary) directories not explicitly included in package.
int | fsmStage (FSM_t fsm, fileStage stage)
| File state machine driver.
const char * | fileActionString (fileAction a)
| Return formatted string representation of file disposition.
const char * | fileStageString (fileStage a)
| Return formatted string representation of file stages.
## Variables
int | _fsm_debug = 0
int | _fsm_threads = 0
int | strict_erasures = 0

## Detailed Description

File state machine to handle a payload from a package.

Definition in file fsm.c.

## Define Documentation

#define _fafilter | ( |  | _a ) |

Value:

    (!((_a) == FA_CREATE || (_a) == FA_ERASE || (_a) == FA_COPYIN || (_a) == FA_COPYOUT) \
            ? fileActionString(_a) : "")

Referenced by fsmStage().

#define _FSM_DEBUG 0

Definition at line 36 of file fsm.c.

#define _RPMFI_INTERNAL

Definition at line 16 of file fsm.c.

#define _tsmask (RPMTRANS_FLAG_PKGCOMMIT | RPMTRANS_FLAG_COMMIT)

Referenced by fsmStage().

#define alloca_strdup | ( |  | _s ) | strcpy(alloca(strlen(_s)+1), (_s))

Definition at line 34 of file fsm.c.

#define fsmUNSAFE fsmStage

Definition at line 18 of file fsm.c.

Referenced by fsmMakeLinks(), fsmMkdirs(), fsmSetup(), fsmStage(), fsmTeardown(), and writeFile().

#define IS_DEV_LOG | ( |  | _x ) |

Value:

    ((_x) != NULL && strlen(_x) >= (sizeof("/dev/log")-1) && \
            !strncmp((_x), "/dev/log", sizeof("/dev/log")-1) && \
            ((_x)[sizeof("/dev/log")-1] == '\0' || \
             (_x)[sizeof("/dev/log")-1] == ';'))

Definition at line 1452 of file fsm.c.

Referenced by fsmStage().

#define SUFFIX_RPMNEW ".rpmnew"

Definition at line 68 of file fsm.c.

Referenced by fsmMapPath().

#define SUFFIX_RPMORIG ".rpmorig"

Definition at line 66 of file fsm.c.

Referenced by fsmMapPath().

#define SUFFIX_RPMSAVE ".rpmsave"

Definition at line 67 of file fsm.c.

Referenced by fsmMapPath().

## Function Documentation

const char* fileActionString | ( | fileAction | a ) |

Return formatted string representation of file disposition.

Parameters:

  a | file dispostion

Returns:
  formatted string

Definition at line 2488 of file fsm.c.

References FA_ALTNAME, FA_BACKUP, FA_COPYIN, FA_COPYOUT, FA_CREATE, FA_ERASE, FA_SAVE, FA_SKIP, FA_SKIPCOLOR, FA_SKIPNETSHARED, FA_SKIPNSTATE, and FA_UNKNOWN.

const char* fileStageString | ( | fileStage | a ) |

Return formatted string representation of file stages.

Parameters:

  a | file stage

Returns:
  formatted string

Definition at line 2508 of file fsm.c.

References FSM_CHMOD, FSM_CHOWN, FSM_CHROOT, FSM_COMMIT, FSM_CREATE, FSM_DESTROY, FSM_DREAD, FSM_DWRITE, FSM_EAT, FSM_FINI, FSM_HREAD, FSM_HWRITE, FSM_INIT, FSM_LCHOWN, FSM_LINK, FSM_LSETFCON, FSM_LSTAT, FSM_MAP, FSM_MKDIR, FSM_MKDIRS, FSM_MKFIFO, FSM_MKLINKS, FSM_MKNOD, FSM_NEXT, FSM_NOTIFY, FSM_PAD, FSM_PKGBUILD, FSM_PKGCOMMIT, FSM_PKGERASE, FSM_PKGINSTALL, FSM_PKGUNDO, FSM_POS, FSM_POST, FSM_PRE, FSM_PROCESS, FSM_RCLOSE, FSM_READ, FSM_READLINK, FSM_RENAME, FSM_RMDIR, FSM_RMDIRS, FSM_ROPEN, FSM_STAT, FSM_SYMLINK, FSM_TRAILER, FSM_UNDO, FSM_UNKNOWN, FSM_UNLINK, FSM_UTIME, FSM_VERIFY, FSM_WCLOSE, FSM_WOPEN, and FSM_WRITE.

Referenced by fsmStage().

FSM_t freeFSM | ( | FSM_t | fsm ) |

Destroy file state machine instance.

Parameters:

  fsm | file state machine

Returns:
  always NULL

Definition at line 516 of file fsm.c.

References _free(), fsm_s::dnlx, freeHardLink(), fsm_s::iter, fsm_s::ldn, fsm_s::li, fsm_s::links, mapFreeIterator(), hardLink_s::next, and fsm_s::path.

Referenced by rpmfiFree().

rpmfi fsmGetFi | ( | const FSM_t | fsm ) |

Retrieve transaction element file info from file state machine iterator.

Parameters:

  fsm | file state machine

Returns:
  transaction element file info

Definition at line 58 of file fsm.c.

References fsmIterator_s::fi, and fsm_s::iter.

Referenced by dnlInitIterator(), fsmMapAttrs(), fsmMapFContext(), fsmMapPath(), fsmMkdirs(), fsmStage(), saveHardLink(), and writeFile().

rpmts fsmGetTs | ( | const FSM_t | fsm ) |

Retrieve transaction set from file state machine iterator.

Parameters:

  fsm | file state machine

Returns:
  transaction set

Definition at line 51 of file fsm.c.

References fsm_s::iter, and fsmIterator_s::ts.

Referenced by fsmMapAttrs(), fsmMapFContext(), fsmMkdirs(), and fsmStage().

int fsmMapAttrs | ( | FSM_t | fsm ) |

Map file stat(2) info.

Parameters:

  fsm | file state machine

Definition at line 753 of file fsm.c.

References _, CPIO_MAP_GID, CPIO_MAP_MODE, CPIO_MAP_TYPE, CPIO_MAP_UID, fsm_s::digest, fsm_s::digestlen, fsm_s::fdigest, fsm_s::fdigestalgo, FSM_PKGINSTALL, fsmGetFi(), fsmGetTs(), gnameToGid(), fsm_s::goal, headerIsEntry(), fsm_s::ix, fsm_s::mapFlags, rpmlog(), RPMLOG_WARNING, RPMTRANS_FLAG_NOFDIGESTS, rpmtsFlags(), fsm_s::sb, and unameToUid().

Referenced by fsmStage().

static int fsmMapFContext | ( | FSM_t | fsm ) | `[static]`

Definition at line 625 of file fsm.c.

References fsm_s::fcontext, fsmGetFi(), fsmGetTs(), fsm_s::ix, matchpathcon, fsm_s::path, RPMTRANS_FLAG_NOCONTEXTS, rpmtsFlags(), rpmtsSELinuxEnabled(), and fsm_s::sb.

Referenced by fsmStage().

int fsmMapPath | ( | FSM_t | fsm ) |

Map next file path and action.

Parameters:

  fsm | file state machine

Definition at line 655 of file fsm.c.

References _free(), fsm_s::action, fsm_s::astriplen, fsm_s::baseName, CPIO_MAP_PATH, fsm_s::dirName, FA_ALTNAME, FA_BACKUP, FA_COPYIN, FA_COPYOUT, FA_CREATE, FA_ERASE, FA_SAVE, FA_SKIP, FA_SKIPCOLOR, FA_SKIPNETSHARED, FA_SKIPNSTATE, FA_UNKNOWN, fsm_s::fflags, fsmFsPath(), fsmGetFi(), fsm_s::ix, fsm_s::mapFlags, fsm_s::nsuffix, fsm_s::osuffix, fsm_s::path, RPMFILE_GHOST, RPMFILE_STATE_NETSHARED, RPMFILE_STATE_NOTINSTALLED, RPMFILE_STATE_WRONGCOLOR, rpmteType(), fsm_s::sb, fsm_s::subdir, fsm_s::suffix, SUFFIX_RPMNEW, SUFFIX_RPMORIG, SUFFIX_RPMSAVE, TR_ADDED, and TR_REMOVED.

Referenced by fsmStage().

static int fsmMkdirs | ( | FSM_t | fsm ) | `[static]`

Create (if necessary) directories not explicitly included in package.

Parameters:

  fsm | file state machine data

Returns:
  0 on success

Definition at line 1288 of file fsm.c.

References CPIOERR_ENOENT, D_, dnlCount(), dnlFreeIterator(), dnlIndex(), dnlInitIterator(), dnlNextIterator(), fsm_s::dnlx, fsm_s::fcontext, FSM_LSETFCON, FSM_LSTAT, FSM_MKDIR, fsmGetFi(), fsmGetTs(), fsmNext(), fsmUNSAFE, fsm_s::ldn, fsm_s::ldnalloc, fsm_s::ldnlen, fsm_s::osb, fsm_s::path, fsm_s::rdbuf, rpmlog(), RPMLOG_DEBUG, rpmsxFContext(), rpmsxFree(), RPMTRANS_FLAG_NOCONTEXTS, rpmtsFlags(), rpmtsREContext(), rpmtsSELinuxEnabled(), fsm_s::sb, stpcpy(), urlPath(), xcalloc(), and xrealloc().

Referenced by fsmStage().

int fsmNext | ( | FSM_t | fsm,
|  | fileStage | nstage
| ) |  |

File state machine driver.

Parameters:

  fsm | file state machine
  nstage | next stage

Returns:
  0 on success

Definition at line 397 of file fsm.c.

References _fsm_threads, fsmStage(), fsm_s::nstage, rpmsqJoin(), and rpmsqThread().

Referenced by cpioHeaderRead(), cpioHeaderWrite(), cpioTrailerWrite(), extractRegular(), fsmCommitLinks(), fsmMakeLinks(), fsmMkdirs(), fsmRmdirs(), fsmStage(), saveHardLink(), tarHeaderRead(), tarHeaderReadName(), tarHeaderWrite(), tarHeaderWriteBlock(), tarHeaderWriteName(), tarTrailerWrite(), writeFile(), and writeLinkedFile().

static int fsmRmdirs | ( | FSM_t | fsm ) | `[static]`

Remove (if created) directories not explicitly included in package.

Parameters:

  fsm | file state machine data

Returns:
  0 on success

Definition at line 1233 of file fsm.c.

References dnlCount(), dnlFreeIterator(), dnlIndex(), dnlInitIterator(), dnlNextIterator(), fsm_s::dnlx, FSM_RMDIR, fsmNext(), fsm_s::ldn, fsm_s::path, fsm_s::rdbuf, and stpcpy().

Referenced by fsmStage().

int fsmSetup | ( | FSM_t | fsm,
|  | fileStage | goal,
|  | const char * | afmt,
|  | const rpmts | ts,
|  | const rpmfi | fi,
|  | FD_t | cfd,
|  | unsigned int * | archiveSize,
|  | const char ** | failedFile
| ) |  |

Load external data into file state machine.

Parameters:

  fsm | file state machine
  goal |
  afmt | archive format (NULL uses cpio)
  ts | transaction set
  fi | transaction element file info
  cfd | payload descriptor

Return values:

  archiveSize | pointer to archive size
  failedFile | pointer to first file name that failed.

Returns:
  0 on success

Definition at line 532 of file fsm.c.

References _fsm_debug, fsm_s::archiveSize, fsm_s::blksize, fsm_s::cfd, cpioHeaderRead(), cpioHeaderWrite(), cpioTrailerWrite(), fsm_s::failedFile, fdGetCpioPos(), fdLink(), fdSetCpioPos(), FSM_CREATE, FSM_PKGBUILD, FSM_PKGINSTALL, fsmUNSAFE, fsm_s::goal, fsm_s::headerRead, fsm_s::headerWrite, fsm_s::iter, mapInitIterator(), fsm_s::rc, RPMCALLBACK_INST_START, rpmtsGetTid(), rpmtsNotify(), fsm_s::sufbuf, TAR_BLOCK_SIZE, tarHeaderRead(), tarHeaderWrite(), tarTrailerWrite(), and fsm_s::trailerWrite.

Referenced by cpio_doio(), and rpmpsmStage().

int fsmStage | ( | FSM_t | fsm,
|  | fileStage | stage
| ) |  |

File state machine driver.

Parameters:

  fsm | file state machine
  stage | next stage

Returns:
  0 on success

Definition at line 1459 of file fsm.c.

References _, _fafilter, _free(), _fsm_debug, _tsmask, fsm_s::action, alloca(), fsm_s::astriplen, fsm_s::blksize, fsm_s::cfd, Chmod(), Chown(), fsm_s::commit, CPIO_ALL_HARDLINKS, CPIO_FOLLOW_SYMLINKS, CPIO_PAYLOAD_EXTRACT, CPIO_PAYLOAD_LIST, CPIO_SBIT_CHECK, CPIO_TRAILER, CPIOERR_CHMOD_FAILED, CPIOERR_CHOWN_FAILED, CPIOERR_ENOENT, CPIOERR_ENOTEMPTY, CPIOERR_HDR_TRAILER, CPIOERR_LINK_FAILED, CPIOERR_LSETFCON_FAILED, CPIOERR_LSTAT_FAILED, CPIOERR_MISSING_HARDLINK, CPIOERR_MKDIR_FAILED, CPIOERR_MKFIFO_FAILED, CPIOERR_MKNOD_FAILED, CPIOERR_OPEN_FAILED, CPIOERR_READ_FAILED, CPIOERR_READLINK_FAILED, CPIOERR_RENAME_FAILED, CPIOERR_RMDIR_FAILED, CPIOERR_STAT_FAILED, CPIOERR_SYMLINK_FAILED, CPIOERR_UNKNOWN_FILETYPE, CPIOERR_UNLINK_FAILED, CPIOERR_UNMAPPED_FILE, CPIOERR_UTIME_FAILED, CPIOERR_WRITE_FAILED, hardLink_s::createdPath, fsm_s::diskchecked, fsm_s::dnlx, errno, fsm_s::exists, extractRegular(), FA_ERASE, FA_UNKNOWN, fsm_s::failedFile, Fclose(), fsm_s::fcontext, fdGetCpioPos(), fdSetCpioPos(), FDSTAT_DIGEST, fdstat_op(), Ferror(), fsm_s::fflags, fileStageString(), hardLink_s::filex, Fopen(), Fread(), freeHardLink(), FSM_CHMOD, FSM_CHOWN, FSM_CHROOT, FSM_COMMIT, FSM_CREATE, FSM_DEAD, FSM_DESTROY, FSM_DREAD, FSM_DWRITE, FSM_EAT, FSM_FINI, FSM_HREAD, FSM_HWRITE, FSM_INIT, FSM_INTERNAL, FSM_LCHOWN, FSM_LINK, FSM_LSETFCON, FSM_LSTAT, FSM_MAP, FSM_MKDIR, FSM_MKDIRS, FSM_MKFIFO, FSM_MKLINKS, FSM_MKNOD, FSM_NEXT, FSM_NOTIFY, FSM_PAD, FSM_PKGBUILD, FSM_PKGCOMMIT, FSM_PKGERASE, FSM_PKGINSTALL, FSM_POS, FSM_POST, FSM_PRE, FSM_PROCESS, FSM_RCLOSE, FSM_READ, FSM_READLINK, FSM_RENAME, FSM_RMDIR, FSM_RMDIRS, FSM_ROPEN, FSM_STAT, FSM_SYMLINK, FSM_SYSCALL, FSM_TRAILER, FSM_UNDO, FSM_UNKNOWN, FSM_UNLINK, FSM_UTIME, FSM_VERBOSE, FSM_VERIFY, FSM_WCLOSE, FSM_WOPEN, FSM_WRITE, fsmCommitLinks(), fsmFsPath(), fsmGetFi(), fsmGetTs(), fsmMakeLinks(), fsmMapAttrs(), fsmMapFContext(), fsmMapPath(), fsmMkdirs(), fsmNext(), fsmRmdirs(), fsmUNSAFE, Fwrite(), fsm_s::goal, fsm_s::headerRead, fsm_s::headerWrite, IS_DEV_LOG, fsm_s::iter, fsm_s::ix, Lchown(), fsm_s::ldn, fsm_s::ldnalloc, fsm_s::ldnlen, left, fsm_s::li, Link(), hardLink_s::linkIndex, fsm_s::links, hardLink_s::linksLeft, fsm_s::lpath, lsetfilecon, Lstat(), mapFind(), fsm_s::mapFlags, mapNextIterator(), Mkdir(), fsm_s::mkdirsdone, Mkfifo(), Mknod(), hardLink_s::next, hardLink_s::nlink, fsm_s::nsuffix, fsm_s::opath, fsm_s::osb, fsm_s::osuffix, fsm_s::path, fsm_s::postpone, fsm_s::rc, fsm_s::rdb, fsm_s::rdbuf, fsm_s::rdlen, fsm_s::rdnb, fsm_s::rdsize, Readlink(), Rename(), fsm_s::repackaged, fsm_s::rfd, Rmdir(), RPMCALLBACK_INST_PROGRESS, RPMFILE_GHOST, RPMFILE_MISSINGOK, rpmfiTypeString(), rpmlog(), RPMLOG_DEBUG, RPMLOG_ERR, RPMLOG_WARNING, rpmswAdd(), RPMTS_OP_DIGEST, rpmtsFlags(), rpmtsNotify(), rpmtsOp(), S_ISLNK, S_ISSOCK, saveHardLink(), hardLink_s::sb, fsm_s::sb, fsm_s::stage, Stat(), stpcpy(), strict_erasures, fsm_s::subdir, fsm_s::sufbuf, fsm_s::suffix, Symlink(), fsm_s::trailerWrite, Unlink(), urlPath(), Utime(), fsm_s::wfd, fsm_s::wrb, fsm_s::wrbuf, writeFile(), writeLinkedFile(), fsm_s::wrlen, fsm_s::wrnb, fsm_s::wrsize, xmalloc(), and xstrdup().

Referenced by fsmNext().

int fsmTeardown | ( | FSM_t | fsm ) |

Clean file state machine.

Parameters:

  fsm | file state machine

Returns:
  0 on success

Definition at line 607 of file fsm.c.

References _fsm_debug, fsm_s::cfd, fsm_s::failedFile, fdFree(), FSM_DESTROY, fsmUNSAFE, fsm_s::iter, mapFreeIterator(), and fsm_s::rc.

Referenced by cpio_doio(), and rpmpsmStage().

FSM_t newFSM | ( | void | ) |

Create file state machine instance.

Returns:
  file state machine

Definition at line 510 of file fsm.c.

References xcalloc().

Referenced by rpmfiNew().

## Variable Documentation

int _fsm_debug = 0

Definition at line 38 of file fsm.c.

Referenced by fsmSetup(), fsmStage(), and fsmTeardown().

int _fsm_threads = 0

Definition at line 42 of file fsm.c.

Referenced by fsmNext().

int strict_erasures = 0

Definition at line 48 of file fsm.c.

Referenced by fsmStage().