virfile.h 14.2 KB
Newer Older
1
/*
E
Eric Blake 已提交
2
 * virfile.h: safer file handling
3
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright (C) 2010 IBM Corporation
 * Copyright (C) 2010 Stefan Berger
 * Copyright (C) 2010 Eric Blake
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library.  If not, see
O
Osier Yang 已提交
21
 * <http://www.gnu.org/licenses/>.
22 23 24
 *
 */

25
#pragma once
26

27
#include <dirent.h>
28

29 30 31
#include "internal.h"
#include "virbitmap.h"
#include "virstoragefile.h"
32

33
typedef enum {
34 35 36 37
    VIR_FILE_CLOSE_PRESERVE_ERRNO = 1 << 0,
    VIR_FILE_CLOSE_IGNORE_EBADF = 1 << 1,
    VIR_FILE_CLOSE_DONT_LOG = 1 << 2,
} virFileCloseFlags;
38

39
ssize_t saferead(int fd, void *buf, size_t count) G_GNUC_WARN_UNUSED_RESULT;
40
ssize_t safewrite(int fd, const void *buf, size_t count)
41
    G_GNUC_WARN_UNUSED_RESULT;
42
int safezero(int fd, off_t offset, off_t len)
43
    G_GNUC_WARN_UNUSED_RESULT;
44
int virFileAllocate(int fd, off_t offset, off_t len)
45
    G_GNUC_WARN_UNUSED_RESULT;
46

47
/* Don't call these directly - use the macros below */
48
int virFileClose(int *fdptr, virFileCloseFlags flags)
49 50 51
        G_GNUC_WARN_UNUSED_RESULT;
int virFileFclose(FILE **file, bool preserve_errno) G_GNUC_WARN_UNUSED_RESULT;
FILE *virFileFdopen(int *fdptr, const char *mode) G_GNUC_WARN_UNUSED_RESULT;
52

53 54 55 56 57
static inline void virForceCloseHelper(int *fd)
{
    ignore_value(virFileClose(fd, VIR_FILE_CLOSE_PRESERVE_ERRNO));
}

58
/* For use on normal paths; caller must check return value,
59
   and failure sets errno per close. */
60 61
#define VIR_CLOSE(FD) virFileClose(&(FD), 0)
#define VIR_FCLOSE(FILE) virFileFclose(&(FILE), false)
62 63

/* Wrapper around fdopen that consumes fd on success. */
64
#define VIR_FDOPEN(FD, MODE) virFileFdopen(&(FD), MODE)
65 66 67

/* For use on cleanup paths; errno is unaffected by close,
   and no return value to worry about. */
68 69
#define VIR_FORCE_CLOSE(FD) virForceCloseHelper(&(FD))
#define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
70

71 72
/* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expected
 * during mass close after fork(). */
73
#define VIR_MASS_CLOSE(FD) \
74 75
    ignore_value(virFileClose(&(FD), \
                 VIR_FILE_CLOSE_PRESERVE_ERRNO | \
76 77
                 VIR_FILE_CLOSE_IGNORE_EBADF))

78
#define VIR_LOG_CLOSE(FD) \
79 80
    ignore_value(virFileClose(&(FD), \
                 VIR_FILE_CLOSE_PRESERVE_ERRNO | \
81
                 VIR_FILE_CLOSE_DONT_LOG))
82

83 84 85 86 87 88 89
/**
 * VIR_AUTOCLOSE:
 *
 * Macro to automatically force close the fd by calling virForceCloseHelper
 * when the fd goes out of scope. It's used to eliminate VIR_FORCE_CLOSE
 * in cleanup sections.
 */
90
#define VIR_AUTOCLOSE __attribute__((cleanup(virForceCloseHelper))) int
91 92


J
Jiri Denemark 已提交
93 94
/* Opaque type for managing a wrapper around a fd.  */
struct _virFileWrapperFd;
95

J
Jiri Denemark 已提交
96 97
typedef struct _virFileWrapperFd virFileWrapperFd;
typedef virFileWrapperFd *virFileWrapperFdPtr;
98 99 100

int virFileDirectFdFlag(void);

