2 * Copyright (C) 2004, 2007, 2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
31 #include <sys/utime.h>
35 #include <isc/result.h>
39 #include <isc/string.h>
41 #include "errno2result.h"
44 * Emulate UNIX mkstemp, which returns an open FD to the new file
48 gettemp(char *path, int *doopen) {
53 trv = strrchr(path, 'X');
56 /* extra X's get set to 0's */
57 while (*--trv == 'X') {
58 *trv = (pid % 10) + '0';
62 * check the target directory; if you have six X's and it
63 * doesn't exist this runs for a *very* long time.
65 for (start = trv + 1;; --trv) {
70 if (stat(path, &sbuf))
72 if (!S_ISDIR(sbuf.st_mode)) {
84 open(path, O_CREAT|O_EXCL|O_RDWR,
85 _S_IREAD | _S_IWRITE)) >= 0)
89 } else if (stat(path, &sbuf))
90 return (errno == ENOENT ? 1 : 0);
92 /* tricky little algorithm for backward compatibility */
111 mkstemp(char *path) {
114 return (gettemp(path, &fd) ? fd : -1);
118 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
119 * it might be good to provide a mechanism that allows for the results
120 * of a previous stat() to be used again without having to do another stat,
121 * such as perl's mechanism of using "_" in place of a file name to indicate
122 * that the results of the last stat should be used. But then you get into
123 * annoying MP issues. BTW, Win32 has stat().
126 file_stats(const char *file, struct stat *stats) {
127 isc_result_t result = ISC_R_SUCCESS;
129 REQUIRE(file != NULL);
130 REQUIRE(stats != NULL);
132 if (stat(file, stats) != 0)
133 result = isc__errno2result(errno);
139 * isc_file_safemovefile is needed to be defined here to ensure that
140 * any file with the new name is renamed to a backup name and then the
141 * rename is done. If all goes well then the backup can be deleted,
142 * otherwise it gets renamed back.
146 isc_file_safemovefile(const char *oldname, const char *newname) {
154 * Make sure we have something to do
156 if (stat(oldname, &sbuf) != 0) {
162 * Rename to a backup the new file if it still exists
164 if (stat(newname, &sbuf) == 0) {
166 strcpy(buf, newname);
167 strcat(buf, ".XXXXX");
168 tmpfd = mkstemp(buf);
172 _chmod(newname, _S_IREAD | _S_IWRITE);
174 filestatus = MoveFile(newname, buf);
176 /* Now rename the file to the new name
178 _chmod(oldname, _S_IREAD | _S_IWRITE);
180 filestatus = MoveFile(oldname, newname);
181 if (filestatus == 0) {
183 * Try to rename the backup back to the original name
184 * if the backup got created
186 if (exists == TRUE) {
187 filestatus = MoveFile(buf, newname);
195 * Delete the backup file if it got created
198 filestatus = DeleteFile(buf);
203 isc_file_getmodtime(const char *file, isc_time_t *time) {
206 REQUIRE(file != NULL);
207 REQUIRE(time != NULL);
209 if ((fh = open(file, _O_RDONLY | _O_BINARY)) < 0)
210 return (isc__errno2result(errno));
212 if (!GetFileTime((HANDLE) _get_osfhandle(fh),
219 return (isc__errno2result(errno));
222 return (ISC_R_SUCCESS);
226 isc_file_settime(const char *file, isc_time_t *time) {
229 REQUIRE(file != NULL && time != NULL);
231 if ((fh = open(file, _O_RDWR | _O_BINARY)) < 0)
232 return (isc__errno2result(errno));
235 * Set the date via the filedate system call and return. Failing
236 * this call implies the new file times are not supported by the
237 * underlying file system.
239 if (!SetFileTime((HANDLE) _get_osfhandle(fh),
246 return (isc__errno2result(errno));
250 return (ISC_R_SUCCESS);
255 #define TEMPLATE "XXXXXXXXXX.tmp" /* 14 characters. */
258 isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
259 return (isc_file_template(path, TEMPLATE, buf, buflen));
263 isc_file_template(const char *path, const char *templet, char *buf,
267 REQUIRE(path != NULL);
268 REQUIRE(templet != NULL);
269 REQUIRE(buf != NULL);
271 s = strrchr(templet, '\\');
275 s = strrchr(path, '\\');
278 if ((s - path + 1 + strlen(templet) + 1) > buflen)
279 return (ISC_R_NOSPACE);
281 strncpy(buf, path, s - path + 1);
282 buf[s - path + 1] = '\0';
283 strcat(buf, templet);
285 if ((strlen(templet) + 1) > buflen)
286 return (ISC_R_NOSPACE);
288 strcpy(buf, templet);
291 return (ISC_R_SUCCESS);
295 isc_file_renameunique(const char *file, char *templet) {
298 isc_result_t result = ISC_R_SUCCESS;
300 REQUIRE(file != NULL);
301 REQUIRE(templet != NULL);
303 fd = mkstemp(templet);
305 result = isc__errno2result(errno);
309 if (result == ISC_R_SUCCESS) {
310 res = isc_file_safemovefile(file, templet);
312 result = isc__errno2result(errno);
313 (void)unlink(templet);
320 isc_file_openuniqueprivate(char *templet, FILE **fp) {
321 int mode = _S_IREAD | _S_IWRITE;
322 return (isc_file_openuniquemode(templet, mode, fp));
326 isc_file_openunique(char *templet, FILE **fp) {
327 int mode = _S_IREAD | _S_IWRITE;
328 return (isc_file_openuniquemode(templet, mode, fp));
332 isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
335 isc_result_t result = ISC_R_SUCCESS;
337 REQUIRE(templet != NULL);
338 REQUIRE(fp != NULL && *fp == NULL);
341 * Win32 does not have mkstemp. Using emulation above.
343 fd = mkstemp(templet);
346 result = isc__errno2result(errno);
347 if (result == ISC_R_SUCCESS) {
351 (void)fchmod(fd, mode);
353 f = fdopen(fd, "w+");
355 result = isc__errno2result(errno);
356 (void)remove(templet);
366 isc_file_remove(const char *filename) {
369 REQUIRE(filename != NULL);
371 r = unlink(filename);
373 return (ISC_R_SUCCESS);
375 return (isc__errno2result(errno));
379 isc_file_rename(const char *oldname, const char *newname) {
382 REQUIRE(oldname != NULL);
383 REQUIRE(newname != NULL);
385 r = isc_file_safemovefile(oldname, newname);
387 return (ISC_R_SUCCESS);
389 return (isc__errno2result(errno));
393 isc_file_exists(const char *pathname) {
396 REQUIRE(pathname != NULL);
398 return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
402 isc_file_isplainfile(const char *filename) {
404 * This function returns success if filename is a plain file.
406 struct stat filestat;
407 memset(&filestat,0,sizeof(struct stat));
409 if ((stat(filename, &filestat)) == -1)
410 return(isc__errno2result(errno));
412 if(! S_ISREG(filestat.st_mode))
413 return(ISC_R_INVALIDFILE);
415 return(ISC_R_SUCCESS);
419 isc_file_isabsolute(const char *filename) {
420 REQUIRE(filename != NULL);
422 * Look for c:\path\... style, c:/path/... or \\computer\shar\path...
423 * the UNC style file specs
425 if ((filename[0] == '\\') && (filename[1] == '\\'))
427 if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '\\')
429 if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '/')
435 isc_file_iscurrentdir(const char *filename) {
436 REQUIRE(filename != NULL);
437 return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
441 isc_file_ischdiridempotent(const char *filename) {
442 REQUIRE(filename != NULL);
444 if (isc_file_isabsolute(filename))
446 if (filename[0] == '\\')
448 if (filename[0] == '/')
450 if (isc_file_iscurrentdir(filename))
456 isc_file_basename(const char *filename) {
459 REQUIRE(filename != NULL);
461 s = strrchr(filename, '\\');
468 isc_file_progname(const char *filename, char *progname, size_t namelen) {
473 REQUIRE(filename != NULL);
474 REQUIRE(progname != NULL);
477 * Strip the path from the name
479 s = isc_file_basename(filename);
481 return (ISC_R_NOSPACE);
485 * Strip any and all suffixes
489 if (namelen <= strlen(s))
490 return (ISC_R_NOSPACE);
493 return (ISC_R_SUCCESS);
497 * Copy the result to the buffer
501 return (ISC_R_NOSPACE);
503 strncpy(progname, s, len);
504 progname[len] = '\0';
505 return (ISC_R_SUCCESS);
509 isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
513 REQUIRE(filename != NULL);
514 REQUIRE(path != NULL);
516 retval = GetFullPathName(filename, pathlen, path, &ptrname);
518 /* Something went wrong in getting the path */
520 return (ISC_R_NOTFOUND);
521 /* Caller needs to provide a larger buffer to contain the string */
522 if (retval >= pathlen)
523 return (ISC_R_NOSPACE);
524 return (ISC_R_SUCCESS);
528 isc_file_truncate(const char *filename, isc_offset_t size) {
531 REQUIRE(filename != NULL && size >= 0);
533 if ((fh = open(filename, _O_RDWR | _O_BINARY)) < 0)
534 return (isc__errno2result(errno));
536 if(_chsize(fh, size) != 0) {
538 return (isc__errno2result(errno));
542 return (ISC_R_SUCCESS);
546 isc_file_safecreate(const char *filename, FILE **fp) {
553 REQUIRE(filename != NULL);
554 REQUIRE(fp != NULL && *fp == NULL);
556 result = file_stats(filename, &sb);
557 if (result == ISC_R_SUCCESS) {
558 if ((sb.st_mode & S_IFREG) == 0)
559 return (ISC_R_INVALIDFILE);
560 flags = O_WRONLY | O_TRUNC;
561 } else if (result == ISC_R_FILENOTFOUND) {
562 flags = O_WRONLY | O_CREAT | O_EXCL;
566 fd = open(filename, flags, S_IRUSR | S_IWUSR);
568 return (isc__errno2result(errno));
572 result = isc__errno2result(errno);
578 return (ISC_R_SUCCESS);
582 isc_file_splitpath(isc_mem_t *mctx, char *path, char **dirname, char **basename)
584 char *dir, *file, *slash;
587 slash = strrchr(path, '/');
589 backslash = strrchr(path, '\\');
590 if ((slash != NULL && backslash != NULL && backslash > slash) ||
591 (slash == NULL && backslash != NULL))
596 dir = isc_mem_strdup(mctx, "/");
597 } else if (slash != NULL) {
599 dir = isc_mem_allocate(mctx, slash - path);
601 strlcpy(dir, path, slash - path);
604 dir = isc_mem_strdup(mctx, ".");
608 return (ISC_R_NOMEMORY);
611 isc_mem_free(mctx, dir);
612 return (ISC_R_INVALIDFILE);
618 return (ISC_R_SUCCESS);