Skip to content

Commit

Permalink
Fix repeated write to the same file.
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon authored and dpgeorge committed Oct 13, 2016
1 parent f52ccee commit 525d88c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
18 changes: 9 additions & 9 deletions source/microbit/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ mp_obj_t microbit_file_name(file_descriptor_obj *fd) {

static file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary);

static void clear_file(uint8_t chunk) {
do {
persistent_write_byte_unchecked(&(file_system_chunks[chunk].marker), FREED_CHUNK);
DEBUG(("FILE DEBUG: Freeing chunk %d.\n", chunk));
chunk = file_system_chunks[chunk].next_chunk;
} while (chunk <= chunks_in_file_system);
}

file_descriptor_obj *microbit_file_open(const char *name, uint32_t name_len, bool write, bool binary) {
if (name_len > MAX_FILENAME_LENGTH) {
return NULL;
Expand All @@ -274,7 +282,7 @@ file_descriptor_obj *microbit_file_open(const char *name, uint32_t name_len, boo
if (write) {
if (index != FILE_NOT_FOUND) {
// Free old file
persistent_write_byte_unchecked(&(file_system_chunks[index].marker), FREED_CHUNK);
clear_file(index);
}
index = find_chunk_and_erase();
if (index == FILE_NOT_FOUND) {
Expand Down Expand Up @@ -307,14 +315,6 @@ static file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bo
return res;
}

static void clear_file(uint8_t chunk) {
do {
persistent_write_byte_unchecked(&(file_system_chunks[chunk].marker), FREED_CHUNK);
DEBUG(("FILE DEBUG: Freeing chunk %d.\n", chunk));
chunk = file_system_chunks[chunk].next_chunk;
} while (chunk <= chunks_in_file_system);
}

mp_obj_t microbit_remove(mp_obj_t filename) {
mp_uint_t name_len;
const char *name = mp_obj_str_get_data(filename, &name_len);
Expand Down
7 changes: 3 additions & 4 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,17 @@ def test_text_file():
os.remove("test2.txt")
assert not os.listdir()


def test_many_files():
for i in range(100):
for i in range(80):
name = "%d.dat" % i
write_data_to_file(name, i*3, 16, 4)
verify_file(name, data_stream(i*3), 16, 4, 'b')
for i in range(100):
for i in range(80):
os.remove("%d.dat" % i)
name = "_%d.dat" % i
write_data_to_file(name, i*3, 16, 4)
verify_file(name, data_stream(i*3), 16, 4, 'b')
for i in range(100):
for i in range(80):
os.remove("_%d.dat" % i)
assert not os.listdir()

Expand Down
18 changes: 9 additions & 9 deletions tests/test_files2.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ def clear_files():
os.remove(f)

def test_interleaved_small_files():
for i in range(100):
for i in range(80):
name = "%d.dat" % i
write_data_to_file(name, i*3, 16, 6)
for i in range(0, 100, 2):
for i in range(0, 80, 2):
os.remove("%d.dat" % i)
for i in range(100, 150):
for i in range(80, 120):
name = "%d.dat" % i
write_data_to_file(name, i*3, 16, 6)
verify_file(name, data_stream(i*3), 16, 6, 'b')
for i in range(1, 100, 2):
for i in range(1, 80, 2):
os.remove("%d.dat" % i)
for i in range(100, 150):
for i in range(80, 120):
os.remove("%d.dat" % i)
assert not os.listdir()

Expand All @@ -70,21 +70,21 @@ def test_interleaved_large_files():
out_buf[i] = 100-i
with open("test1.dat", "wb") as fd1:
with open("test2.dat", "wb") as fd2:
for i in range(90):
for i in range(60):
fd1.write(out_buf)
fd2.write(out_buf)
os.remove("test2.dat")
with open("test3.dat", "wb") as fd3:
for i in range(90):
for i in range(60):
fd3.write(out_buf)
assert sorted(os.listdir()) == [ "test1.dat", "test3.dat" ]
in_buf = bytearray(100)
with open("test1.dat", "rb") as fd:
for i in range(90):
for i in range(60):
fd.readinto(in_buf)
assert in_buf == out_buf
with open("test3.dat", "rb") as fd:
for i in range(90):
for i in range(60):
fd.readinto(in_buf)
assert in_buf == out_buf

Expand Down
34 changes: 20 additions & 14 deletions tests/test_files3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@
#We don't have space for Beowolf, so here's the next best thing...
text = """
’Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
And the mome raths outgrabe.
“Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!”
The frumious Bandersnatch!”
He took his vorpal sword in hand;
Long time the manxome foe he sought—
Long time the manxome foe he sought—
So rested he by the Tumtum tree
And stood awhile in thought.
And stood awhile in thought.
And, as in uffish thought he stood,
The Jabberwock, with eyes of flame,
The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
And burbled as it came!
And burbled as it came!
One, two! One, two! And through and through
The vorpal blade went snicker-snack!
The vorpal blade went snicker-snack!
He left it dead, and with its head
He went galumphing back.
He went galumphing back.
“And hast thou slain the Jabberwock?
Come to my arms, my beamish boy!
Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!”
He chortled in his joy.
He chortled in his joy.
’Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
And the mome raths outgrabe.
"""

def test_read_while_writing():
Expand Down Expand Up @@ -73,11 +73,17 @@ def test_removing_mid_write():
except OSError:
pass

def test_repeated_write():
for i in range(40):
with open("jabbawocky.txt", "w") as j:
j.write(text)

display.clear()
try:
test_read_while_writing()
test_removing_mid_read()
test_removing_mid_write()
test_repeated_write()
print("File test: PASS")
display.show(Image.HAPPY)
except Exception as ae:
Expand Down

0 comments on commit 525d88c

Please sign in to comment.