This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #32 Adds a test, and modifies `create_file()` implementations across supported platforms so that they exhibit the following behavior: 1. When creating a file for a path that already exists, creation should succeed, truncating the original file. 2. When creating a file (for writing) that has already been opened for the process, creation should fail, and the original file should be unaffected. - [x] windows - CreateFile api just does what we want. That one was already working. - [x] osx - has a special flag we can pass to `open` that does what we want. - [x] linux - use the `flock` api. According to `man 2 flock` this has been a syscall since kernel 2.0. Potentially racy, but that's probably ok.
- Loading branch information
Showing
4 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//! Test that we can't create the same file for writing within this process | ||
//! using the file_create() platform api. | ||
#include "platform.h" | ||
#include "logger.h" | ||
|
||
#include <cstdio> | ||
#include <stdexcept> | ||
|
||
/// Helper for passing size static strings as function args. | ||
/// For a function: `f(char*,size_t)` use `f(SIZED("hello"))`. | ||
/// Expands to `f("hello",6)`. | ||
#define SIZED(str) str, sizeof(str) | ||
|
||
#define L (aq_logger) | ||
#define LOG(...) L(0, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) | ||
#define ERR(...) L(1, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) | ||
#define EXPECT(e, ...) \ | ||
do { \ | ||
if (!(e)) { \ | ||
char buf[1 << 8] = { 0 }; \ | ||
ERR(__VA_ARGS__); \ | ||
snprintf(buf, sizeof(buf) - 1, __VA_ARGS__); \ | ||
throw std::runtime_error(buf); \ | ||
} \ | ||
} while (0) | ||
#define CHECK(e) EXPECT(e, "Expression evaluated as false: %s", #e) | ||
|
||
void | ||
reporter(int is_error, | ||
const char* file, | ||
int line, | ||
const char* function, | ||
const char* msg) | ||
{ | ||
fprintf(is_error ? stderr : stdout, | ||
"%s%s(%d) - %s: %s\n", | ||
is_error ? "ERROR " : "", | ||
file, | ||
line, | ||
function, | ||
msg); | ||
} | ||
|
||
int | ||
main(int argc, char** argv) | ||
{ | ||
logger_set_reporter(reporter); | ||
|
||
const char filename[] = "does-not-exist"; | ||
|
||
remove(filename); | ||
|
||
try { | ||
struct file file; | ||
|
||
EXPECT(0 == file_exists(SIZED(filename)), | ||
"The file \"%s\" must not already be present at the start of " | ||
"this test.", | ||
filename); | ||
|
||
EXPECT(file_create(&file, SIZED(filename)), | ||
"Expected the first creation of \"%s\" to succeed.", | ||
filename); | ||
file_close(&file); | ||
|
||
EXPECT( | ||
file_create(&file, SIZED(filename)), | ||
"Expected creation of \"%s\" to succeed. It was previously created, " | ||
"then closed, so it exists on the file system", | ||
filename); | ||
EXPECT( | ||
!file_create(&file, SIZED(filename)), | ||
"Expected creation of \"%s\" to fail. It hasn't been closed yet.", | ||
filename); | ||
|
||
remove(filename); | ||
return 0; | ||
} catch (const std::exception& e) { | ||
ERR("%s", e.what()); | ||
} catch (...) { | ||
ERR("Unknown exception"); | ||
} | ||
remove(filename); | ||
return 1; | ||
} |