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 26
#ifndef LIBVIRT_VIRFILE_H
# define LIBVIRT_VIRFILE_H
27

28
# include <dirent.h>
29 30

# include "internal.h"
31
# include "virbitmap.h"
32
# include "virstoragefile.h"
33
# include "virautoclean.h"
34

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

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

49
/* Don't call these directly - use the macros below */
50
int virFileClose(int *fdptr, virFileCloseFlags flags)
51
        ATTRIBUTE_RETURN_CHECK;
E
Eric Blake 已提交
52 53
int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK;
FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
54

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

60
/* For use on normal paths; caller must check return value,
61
   and failure sets errno per close. */
62
# define VIR_CLOSE(FD) virFileClose(&(FD), 0)
E
Eric Blake 已提交
63
# define VIR_FCLOSE(FILE) virFileFclose(&(FILE), false)
64 65

/* Wrapper around fdopen that consumes fd on success. */
E
Eric Blake 已提交
66
# define VIR_FDOPEN(FD, MODE) virFileFdopen(&(FD), MODE)
67 68 69

/* For use on cleanup paths; errno is unaffected by close,
   and no return value to worry about. */
70
# define VIR_FORCE_CLOSE(FD) virForceCloseHelper(&(FD))
E
Eric Blake 已提交
71
# define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
72

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

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

85 86 87 88 89 90 91 92 93 94
/**
 * 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.
 */
# define VIR_AUTOCLOSE __attribute__((cleanup(virForceCloseHelper))) int


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

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

int virFileDirectFdFlag(void);

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

virFileWrapperFdPtr virFileWrapperFdNew(int *fd,
                                        const char *name,
                                        unsigned int flags)
111 112
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;

J
Jiri Denemark 已提交
113
int virFileWrapperFdClose(virFileWrapperFdPtr dfd);
114

J
Jiri Denemark 已提交
115
void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
116

117 118 119 120
int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock)
    ATTRIBUTE_NOINLINE;
int virFileUnlock(int fd, off_t start, off_t len)
    ATTRIBUTE_NOINLINE;
121

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

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

133 134
int virFileTouch(const char *path, mode_t mode);

135 136 137 138
int virFileUpdatePerm(const char *path,
                      mode_t mode_remove,
                      mode_t mode_add);

139 140 141
int virFileLoopDeviceAssociate(const char *file,
                               char **dev);

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

147 148
int virFileDeleteTree(const char *dir);

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

int virFileWriteStr(const char *path, const char *str, mode_t mode)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;

int virFileLinkPointsTo(const char *checkLink,
166 167 168 169 170 171
                        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);
172 173 174 175 176 177 178 179 180

int virFileResolveLink(const char *linkpath,
                       char **resultpath) ATTRIBUTE_RETURN_CHECK;
int virFileResolveAllLinks(const char *linkpath,
                           char **resultpath) ATTRIBUTE_RETURN_CHECK;

int virFileIsLink(const char *linkpath)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;

181 182 183
int virFileReadLink(const char *linkpath, char **resultpath)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;

184 185
char *virFindFileInPath(const char *file);

186 187 188 189 190 191 192 193 194 195 196 197 198 199
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);
void virFileActivateDirOverride(const char *argv0)
    ATTRIBUTE_NONNULL(1);

L
Laine Stump 已提交
200
off_t virFileLength(const char *path, int fd) ATTRIBUTE_NONNULL(1);
201
bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
202
bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NOINLINE;
203
bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
204
bool virFileIsRegular(const char *file) ATTRIBUTE_NONNULL(1);
205

206 207 208 209 210 211 212
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),
213
    VIR_FILE_SHFS_CEPH = (1 << 6),
214
    VIR_FILE_SHFS_GPFS = (1 << 7),
215 216 217 218
};

int virFileIsSharedFSType(const char *path, int fstypes) ATTRIBUTE_NONNULL(1);
int virFileIsSharedFS(const char *path) ATTRIBUTE_NONNULL(1);
219
int virFileIsMountPoint(const char *file) ATTRIBUTE_NONNULL(1);
220 221
int virFileIsCDROM(const char *path)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
222

223 224 225 226 227 228 229 230 231
int virFileGetMountSubtree(const char *mtabpath,
                           const char *prefix,
                           char ***mountsret,
                           size_t *nmountsret) ATTRIBUTE_RETURN_CHECK;
int virFileGetMountReverseSubtree(const char *mtabpath,
                                  const char *prefix,
                                  char ***mountsret,
                                  size_t *nmountsret) ATTRIBUTE_RETURN_CHECK;

232
char *virFileSanitizePath(const char *path);
233
char *virFileCanonicalizePath(const char *path) ATTRIBUTE_NOINLINE;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

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)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
int virFileOpenAs(const char *path, int openflags, mode_t mode,
                  uid_t uid, gid_t gid,
                  unsigned int flags)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
249
int virFileRemove(const char *path, uid_t uid, gid_t gid);
250

251 252 253
int virFileChownFiles(const char *name, uid_t uid, gid_t gid)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;

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

273 274 275
int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
int virFileMakePathWithMode(const char *path,
                            mode_t mode) ATTRIBUTE_RETURN_CHECK;
276
int virFileMakeParentPath(const char *path) ATTRIBUTE_RETURN_CHECK;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

char *virFileBuildPath(const char *dir,
                       const char *name,
                       const char *ext) ATTRIBUTE_RETURN_CHECK;


# ifdef WIN32
/* 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.
 */
#  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 ";"

# else  /* !WIN32 */

#  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 ":"

# endif /* !WIN32 */

bool virFileIsAbsPath(const char *path);
int virFileAbsPath(const char *path,
                   char **abspath) ATTRIBUTE_RETURN_CHECK;
const char *virFileSkipRoot(const char *path);
308
void virFileRemoveLastComponent(char *path);
309 310 311 312 313 314 315 316 317 318 319 320

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

char *virFileFindMountPoint(const char *type);

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

321 322 323
int virFilePrintf(FILE *fp, const char *msg, ...)
    ATTRIBUTE_FMT_PRINTF(2, 3);

324 325 326 327 328 329 330 331 332 333 334 335
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);
336

337 338 339
virHugeTLBFSPtr virFileGetDefaultHugepage(virHugeTLBFSPtr fs,
                                          size_t nfs);

340 341 342 343 344
int virFileSetupDev(const char *path,
                    const char *mount_options);

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

346 347 348
int virFileMoveMount(const char *src,
                     const char *dst);

M
Michal Privoznik 已提交
349 350 351 352 353 354 355 356 357 358
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);
359 360

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

362 363 364 365 366 367 368 369 370 371 372
int virFileReadValueInt(int *value, const char *format, ...)
 ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueUint(unsigned int *value, const char *format, ...)
 ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...)
 ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
 ATTRIBUTE_FMT_PRINTF(2, 3);
int virFileReadValueString(char **value, const char *format, ...)
 ATTRIBUTE_FMT_PRINTF(2, 3);

373 374
int virFileWaitForExists(const char *path, size_t ms, size_t tries);

375

M
Michal Privoznik 已提交
376 377 378 379
int virFileInData(int fd,
                  int *inData,
                  long long *length);

380
VIR_DEFINE_AUTOPTR_FUNC(virFileWrapperFd, virFileWrapperFdFree);
381

382 383
int virFileGetXAttr(const char *path,
                    const char *name,
384 385
                    char **value)
    ATTRIBUTE_NOINLINE;
386 387 388

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

int virFileRemoveXAttr(const char *path,
393 394
                       const char *name)
    ATTRIBUTE_NOINLINE;
395

396
#endif /* LIBVIRT_VIRFILE_H */