2010-10-19 14:23:51 +00:00
|
|
|
/*
|
2011-07-19 18:32:58 +00:00
|
|
|
* virfile.h: safer file handling
|
2010-10-19 14:23:51 +00:00
|
|
|
*
|
2013-05-09 18:59:04 +00:00
|
|
|
* Copyright (C) 2010-2011, 2013 Red Hat, Inc.
|
2010-10-19 14:23:51 +00:00
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-10-19 14:23:51 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
#ifndef __VIR_FILE_H_
|
|
|
|
# define __VIR_FILE_H_
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2010-11-17 02:13:29 +00:00
|
|
|
# include <stdio.h>
|
2010-10-19 14:23:51 +00:00
|
|
|
|
|
|
|
# include "internal.h"
|
2013-04-22 14:06:16 +00:00
|
|
|
# include "virstoragefile.h"
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2012-06-07 13:16:50 +00:00
|
|
|
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;
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
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;
|
|
|
|
int safezero(int fd, off_t offset, off_t len)
|
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
2010-11-17 02:13:29 +00:00
|
|
|
/* Don't call these directly - use the macros below */
|
2012-06-07 13:16:50 +00:00
|
|
|
int virFileClose(int *fdptr, virFileCloseFlags flags)
|
2012-05-30 13:54:18 +00:00
|
|
|
ATTRIBUTE_RETURN_CHECK;
|
2011-07-19 18:32:58 +00:00
|
|
|
int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
|
2010-10-19 14:23:51 +00:00
|
|
|
|
|
|
|
/* For use on normal paths; caller must check return value,
|
maint: reject raw close, popen in 'make syntax-check'
commit f1fe9671e was supposed to make sure we use files.h
macros to avoid double close, but it didn't work.
Meanwhile, virCommand is vastly superior to system(), fork(),
and popen() (also to virExec, but we haven't completed that
conversion), so enforce that, too.
* cfg.mk (sc_prohibit_close): Fix typo that excluded close, and
add pclose.
(sc_prohibit_fork_wrappers): New rule, for fork, system, and popen.
* .x-sc_prohibit_close: More exemptions.
* .x-sc_prohibit_fork_wrappers: New file.
* Makefile.am (syntax_check_exceptions): Ship new file.
* src/datatypes.c (virReleaseConnect): Tweak comment to avoid
false positive.
* src/util/files.h (VIR_CLOSE): Likewise.
2011-01-27 22:16:14 +00:00
|
|
|
and failure sets errno per close. */
|
2012-06-07 13:16:50 +00:00
|
|
|
# define VIR_CLOSE(FD) virFileClose(&(FD), 0)
|
2011-07-19 18:32:58 +00:00
|
|
|
# define VIR_FCLOSE(FILE) virFileFclose(&(FILE), false)
|
2010-11-17 02:13:29 +00:00
|
|
|
|
|
|
|
/* Wrapper around fdopen that consumes fd on success. */
|
2011-07-19 18:32:58 +00:00
|
|
|
# define VIR_FDOPEN(FD, MODE) virFileFdopen(&(FD), MODE)
|
2010-10-19 14:23:51 +00:00
|
|
|
|
|
|
|
/* For use on cleanup paths; errno is unaffected by close,
|
|
|
|
and no return value to worry about. */
|
2012-06-07 13:16:50 +00:00
|
|
|
# define VIR_FORCE_CLOSE(FD) \
|
|
|
|
ignore_value(virFileClose(&(FD), VIR_FILE_CLOSE_PRESERVE_ERRNO))
|
2011-07-19 18:32:58 +00:00
|
|
|
# define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
|
2010-10-19 14:23:51 +00:00
|
|
|
|
2012-05-30 13:54:18 +00:00
|
|
|
/* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expected
|
|
|
|
* during mass close after fork(). */
|
2012-06-07 13:16:50 +00:00
|
|
|
# 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))
|
2012-05-30 13:54:18 +00:00
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
/* Opaque type for managing a wrapper around a fd. */
|
|
|
|
struct _virFileWrapperFd;
|
2011-07-11 21:26:33 +00:00
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
typedef struct _virFileWrapperFd virFileWrapperFd;
|
|
|
|
typedef virFileWrapperFd *virFileWrapperFdPtr;
|
2011-07-11 21:26:33 +00:00
|
|
|
|
|
|
|
int virFileDirectFdFlag(void);
|
|
|
|
|
2012-02-20 03:21:00 +00:00
|
|
|
enum virFileWrapperFdFlags {
|
2012-02-06 13:40:48 +00:00
|
|
|
VIR_FILE_WRAPPER_BYPASS_CACHE = (1 << 0),
|
|
|
|
VIR_FILE_WRAPPER_NON_BLOCKING = (1 << 1),
|
2012-02-20 03:21:00 +00:00
|
|
|
};
|
2012-02-06 13:40:48 +00:00
|
|
|
|
|
|
|
virFileWrapperFdPtr virFileWrapperFdNew(int *fd,
|
|
|
|
const char *name,
|
|
|
|
unsigned int flags)
|
2011-07-11 21:26:33 +00:00
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
int virFileWrapperFdClose(virFileWrapperFdPtr dfd);
|
2011-07-11 21:26:33 +00:00
|
|
|
|
2012-02-06 13:40:48 +00:00
|
|
|
void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
|
2011-07-11 21:26:33 +00:00
|
|
|
|
2011-07-06 16:06:58 +00:00
|
|
|
int virFileLock(int fd, bool shared, off_t start, off_t len);
|
|
|
|
int virFileUnlock(int fd, off_t start, off_t len);
|
|
|
|
|
2011-10-13 10:17:12 +00:00
|
|
|
typedef int (*virFileRewriteFunc)(int fd, void *opaque);
|
|
|
|
int virFileRewrite(const char *path,
|
|
|
|
mode_t mode,
|
|
|
|
virFileRewriteFunc rewrite,
|
|
|
|
void *opaque);
|
|
|
|
|
2012-01-11 09:58:59 +00:00
|
|
|
int virFileTouch(const char *path, mode_t mode);
|
|
|
|
|
2012-04-27 13:50:22 +00:00
|
|
|
int virFileUpdatePerm(const char *path,
|
|
|
|
mode_t mode_remove,
|
|
|
|
mode_t mode_add);
|
|
|
|
|
2012-07-03 14:06:27 +00:00
|
|
|
int virFileLoopDeviceAssociate(const char *file,
|
|
|
|
char **dev);
|
|
|
|
|
2013-04-22 14:06:16 +00:00
|
|
|
int virFileNBDDeviceAssociate(const char *file,
|
|
|
|
enum virStorageFileFormat fmt,
|
|
|
|
bool readonly,
|
|
|
|
char **dev);
|
|
|
|
|
2013-03-28 14:46:45 +00:00
|
|
|
int virFileDeleteTree(const char *dir);
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
int virFileReadLimFD(int fd, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
|
|
|
int virFileReadAll(const char *path, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
|
|
|
int virFileWriteStr(const char *path, const char *str, mode_t mode)
|
|
|
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
|
|
|
int virFileMatchesNameSuffix(const char *file,
|
|
|
|
const char *name,
|
|
|
|
const char *suffix);
|
|
|
|
|
|
|
|
int virFileHasSuffix(const char *str,
|
|
|
|
const char *suffix);
|
|
|
|
|
|
|
|
int virFileStripSuffix(char *str,
|
|
|
|
const char *suffix) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
|
|
|
int virFileLinkPointsTo(const char *checkLink,
|
|
|
|
const char *checkDest);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
char *virFindFileInPath(const char *file);
|
|
|
|
|
|
|
|
bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
|
|
|
|
bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1);
|
|
|
|
bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
|
|
|
|
|
|
|
|
char *virFileSanitizePath(const char *path);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
VIR_DIR_CREATE_NONE = 0,
|
|
|
|
VIR_DIR_CREATE_AS_UID = (1 << 0),
|
|
|
|
VIR_DIR_CREATE_FORCE_PERMS = (1 << 1),
|
|
|
|
VIR_DIR_CREATE_ALLOW_EXIST = (1 << 2),
|
|
|
|
};
|
|
|
|
int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
|
|
|
|
unsigned int flags) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
int virFileMakePathWithMode(const char *path,
|
|
|
|
mode_t mode) ATTRIBUTE_RETURN_CHECK;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
int virFileOpenTty(int *ttymaster,
|
|
|
|
char **ttyName,
|
|
|
|
int rawmode);
|
|
|
|
|
|
|
|
char *virFileFindMountPoint(const char *type);
|
|
|
|
|
|
|
|
void virFileWaitForDevices(void);
|
|
|
|
|
|
|
|
/* NB: this should be combined with virFileBuildPath */
|
|
|
|
# define virBuildPath(path, ...) \
|
|
|
|
virBuildPathInternal(path, __VA_ARGS__, NULL)
|
|
|
|
int virBuildPathInternal(char **path, ...) ATTRIBUTE_SENTINEL;
|
|
|
|
|
2013-05-17 14:11:24 +00:00
|
|
|
int virFilePrintf(FILE *fp, const char *msg, ...)
|
|
|
|
ATTRIBUTE_FMT_PRINTF(2, 3);
|
|
|
|
|
2013-05-09 18:59:04 +00:00
|
|
|
#endif /* __VIR_FILE_H */
|