summaryrefslogtreecommitdiff
path: root/include/rpm/rpmfileutil.h
blob: a2396d494aadaca349e6b30a5efbe5b4175f680f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifndef _RPMFILEUTIL_H
#define _RPMFILEUTIL_H

/** \ingroup rpmfileutil rpmio
 * \file rpmfileutil.h
 * File and path manipulation helper functions.
 */

#include <rpm/rpmutil.h>
#include <rpm/rpmio.h>
#include <rpm/argv.h>

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup rpmfileutil
 */
typedef enum rpmCompressedMagic_e {
    COMPRESSED_NOT		= 0,	/*!< not compressed */
    COMPRESSED_OTHER		= 1,	/*!< gzip can handle */
    COMPRESSED_BZIP2		= 2,	/*!< bzip2 can handle */
    COMPRESSED_ZIP		= 3,	/*!< unzip can handle */
    COMPRESSED_LZMA		= 4,	/*!< lzma can handle */
    COMPRESSED_XZ		= 5,	/*!< xz can handle */
    COMPRESSED_LZIP		= 6,	/*!< lzip can handle */
    COMPRESSED_LRZIP		= 7,	/*!< lrzip can handle */
    COMPRESSED_7ZIP		= 8,	/*!< 7zip can handle */
    COMPRESSED_GEM		= 9,	/*!< gem can handle */
    COMPRESSED_ZSTD		= 10	/*!< zstd can handle */
} rpmCompressedMagic;

/** \ingroup rpmfileutil
 * RPM glob flags, largely modelled after glob(3) flags.
 */
typedef enum rpmglobFlags_e {
    RPMGLOB_NONE		= 0,
    RPMGLOB_NOCHECK		= (1 << 0), /*!< same as GLOB_NOCHECK */
} rpmglobFlags;

/** \ingroup rpmfileutil
 * Calculate a file digest and size.
 * @param algo		digest algorithm
 * @param fn		file name
 * @param asAscii	return digest as ascii string?
 * @param[out] digest	address of calculated digest
 * @return		0 on success, 1 on error
 */
int rpmDoDigest(int algo, const char * fn,int asAscii, unsigned char * digest);

/** \ingroup rpmfileutil
 * Thin wrapper for mkstemp(3). 
 * @param templ			template for temporary filename
 * @return 			file handle or NULL on error
 */
FD_t rpmMkTemp(char *templ);

/** \ingroup rpmfileutil
 * Return file handle for a temporaray file.
 * A unique temporaray file path will be created in
 * [prefix/]%{_tmppath} directory.
 * The file name and the open file handle are returned.
 *
 * @param prefix	leading part of temp file path
 * @param[out] fn		temp file name (or NULL)
 * @return fdptr	open file handle or NULL on error
 */
FD_t rpmMkTempFile(const char * prefix, char **fn);

/** \ingroup rpmfileutil
 * Insure that directories in path exist, creating as needed.
 * @param path		directory path
 * @param mode		directory mode (if created)
 * @param uid		directory uid (if created), or -1 to skip
 * @param gid		directory uid (if created), or -1 to skip
 * @return		0 on success, errno (or -1) on error
 */
int rpmioMkpath(const char * path, mode_t mode, uid_t uid, gid_t gid);

/** \ingroup rpmfileutil
 * Create several directories (including parents if needed) in one go.
 * Macros in pathstr will be expanded in the process.
 * @param root		leading root directory (or NULL for none)
 * @param pathstr	list of directories separated with :
 * @return		0 if all directories were successfully created
 * 			(or already existed), non-zero otherwise
 */
int rpmMkdirs(const char *root, const char *pathstr);

/** \ingroup rpmfileutil
 * Canonicalize file path.
 * @param path		path to canonicalize (in-place)
 * @return		pointer to path
 */
char * rpmCleanPath	(char * path);

/** \ingroup rpmfileutil
 * Merge 3 args into path, any or all of which may be a url.
 * The leading part of the first URL encountered is used
 * for the result, other URL prefixes are discarded, permitting
 * a primitive form of URL inheiritance.
 * @param urlroot	root URL (often path to chroot, or NULL)
 * @param urlmdir	directory URL (often a directory, or NULL)
 * @param urlfile	file URL (often a file, or NULL)
 * @return		expanded, merged, canonicalized path (malloc'ed)
 */
char * rpmGenPath	(const char * urlroot,
			const char * urlmdir,
			const char * urlfile);

/** \ingroup rpmfileutil
 * Return (malloc'ed) expanded, canonicalized, file path.
 * @param path		macro(s) to expand (NULL terminates list)
 * @return		canonicalized path (malloc'ed)
 */
char * rpmGetPath (const char * path, ...) RPM_GNUC_NULL_TERMINATED;

/** \ingroup rpmfileutil
 * Expand a glob pattern into matching paths.
 * A pattern that is not a glob is returned as is.
 * @param pattern	glob pattern
 * @param flags		bit(s) to control glob operation
 * @param[out] *argcPtr	no. of paths
 * @param[out] *argvPtr	ARGV_t array of paths
 * @return		0 on success
 */
int rpmGlobPath(const char * pattern, rpmglobFlags flags,
		int * argcPtr, ARGV_t * argvPtr);

/** \ingroup rpmfileutil
 * Expand a glob pattern into matching paths, fail if nothing matches.
 * A pattern that is not a glob is returned as is.
 * @param pattern	glob pattern
 * @param[out] *argcPtr	no. of paths
 * @param[out] *argvPtr	ARGV_t array of paths
 * @return		0 on success
 */
int rpmGlob(const char * pattern, int * argcPtr, ARGV_t * argvPtr);

/** \ingroup rpmfileutil
 * Escape isspace(3) characters in string.
 * @param s             string
 * @return              escaped string
 */
char * rpmEscapeSpaces(const char * s);

/** \ingroup rpmfileutil
 * Return type of compression used in file.
 * @param file		name of file
 * @param[out] compressed	address of compression type
 * @return		0 on success, 1 on I/O error
 */
int rpmFileIsCompressed (const char * file, rpmCompressedMagic * compressed);

/** \ingroup rpmfileutil
 * Check if path (string) ends with given suffix
 * @param path		(path) string
 * @param suffix	suffix string to check for
 * @return		1 if true, 0 otherwise
 */
int rpmFileHasSuffix(const char *path, const char *suffix);

/** \ingroup rpmfileutil
 * Like getcwd() but the result is malloced.
 * @return              current working directory (malloc'ed)
 */
char * rpmGetCwd(void);

#ifdef __cplusplus
}
#endif
#endif /* _RPMFILEUTIL_H */