101
typedef enum {
J
Jiri Denemark 已提交
102 103
    VIR_FILE_WRAPPER_BYPASS_CACHE   = (1 << 0),
    VIR_FILE_WRAPPER_NON_BLOCKING   = (1 << 1),
104
} virFileWrapperFdFlags;
J
Jiri Denemark 已提交
105 106 107 108

virFileWrapperFdPtr virFileWrapperFdNew(int *fd,
                                        const char *name,
                                        unsigned int flags)
109
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
110

J
Jiri Denemark 已提交
111
int virFileWrapperFdClose(virFileWrapperFdPtr dfd);
112

J
Jiri Denemark 已提交
113
void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
114

115
int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock)
116
    G_GNUC_NO_INLINE;
117
int virFileUnlock(int fd, off_t start, off_t len)
118
    G_GNUC_NO_INLINE;
119

M
Martin Kletzander 已提交
120 121
int virFileFlock(int fd, bool lock, bool shared);

122
typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
123 124 125
int virFileRewrite(const char *path,
                   mode_t mode,
                   virFileRewriteFunc rewrite,
126 127 128 129
                   const void *opaque);
int virFileRewriteStr(const char *path,
                      mode_t mode,
                      const char *str);
130

131 132
int virFileTouch(const char *path, mode_t mode);

133 134 135 136
int virFileUpdatePerm(const char *path,
                      mode_t mode_remove,
                      mode_t mode_add);

137 138 139
int virFileLoopDeviceAssociate(const char *file,
                               char **dev);

140
int virFileNBDDeviceAssociate(const char *file,
141
                              virStorageFileFormat fmt,
142 143 144
                              bool readonly,
                              char **dev);

145 146
int virFileDeleteTree(const char *dir);

147
int virFileReadHeaderFD(int fd, int maxlen, char **buf)
148
    G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(3);
149
int virFileReadHeaderQuiet(const char *path, int maxlen, char **buf)
150
    G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
151
int virFileReadLimFD(int fd, int maxlen, char **buf)
152
    G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(3);
153
int virFileReadAll(const char *path, int maxlen, char **buf)
154
    G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
J
Ján Tomko 已提交
155
int virFileReadAllQuiet(const char *path, int maxlen, char **buf)
156
    G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
157
int virFileReadBufQuiet(const char *file, char *buf, int len)
158
    G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
159 160

int virFileWriteStr(const char *path, const char *str, mode_t mode)
161
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
162 163

int virFileLinkPointsTo(const char *checkLink,
164 165 166 167 168 169
                        const char *checkDest)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
int virFileRelLinkPointsTo(const char *directory,
                           const char *checkLink,
                           const char *checkDest)
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
170 171

int virFileResolveLink(const char *linkpath,
172
                       char **resultpath) G_GNUC_WARN_UNUSED_RESULT;
173
int virFileResolveAllLinks(const char *linkpath,
174
                           char **resultpath) G_GNUC_WARN_UNUSED_RESULT;
175 176

int virFileIsLink(const char *linkpath)
177
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
178 179 180

char *virFindFileInPath(const char *file);

181 182 183 184 185 186 187 188 189 190 191
char *virFileFindResource(const char *filename,
                          const char *builddir,
                          const char *installdir)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
char *virFileFindResourceFull(const char *filename,
                              const char *prefix,
                              const char *suffix,
                              const char *builddir,
                              const char *installdir,
                              const char *envname)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5);
192
void virFileActivateDirOverrideForProg(const char *argv0)
193
    ATTRIBUTE_NONNULL(1);
194
void virFileActivateDirOverrideForLib(void);
195

L
Laine Stump 已提交
196
off_t virFileLength(const char *path, int fd) ATTRIBUTE_NONNULL(1);
197
bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
198
bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1) G_GNUC_NO_INLINE;
199
bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
200
bool virFileIsRegular(const char *file) ATTRIBUTE_NONNULL(1);
201

202 203 204 205 206 207 208
enum {
    VIR_FILE_SHFS_NFS = (1 << 0),
    VIR_FILE_SHFS_GFS2 = (1 << 1),
    VIR_FILE_SHFS_OCFS = (1 << 2),
    VIR_FILE_SHFS_AFS = (1 << 3),
    VIR_FILE_SHFS_SMB = (1 << 4),
    VIR_FILE_SHFS_CIFS = (1 << 5),
209
    VIR_FILE_SHFS_CEPH = (1 << 6),
210
    VIR_FILE_SHFS_GPFS = (1 << 7),
211
    VIR_FILE_SHFS_QB = (1 << 8),
212
    VIR_FILE_SHFS_ACFS = (1 << 9),
213 214 215 216
};

