Push archivePos from rpmfi to fsm
[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_PKGCOMMIT       = _fd(10),
38     FSM_PKGUNDO         = _fd(11),
39
40     FSM_CREATE  =  _fd(17),
41     FSM_MAP     =  _fd(18),
42     FSM_MKDIRS  =  _fi(19),
43     FSM_RMDIRS  =  _fi(20),
44     FSM_MKLINKS =  _fi(21),
45     FSM_NOTIFY  =  _fd(22),
46     FSM_DESTROY =  _fd(23),
47     FSM_VERIFY  =  _fd(24),
48     FSM_COMMIT  =  _fd(25),
49
50     FSM_UNLINK  =  _fs(33),
51     FSM_RENAME  =  _fs(34),
52     FSM_MKDIR   =  _fs(35),
53     FSM_RMDIR   =  _fs(36),
54     FSM_LSETFCON=  _fs(39),
55     FSM_CHOWN   =  _fs(40),
56     FSM_LCHOWN  =  _fs(41),
57     FSM_CHMOD   =  _fs(42),
58     FSM_UTIME   =  _fs(43),
59     FSM_SYMLINK =  _fs(44),
60     FSM_LINK    =  _fs(45),
61     FSM_MKFIFO  =  _fs(46),
62     FSM_MKNOD   =  _fs(47),
63     FSM_LSTAT   =  _fs(48),
64     FSM_STAT    =  _fs(49),
65     FSM_READLINK=  _fs(50),
66     FSM_CHROOT  =  _fs(51),
67     FSM_SETCAP  =  _fs(52),
68
69     FSM_NEXT    =  _fd(65),
70     FSM_EAT     =  _fd(66),
71     FSM_POS     =  _fd(67),
72     FSM_PAD     =  _fd(68),
73     FSM_TRAILER =  _fd(69),
74     FSM_HREAD   =  _fd(70),
75     FSM_HWRITE  =  _fd(71),
76     FSM_DREAD   =  _fs(72),
77     FSM_DWRITE  =  _fs(73),
78
79     FSM_ROPEN   =  _fs(129),
80     FSM_READ    =  _fs(130),
81     FSM_RCLOSE  =  _fs(131),
82     FSM_WOPEN   =  _fs(132),
83     FSM_WRITE   =  _fs(133),
84     FSM_WCLOSE  =  _fs(134)
85 } fileStage;
86 #undef  _fv
87 #undef  _fi
88 #undef  _fs
89 #undef  _fd
90
91 /** \ingroup payload
92  */
93 typedef enum cpioMapFlags_e {
94     CPIO_MAP_PATH       = (1 << 0),
95     CPIO_MAP_MODE       = (1 << 1),
96     CPIO_MAP_UID        = (1 << 2),
97     CPIO_MAP_GID        = (1 << 3),
98     CPIO_FOLLOW_SYMLINKS= (1 << 4), /*!< only for building. */
99     CPIO_MAP_ABSOLUTE   = (1 << 5),
100     CPIO_MAP_ADDDOT     = (1 << 6),
101     CPIO_ALL_HARDLINKS  = (1 << 7), /*!< fail if hardlinks are missing. */
102     CPIO_MAP_TYPE       = (1 << 8),  /*!< only for building. */
103     CPIO_SBIT_CHECK     = (1 << 9)
104 } cpioMapFlags;
105
106 typedef struct fsmIterator_s * FSMI_t;
107 typedef struct fsm_s * FSM_t;
108
109 typedef struct hardLink_s * hardLink_t;
110
111 /** \ingroup payload
112  * File name and stat information.
113  */
114 struct fsm_s {
115     const char * path;          /*!< Current file name. */
116     const char * opath;         /*!< Original file name. */
117     FD_t cfd;                   /*!< Payload file handle. */
118     FD_t rfd;                   /*!<  read: File handle. */
119     char * rdbuf;               /*!<  read: Buffer. */
120     char * rdb;                 /*!<  read: Buffer allocated. */
121     size_t rdsize;              /*!<  read: Buffer allocated size. */
122     size_t rdlen;               /*!<  read: Number of bytes requested.*/
123     size_t rdnb;                /*!<  read: Number of bytes returned. */
124     FD_t wfd;                   /*!< write: File handle. */
125     char * wrbuf;               /*!< write: Buffer. */
126     char * wrb;                 /*!< write: Buffer allocated. */
127     size_t wrsize;              /*!< write: Buffer allocated size. */
128     size_t wrlen;               /*!< write: Number of bytes requested.*/
129     size_t wrnb;                /*!< write: Number of bytes returned. */
130     FSMI_t iter;                /*!< File iterator. */
131     int ix;                     /*!< Current file iterator index. */
132     hardLink_t links;           /*!< Pending hard linked file(s). */
133     hardLink_t li;              /*!< Current hard linked file(s). */
134     rpm_loff_t * archiveSize;   /*!< Pointer to archive size. */
135     rpm_loff_t archivePos;      /*!< Current archive position. */
136     char ** failedFile;         /*!< First file name that failed. */
137     const char * subdir;        /*!< Current file sub-directory. */
138     char subbuf[64];    /* XXX eliminate */
139     const char * osuffix;       /*!< Old, preserved, file suffix. */
140     const char * nsuffix;       /*!< New, created, file suffix. */
141     const char * suffix;        /*!< Current file suffix. */
142     char sufbuf[64];    /* XXX eliminate */
143     short * dnlx;               /*!< Last dirpath verified indexes. */
144     char * ldn;                 /*!< Last dirpath verified. */
145     int ldnlen;                 /*!< Last dirpath current length. */
146     int ldnalloc;               /*!< Last dirpath allocated length. */
147     int postpone;               /*!< Skip remaining stages? */
148     int diskchecked;            /*!< Has stat(2) been performed? */
149     int exists;                 /*!< Does current file exist on disk? */
150     int mkdirsdone;             /*!< Have "orphan" dirs been created? */
151     int astriplen;              /*!< Length of buildroot prefix. */
152     int rc;                     /*!< External file stage return code. */
153     int commit;                 /*!< Commit synchronously? */
154     cpioMapFlags mapFlags;      /*!< Bit(s) to control mapping. */
155     const char * dirName;       /*!< File directory name. */
156     const char * baseName;      /*!< File base name. */
157     const char * digest;        /*!< Binary checksum (NULL disables). */
158     security_context_t fcontext;/*!< File security context (NULL disables). */
159     cap_t fcaps;                /*!< File capabilities */
160     pgpHashAlgo digestalgo;     /*!< File checksum algorithm */
161     
162     unsigned fflags;            /*!< File flags. */
163     rpmFileAction action;       /*!< File disposition. */
164     fileStage goal;             /*!< Package state machine goal. */
165     fileStage stage;            /*!< External file stage. */
166     fileStage nstage;           /*!< Next file stage. */
167     struct stat sb;             /*!< Current file stat(2) info. */
168     struct stat osb;            /*!< Original file stat(2) info. */
169 };
170
171 #ifdef __cplusplus
172 extern "C" {
173 #endif
174
175 /**
176  * Create file state machine instance.
177  * @return              file state machine
178  */
179 RPM_GNUC_INTERNAL
180 FSM_t newFSM(void);
181
182 /**
183  * Destroy file state machine instance.
184  * @param fsm           file state machine
185  * @return              always NULL
186  */
187 RPM_GNUC_INTERNAL
188 FSM_t freeFSM(FSM_t fsm);
189
190 /**
191  * Load external data into file state machine.
192  * @param fsm           file state machine
193  * @param goal
194  * @param ts            transaction set
195  * @param fi            transaction element file info
196  * @param cfd
197  * @retval archiveSize  pointer to archive size
198  * @retval failedFile   pointer to first file name that failed (malloced)
199  * @return              0 on success
200  */
201 int fsmSetup(FSM_t fsm, fileStage goal,
202                 const rpmts ts,
203                 const rpmfi fi,
204                 FD_t cfd,
205                 rpm_loff_t * archiveSize,
206                 char ** failedFile);
207
208 /**
209  * Clean file state machine.
210  * @param fsm           file state machine
211  * @return              0 on success
212  */
213 int fsmTeardown(FSM_t fsm);
214
215 /**
216  * File state machine driver.
217  * @param fsm           file state machine
218  * @param nstage                next stage
219  * @return              0 on success
220  */
221 RPM_GNUC_INTERNAL
222 int fsmNext(FSM_t fsm, fileStage nstage);
223
224 #ifdef __cplusplus
225 }
226 #endif
227
228 #endif  /* H_FSM */