virfile.h 3.8 KB
Newer Older
1
/*
E
Eric Blake 已提交
2
 * virfile.h: safer file handling
3
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2011 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 27 28
 *
 */


#ifndef __VIR_FILES_H_
# define __VIR_FILES_H_

29
# include <stdio.h>
30 31 32

# include "internal.h"

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

39
/* Don't call these directly - use the macros below */
40
int virFileClose(int *fdptr, virFileCloseFlags flags)
41
        ATTRIBUTE_RETURN_CHECK;
E
Eric Blake 已提交
42 43
int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK;
FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
44 45

/* For use on normal paths; caller must check return value,
46
   and failure sets errno per close. */
47
# define VIR_CLOSE(FD) virFileClose(&(FD), 0)
E
Eric Blake 已提交
48
# define VIR_FCLOSE(FILE) virFileFclose(&(FILE), false)
49 50

/* Wrapper around fdopen that consumes fd on success. */
E
Eric Blake 已提交
51
# define VIR_FDOPEN(FD, MODE) virFileFdopen(&(FD), MODE)
52 53 54

/* For use on cleanup paths; errno is unaffected by close,
   and no return value to worry about. */
55 56
# define VIR_FORCE_CLOSE(FD) \
    ignore_value(virFileClose(&(FD), VIR_FILE_CLOSE_PRESERVE_ERRNO))
E
Eric Blake 已提交
57
# define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
58

59 60
/* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expected
 * during mass close after fork(). */
61 62 63 64 65 66 67 68 69
# define VIR_MASS_CLOSE(FD)                         \
    ignore_value(virFileClose(&(FD),                \
                 VIR_FILE_CLOSE_PRESERVE_ERRNO |    \
                 VIR_FILE_CLOSE_IGNORE_EBADF))

# define VIR_LOG_CLOSE(FD)                          \
    ignore_value(virFileClose(&(FD),                \
                 VIR_FILE_CLOSE_PRESERVE_ERRNO |    \
                 VIR_FILE_CLOSE_DONT_LOG))
70

J
Jiri Denemark 已提交
71 72
/* Opaque type for managing a wrapper around a fd.  */
struct _virFileWrapperFd;
73

J
Jiri Denemark 已提交
74 75
typedef struct _virFileWrapperFd virFileWrapperFd;
typedef virFileWrapperFd *virFileWrapperFdPtr;
76 77 78

int virFileDirectFdFlag(void);

L
Lincoln Myers 已提交
79
enum virFileWrapperFdFlags {
J
Jiri Denemark 已提交
80 81
    VIR_FILE_WRAPPER_BYPASS_CACHE   = (1 << 0),
    VIR_FILE_WRAPPER_NON_BLOCKING   = (1 << 1),
L
Lincoln Myers 已提交
82
};
J
Jiri Denemark 已提交
83 84 85 86

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

J
Jiri Denemark 已提交
89
int virFileWrapperFdClose(virFileWrapperFdPtr dfd);
90

J
Jiri Denemark 已提交
91
void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
92

93 94 95
int virFileLock(int fd, bool shared, off_t start, off_t len);
int virFileUnlock(int fd, off_t start, off_t len);

96 97 98 99 100 101
typedef int (*virFileRewriteFunc)(int fd, void *opaque);
int virFileRewrite(const char *path,
                   mode_t mode,
                   virFileRewriteFunc rewrite,
                   void *opaque);

102 103
int virFileTouch(const char *path, mode_t mode);

104 105 106 107
int virFileUpdatePerm(const char *path,
                      mode_t mode_remove,
                      mode_t mode_add);

108 109 110
int virFileLoopDeviceAssociate(const char *file,
                               char **dev);

111 112
int virFileDeleteTree(const char *dir);

113
#endif /* __VIR_FILES_H */