int virFileIsSharedFSType(const char *path, int fstypes) ATTRIBUTE_NONNULL(1);
int virFileIsSharedFS(const char *path) ATTRIBUTE_NONNULL(1);
217
int virFileIsMountPoint(const char *file) ATTRIBUTE_NONNULL(1);
218
int virFileIsCDROM(const char *path)
219
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
220

221 222 223
int virFileGetMountSubtree(const char *mtabpath,
                           const char *prefix,
                           char ***mountsret,
224
                           size_t *nmountsret) G_GNUC_WARN_UNUSED_RESULT;
225 226 227
int virFileGetMountReverseSubtree(const char *mtabpath,
                                  const char *prefix,
                                  char ***mountsret,
228
                                  size_t *nmountsret) G_GNUC_WARN_UNUSED_RESULT;
229

230
char *virFileSanitizePath(const char *path);
231
char *virFileCanonicalizePath(const char *path) G_GNUC_NO_INLINE;
232 233 234 235 236 237 238 239 240 241

enum {
    VIR_FILE_OPEN_NONE        = 0,
    VIR_FILE_OPEN_NOFORK      = (1 << 0),
    VIR_FILE_OPEN_FORK        = (1 << 1),
    VIR_FILE_OPEN_FORCE_MODE  = (1 << 2),
    VIR_FILE_OPEN_FORCE_OWNER = (1 << 3),
};
int virFileAccessibleAs(const char *path, int mode,
                        uid_t uid, gid_t gid)
242
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
243 244 245
int virFileOpenAs(const char *path, int openflags, mode_t mode,
                  uid_t uid, gid_t gid,
                  unsigned int flags)
246
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
247
int virFileRemove(const char *path, uid_t uid, gid_t gid);
248

249
int virFileChownFiles(const char *name, uid_t uid, gid_t gid)
250
    ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT;
251

252 253 254
enum {
    VIR_DIR_CREATE_NONE        = 0,
    VIR_DIR_CREATE_AS_UID      = (1 << 0),
255
    VIR_DIR_CREATE_ALLOW_EXIST = (1 << 1),
256 257
};
int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
258
                 unsigned int flags) G_GNUC_WARN_UNUSED_RESULT;
J
Ján Tomko 已提交
259
int virDirOpen(DIR **dirp, const char *dirname)
260
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
J
Ján Tomko 已提交
261
int virDirOpenIfExists(DIR **dirp, const char *dirname)
262
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
J
Ján Tomko 已提交
263
int virDirOpenQuiet(DIR **dirp, const char *dirname)
264
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
265
int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
266
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
J
Ján Tomko 已提交
267 268
void virDirClose(DIR **dirp)
    ATTRIBUTE_NONNULL(1);
269
#define VIR_DIR_CLOSE(dir)  virDirClose(&(dir))
270

271
int virFileMakePath(const char *path) G_GNUC_WARN_UNUSED_RESULT;
272
int virFileMakePathWithMode(const char *path,
273 274
                            mode_t mode) G_GNUC_WARN_UNUSED_RESULT;
int virFileMakeParentPath(const char *path) G_GNUC_WARN_UNUSED_RESULT;
275 276 277

char *virFileBuildPath(const char *dir,
                       const char *name,
278
                       const char *ext) G_GNUC_WARN_UNUSED_RESULT;
279 280


281
#ifdef WIN32
282 283 284 285
/* On Win32, the canonical directory separator is the backslash, and
 * the search path separator is the semicolon. Note that also the
 * (forward) slash works as directory separator.
 */
286 287 288 289 290
# define VIR_FILE_DIR_SEPARATOR '\\'
# define VIR_FILE_DIR_SEPARATOR_S "\\"
# define VIR_FILE_IS_DIR_SEPARATOR(c) ((c) == VIR_FILE_DIR_SEPARATOR || (c) == '/')
# define VIR_FILE_PATH_SEPARATOR ';'
# define VIR_FILE_PATH_SEPARATOR_S ";"
291

