#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <isc/dir.h>
#include <isc/file.h>
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/random.h>
#include <isc/sha2.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/util.h>
#include "errno2result.h"
Go to the source code of this file.
Defines | |
#define | TEMPLATE "tmp-XXXXXXXXXX" |
14 characters. | |
#define | DISALLOW "\\/ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
#define | PATH_MAX 1024 |
Functions | |
static isc_result_t | file_stats (const char *file, struct stat *stats) |
static isc_result_t | fd_stats (int fd, struct stat *stats) |
isc_result_t | isc_file_getsizefd (int fd, off_t *size) |
Return the size of the file (stored in the parameter pointed to by 'size') in bytes. | |
isc_result_t | isc_file_mode (const char *file, mode_t *modep) |
isc_result_t | isc_file_getmodtime (const char *file, isc_time_t *modtime) |
Get the time of last modification of a file. | |
isc_result_t | isc_file_getsize (const char *file, off_t *size) |
Return the size of the file (stored in the parameter pointed to by 'size') in bytes. | |
isc_result_t | isc_file_settime (const char *file, isc_time_t *when) |
isc_result_t | isc_file_mktemplate (const char *path, char *buf, size_t buflen) |
Generate a template string suitable for use with isc_file_openunique(). | |
isc_result_t | isc_file_template (const char *path, const char *templet, char *buf, size_t buflen) |
Create an OS specific template using 'path' to define the directory 'templet' to describe the filename and store the result in 'buf' such that path can be renamed to buf atomically. | |
isc_result_t | isc_file_renameunique (const char *file, char *templet) |
Rename 'file' using 'templet' as a template for the new file name. | |
isc_result_t | isc_file_openunique (char *templet, FILE **fp) |
isc_result_t | isc_file_openuniqueprivate (char *templet, FILE **fp) |
isc_result_t | isc_file_openuniquemode (char *templet, int mode, FILE **fp) |
isc_result_t | isc_file_bopenunique (char *templet, FILE **fp) |
isc_result_t | isc_file_bopenuniqueprivate (char *templet, FILE **fp) |
isc_result_t | isc_file_bopenuniquemode (char *templet, int mode, FILE **fp) |
Create and open a file with a unique name based on 'templet'. isc_file_bopen*() open the file in binary mode in Windows. isc_file_open*() open the file in text mode in Windows. | |
isc_result_t | isc_file_remove (const char *filename) |
Remove the file named by 'filename'. | |
isc_result_t | isc_file_rename (const char *oldname, const char *newname) |
Rename the file 'oldname' to 'newname'. | |
isc_boolean_t | isc_file_exists (const char *pathname) |
Return ISC_TRUE if the calling process can tell that the given file exists. Will not return true if the calling process has insufficient privileges to search the entire path. | |
isc_result_t | isc_file_isplainfile (const char *filename) |
isc_result_t | isc_file_isplainfilefd (int fd) |
Check that the file is a plain file. | |
isc_result_t | isc_file_isdirectory (const char *filename) |
Check that 'name' exists and is a directory. | |
isc_boolean_t | isc_file_isabsolute (const char *filename) |
Return ISC_TRUE if the given file name is absolute. | |
isc_boolean_t | isc_file_iscurrentdir (const char *filename) |
Return ISC_TRUE if the given file name is the current directory ("."). | |
isc_boolean_t | isc_file_ischdiridempotent (const char *filename) |
Return ISC_TRUE if calling chdir(filename) multiple times will give the same result as calling it once. | |
const char * | isc_file_basename (const char *filename) |
Return the final component of the path in the file name. | |
isc_result_t | isc_file_progname (const char *filename, char *buf, size_t buflen) |
Given an operating system specific file name "filename" referring to a program, return the canonical program name. | |
static isc_result_t | dir_current (char *dirname, size_t length) |
isc_result_t | isc_file_absolutepath (const char *filename, char *path, size_t pathlen) |
Given a file name, return the fully qualified path to the file. | |
isc_result_t | isc_file_truncate (const char *filename, isc_offset_t size) |
Truncate/extend the file specified to 'size' bytes. | |
isc_result_t | isc_file_safecreate (const char *filename, FILE **fp) |
Open 'filename' for writing, truncating if necessary. Ensure that if it existed it was a normal file. If creating the file, ensure that only the owner can read/write it. | |
isc_result_t | isc_file_splitpath (isc_mem_t *mctx, char *path, char **dirname, char **basename) |
Split a path into dirname and basename. If 'path' contains no slash (or, on windows, backslash), then '*dirname' is set to ".". | |
void * | isc_file_mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) |
Portable front-end to mmap(). If mmap() is not defined on this platform, then we simulate it by calling malloc() and read(). (In this event, the addr, prot, and flags parameters are ignored). | |
int | isc_file_munmap (void *addr, size_t len) |
Portable front-end to munmap(). If munmap() is not defined on this platform, then we simply free the memory. | |
isc_result_t | isc_file_sanitize (const char *dir, const char *base, const char *ext, char *path, size_t length) |
Generate a sanitized filename, such as for MKEYS or NZF files. | |
Variables | |
static const char | alphnum [] |
Definition in file file.c.
#define TEMPLATE "tmp-XXXXXXXXXX" |
#define DISALLOW "\\/ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
static isc_result_t file_stats | ( | const char * | file, | |
struct stat * | stats | |||
) | [static] |
Definition at line 90 of file file.c.
References isc__errno2result, ISC_R_SUCCESS, and REQUIRE.
Referenced by isc_file_exists(), isc_file_getmodtime(), isc_file_getsize(), isc_file_mode(), and isc_file_safecreate().
static isc_result_t fd_stats | ( | int | fd, | |
struct stat * | stats | |||
) | [static] |
Definition at line 103 of file file.c.
References isc__errno2result, ISC_R_SUCCESS, and REQUIRE.
Referenced by isc_file_getsizefd().
isc_result_t isc_file_getsizefd | ( | int | fd, | |
off_t * | size | |||
) |
Return the size of the file (stored in the parameter pointed to by 'size') in bytes.
Returns:
Definition at line 115 of file file.c.
References fd_stats(), ISC_R_SUCCESS, and REQUIRE.
Referenced by ATF_TC_BODY(), and deserialize32().
isc_result_t isc_file_mode | ( | const char * | file, | |
mode_t * | modep | |||
) |
Definition at line 130 of file file.c.
References file_stats(), ISC_R_SUCCESS, and REQUIRE.
Referenced by dst__privstruct_writefile().
isc_result_t isc_file_getmodtime | ( | const char * | file, | |
isc_time_t * | time | |||
) |
Get the time of last modification of a file.
Notes:
Definition at line 144 of file file.c.
References file_stats(), ISC_R_SUCCESS, isc_time_set(), and REQUIRE.
Referenced by zone_postload(), zone_registerinclude(), and zone_touched().
isc_result_t isc_file_getsize | ( | const char * | file, | |
off_t * | size | |||
) |
Return the size of the file (stored in the parameter pointed to by 'size') in bytes.
Returns:
Definition at line 164 of file file.c.
References file_stats(), ISC_R_SUCCESS, and REQUIRE.
isc_result_t isc_file_settime | ( | const char * | file, | |
isc_time_t * | when | |||
) |
Definition at line 180 of file file.c.
References isc__errno2result, ISC_R_RANGE, ISC_R_SUCCESS, isc_time_nanoseconds(), isc_time_seconds(), and REQUIRE.
Referenced by setmodtime(), and zone_xfrdone().
isc_result_t isc_file_mktemplate | ( | const char * | path, | |
char * | buf, | |||
size_t | buflen | |||
) |
Generate a template string suitable for use with isc_file_openunique().
Notes:
Definition at line 223 of file file.c.
References isc_file_template(), and TEMPLATE.
isc_result_t isc_file_template | ( | const char * | path, | |
const char * | templet, | |||
char * | buf, | |||
size_t | buflen | |||
) |
Create an OS specific template using 'path' to define the directory 'templet' to describe the filename and store the result in 'buf' such that path can be renamed to buf atomically.
Definition at line 228 of file file.c.
References ISC_R_NOSPACE, ISC_R_SUCCESS, REQUIRE, and s.
Referenced by isc_file_mktemplate(), nzf_remove(), and zone_saveunique().
isc_result_t isc_file_renameunique | ( | const char * | file, | |
char * | templet | |||
) |
Rename 'file' using 'templet' as a template for the new file name.
Definition at line 263 of file file.c.
References alphnum, isc__errno2result, ISC_R_FAILURE, ISC_R_SUCCESS, isc_random_get(), and REQUIRE.
Referenced by zone_saveunique().
isc_result_t isc_file_openunique | ( | char * | templet, | |
FILE ** | fp | |||
) |
Definition at line 307 of file file.c.
References isc_file_openuniquemode().
Referenced by dump(), main(), nzf_remove(), and opentmp().
isc_result_t isc_file_openuniqueprivate | ( | char * | templet, | |
FILE ** | fp | |||
) |
Definition at line 313 of file file.c.
References isc_file_openuniquemode().
Referenced by destroy().
isc_result_t isc_file_openuniquemode | ( | char * | templet, | |
int | mode, | |||
FILE ** | fp | |||
) |
Definition at line 319 of file file.c.
References alphnum, isc__errno2result, isc_lctx, ISC_LOG_ERROR, isc_log_write(), ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_FILE, ISC_R_FAILURE, ISC_R_SUCCESS, isc_random_get(), and REQUIRE.
Referenced by isc_file_bopenunique(), isc_file_bopenuniquemode(), isc_file_bopenuniqueprivate(), isc_file_openunique(), and isc_file_openuniqueprivate().
isc_result_t isc_file_bopenunique | ( | char * | templet, | |
FILE ** | fp | |||
) |
isc_result_t isc_file_bopenuniqueprivate | ( | char * | templet, | |
FILE ** | fp | |||
) |
isc_result_t isc_file_bopenuniquemode | ( | char * | templet, | |
int | mode, | |||
FILE ** | fp | |||
) |
Create and open a file with a unique name based on 'templet'. isc_file_bopen*() open the file in binary mode in Windows. isc_file_open*() open the file in text mode in Windows.
Notes:
Definition at line 389 of file file.c.
References isc_file_openuniquemode().
isc_result_t isc_file_remove | ( | const char * | filename | ) |
Remove the file named by 'filename'.
Definition at line 394 of file file.c.
References isc__errno2result, ISC_R_SUCCESS, r, and REQUIRE.
Referenced by cleanup_session_key(), closeandrename(), dns_journal_compact(), dns_master_dumpinc3(), dns_view_saventa(), generate_session_key(), journal_file_create(), ns_server_delzone(), removetempfile(), roll_log(), and synczone().
isc_result_t isc_file_rename | ( | const char * | oldname, | |
const char * | newname | |||
) |
Rename the file 'oldname' to 'newname'.
Definition at line 407 of file file.c.
References isc__errno2result, ISC_R_SUCCESS, r, and REQUIRE.
Referenced by closeandrename(), destroy(), main(), nzf_remove(), and roll_log().
isc_boolean_t isc_file_exists | ( | const char * | pathname | ) |
Return ISC_TRUE if the calling process can tell that the given file exists. Will not return true if the calling process has insufficient privileges to search the entire path.
Definition at line 421 of file file.c.
References file_stats(), ISC_R_SUCCESS, ISC_TF, and REQUIRE.
Referenced by get_rndckey(), inuse(), isc_file_sanitize(), parse_config(), read_confkey(), read_sessionkey(), and zone_load().
isc_result_t isc_file_isplainfile | ( | const char * | filename | ) |
Definition at line 430 of file file.c.
References isc__errno2result, ISC_R_INVALIDFILE, and ISC_R_SUCCESS.
Referenced by channel_fromconf().
isc_result_t isc_file_isplainfilefd | ( | int | fd | ) |
Check that the file is a plain file.
Returns:
Definition at line 447 of file file.c.
References isc__errno2result, ISC_R_INVALIDFILE, and ISC_R_SUCCESS.
Referenced by dns_rbt_serialize_tree(), and serialize().
isc_result_t isc_file_isdirectory | ( | const char * | name | ) |
Check that 'name' exists and is a directory.
Returns:
Definition at line 464 of file file.c.
References isc__errno2result, ISC_R_INVALIDFILE, and ISC_R_SUCCESS.
Referenced by check_zoneconf(), and configure_view_dnsseckeys().
isc_boolean_t isc_file_isabsolute | ( | const char * | filename | ) |
isc_boolean_t isc_file_iscurrentdir | ( | const char * | filename | ) |
isc_boolean_t isc_file_ischdiridempotent | ( | const char * | filename | ) |
Return ISC_TRUE if calling chdir(filename) multiple times will give the same result as calling it once.
Definition at line 495 of file file.c.
References ISC_FALSE, isc_file_isabsolute(), isc_file_iscurrentdir(), ISC_TRUE, and REQUIRE.
Referenced by directory_callback().
const char* isc_file_basename | ( | const char * | filename | ) |
Return the final component of the path in the file name.
Definition at line 505 of file file.c.
Referenced by isc_file_progname(), and setup_syslog().
isc_result_t isc_file_progname | ( | const char * | filename, | |
char * | buf, | |||
size_t | buflen | |||
) |
Given an operating system specific file name "filename" referring to a program, return the canonical program name.
Any directory prefix or executable file name extension (if used on the OS in case) is stripped. On systems where program names are case insensitive, the name is canonicalized to all lower case. The name is written to 'buf', an array of 'buflen' chars, and null terminated.
Returns:
Definition at line 518 of file file.c.
References isc_file_basename(), ISC_R_NOSPACE, ISC_R_SUCCESS, and REQUIRE.
Referenced by main().
static isc_result_t dir_current | ( | char * | dirname, | |
size_t | length | |||
) | [static] |
Definition at line 542 of file file.c.
References isc__errno2result, ISC_R_NOSPACE, ISC_R_SUCCESS, and REQUIRE.
Referenced by isc_file_absolutepath().
isc_result_t isc_file_absolutepath | ( | const char * | filename, | |
char * | path, | |||
size_t | pathlen | |||
) |
Given a file name, return the fully qualified path to the file.
Definition at line 567 of file file.c.
References dir_current(), ISC_R_NOSPACE, and ISC_R_SUCCESS.
Referenced by setup().
isc_result_t isc_file_truncate | ( | const char * | filename, | |
isc_offset_t | size | |||
) |
Truncate/extend the file specified to 'size' bytes.
Definition at line 579 of file file.c.
References isc__errno2result, and ISC_R_SUCCESS.
isc_result_t isc_file_safecreate | ( | const char * | filename, | |
FILE ** | fp | |||
) |
Open 'filename' for writing, truncating if necessary. Ensure that if it existed it was a normal file. If creating the file, ensure that only the owner can read/write it.
Definition at line 588 of file file.c.
References file_stats(), isc__errno2result, ISC_R_FILENOTFOUND, ISC_R_INVALIDFILE, ISC_R_SUCCESS, and REQUIRE.
isc_result_t isc_file_splitpath | ( | isc_mem_t * | mctx, | |
char * | path, | |||
char ** | dirname, | |||
char ** | basename | |||
) |
Split a path into dirname and basename. If 'path' contains no slash (or, on windows, backslash), then '*dirname' is set to ".".
Allocates memory for '*dirname', which can be freed with isc_mem_free().
Returns:
Definition at line 624 of file file.c.
References isc_mem_allocate, isc_mem_free, isc_mem_strdup, ISC_R_INVALIDFILE, ISC_R_NOMEMORY, and ISC_R_SUCCESS.
Referenced by main().
void* isc_file_mmap | ( | void * | addr, | |
size_t | len, | |||
int | prot, | |||
int | flags, | |||
int | fd, | |||
off_t | offset | |||
) |
Portable front-end to mmap(). If mmap() is not defined on this platform, then we simulate it by calling malloc() and read(). (In this event, the addr, prot, and flags parameters are ignored).
Definition at line 661 of file file.c.
References free(), and UNUSED.
Referenced by deserialize32().
int isc_file_munmap | ( | void * | addr, | |
size_t | len | |||
) |
Portable front-end to munmap(). If munmap() is not defined on this platform, then we simply free the memory.
Definition at line 692 of file file.c.
References free(), and UNUSED.
Referenced by free_rbtdb().
isc_result_t isc_file_sanitize | ( | const char * | dir, | |
const char * | base, | |||
const char * | ext, | |||
char * | path, | |||
size_t | length | |||
) |
Generate a sanitized filename, such as for MKEYS or NZF files.
Historically, MKEYS and NZF files used SHA256 hashes of the view name for the filename; this was to deal with the possibility of forbidden characters such as "/" being in a view name, and to avoid problems with case-insensitive file systems.
Given a basename 'base' and an extension 'ext', this function checks for the existence of file using the old-style name format in directory 'dir'. If found, it returns the path to that file. If there is no file already in place, a new pathname is generated; if the basename contains any excluded characters, then a truncated SHA256 hash is used, otherwise the basename is used. The path name is copied into 'path', which must point to a buffer of at least 'length' bytes.
Requires:
Returns:
Definition at line 709 of file file.c.
References DISALLOW, hash, isc_file_exists(), ISC_R_NOSPACE, ISC_R_SUCCESS, isc_sha256_data(), PATH_MAX, and REQUIRE.
Referenced by add_keydata_zone(), ATF_TC_BODY(), ATF_TP_ADD_TCS(), dns_view_create(), and dns_view_setnewzones().
const char alphnum[] [static] |
Initial value:
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Definition at line 259 of file file.c.
Referenced by isc_file_openuniquemode(), and isc_file_renameunique().