mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-03 16:15:27 +00:00
164. [cleanup] Added functions isc_file_fopen(), isc_file_fclose(),
isc_file_fseek(), isc_file_fread(), isc_file_fwrite(), isc_file_fflush(), isc_file_ffsync(), isc_file_remove() to encapsulate nonportable usage of errno and fflush().
This commit is contained in:
@@ -90,6 +90,7 @@ isc_file_mktemplate(const char *path, char *buf, size_t buflen);
|
||||
* of the path with the internal template string.
|
||||
*/
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_file_openunique(char *templet, FILE **fp);
|
||||
/*
|
||||
@@ -147,6 +148,58 @@ isc_file_openunique(char *templet, FILE **fp);
|
||||
* Something totally unexpected happened.
|
||||
*/
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_file_fopen(const char *filename, const char *mode, FILE **fp);
|
||||
|
||||
isc_result_t
|
||||
isc_file_fclose(FILE *f);
|
||||
|
||||
isc_result_t
|
||||
isc_file_fseek(FILE *f, long offset, int whence);
|
||||
|
||||
isc_result_t
|
||||
isc_file_fread(void *ptr, size_t size, size_t nmemb, FILE *f,
|
||||
size_t *nret);
|
||||
|
||||
isc_result_t
|
||||
isc_file_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *f,
|
||||
size_t *nret);
|
||||
|
||||
isc_result_t
|
||||
isc_file_fflush(FILE *f);
|
||||
|
||||
/*
|
||||
* These functions are wrappers around the corresponding
|
||||
* stdio functions, returning a detailed error code in the
|
||||
* form of an an isc_result_t. ANSI C does not guarantee
|
||||
* that stdio functions set errno, hence these functions
|
||||
* must use platform dependent methods (e.g., the POSIX errno)
|
||||
* to construct the error code.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
isc_file_ffsync(FILE *f);
|
||||
/*
|
||||
* Invoke fsync() on the file descriptor underlying
|
||||
* an stdio stream, or an equivalent system-dependent
|
||||
* operation. Note that this function has no direct
|
||||
* counterpart in the stdio library.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
isc_file_remove(const char *filename);
|
||||
/*
|
||||
* Remove the file named by 'filename'.
|
||||
*/
|
||||
|
||||
/*
|
||||
* XXX We should also have a isc_file_writeeopen() function
|
||||
* for safely open a file in a publicly writable directory
|
||||
* (see write_open() in BIND 8's ns_config.c).
|
||||
*/
|
||||
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif /* ISC_FILE_H */
|
||||
|
@@ -21,6 +21,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <isc/buffer.h>
|
||||
#include <isc/file.h>
|
||||
#include <isc/lex.h>
|
||||
#include <isc/mem.h>
|
||||
#include <isc/string.h>
|
||||
@@ -210,7 +211,8 @@ new_source(isc_lex_t *lex, isc_boolean_t is_file, isc_boolean_t need_close,
|
||||
|
||||
isc_result_t
|
||||
isc_lex_openfile(isc_lex_t *lex, const char *filename) {
|
||||
FILE *stream;
|
||||
isc_result_t result;
|
||||
FILE *stream = NULL;
|
||||
|
||||
/*
|
||||
* Open 'filename' and make it the current input source for 'lex'.
|
||||
@@ -218,17 +220,10 @@ isc_lex_openfile(isc_lex_t *lex, const char *filename) {
|
||||
|
||||
REQUIRE(VALID_LEX(lex));
|
||||
|
||||
/*
|
||||
* XXX we should really call something like isc_file_open() to
|
||||
* get maximally safe file opening.
|
||||
*/
|
||||
stream = fopen(filename, "r");
|
||||
/*
|
||||
* The C standard doesn't say that errno is set by fopen(), so
|
||||
* we just return a generic error.
|
||||
*/
|
||||
if (stream == NULL)
|
||||
return (ISC_R_FAILURE);
|
||||
result = isc_file_fopen(filename, "r", &stream);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
return (result);
|
||||
|
||||
flockfile(stream);
|
||||
|
||||
return (new_source(lex, ISC_TRUE, ISC_TRUE, stream, filename));
|
||||
|
@@ -19,7 +19,7 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h> /* Required for mkstemp on NetBSD. */
|
||||
#include <unistd.h> /* Required for mkstemp on NetBSD. */
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
@@ -28,6 +28,36 @@
|
||||
#include <isc/time.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
/*
|
||||
* Convert a POSIX errno value into an isc_result_t. The
|
||||
* list of supported errno values is not complete; new users
|
||||
* of this function should add any expected errors that are
|
||||
* not already there.
|
||||
*/
|
||||
static isc_result_t
|
||||
posix_result(int posixerrno) {
|
||||
switch (posixerrno) {
|
||||
case ENOTDIR:
|
||||
case ELOOP:
|
||||
case EINVAL:
|
||||
case ENAMETOOLONG:
|
||||
case EBADF:
|
||||
return (ISC_R_INVALIDFILE);
|
||||
case ENOENT:
|
||||
return (ISC_R_FILENOTFOUND);
|
||||
case EACCES:
|
||||
return (ISC_R_NOPERM);
|
||||
case EEXIST:
|
||||
return (ISC_R_FILEEXISTS);
|
||||
case EIO:
|
||||
return (ISC_R_IOERROR);
|
||||
case ENOMEM:
|
||||
return (ISC_R_NOMEMORY);
|
||||
default:
|
||||
return (ISC_R_UNEXPECTED);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
|
||||
* it might be good to provide a mechanism that allows for the results
|
||||
@@ -39,31 +69,10 @@
|
||||
static isc_result_t
|
||||
file_stats(const char *file, struct stat *stats) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
if (stat(file, stats) != 0) {
|
||||
switch (errno) {
|
||||
case ENOTDIR:
|
||||
case ENOENT:
|
||||
result = ISC_R_NOTFOUND;
|
||||
break;
|
||||
case ELOOP:
|
||||
case EINVAL:
|
||||
case ENAMETOOLONG:
|
||||
result = ISC_R_INVALIDFILE;
|
||||
break;
|
||||
case EACCES:
|
||||
result = ISC_R_NOPERM;
|
||||
break;
|
||||
case EIO:
|
||||
result = ISC_R_IOERROR;
|
||||
break;
|
||||
case EFAULT:
|
||||
default:
|
||||
result = ISC_R_UNEXPECTED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (stat(file, stats) != 0)
|
||||
result = posix_result(errno);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
@@ -100,21 +109,20 @@ isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
|
||||
if (s != NULL) {
|
||||
if ((s - path + 1 + sizeof(TEMPLATE)) > buflen)
|
||||
return (ISC_R_NOSPACE);
|
||||
|
||||
|
||||
strncpy(buf, path, s - path + 1);
|
||||
buf[s - path + 1] = '\0';
|
||||
strcat(buf, TEMPLATE);
|
||||
|
||||
} else {
|
||||
if (sizeof(TEMPLATE) > buflen)
|
||||
return (ISC_R_NOSPACE);
|
||||
|
||||
|
||||
strcpy(buf, TEMPLATE);
|
||||
}
|
||||
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_file_openunique(char *templet, FILE **fp) {
|
||||
int fd;
|
||||
@@ -130,34 +138,11 @@ isc_file_openunique(char *templet, FILE **fp) {
|
||||
fd = mkstemp(templet);
|
||||
|
||||
if (fd == -1)
|
||||
switch (errno) {
|
||||
case ENOTDIR:
|
||||
case ELOOP:
|
||||
case EINVAL:
|
||||
case ENAMETOOLONG:
|
||||
result = ISC_R_INVALIDFILE;
|
||||
break;
|
||||
case EACCES:
|
||||
result = ISC_R_NOPERM;
|
||||
break;
|
||||
case EEXIST:
|
||||
result = ISC_R_EXISTS;
|
||||
break;
|
||||
case EIO:
|
||||
result = ISC_R_IOERROR;
|
||||
break;
|
||||
default:
|
||||
result = ISC_R_UNEXPECTED;
|
||||
}
|
||||
|
||||
result = posix_result(errno);
|
||||
if (result == ISC_R_SUCCESS) {
|
||||
f = fdopen(fd, "w+");
|
||||
if (f == NULL) {
|
||||
if (errno == ENOMEM)
|
||||
result = ISC_R_NOMEMORY;
|
||||
else
|
||||
result = ISC_R_UNEXPECTED;
|
||||
|
||||
result = posix_result(errno);
|
||||
(void)remove(templet);
|
||||
(void)close(fd);
|
||||
|
||||
@@ -167,3 +152,101 @@ isc_file_openunique(char *templet, FILE **fp) {
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_fopen(const char *filename, const char *mode, FILE **fp) {
|
||||
FILE *f;
|
||||
|
||||
f = fopen(filename, mode);
|
||||
if (f == NULL)
|
||||
return (posix_result(errno));
|
||||
*fp = f;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_fclose(FILE *f) {
|
||||
int r;
|
||||
|
||||
r = fclose(f);
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (posix_result(errno));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_fseek(FILE *f, long offset, int whence) {
|
||||
int r;
|
||||
|
||||
r = fseek(f, offset, whence);
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (posix_result(errno));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_fread(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
size_t r;
|
||||
|
||||
clearerr(f);
|
||||
r = fread(ptr, size, nmemb, f);
|
||||
if (r != nmemb) {
|
||||
if (feof(f))
|
||||
result = ISC_R_EOF;
|
||||
else
|
||||
result = posix_result(errno);
|
||||
}
|
||||
if (nret != NULL)
|
||||
*nret = r;
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
size_t r;
|
||||
|
||||
clearerr(f);
|
||||
r = fwrite(ptr, size, nmemb, f);
|
||||
if (r != nmemb)
|
||||
result = posix_result(errno);
|
||||
if (nret != NULL)
|
||||
*nret = r;
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_fflush(FILE *f) {
|
||||
int r;
|
||||
|
||||
r = fflush(f);
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (posix_result(errno));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_ffsync(FILE *f) {
|
||||
int r;
|
||||
|
||||
r = fsync(fileno(f));
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (posix_result(errno));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_remove(const char *filename) {
|
||||
int r;
|
||||
|
||||
r = unlink(filename);
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (posix_result(errno));
|
||||
}
|
||||
|
Reference in New Issue
Block a user