file.c revision ad38e19578815fc064ea0a5f455fa8beffc99e63
ddad35552931651426ad70912d29d9ab7d2a8d80Automatic Updater * Copyright (C) 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉/* $Id: file.c,v 1.14 2001/07/11 04:32:15 mayer Exp $ */
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington * Emulate UNIX mkstemp, which returns an open FD to the new file
b3e77535185043f089b346166440402d092030c3David Lawrence /* extra X's get set to 0's */
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * check the target directory; if you have six X's and it
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * doesn't exist this runs for a *very* long time.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* tricky little algorithm for backward compatibility */
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson /*NOTREACHED*/
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * it might be good to provide a mechanism that allows for the results
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * of a previous stat() to be used again without having to do another stat,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * such as perl's mechanism of using "_" in place of a file name to indicate
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * that the results of the last stat should be used. But then you get into
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * annoying MP issues. BTW, Win32 has stat().
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencefile_stats(const char *file, struct stat *stats) {
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * isc_file_safemovefile is needed to be defined here to ensure that
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * any file with the new name is renamed to a backup name and then the
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * rename is done. If all goes well then the backup can be deleted,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * otherwise it gets renamed back.
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsisc_file_safemovefile(const char *oldname, const char *newname) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Make sure we have something to do
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * Rename to a backup the new file if it still exists
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* Now rename the file to the new name
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * Try to rename the backup back to the original name
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * if the backup got created
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Delete the backup file if it got created
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsisc_file_getmodtime(const char *file, isc_time_t *time) {
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * XXXDCL some operating systems provide nanoseconds, too,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * such as BSD/OS via st_mtimespec.
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafssonisc_file_settime(const char *file, isc_time_t *time) {
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews * tv_sec is at least a 32 bit quantity on all platforms we're
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * dealing with, but it is signed on most (all?) of them,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * so we need to make sure the high bit isn't set. This unfortunately
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * loses when either:
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * * tv_sec becomes a signed 64 bit integer but long is 32 bits
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * and isc_time_seconds > LONG_MAX, or
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * * isc_time_seconds is changed to be > 32 bits but long is 32 bits
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * and isc_time_seconds has at least 33 significant bits.
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 timem.actime = timem.modtime = (long)isc_time_seconds(time);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Here is the real check for the high bit being set.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister (1UL << (sizeof(timem.actime) * CHAR_BIT - 1))) != 0)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define TEMPLATE "XXXXXXXXXX.tmp" /* 14 characters. */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceisc_file_mktemplate(const char *path, char *buf, size_t buflen) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (isc_file_template(path, TEMPLATE, buf, buflen));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerisc_file_template(const char *path, const char *templet, char *buf,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister if ((s - path + 1 + strlen(templet) + 1) > buflen)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerisc_file_renameunique(const char *file, char *templet) {
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 res = isc_file_safemovefile(file, templet);
b3e77535185043f089b346166440402d092030c3David Lawrenceisc_file_openunique(char *templet, FILE **fp) {
b3e77535185043f089b346166440402d092030c3David Lawrence * Win32 does not have mkstemp. Using emulation above.
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Bristerisc_file_rename(const char *oldname, const char *newname) {
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * Look for c:\path\... style or \\computer\shar\path...
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * the UNC style file specs
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if ((filename[0] == '\\') && (filename[1] == '\\'))
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (isalpha(filename[0]) != 0 && filename[1] == ':' && filename[2] == '\\')
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtonisc_file_iscurrentdir(const char *filename) {
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington return (s + 1);
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtonisc_file_progname(const char *filename, char *progname, size_t namelen) {
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington const char *s;
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews * Strip the path from the name
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews * Strip any and all suffixes
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * Copy the result to the buffer