This commit was manufactured by cvs2git to create branch 'v9_3'.
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
* Copyright (C) 1999-2001 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
||||
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
||||
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
||||
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: journal.h,v 1.23 2001/05/21 23:56:33 gson Exp $ */
|
||||
|
||||
#ifndef DNS_JOURNAL_H
|
||||
#define DNS_JOURNAL_H 1
|
||||
|
||||
/*****
|
||||
***** Module Info
|
||||
*****/
|
||||
|
||||
/*
|
||||
* Database journalling.
|
||||
*/
|
||||
|
||||
/***
|
||||
*** Imports
|
||||
***/
|
||||
|
||||
#include <isc/lang.h>
|
||||
#include <isc/magic.h>
|
||||
|
||||
#include <dns/name.h>
|
||||
#include <dns/diff.h>
|
||||
#include <dns/rdata.h>
|
||||
#include <dns/types.h>
|
||||
|
||||
/***
|
||||
*** Types
|
||||
***/
|
||||
|
||||
/*
|
||||
* A dns_journal_t represents an open journal file. This is an opaque type.
|
||||
*
|
||||
* A particular dns_journal_t object may be opened for writing, in which case
|
||||
* it can be used for writing transactions to a journal file, or it can be
|
||||
* opened for reading, in which case it can be used for reading transactions
|
||||
* from (iterating over) a journal file. A single dns_journal_t object may
|
||||
* not be used for both purposes.
|
||||
*/
|
||||
typedef struct dns_journal dns_journal_t;
|
||||
|
||||
|
||||
/***
|
||||
*** Functions
|
||||
***/
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
isc_result_t
|
||||
dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
|
||||
dns_diffop_t op, dns_difftuple_t **tp);
|
||||
/*
|
||||
* Create a diff tuple for the current database SOA.
|
||||
* XXX this probably belongs somewhere else.
|
||||
*/
|
||||
|
||||
|
||||
#define DNS_SERIAL_GT(a, b) ((int)(((a) - (b)) & 0xFFFFFFFF) > 0)
|
||||
#define DNS_SERIAL_GE(a, b) ((int)(((a) - (b)) & 0xFFFFFFFF) >= 0)
|
||||
/*
|
||||
* Compare SOA serial numbers. DNS_SERIAL_GT(a, b) returns true iff
|
||||
* a is "greater than" b where "greater than" is as defined in RFC1982.
|
||||
* DNS_SERIAL_GE(a, b) returns true iff a is "greater than or equal to" b.
|
||||
*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
* Journal object creation and destruction.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
|
||||
dns_journal_t **journalp);
|
||||
/*
|
||||
* Open the journal file 'filename' and create a dns_journal_t object for it.
|
||||
*
|
||||
* If 'write' is ISC_TRUE, the journal is open for writing. If it does
|
||||
* not exist, it is created.
|
||||
*
|
||||
* If 'write' is ISC_FALSE, the journal is open for reading. If it does
|
||||
* not exist, ISC_R_NOTFOUND is returned.
|
||||
*/
|
||||
|
||||
void
|
||||
dns_journal_destroy(dns_journal_t **journalp);
|
||||
/*
|
||||
* Destroy a dns_journal_t, closing any open files and freeing its memory.
|
||||
*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
* Writing transactions to journals.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_begin_transaction(dns_journal_t *j);
|
||||
/*
|
||||
* Prepare to write a new transaction to the open journal file 'j'.
|
||||
*
|
||||
* Requires:
|
||||
* 'j' is open for writing.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff);
|
||||
/*
|
||||
* Write 'diff' to the current transaction of journal file 'j'.
|
||||
*
|
||||
* Requires:
|
||||
* 'j' is open for writing and dns_journal_begin_transaction()
|
||||
* has been called.
|
||||
*
|
||||
* 'diff' is a full or partial, correctly ordered IXFR
|
||||
* difference sequence.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_commit(dns_journal_t *j);
|
||||
/*
|
||||
* Commit the current transaction of journal file 'j'.
|
||||
*
|
||||
* Requires:
|
||||
* 'j' is open for writing and dns_journal_begin_transaction()
|
||||
* has been called.
|
||||
*
|
||||
* dns_journal_writediff() has been called one or more times
|
||||
* to form a complete, correctly ordered IXFR difference
|
||||
* sequence.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff);
|
||||
/*
|
||||
* Write a complete transaction at once to a journal file,
|
||||
* sorting it if necessary, and commit it. Equivalent to calling
|
||||
* dns_diff_sort(), dns_journal_begin_transaction(),
|
||||
* dns_journal_writediff(), and dns_journal_commit().
|
||||
*
|
||||
* Requires:
|
||||
* 'j' is open for writing.
|
||||
*
|
||||
* 'diff' contains exactly one SOA deletion, one SOA addition
|
||||
* with a greater serial number, and possibly other changes,
|
||||
* in arbitrary order.
|
||||
*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
* Reading transactions from journals.
|
||||
*/
|
||||
|
||||
isc_uint32_t
|
||||
dns_journal_first_serial(dns_journal_t *j);
|
||||
isc_uint32_t
|
||||
dns_journal_last_serial(dns_journal_t *j);
|
||||
/*
|
||||
* Get the first and last addressable serial number in the journal.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_iter_init(dns_journal_t *j,
|
||||
isc_uint32_t begin_serial, isc_uint32_t end_serial);
|
||||
/*
|
||||
* Prepare to iterate over the transactions that will bring the database
|
||||
* from SOA serial number 'begin_serial' to 'end_serial'.
|
||||
*
|
||||
* Returns:
|
||||
* ISC_R_SUCCESS
|
||||
* ISC_R_RANGE begin_serial is outside the addressable range.
|
||||
* ISC_R_NOTFOUND begin_serial is within the range of adressable
|
||||
* serial numbers covered by the journal, but
|
||||
* this particular serial number does not exist.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_first_rr(dns_journal_t *j);
|
||||
isc_result_t
|
||||
dns_journal_next_rr(dns_journal_t *j);
|
||||
/*
|
||||
* Position the iterator at the first/next RR in a journal
|
||||
* transaction sequence established using dns_journal_iter_init().
|
||||
*
|
||||
* Requires:
|
||||
* dns_journal_iter_init() has been called.
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, isc_uint32_t *ttl,
|
||||
dns_rdata_t **rdata);
|
||||
/*
|
||||
* Get the name, ttl, and rdata of the current journal RR.
|
||||
*
|
||||
* Requires:
|
||||
* The last call to dns_journal_first_rr() or dns_journal_next_rr()
|
||||
* returned ISC_R_SUCCESS.
|
||||
*/
|
||||
|
||||
/**************************************************************************/
|
||||
/*
|
||||
* Database roll-forward.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db, const char *filename);
|
||||
/*
|
||||
* Roll forward (play back) the journal file "filename" into the
|
||||
* database "db". This should be called when the server starts
|
||||
* after a shutdown or crash.
|
||||
*
|
||||
* Requires:
|
||||
* 'mctx' is a valid memory context.
|
||||
* 'db' is a valid database which does not have a version
|
||||
* open for writing.
|
||||
* 'filename' is the name of the journal file belonging to 'db'.
|
||||
*
|
||||
* Returns:
|
||||
* DNS_R_NOJOURNAL when journal does not exist.
|
||||
* ISC_R_NOTFOUND when current serial in not in journal.
|
||||
* ISC_R_RANGE when current serial in not in journals range.
|
||||
* ISC_R_SUCCESS journal has been applied successfully to database.
|
||||
* others
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file);
|
||||
/* For debugging not general use */
|
||||
|
||||
isc_result_t
|
||||
dns_db_diff(isc_mem_t *mctx,
|
||||
dns_db_t *dba, dns_dbversion_t *dbvera,
|
||||
dns_db_t *dbb, dns_dbversion_t *dbverb,
|
||||
const char *journal_filename);
|
||||
/*
|
||||
* Compare the databases 'dba' and 'dbb' and generate a journal
|
||||
* entry containing the changes to make 'dba' from 'dbb' (note
|
||||
* the order). This journal entry will consist of a single,
|
||||
* possibly very large transaction. Append the journal
|
||||
* entry to the journal file specified by 'journal_filename'.
|
||||
*/
|
||||
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif /* DNS_JOURNAL_H */
|
||||
+1908
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* Copyright (C) 2000, 2001 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
||||
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
||||
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
||||
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: file.h,v 1.24 2001/07/16 18:33:00 gson Exp $ */
|
||||
|
||||
#ifndef ISC_FILE_H
|
||||
#define ISC_FILE_H 1
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <isc/lang.h>
|
||||
#include <isc/types.h>
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
isc_result_t
|
||||
isc_file_settime(const char *file, isc_time_t *time);
|
||||
|
||||
isc_result_t
|
||||
isc_file_getmodtime(const char *file, isc_time_t *time);
|
||||
/*
|
||||
* Get the time of last modication of a file.
|
||||
*
|
||||
* Notes:
|
||||
* The time that is set is relative to the (OS-specific) epoch, as are
|
||||
* all isc_time_t structures.
|
||||
*
|
||||
* Requires:
|
||||
* file != NULL.
|
||||
* time != NULL.
|
||||
*
|
||||
* Ensures:
|
||||
* If the file could not be accessed, 'time' is unchanged.
|
||||
*
|
||||
* Returns:
|
||||
* ISC_R_SUCCESS
|
||||
* Success.
|
||||
* ISC_R_NOTFOUND
|
||||
* No such file exists.
|
||||
* ISC_R_INVALIDFILE
|
||||
* The path specified was not usable by the operating system.
|
||||
* ISC_R_NOPERM
|
||||
* The file's metainformation could not be retrieved because
|
||||
* permission was denied to some part of the file's path.
|
||||
* ISC_R_EIO
|
||||
* Hardware error interacting with the filesystem.
|
||||
* ISC_R_UNEXPECTED
|
||||
* Something totally unexpected happened.
|
||||
*
|
||||
*/
|
||||
|
||||
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:
|
||||
* This function is intended to make creating temporary files
|
||||
* portable between different operating systems.
|
||||
*
|
||||
* The path is prepended to an implementation-defined string and
|
||||
* placed into buf. The string has no path characters in it,
|
||||
* and its maximum length is 14 characters plus a NUL. Thus
|
||||
* buflen should be at least strlen(path) + 15 characters or
|
||||
* an error will be returned.
|
||||
*
|
||||
* Requires:
|
||||
* buf != NULL.
|
||||
*
|
||||
* Ensures:
|
||||
* If result == ISC_R_SUCCESS:
|
||||
* buf contains a string suitable for use as the template argument
|
||||
* to isc_file_openunique.
|
||||
*
|
||||
* If result != ISC_R_SUCCESS:
|
||||
* buf is unchanged.
|
||||
*
|
||||
* Returns:
|
||||
* ISC_R_SUCCESS Success.
|
||||
* ISC_R_NOSPACE buflen indicates buf is too small for the catenation
|
||||
* of the path with the internal template string.
|
||||
*/
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_file_openunique(char *templet, FILE **fp);
|
||||
/*
|
||||
* Create and open a file with a unique name based on 'templet'.
|
||||
*
|
||||
* Notes:
|
||||
* 'template' is a reserved work in C++. If you want to complain
|
||||
* about the spelling of 'templet', first look it up in the
|
||||
* Merriam-Webster English dictionary. (http://www.m-w.com/)
|
||||
*
|
||||
* This function works by using the template to generate file names.
|
||||
* The template must be a writable string, as it is modified in place.
|
||||
* Trailing X characters in the file name (full file name on Unix,
|
||||
* basename on Win32 -- eg, tmp-XXXXXX vs XXXXXX.tmp, respectively)
|
||||
* are replaced with ASCII characters until a non-existent filename
|
||||
* is found. If the template does not include pathname information,
|
||||
* the files in the working directory of the program are searched.
|
||||
*
|
||||
* isc_file_mktemplate is a good, portable way to get a template.
|
||||
*
|
||||
* Requires:
|
||||
* 'fp' is non-NULL and '*fp' is NULL.
|
||||
*
|
||||
* 'template' is non-NULL, and of a form suitable for use by
|
||||
* the system as described above.
|
||||
*
|
||||
* Ensures:
|
||||
* If result is ISC_R_SUCCESS:
|
||||
* *fp points to an stream opening in stdio's "w+" mode.
|
||||
*
|
||||
* If result is not ISC_R_SUCCESS:
|
||||
* *fp is NULL.
|
||||
*
|
||||
* No file is open. Even if one was created (but unable
|
||||
* to be reopened as a stdio FILE pointer) then it has been
|
||||
* removed.
|
||||
*
|
||||
* This function does *not* ensure that the template string has not been
|
||||
* modified, even if the operation was unsuccessful.
|
||||
*
|
||||
* Returns:
|
||||
* ISC_R_SUCCESS
|
||||
* Success.
|
||||
* ISC_R_EXISTS
|
||||
* No file with a unique name could be created based on the
|
||||
* template.
|
||||
* ISC_R_INVALIDFILE
|
||||
* The path specified was not usable by the operating system.
|
||||
* ISC_R_NOPERM
|
||||
* The file could not be created because permission was denied
|
||||
* to some part of the file's path.
|
||||
* ISC_R_EIO
|
||||
* Hardware error interacting with the filesystem.
|
||||
* ISC_R_UNEXPECTED
|
||||
* Something totally unexpected happened.
|
||||
*/
|
||||
|
||||
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 iff 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_boolean_t
|
||||
isc_file_isabsolute(const char *filename);
|
||||
/*
|
||||
* Return ISC_TRUE iff the given file name is absolute.
|
||||
*/
|
||||
|
||||
isc_boolean_t
|
||||
isc_file_iscurrentdir(const char *filename);
|
||||
/*
|
||||
* Return ISC_TRUE iff 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.
|
||||
* 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:
|
||||
* ISC_R_SUCCESS
|
||||
* ISC_R_NOSPACE The name did not fit in 'buf'.
|
||||
*/
|
||||
|
||||
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_absolutepath(const char *filename, char *path, size_t pathlen);
|
||||
/*
|
||||
* Given a file name, return the fully qualified path to the file.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 */
|
||||
@@ -0,0 +1,315 @@
|
||||
/*
|
||||
* Copyright (C) 2000, 2001 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
||||
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
||||
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
||||
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/* $Id: file.c,v 1.38 2001/07/16 18:33:01 gson Exp $ */
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h> /* Required for utimes on some platforms. */
|
||||
#include <unistd.h> /* Required for mkstemp on NetBSD. */
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <isc/dir.h>
|
||||
#include <isc/file.h>
|
||||
#include <isc/string.h>
|
||||
#include <isc/time.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
#include "errno2result.h"
|
||||
|
||||
/*
|
||||
* XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
|
||||
* it might be good to provide a mechanism that allows for the results
|
||||
* of a previous stat() to be used again without having to do another stat,
|
||||
* such as perl's mechanism of using "_" in place of a file name to indicate
|
||||
* that the results of the last stat should be used. But then you get into
|
||||
* annoying MP issues. BTW, Win32 has stat().
|
||||
*/
|
||||
static isc_result_t
|
||||
file_stats(const char *file, struct stat *stats) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
REQUIRE(file != NULL);
|
||||
REQUIRE(stats != NULL);
|
||||
|
||||
if (stat(file, stats) != 0)
|
||||
result = isc__errno2result(errno);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_getmodtime(const char *file, isc_time_t *time) {
|
||||
isc_result_t result;
|
||||
struct stat stats;
|
||||
|
||||
REQUIRE(file != NULL);
|
||||
REQUIRE(time != NULL);
|
||||
|
||||
result = file_stats(file, &stats);
|
||||
|
||||
if (result == ISC_R_SUCCESS)
|
||||
/*
|
||||
* XXXDCL some operating systems provide nanoseconds, too,
|
||||
* such as BSD/OS via st_mtimespec.
|
||||
*/
|
||||
isc_time_set(time, stats.st_mtime, 0);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_settime(const char *file, isc_time_t *time) {
|
||||
struct timeval times[2];
|
||||
|
||||
REQUIRE(file != NULL && time != NULL);
|
||||
|
||||
/*
|
||||
* tv_sec is at least a 32 bit quantity on all platforms we're
|
||||
* dealing with, but it is signed on most (all?) of them,
|
||||
* so we need to make sure the high bit isn't set. This unfortunately
|
||||
* loses when either:
|
||||
* * tv_sec becomes a signed 64 bit integer but long is 32 bits
|
||||
* and isc_time_seconds > LONG_MAX, or
|
||||
* * isc_time_seconds is changed to be > 32 bits but long is 32 bits
|
||||
* and isc_time_seconds has at least 33 significant bits.
|
||||
*/
|
||||
times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
|
||||
|
||||
/*
|
||||
* Here is the real check for the high bit being set.
|
||||
*/
|
||||
if ((times[0].tv_sec &
|
||||
(1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
|
||||
return (ISC_R_RANGE);
|
||||
|
||||
/*
|
||||
* isc_time_nanoseconds guarantees a value that divided by 1000 will
|
||||
* fit into the minimum possible size tv_usec field. Unfortunately,
|
||||
* we don't know what that type is so can't cast directly ... but
|
||||
* we can at least cast to signed so the IRIX compiler shuts up.
|
||||
*/
|
||||
times[0].tv_usec = times[1].tv_usec =
|
||||
(isc_int32_t)(isc_time_nanoseconds(time) / 1000);
|
||||
|
||||
if (utimes(file, times) < 0)
|
||||
return (isc__errno2result(errno));
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
}
|
||||
|
||||
#undef TEMPLATE
|
||||
#define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */
|
||||
|
||||
isc_result_t
|
||||
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
|
||||
return (isc_file_template(path, TEMPLATE, buf, buflen));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_template(const char *path, const char *templet, char *buf,
|
||||
size_t buflen) {
|
||||
char *s;
|
||||
|
||||
REQUIRE(path != NULL);
|
||||
REQUIRE(templet != NULL);
|
||||
REQUIRE(buf != NULL);
|
||||
|
||||
s = strrchr(templet, '/');
|
||||
if (s != NULL)
|
||||
templet = s + 1;
|
||||
|
||||
s = strrchr(path, '/');
|
||||
|
||||
if (s != NULL) {
|
||||
if ((s - path + 1 + strlen(templet) + 1) > buflen)
|
||||
return (ISC_R_NOSPACE);
|
||||
|
||||
strncpy(buf, path, s - path + 1);
|
||||
buf[s - path + 1] = '\0';
|
||||
strcat(buf, templet);
|
||||
} else {
|
||||
if ((strlen(templet) + 1) > buflen)
|
||||
return (ISC_R_NOSPACE);
|
||||
|
||||
strcpy(buf, templet);
|
||||
}
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_renameunique(const char *file, char *templet) {
|
||||
int fd = -1;
|
||||
int res = 0;
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
REQUIRE(file != NULL);
|
||||
REQUIRE(templet != NULL);
|
||||
|
||||
fd = mkstemp(templet);
|
||||
if (fd == -1) {
|
||||
result = isc__errno2result(errno);
|
||||
}
|
||||
if (result == ISC_R_SUCCESS) {
|
||||
res = rename(file, templet);
|
||||
if (res != 0) {
|
||||
result = isc__errno2result(errno);
|
||||
(void)unlink(templet);
|
||||
}
|
||||
}
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_openunique(char *templet, FILE **fp) {
|
||||
int fd;
|
||||
FILE *f;
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
REQUIRE(templet != NULL);
|
||||
REQUIRE(fp != NULL && *fp == NULL);
|
||||
|
||||
/*
|
||||
* Win32 does not have mkstemp.
|
||||
*/
|
||||
fd = mkstemp(templet);
|
||||
|
||||
if (fd == -1)
|
||||
result = isc__errno2result(errno);
|
||||
if (result == ISC_R_SUCCESS) {
|
||||
f = fdopen(fd, "w+");
|
||||
if (f == NULL) {
|
||||
result = isc__errno2result(errno);
|
||||
(void)remove(templet);
|
||||
(void)close(fd);
|
||||
|
||||
} else
|
||||
*fp = f;
|
||||
}
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_remove(const char *filename) {
|
||||
int r;
|
||||
|
||||
REQUIRE(filename != NULL);
|
||||
|
||||
r = unlink(filename);
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (isc__errno2result(errno));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_rename(const char *oldname, const char *newname) {
|
||||
int r;
|
||||
|
||||
REQUIRE(oldname != NULL);
|
||||
REQUIRE(newname != NULL);
|
||||
|
||||
r = rename(oldname, newname);
|
||||
if (r == 0)
|
||||
return (ISC_R_SUCCESS);
|
||||
else
|
||||
return (isc__errno2result(errno));
|
||||
}
|
||||
|
||||
isc_boolean_t
|
||||
isc_file_exists(const char *pathname) {
|
||||
struct stat stats;
|
||||
|
||||
REQUIRE(pathname != NULL);
|
||||
|
||||
return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
|
||||
}
|
||||
|
||||
isc_boolean_t
|
||||
isc_file_isabsolute(const char *filename) {
|
||||
REQUIRE(filename != NULL);
|
||||
return (ISC_TF(filename[0] == '/'));
|
||||
}
|
||||
|
||||
isc_boolean_t
|
||||
isc_file_iscurrentdir(const char *filename) {
|
||||
REQUIRE(filename != NULL);
|
||||
return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
|
||||
}
|
||||
|
||||
isc_boolean_t
|
||||
isc_file_ischdiridempotent(const char *filename) {
|
||||
REQUIRE(filename != NULL);
|
||||
if (isc_file_isabsolute(filename))
|
||||
return (ISC_TRUE);
|
||||
if (isc_file_iscurrentdir(filename))
|
||||
return (ISC_TRUE);
|
||||
return (ISC_FALSE);
|
||||
}
|
||||
|
||||
const char *
|
||||
isc_file_basename(const char *filename) {
|
||||
char *s;
|
||||
|
||||
REQUIRE(filename != NULL);
|
||||
|
||||
s = strrchr(filename, '/');
|
||||
if (s == NULL)
|
||||
return (filename);
|
||||
|
||||
return (s + 1);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_progname(const char *filename, char *buf, size_t buflen) {
|
||||
const char *base;
|
||||
size_t len;
|
||||
|
||||
REQUIRE(filename != NULL);
|
||||
REQUIRE(buf != NULL);
|
||||
|
||||
base = isc_file_basename(filename);
|
||||
len = strlen(base) + 1;
|
||||
|
||||
if (len > buflen)
|
||||
return (ISC_R_NOSPACE);
|
||||
memcpy(buf, base, len);
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
|
||||
isc_result_t result;
|
||||
result = isc_dir_current(path, pathlen, ISC_TRUE);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
return (result);
|
||||
if (strlen(path) + strlen(filename) + 1 > pathlen)
|
||||
return (ISC_R_NOSPACE);
|
||||
strcat(path, filename);
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
+3955
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user