diff options
author | Neil Moore <neil@s-z.org> | 2014-05-27 11:38:18 -0400 |
---|---|---|
committer | Neil Moore <neil@s-z.org> | 2014-05-27 12:07:48 -0400 |
commit | b6e3c3094cb829c04b85ceb4be1e5306485d03ce (patch) | |
tree | e536bf86b6f6844c6e069a46ac71ec8b8aae450d /crawl-ref/source/files.cc | |
parent | c5af6dabdc7ad82ab8c02263b25a7576451feac1 (diff) | |
download | crawl-ref-b6e3c3094cb829c04b85ceb4be1e5306485d03ce.tar.gz crawl-ref-b6e3c3094cb829c04b85ceb4be1e5306485d03ce.zip |
Function to open and lock new files only.
Currently unused, but there are plans.
Diffstat (limited to 'crawl-ref/source/files.cc')
-rw-r--r-- | crawl-ref/source/files.cc | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index daca16f644..4e73c427dc 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -2328,6 +2328,30 @@ FILE *lk_open(const char *mode, const string &file) return handle; } +/** + * Attempts to open and lock a file for exclusive write access; fails if + * the file already exists. + * + * @param file The path to the file to be opened. + * @return A locked file handle for the specified file, if + * successful; else NULL. + */ +FILE *lk_open_exclusive(const string &file) +{ + int fd = open_u(file.c_str(), O_WRONLY|O_BINARY|O_EXCL|O_CREAT, 0666); + if (fd < 0) + return NULL; + + if (!lock_file(fd, true)) + { + mprf(MSGCH_ERROR, "ERROR: Could not lock file %s", file.c_str()); + close(fd); + return NULL; + } + + return fdopen(fd, "wb"); +} + void lk_close(FILE *handle, const string &file) { if (handle == NULL || handle == stdin) |