1 | /** @file
|
---|
2 | * IPRT - Tar archive I/O.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_tar_h
|
---|
27 | #define ___iprt_tar_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/time.h>
|
---|
32 |
|
---|
33 | RT_C_DECLS_BEGIN
|
---|
34 |
|
---|
35 | /** @defgroup grp_rt_tar RTTar - Tar archive I/O
|
---|
36 | * @ingroup grp_rt
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /** A tar handle */
|
---|
41 | typedef R3PTRTYPE(struct RTTARINTERNAL *) RTTAR;
|
---|
42 | /** Pointer to a RTTAR interface handle. */
|
---|
43 | typedef RTTAR *PRTTAR;
|
---|
44 | /** Nil RTTAR interface handle. */
|
---|
45 | #define NIL_RTTAR ((RTTAR)0)
|
---|
46 |
|
---|
47 | /** A tar file handle */
|
---|
48 | typedef R3PTRTYPE(struct RTTARFILEINTERNAL *) RTTARFILE;
|
---|
49 | /** Pointer to a RTTARFILE interface handle. */
|
---|
50 | typedef RTTARFILE *PRTTARFILE;
|
---|
51 | /** Nil RTTARFILE interface handle. */
|
---|
52 | #define NIL_RTTARFILE ((RTTARFILE)0)
|
---|
53 |
|
---|
54 | /** Maximum length of a tar filename, excluding the terminating '\0'. More
|
---|
55 | * does not fit into a tar record. */
|
---|
56 | #define RTTAR_NAME_MAX 99
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Opens a Tar archive.
|
---|
60 | *
|
---|
61 | * Use the mask to specify the access type. In create mode the target file
|
---|
62 | * have not to exists.
|
---|
63 | *
|
---|
64 | * @returns IPRT status code.
|
---|
65 | *
|
---|
66 | * @param phTar Where to store the RTTAR handle.
|
---|
67 | * @param pszTarname The file name of the tar archive to open.
|
---|
68 | * @param fMode Open flags, i.e a combination of the RTFILE_O_* defines.
|
---|
69 | * The ACCESS, ACTION and DENY flags are mandatory!
|
---|
70 | * @param fStream Open the file in stream mode. Within this mode no
|
---|
71 | * seeking is allowed. Use this together with
|
---|
72 | * RTTarFileCurrent, RTTarFileOpenCurrent,
|
---|
73 | * RTTarFileSeekNextFile and the read method to
|
---|
74 | * sequential read a tar file. Currently ignored with
|
---|
75 | * RTFILE_O_WRITE.
|
---|
76 | */
|
---|
77 | RTR3DECL(int) RTTarOpen(PRTTAR phTar, const char *pszTarname, uint32_t fMode, bool fStream);
|
---|
78 |
|
---|
79 | #if 0
|
---|
80 | /**
|
---|
81 | * Opens a Tar archive by handle.
|
---|
82 | *
|
---|
83 | * Use the mask to specify the access type. In create mode the target file
|
---|
84 | * have not to exists.
|
---|
85 | *
|
---|
86 | * @returns IPRT status code.
|
---|
87 | *
|
---|
88 | * @param phTar Where to store the RTTAR handle.
|
---|
89 | * @param hFile The file handle of the tar file. This is expected
|
---|
90 | * to be a regular file at the moment.
|
---|
91 | * @param fStream Open the file in stream mode. Within this mode no
|
---|
92 | * seeking is allowed. Use this together with
|
---|
93 | * RTTarFileCurrent, RTTarFileOpenCurrent,
|
---|
94 | * RTTarFileSeekNextFile and the read method to
|
---|
95 | * sequential read a tar file. Currently ignored with
|
---|
96 | * RTFILE_O_WRITE.
|
---|
97 | */
|
---|
98 | RTR3DECL(int) RTTarOpenByHandle(PRTTAR phTar, RTFILE hFile, uint32_t fMode, bool fStream);
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Close the Tar archive.
|
---|
103 | *
|
---|
104 | * @returns IPRT status code.
|
---|
105 | *
|
---|
106 | * @param hTar Handle to the RTTAR interface.
|
---|
107 | */
|
---|
108 | RTR3DECL(int) RTTarClose(RTTAR hTar);
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Open a file in the Tar archive.
|
---|
112 | *
|
---|
113 | * @returns IPRT status code.
|
---|
114 | *
|
---|
115 | * @param hTar The handle of the tar archive.
|
---|
116 | * @param phFile Where to store the handle to the opened file.
|
---|
117 | * @param pszFilename Path to the file which is to be opened. (UTF-8)
|
---|
118 | * @param fOpen Open flags, i.e a combination of the RTFILE_O_* defines.
|
---|
119 | * The ACCESS, ACTION flags are mandatory! DENY flags
|
---|
120 | * are currently not supported.
|
---|
121 | *
|
---|
122 | * @remarks Write mode means append mode only. It is not possible to make
|
---|
123 | * changes to existing files.
|
---|
124 | *
|
---|
125 | * @remarks Currently it is not possible to open more than one file in write
|
---|
126 | * mode. Although open more than one file in read only mode (even when
|
---|
127 | * one file is opened in write mode) is always possible.
|
---|
128 | */
|
---|
129 | RTR3DECL(int) RTTarFileOpen(RTTAR hTar, PRTTARFILE phFile, const char *pszFilename, uint32_t fOpen);
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Close the file opened by RTTarFileOpen.
|
---|
133 | *
|
---|
134 | * @returns IPRT status code.
|
---|
135 | *
|
---|
136 | * @param hFile The file handle to close.
|
---|
137 | */
|
---|
138 | RTR3DECL(int) RTTarFileClose(RTTARFILE hFile);
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Changes the read & write position in a file.
|
---|
142 | *
|
---|
143 | * @returns IPRT status code.
|
---|
144 | *
|
---|
145 | * @param hFile Handle to the file.
|
---|
146 | * @param offSeek Offset to seek.
|
---|
147 | * @param uMethod Seek method, i.e. one of the RTFILE_SEEK_* defines.
|
---|
148 | * @param poffActual Where to store the new file position.
|
---|
149 | * NULL is allowed.
|
---|
150 | */
|
---|
151 | RTR3DECL(int) RTTarFileSeek(RTTARFILE hFile, uint64_t offSeek, unsigned uMethod, uint64_t *poffActual);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Gets the current file position.
|
---|
155 | *
|
---|
156 | * @returns File offset.
|
---|
157 | * @returns UINT64_MAX on failure.
|
---|
158 | *
|
---|
159 | * @param hFile Handle to the file.
|
---|
160 | */
|
---|
161 | RTR3DECL(uint64_t) RTTarFileTell(RTTARFILE hFile);
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Read bytes from a file.
|
---|
165 | *
|
---|
166 | * @returns IPRT status code.
|
---|
167 | *
|
---|
168 | * @param hFile Handle to the file.
|
---|
169 | * @param pvBuf Where to put the bytes we read.
|
---|
170 | * @param cbToRead How much to read.
|
---|
171 | * @param *pcbRead How much we actually read .
|
---|
172 | * If NULL an error will be returned for a partial read.
|
---|
173 | */
|
---|
174 | RTR3DECL(int) RTTarFileRead(RTTARFILE hFile, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Read bytes from a file at a given offset.
|
---|
178 | * This function may modify the file position.
|
---|
179 | *
|
---|
180 | * @returns IPRT status code.
|
---|
181 | *
|
---|
182 | * @param hFile Handle to the file.
|
---|
183 | * @param off Where to read.
|
---|
184 | * @param pvBuf Where to put the bytes we read.
|
---|
185 | * @param cbToRead How much to read.
|
---|
186 | * @param *pcbRead How much we actually read .
|
---|
187 | * If NULL an error will be returned for a partial read.
|
---|
188 | */
|
---|
189 | RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Write bytes to a file.
|
---|
193 | *
|
---|
194 | * @returns IPRT status code.
|
---|
195 | *
|
---|
196 | * @param hFile Handle to the file.
|
---|
197 | * @param pvBuf What to write.
|
---|
198 | * @param cbToWrite How much to write.
|
---|
199 | * @param *pcbWritten How much we actually wrote.
|
---|
200 | * If NULL an error will be returned for a partial write.
|
---|
201 | */
|
---|
202 | RTR3DECL(int) RTTarFileWrite(RTTARFILE hFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Write bytes to a file at a given offset.
|
---|
206 | * This function may modify the file position.
|
---|
207 | *
|
---|
208 | * @returns IPRT status code.
|
---|
209 | *
|
---|
210 | * @param hFile Handle to the file.
|
---|
211 | * @param off Where to write.
|
---|
212 | * @param pvBuf What to write.
|
---|
213 | * @param cbToWrite How much to write.
|
---|
214 | * @param *pcbWritten How much we actually wrote.
|
---|
215 | * If NULL an error will be returned for a partial write.
|
---|
216 | */
|
---|
217 | RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Query the size of the file.
|
---|
221 | *
|
---|
222 | * @returns IPRT status code.
|
---|
223 | *
|
---|
224 | * @param hFile Handle to the file.
|
---|
225 | * @param pcbSize Where to store the filesize.
|
---|
226 | */
|
---|
227 | RTR3DECL(int) RTTarFileGetSize(RTTARFILE hFile, uint64_t *pcbSize);
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Set the size of the file.
|
---|
231 | *
|
---|
232 | * @returns IPRT status code.
|
---|
233 | *
|
---|
234 | * @param hFile Handle to the file.
|
---|
235 | * @param cbSize The new file size.
|
---|
236 | */
|
---|
237 | RTR3DECL(int) RTTarFileSetSize(RTTARFILE hFile, uint64_t cbSize);
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Gets the mode flags of an open file.
|
---|
241 | *
|
---|
242 | * @returns IPRT status code.
|
---|
243 | *
|
---|
244 | * @param hFile Handle to the file.
|
---|
245 | * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
|
---|
246 | */
|
---|
247 | RTR3DECL(int) RTTarFileGetMode(RTTARFILE hFile, uint32_t *pfMode);
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Changes the mode flags of an open file.
|
---|
251 | *
|
---|
252 | * @returns IPRT status code.
|
---|
253 | *
|
---|
254 | * @param hFile Handle to the file.
|
---|
255 | * @param fMode The new file mode, see @ref grp_rt_fs for details.
|
---|
256 | */
|
---|
257 | RTR3DECL(int) RTTarFileSetMode(RTTARFILE hFile, uint32_t fMode);
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Gets the modification timestamp of the file.
|
---|
261 | *
|
---|
262 | * @returns IPRT status code.
|
---|
263 | *
|
---|
264 | * @param pFile Handle to the file.
|
---|
265 | * @param pTime Where to store the time.
|
---|
266 | */
|
---|
267 | RTR3DECL(int) RTTarFileGetTime(RTTARFILE hFile, PRTTIMESPEC pTime);
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Sets the modification timestamp of the file.
|
---|
271 | *
|
---|
272 | * @returns IPRT status code.
|
---|
273 | *
|
---|
274 | * @param pFile Handle to the file.
|
---|
275 | * @param pTime The time to store.
|
---|
276 | */
|
---|
277 | RTR3DECL(int) RTTarFileSetTime(RTTARFILE hFile, PRTTIMESPEC pTime);
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Gets the owner and/or group of an open file.
|
---|
281 | *
|
---|
282 | * @returns IPRT status code.
|
---|
283 | *
|
---|
284 | * @param hFile Handle to the file.
|
---|
285 | * @param pUid Where to store the owner user id. NULL is ok.
|
---|
286 | * @param pGid Where to store the group id. NULL is ok.
|
---|
287 | */
|
---|
288 | RTR3DECL(int) RTTarFileGetOwner(RTTARFILE hFile, uint32_t *pUid, uint32_t *pGid);
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Changes the owner and/or group of an open file.
|
---|
292 | *
|
---|
293 | * @returns IPRT status code.
|
---|
294 | *
|
---|
295 | * @param hFile Handle to the file.
|
---|
296 | * @param uid The new file owner user id. Use -1 (or ~0) to leave this unchanged.
|
---|
297 | * @param gid The new group id. Use -1 (or ~0) to leave this unchanged.
|
---|
298 | */
|
---|
299 | RTR3DECL(int) RTTarFileSetOwner(RTTARFILE hFile, uint32_t uid, uint32_t gid);
|
---|
300 |
|
---|
301 | /******************************************************************************
|
---|
302 | * Convenience Functions *
|
---|
303 | ******************************************************************************/
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Check if the specified file exists in the Tar archive.
|
---|
307 | *
|
---|
308 | * (The matching is case sensitive.)
|
---|
309 | *
|
---|
310 | * @note Currently only regular files are supported.
|
---|
311 | *
|
---|
312 | * @returns IPRT status code.
|
---|
313 | * @retval VINF_SUCCESS when the file exists in the Tar archive.
|
---|
314 | * @retval VERR_FILE_NOT_FOUND when the file not exists in the Tar archive.
|
---|
315 | *
|
---|
316 | * @param pszTarFile Tar file to check.
|
---|
317 | * @param pszFile Filename to check for.
|
---|
318 | *
|
---|
319 | * @todo This is predicate function which SHALL return bool!
|
---|
320 | */
|
---|
321 | RTR3DECL(int) RTTarFileExists(const char *pszTarFile, const char *pszFile);
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Create a file list from a Tar archive.
|
---|
325 | *
|
---|
326 | * @note Currently only regular files are supported.
|
---|
327 | *
|
---|
328 | * @returns IPRT status code.
|
---|
329 | *
|
---|
330 | * @param pszTarFile Tar file to list files from.
|
---|
331 | * @param ppapszFiles On success an array with array with the filenames is
|
---|
332 | * returned. The names must be freed with RTStrFree and
|
---|
333 | * the array with RTMemFree.
|
---|
334 | * @param pcFiles On success the number of entries in ppapszFiles.
|
---|
335 | */
|
---|
336 | RTR3DECL(int) RTTarList(const char *pszTarFile, char ***ppapszFiles, size_t *pcFiles);
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Extract a file from a Tar archive into a memory buffer.
|
---|
340 | *
|
---|
341 | * The caller is responsible for the deletion of the returned memory buffer.
|
---|
342 | *
|
---|
343 | * (The matching is case sensitive.)
|
---|
344 | *
|
---|
345 | * @note Currently only regular files are supported. Also some of the header
|
---|
346 | * fields are not used (uid, gid, uname, gname, mtime).
|
---|
347 | *
|
---|
348 | * @returns IPRT status code.
|
---|
349 | *
|
---|
350 | * @param pszTarFile Tar file to extract files from.
|
---|
351 | * @param ppBuf The buffer which will held the extracted data.
|
---|
352 | * @param pcbSize The size (in bytes) of ppBuf after successful
|
---|
353 | * extraction.
|
---|
354 | * @param pszFile The file to extract.
|
---|
355 | * @param pfnProgressCallback Progress callback function. Optional.
|
---|
356 | * @param pvUser User defined data for the progress
|
---|
357 | * callback. Optional.
|
---|
358 | */
|
---|
359 | RTR3DECL(int) RTTarExtractFileToBuf(const char *pszTarFile, void **ppvBuf, size_t *pcbSize, const char *pszFile,
|
---|
360 | PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Extract a set of files from a Tar archive.
|
---|
364 | *
|
---|
365 | * Also note that this function is atomic. If an error occurs all previously
|
---|
366 | * extracted files will be deleted.
|
---|
367 | *
|
---|
368 | * (The matching is case sensitive.)
|
---|
369 | *
|
---|
370 | * @note Currently only regular files are supported. Also some of the header
|
---|
371 | * fields are not used (uid, gid, uname, gname, mtime).
|
---|
372 | *
|
---|
373 | * @returns IPRT status code.
|
---|
374 | *
|
---|
375 | * @param pszTarFile Tar file to extract files from.
|
---|
376 | * @param pszOutputDir Where to store the extracted files. Must exist.
|
---|
377 | * @param papszFiles Which files should be extracted.
|
---|
378 | * @param cFiles The number of files in papszFiles.
|
---|
379 | * @param pfnProgressCallback Progress callback function. Optional.
|
---|
380 | * @param pvUser User defined data for the progress
|
---|
381 | * callback. Optional.
|
---|
382 | */
|
---|
383 | RTR3DECL(int) RTTarExtractFiles(const char *pszTarFile, const char *pszOutputDir, const char * const *papszFiles, size_t cFiles, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Extract all files of the archive.
|
---|
387 | *
|
---|
388 | * @note Currently only regular files are supported. Also some of the header
|
---|
389 | * fields are not used (uid, gid, uname, gname, mtime).
|
---|
390 | *
|
---|
391 | * @returns IPRT status code.
|
---|
392 | *
|
---|
393 | * @param pszTarFile Tar file to extract the files from.
|
---|
394 | * @param pszOutputDir Where to store the extracted files. Must exist.
|
---|
395 | * @param pfnProgressCallback Progress callback function. Optional.
|
---|
396 | * @param pvUser User defined data for the progress
|
---|
397 | * callback. Optional.
|
---|
398 | */
|
---|
399 | RTR3DECL(int) RTTarExtractAll(const char *pszTarFile, const char *pszOutputDir, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Create a Tar archive out of the given files.
|
---|
403 | *
|
---|
404 | * @note Currently only regular files are supported.
|
---|
405 | *
|
---|
406 | * @returns IPRT status code.
|
---|
407 | *
|
---|
408 | * @param pszTarFile Where to create the Tar archive.
|
---|
409 | * @param papszFiles Which files should be included.
|
---|
410 | * @param cFiles The number of files in papszFiles.
|
---|
411 | * @param pfnProgressCallback Progress callback function. Optional.
|
---|
412 | * @param pvUser User defined data for the progress
|
---|
413 | * callback. Optional.
|
---|
414 | */
|
---|
415 | RTR3DECL(int) RTTarCreate(const char *pszTarFile, const char * const *papszFiles, size_t cFiles, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
416 |
|
---|
417 | /******************************************************************************
|
---|
418 | * Streaming Functions *
|
---|
419 | ******************************************************************************/
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Return the filename where RTTar currently stays at.
|
---|
423 | *
|
---|
424 | * @returns IPRT status code.
|
---|
425 | *
|
---|
426 | * @param hTar Handle to the RTTAR interface.
|
---|
427 | * @param ppszFilename On success the filename.
|
---|
428 | */
|
---|
429 | RTR3DECL(int) RTTarCurrentFile(RTTAR hTar, char **ppszFilename);
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Jumps to the next file from the current RTTar position.
|
---|
433 | *
|
---|
434 | * @returns IPRT status code.
|
---|
435 | *
|
---|
436 | * @param hTar Handle to the RTTAR interface.
|
---|
437 | */
|
---|
438 | RTR3DECL(int) RTTarSeekNextFile(RTTAR hTar);
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Opens the file where RTTar currently stays at.
|
---|
442 | *
|
---|
443 | * @returns IPRT status code.
|
---|
444 | *
|
---|
445 | * @param hTar Handle to the RTTAR interface.
|
---|
446 | * @param phFile Where to store the handle to the opened file.
|
---|
447 | * @param ppszFilename On success the filename.
|
---|
448 | * @param fOpen Open flags, i.e a combination of the RTFILE_O_* defines.
|
---|
449 | * The ACCESS, ACTION flags are mandatory! Currently
|
---|
450 | * only RTFILE_O_OPEN | RTFILE_O_READ is supported.
|
---|
451 | */
|
---|
452 | RTR3DECL(int) RTTarFileOpenCurrentFile(RTTAR hTar, PRTTARFILE phFile, char **ppszFilename, uint32_t fOpen);
|
---|
453 |
|
---|
454 |
|
---|
455 | /** @} */
|
---|
456 |
|
---|
457 | RT_C_DECLS_END
|
---|
458 |
|
---|
459 | #endif /* ___iprt_tar_h */
|
---|
460 |
|
---|