diff options
author | Ken Raeburn <raeburn@mit.edu> | 2004-10-18 23:14:51 +0000 |
---|---|---|
committer | Ken Raeburn <raeburn@mit.edu> | 2004-10-18 23:14:51 +0000 |
commit | fb8314743c17c15ebcfcebee3565069079a819fc (patch) | |
tree | 55f0b504462f595df9414882a9140036b361bbf2 /src/util/profile/prof_file.c | |
parent | 32e9f9545401a17df3443a8e2969d6f84b29e74a (diff) | |
download | krb5-fb8314743c17c15ebcfcebee3565069079a819fc.tar.gz krb5-fb8314743c17c15ebcfcebee3565069079a819fc.tar.xz krb5-fb8314743c17c15ebcfcebee3565069079a819fc.zip |
Allow profile library caller to write the modified data to a different
file than was originally read.
* prof_file.c (write_data_to_file): New function, split out from
profile_flush_file_data. Add argument can_create indicating whether the old
file should already exist or not.
(profile_flush_file_data): Call it.
(profile_flush_file_data_to_file): New function.
* prof_int.h (profile_flush_file_data_to_file): Declare it.
(profile_flush_file_to_file): New macro.
* prof_init.c (profile_flush_to_file): New function.
* profile.hin (profile_flush_to_file): Declare.
* profile.swg (profile_flush_to_file): Declare.
* profile_tcl.c: Regenerated.
* prof_test1: Use profile_flush_to_file instead of profile_flush, and reload
from the new filename.
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16825 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/util/profile/prof_file.c')
-rw-r--r-- | src/util/profile/prof_file.c | 80 |
1 files changed, 55 insertions, 25 deletions
diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index d8386dcbb8..55fd34502a 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -367,37 +367,26 @@ make_hard_link(const char *oldpath, const char *newpath) #endif } -errcode_t profile_flush_file_data(prf_data_t data) +static errcode_t write_data_to_file(prf_data_t data, const char *outfile, + int can_create) { FILE *f; profile_filespec_t new_file; profile_filespec_t old_file; errcode_t retval = 0; - if (!data || data->magic != PROF_MAGIC_FILE_DATA) - return PROF_MAGIC_FILE_DATA; - - retval = k5_mutex_lock(&data->lock); - if (retval) - return retval; - - if ((data->flags & PROFILE_FILE_DIRTY) == 0) { - k5_mutex_unlock(&data->lock); - return 0; - } - retval = ENOMEM; new_file = old_file = 0; - new_file = malloc(strlen(data->filespec) + 5); + new_file = malloc(strlen(outfile) + 5); if (!new_file) goto errout; - old_file = malloc(strlen(data->filespec) + 5); + old_file = malloc(strlen(outfile) + 5); if (!old_file) goto errout; - sprintf(new_file, "%s.$$$", data->filespec); - sprintf(old_file, "%s.bak", data->filespec); + sprintf(new_file, "%s.$$$", outfile); + sprintf(old_file, "%s.bak", outfile); errno = 0; @@ -416,17 +405,22 @@ errcode_t profile_flush_file_data(prf_data_t data) } unlink(old_file); - if (make_hard_link(data->filespec, old_file) == 0) { + if (make_hard_link(outfile, old_file) == 0) { /* Okay, got the hard link. Yay. Now we've got our backup version, so just put the new version in place. */ - if (rename(new_file, data->filespec)) { + if (rename(new_file, outfile)) { /* Weird, the rename didn't work. But the old version should still be in place, so no special cleanup is needed. */ retval = errno; goto errout; } + } else if (errno == ENOENT && can_create) { + if (rename(new_file, outfile)) { + retval = errno; + goto errout; + } } else { /* Couldn't make the hard link, so there's going to be a small window where data->filespec does not refer to @@ -434,24 +428,23 @@ errcode_t profile_flush_file_data(prf_data_t data) #ifndef _WIN32 sync(); #endif - if (rename(data->filespec, old_file)) { + if (rename(outfile, old_file)) { retval = errno; goto errout; } - if (rename(new_file, data->filespec)) { + if (rename(new_file, outfile)) { retval = errno; - rename(old_file, data->filespec); /* back out... */ + rename(old_file, outfile); /* back out... */ goto errout; } } data->flags = 0; - if (rw_access(data->filespec)) + if (rw_access(outfile)) data->flags |= PROFILE_FILE_RW; retval = 0; - + errout: - k5_mutex_unlock(&data->lock); if (new_file) free(new_file); if (old_file) @@ -459,6 +452,43 @@ errout: return retval; } +errcode_t profile_flush_file_data(prf_data_t data) +{ + errcode_t retval = 0; + + if (!data || data->magic != PROF_MAGIC_FILE_DATA) + return PROF_MAGIC_FILE_DATA; + + retval = k5_mutex_lock(&data->lock); + if (retval) + return retval; + + if ((data->flags & PROFILE_FILE_DIRTY) == 0) { + k5_mutex_unlock(&data->lock); + return 0; + } + + retval = write_data_to_file(data, data->filespec, 0); + k5_mutex_unlock(&data->lock); + return retval; +} + +errcode_t profile_flush_file_data_to_file(prf_data_t data, const char *outfile) +{ + errcode_t retval = 0; + + if (!data || data->magic != PROF_MAGIC_FILE_DATA) + return PROF_MAGIC_FILE_DATA; + + retval = k5_mutex_lock(&data->lock); + if (retval) + return retval; + retval = write_data_to_file(data, outfile, 1); + k5_mutex_unlock(&data->lock); + return retval; +} + + void profile_dereference_data(prf_data_t data) { |