c25b7c5e83f84cf882a82ccd4f0827290cc19610
[platform/upstream/rpm.git] / lib / fsm.h
1 #ifndef H_FSM
2 #define H_FSM
3
4 /** \ingroup payload
5  * \file lib/fsm.h
6  * File state machine to handle a payload within an rpm package.
7  */
8
9 #include <rpm/rpmfi.h>
10
11 extern int _fsm_debug;
12
13 /**
14  */
15 #define FSM_VERBOSE     0x8000
16 #define FSM_INTERNAL    0x4000
17 #define FSM_SYSCALL     0x2000
18 #define FSM_DEAD        0x1000
19
20 #define _fv(_a)         ((_a) | FSM_VERBOSE)
21 #define _fi(_a)         ((_a) | FSM_INTERNAL)
22 #define _fs(_a)         ((_a) | (FSM_INTERNAL | FSM_SYSCALL))
23 #define _fd(_a)         ((_a) | (FSM_INTERNAL | FSM_DEAD))
24
25 typedef enum fileStage_e {
26     FSM_UNKNOWN =   0,
27     FSM_INIT    =  _fd(1),
28     FSM_PRE     =  _fd(2),
29     FSM_PROCESS =  _fv(3),
30     FSM_POST    =  _fd(4),
31     FSM_UNDO    =  5,
32     FSM_FINI    =  6,
33
34     FSM_PKGINSTALL      = _fd(7),
35     FSM_PKGERASE        = _fd(8),
36     FSM_PKGBUILD        = _fd(9),
37     FSM_PKGUNDO         = _fd(11),
38
39     FSM_CREATE  =  _fd(17),
40     FSM_MAP     =  _fd(18),
41     FSM_MKDIRS  =  _fi(19),
42     FSM_RMDIRS  =  _fi(20),
43     FSM_MKLINKS =  _fi(21),
44     FSM_NOTIFY  =  _fd(22),
45     FSM_DESTROY =  _fd(23),
46     FSM_VERIFY  =  _fd(24),
47     FSM_COMMIT  =  _fd(25),
48
49     FSM_UNLINK  =  _fs(33),
50     FSM_RENAME  =  _fs(34),
51     FSM_MKDIR   =  _fs(35),
52     FSM_RMDIR   =  _fs(36),
53     FSM_LSETFCON=  _fs(39),
54     FSM_CHOWN   =  _fs(40),
55     FSM_LCHOWN  =  _fs(41),
56     FSM_CHMOD   =  _fs(42),
57     FSM_UTIME   =  _fs(43),
58     FSM_SYMLINK =  _fs(44),
59     FSM_LINK    =  _fs(45),
60     FSM_MKFIFO  =  _fs(46),
61     FSM_MKNOD   =  _fs(47),
62     FSM_LSTAT   =  _fs(48),
63     FSM_STAT    =  _fs(49),
64     FSM_READLINK=  _fs(50),
65     FSM_SETCAP  =  _fs(52),
66
67     FSM_NEXT    =  _fd(65),
68     FSM_EAT     =  _fd(66),
69     FSM_POS     =  _fd(67),
70     FSM_PAD     =  _fd(68),
71     FSM_TRAILER =  _fd(69),
72     FSM_HREAD   =  _fd(70),
73     FSM_HWRITE  =  _fd(71),
74     FSM_DREAD   =  _fs(72),
75     FSM_DWRITE  =  _fs(73),
76 } fileStage;
77 #undef  _fv
78 #undef  _fi
79 #undef  _fs
80 #undef  _fd
81
82 /** \ingroup payload
83  */
84 enum cpioMapFlags_e {
85     CPIO_MAP_PATH       = (1 << 0),
86     CPIO_MAP_MODE       = (1 << 1),
87     CPIO_MAP_UID        = (1 << 2),
88     CPIO_MAP_GID        = (1 << 3),
89     CPIO_FOLLOW_SYMLINKS= (1 << 4), /*!< only for building. */
90     CPIO_MAP_ABSOLUTE   = (1 << 5),
91     CPIO_MAP_ADDDOT     = (1 << 6),
92     CPIO_MAP_TYPE       = (1 << 8),  /*!< only for building. */
93     CPIO_SBIT_CHECK     = (1 << 9)
94 };
95 typedef rpmFlags cpioMapFlags;
96
97 typedef struct fsmIterator_s * FSMI_t;
98 typedef struct fsm_s * FSM_t;
99 typedef struct rpmpsm_s * rpmpsm;
100
101 typedef struct hardLink_s * hardLink_t;
102
103 /** \ingroup payload
104  * File name and stat information.
105  */
106 struct fsm_s {
107     char * path;                /*!< Current file name. */
108     FD_t cfd;                   /*!< Payload file handle. */
109     char * rdbuf;               /*!<  read: Buffer. */
110     char * rdb;                 /*!<  read: Buffer allocated. */
111     size_t rdsize;              /*!<  read: Buffer allocated size. */
112     size_t rdnb;                /*!<  read: Number of bytes returned. */
113     char * wrbuf;               /*!< write: Buffer. */
114     char * wrb;                 /*!< write: Buffer allocated. */
115     size_t wrsize;              /*!< write: Buffer allocated size. */
116     size_t wrlen;               /*!< write: Number of bytes requested.*/
117     size_t wrnb;                /*!< write: Number of bytes returned. */
118     FSMI_t iter;                /*!< File iterator. */
119     int ix;                     /*!< Current file iterator index. */
120     hardLink_t links;           /*!< Pending hard linked file(s). */
121     hardLink_t li;              /*!< Current hard linked file(s). */
122     rpm_loff_t * archiveSize;   /*!< Pointer to archive size. */
123     rpm_loff_t archivePos;      /*!< Current archive position. */
124     rpm_loff_t cpioPos;
125     char ** failedFile;         /*!< First file name that failed. */
126     const char * osuffix;       /*!< Old, preserved, file suffix. */
127     const char * nsuffix;       /*!< New, created, file suffix. */
128     char * suffix;              /*!< Current file suffix. */
129     int postpone;               /*!< Skip remaining stages? */
130     int diskchecked;            /*!< Has stat(2) been performed? */
131     int exists;                 /*!< Does current file exist on disk? */
132     int rc;                     /*!< External file stage return code. */
133     cpioMapFlags mapFlags;      /*!< Bit(s) to control mapping. */
134     const char * dirName;       /*!< File directory name. */
135     const char * baseName;      /*!< File base name. */
136     struct selabel_handle *sehandle;    /*!< SELinux label handle (if any). */
137     
138     unsigned fflags;            /*!< File flags. */
139     rpmFileAction action;       /*!< File disposition. */
140     fileStage goal;             /*!< Package state machine goal. */
141     fileStage stage;            /*!< External file stage. */
142     fileStage nstage;           /*!< Next file stage. */
143     struct stat sb;             /*!< Current file stat(2) info. */
144     struct stat osb;            /*!< Original file stat(2) info. */
145
146     rpmpsm psm;                 /*!< "parent" package state machine */
147 };
148
149 #ifdef __cplusplus
150 extern "C" {
151 #endif
152
153 /**
154  * Execute a file state machine goal
155  * @param goal
156  * @param ts            transaction set
157  * @param fi            transaction element file info
158  * @param cfd
159  * @param psm           owner psm (or NULL)
160  * @retval archiveSize  pointer to archive size
161  * @retval failedFile   pointer to first file name that failed (malloced)
162  * @return              0 on success
163  */
164 int rpmfsmRun(fileStage goal, rpmts ts, rpmte te, rpmfi fi, FD_t cfd,
165               rpmpsm psm, rpm_loff_t * archiveSize, char ** failedFile);
166
167 /**
168  * File state machine driver.
169  * @param fsm           file state machine
170  * @param nstage                next stage
171  * @return              0 on success
172  */
173 RPM_GNUC_INTERNAL
174 int fsmNext(FSM_t fsm, fileStage nstage);
175
176 RPM_GNUC_INTERNAL
177 void rpmpsmNotify(rpmpsm psm, int what, rpm_loff_t amount);
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif  /* H_FSM */