292
#else  /* !WIN32 */
293

294 295 296 297 298
# define VIR_FILE_DIR_SEPARATOR '/'
# define VIR_FILE_DIR_SEPARATOR_S "/"
# define VIR_FILE_IS_DIR_SEPARATOR(c) ((c) == VIR_FILE_DIR_SEPARATOR)
# define VIR_FILE_PATH_SEPARATOR ':'
# define VIR_FILE_PATH_SEPARATOR_S ":"
299

300
#endif /* !WIN32 */
301 302 303

bool virFileIsAbsPath(const char *path);
int virFileAbsPath(const char *path,
304
                   char **abspath) G_GNUC_WARN_UNUSED_RESULT;
305
void virFileRemoveLastComponent(char *path);
306 307 308 309 310 311 312 313

int virFileOpenTty(int *ttymaster,
                   char **ttyName,
                   int rawmode);

char *virFileFindMountPoint(const char *type);

/* NB: this should be combined with virFileBuildPath */
314
#define virBuildPath(path, ...) \
315
    virBuildPathInternal(path, __VA_ARGS__, NULL)
316
int virBuildPathInternal(char **path, ...) G_GNUC_NULL_TERMINATED;
317

318
int virFilePrintf(FILE *fp, const char *msg, ...)
319
    G_GNUC_PRINTF(2, 3);
320

321 322 323 324 325 326 327 328 329 330 331 332
typedef struct _virHugeTLBFS virHugeTLBFS;
typedef virHugeTLBFS *virHugeTLBFSPtr;
struct _virHugeTLBFS {
    char *mnt_dir;                  /* Where the FS is mount to */
    unsigned long long size;        /* page size in kibibytes */
    bool deflt;                     /* is this the default huge page size */
};

int virFileGetHugepageSize(const char *path,
                           unsigned long long *size);
int virFileFindHugeTLBFS(virHugeTLBFSPtr *ret_fs,
                         size_t *ret_nfs);
333

334 335 336
virHugeTLBFSPtr virFileGetDefaultHugepage(virHugeTLBFSPtr fs,
                                          size_t nfs);

337 338 339 340 341
int virFileSetupDev(const char *path,
                    const char *mount_options);

int virFileBindMountDevice(const char *src,
                           const char *dst);
M
Michal Privoznik 已提交
342

343 344 345
int virFileMoveMount(const char *src,
                     const char *dst);

M
Michal Privoznik 已提交
346 347 348 349 350 351 352 353 354 355
int virFileGetACLs(const char *file,
                   void **acl);

int virFileSetACLs(const char *file,
                   void *acl);

void virFileFreeACLs(void **acl);

int virFileCopyACLs(const char *src,
                    const char *dst);
356 357

int virFileComparePaths(const char *p1, const char *p2);
358

359
int virFileReadValueInt(int *value, const char *format, ...)
360
 G_GNUC_PRINTF(2, 3);
361
int virFileReadValueUint(unsigned int *value, const char *format, ...)
362
 G_GNUC_PRINTF(2, 3);
363
int virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...)
364
 G_GNUC_PRINTF(2, 3);
365
int virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
366
 G_GNUC_PRINTF(2, 3);
367
int virFileReadValueString(char **value, const char *format, ...)
368
 G_GNUC_PRINTF(2, 3);
369

370 371
int virFileWaitForExists(const char *path, size_t ms, size_t tries);

372

M
Michal Privoznik 已提交
373 374 375 376
int virFileInData(int fd,
                  int *inData,
                  long long *length);

377
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virFileWrapperFd, virFileWrapperFdFree);
378

379 380
int virFileGetXAttr(const char *path,
                    const char *name,
381
                    char **value)
382
    G_GNUC_NO_INLINE;
383

384 385 386
int virFileGetXAttrQuiet(const char *path,
                         const char *name,
                         char **value)
387
    G_GNUC_NO_INLINE;
388

389 390
int virFileSetXAttr(const char *path,
                    const char *name,
391
                    const char *value)
392
    G_GNUC_NO_INLINE;
393 394

int virFileRemoveXAttr(const char *path,
395
                       const char *name)
396
    G_GNUC_NO_INLINE;