Skip to content

Commit

Permalink
apps,lib,test: add "mode" argument to btoep_open
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Sep 1, 2020
1 parent 5c957eb commit 70cdf1f
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 19 deletions.
3 changes: 2 additions & 1 deletion apps/btoep-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ int main(int argc, char** argv) {
}

btoep_dataset dataset;
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path, opts.paths.lock_path)) {
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path,
opts.paths.lock_path, B_OPEN_OR_CREATE_READ_WRITE)) {
print_error(&dataset);
return B_EXIT_CODE_APP_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/btoep-find-offset.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char** argv) {

btoep_dataset dataset;
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path,
opts.paths.lock_path)) {
opts.paths.lock_path, B_OPEN_EXISTING_READ_ONLY)) {
print_error(&dataset);
return B_EXIT_CODE_APP_ERROR;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/btoep-get-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ int main(int argc, char** argv) {
}

btoep_dataset dataset;
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path, opts.paths.lock_path)) {
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path,
opts.paths.lock_path, B_OPEN_EXISTING_READ_ONLY)) {
print_error(&dataset);
return B_EXIT_CODE_APP_ERROR;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/btoep-list-ranges.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ int main(int argc, char** argv) {
}

btoep_dataset dataset;
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path, opts.paths.lock_path)) {
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path,
opts.paths.lock_path, B_OPEN_EXISTING_READ_ONLY)) {
print_error(&dataset);
return B_EXIT_CODE_APP_ERROR;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/btoep-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ int main(int argc, char** argv) {
}

btoep_dataset dataset;
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path, opts.paths.lock_path)) {
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path,
opts.paths.lock_path, B_OPEN_EXISTING_READ_ONLY)) {
print_error(&dataset);
return B_EXIT_CODE_APP_ERROR;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/btoep-set-size.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ int main(int argc, char** argv) {
}

btoep_dataset dataset;
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path, opts.paths.lock_path)) {
if (!btoep_open(&dataset, opts.paths.data_path, opts.paths.index_path,
opts.paths.lock_path, B_OPEN_OR_CREATE_READ_WRITE)) {
print_error(&dataset);
return B_EXIT_CODE_APP_ERROR;
}
Expand Down
9 changes: 7 additions & 2 deletions lib/include/btoep/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#define B_ERR_READ_OUT_OF_BOUNDS 6
#define B_ERR_INVALID_ARGUMENT 7

#define B_OPEN_EXISTING_READ_ONLY 0
#define B_OPEN_EXISTING_READ_WRITE 1
#define B_CREATE_NEW_READ_WRITE 2
#define B_OPEN_OR_CREATE_READ_WRITE 3

#ifdef _MSC_VER
# define OS_MAX_PATH MAX_PATH
typedef LPCTSTR btoep_path;
Expand Down Expand Up @@ -77,8 +82,8 @@ typedef struct {
* State management
*/

// TODO: Allow read-only open
bool btoep_open(btoep_dataset* dataset, btoep_path data_path, btoep_path index_path, btoep_path lock_path);
bool btoep_open(btoep_dataset* dataset, btoep_path data_path,
btoep_path index_path, btoep_path lock_path, int mode);

bool btoep_close(btoep_dataset* dataset);

Expand Down
31 changes: 24 additions & 7 deletions lib/src/dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,26 @@ static inline bool set_io_error(btoep_dataset* dataset) {
return set_error(dataset, B_ERR_IO, true);
}

static bool fd_open(btoep_dataset* dataset, btoep_fd* fd, btoep_path path) {
static bool fd_open(btoep_dataset* dataset, btoep_fd* fd, btoep_path path,
int mode) {
#ifdef _MSC_VER
*fd = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwDesiredAccess = GENERIC_READ;
if (mode != B_OPEN_EXISTING_READ_ONLY)
dwDesiredAccess |= GENERIC_WRITE;
DWORD dwCreationDisposition = OPEN_EXISTING;
if (mode == B_CREATE_NEW_READ_WRITE)
dwCreationDisposition = CREATE_NEW;
else if (mode == B_OPEN_OR_CREATE_READ_WRITE)
dwCreationDisposition = OPEN_ALWAYS;
*fd = CreateFile(path, dwDesiredAccess, 0, NULL, dwCreationDisposition,
FILE_ATTRIBUTE_NORMAL, NULL);
#else
*fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
int flag = (mode == B_OPEN_EXISTING_READ_ONLY) ? O_RDONLY : O_RDWR;
if (mode & (B_CREATE_NEW_READ_WRITE & B_OPEN_OR_CREATE_READ_WRITE))
flag |= O_CREAT;
if (mode == B_CREATE_NEW_READ_WRITE)
flag |= O_EXCL;
*fd = open(path, flag, S_IRUSR | S_IWUSR);
#endif
if (*fd == INVALID_FILE_FD)
return set_io_error(dataset);
Expand Down Expand Up @@ -175,8 +190,8 @@ static bool btoep_unlock(btoep_dataset* dataset) {
#endif
}

// TODO: Allow read-only open
bool btoep_open(btoep_dataset* dataset, btoep_path data_path, btoep_path index_path, btoep_path lock_path) {
bool btoep_open(btoep_dataset* dataset, btoep_path data_path,
btoep_path index_path, btoep_path lock_path, int mode) {
if (dataset == NULL || data_path == NULL ||
!copy_path(dataset->data_path, data_path, NULL, NULL) ||
!copy_path(dataset->index_path, index_path, data_path, ".idx") ||
Expand All @@ -187,12 +202,14 @@ bool btoep_open(btoep_dataset* dataset, btoep_path data_path, btoep_path index_p
if (!btoep_lock(dataset))
return false;

if (!fd_open(dataset, &dataset->data_fd, data_path)) {
// TODO: Either both files should be created, or neither.

if (!fd_open(dataset, &dataset->data_fd, data_path, mode)) {
btoep_unlock(dataset); // TODO: Return value
return false;
}

if (!fd_open(dataset, &dataset->index_fd, dataset->index_path)) {
if (!fd_open(dataset, &dataset->index_fd, dataset->index_path, mode)) {
fd_close(dataset, dataset->data_fd); // TODO: Return value
btoep_unlock(dataset); // TODO: Return value
return false;
Expand Down
4 changes: 4 additions & 0 deletions test/system/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def cmd_stdout(self, args, text=False, **kwargs):
result = self.cmd(args, expected_stdout=None, **kwargs)
return (tostring(result.stdout) if text else result.stdout)

def cmd_stderr(self, args, text=True, **kwargs):
result = self.cmd(args, expected_stderr=None, **kwargs)
return (tostring(result.stderr) if text else result.stderr)

def assertInfo(self, cmd, options):
version = self.cmd_stdout([cmd, '--version'], text=True)
self.assertTrue(version.startswith(cmd + ' '))
Expand Down
8 changes: 8 additions & 0 deletions test/system/test-find-offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,13 @@ def test_non_empty_index(self):
self.assertEqual(self.findOffset(dataset, 385, 'no-data'), 386)
self.assertEqual(self.findOffset(dataset, 10000, 'no-data'), 10000)

def test_fs_error(self):
# Test that the command fails if the dataset does not exist.
dataset = self.reserveDataset()
stderr = self.cmd_stderr(['btoep-find-offset', '--dataset', dataset,
'--start-at=0', '--stop-at=data'],
expected_returncode = ExitCode.APP_ERROR)
self.assertTrue(stderr.startswith('Error: System input/output error: '))

if __name__ == '__main__':
unittest.main()
9 changes: 8 additions & 1 deletion test/system/test-get-index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helper import SystemTest
from helper import ExitCode, SystemTest
import unittest

class GetIndexTest(SystemTest):
Expand Down Expand Up @@ -68,5 +68,12 @@ def test_get_index(self):
index = self.cmdGetIndex(dataset, min_range_length=12)
self.assertEqual(index, b'')

def test_fs_error(self):
# Test that the command fails if the dataset does not exist.
dataset = self.reserveDataset()
stderr = self.cmd_stderr(['btoep-get-index', '--dataset', dataset],
expected_returncode = ExitCode.APP_ERROR)
self.assertTrue(stderr.startswith('Error: System input/output error: '))

if __name__ == '__main__':
unittest.main()
9 changes: 8 additions & 1 deletion test/system/test-list-ranges.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from helper import SystemTest
from helper import ExitCode, SystemTest
import unittest

class ListRangesTest(SystemTest):
Expand Down Expand Up @@ -52,5 +52,12 @@ def test_list_ranges(self):
ranges = self.cmdListRanges(dataset, True)
self.assertEqual(ranges, '0..128\n257..257\n386..524287\n')

def test_fs_error(self):
# Test that the command fails if the dataset does not exist.
dataset = self.reserveDataset()
stderr = self.cmd_stderr(['btoep-list-ranges', '--dataset', dataset],
expected_returncode = ExitCode.APP_ERROR)
self.assertTrue(stderr.startswith('Error: System input/output error: '))

if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions test/system/test-read.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,12 @@ def test_read(self):
self.assertEqual(self.cmdRead(dataset, offset=1024 * 511), b'\x0a' * 1024)
self.assertOutOfBounds(dataset, length=1024 * 513)

def test_fs_error(self):
# Test that the command fails if the dataset does not exist.
dataset = self.reserveDataset()
stderr = self.cmd_stderr(['btoep-read', '--dataset', dataset],
expected_returncode = ExitCode.APP_ERROR)
self.assertTrue(stderr.startswith('Error: System input/output error: '))

if __name__ == '__main__':
unittest.main()
9 changes: 8 additions & 1 deletion test/unit/test-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ static void test_data(void) {
int error;

// Create the (empty) dataset.
assert(btoep_open(&dataset, "test_data", NULL, NULL));
assert(btoep_open(&dataset, "test_data", NULL, NULL,
B_CREATE_NEW_READ_WRITE));

// Make sure the index is empty.
assert(btoep_index_iterator_start(&dataset));
Expand Down Expand Up @@ -199,6 +200,12 @@ static void test_data(void) {
assert(btoep_index_iterator_is_eof(&dataset));

assert(btoep_close(&dataset));

// Creating the same dataset again should fail.
assert(!btoep_open(&dataset, "test_data", NULL, NULL,
B_CREATE_NEW_READ_WRITE));
btoep_get_error(&dataset, &error, NULL);
assert(error == B_ERR_IO);
}

TEST_MAIN(test_data)
3 changes: 2 additions & 1 deletion test/unit/test-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ static void test_index(void) {
bool b;

// Create the (empty) dataset.
assert(btoep_open(&dataset, "test_index", NULL, NULL));
assert(btoep_open(&dataset, "test_index", NULL, NULL,
B_CREATE_NEW_READ_WRITE));

// Make sure the index is empty.
assert(btoep_index_iterator_start(&dataset));
Expand Down

0 comments on commit 70cdf1f

Please sign in to comment.