Skip to content

Commit 525d88c

Browse files
markshannondpgeorge
authored andcommitted
Fix repeated write to the same file.
1 parent f52ccee commit 525d88c

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

source/microbit/filesystem.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ mp_obj_t microbit_file_name(file_descriptor_obj *fd) {
266266

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

269+
static void clear_file(uint8_t chunk) {
270+
do {
271+
persistent_write_byte_unchecked(&(file_system_chunks[chunk].marker), FREED_CHUNK);
272+
DEBUG(("FILE DEBUG: Freeing chunk %d.\n", chunk));
273+
chunk = file_system_chunks[chunk].next_chunk;
274+
} while (chunk <= chunks_in_file_system);
275+
}
276+
269277
file_descriptor_obj *microbit_file_open(const char *name, uint32_t name_len, bool write, bool binary) {
270278
if (name_len > MAX_FILENAME_LENGTH) {
271279
return NULL;
@@ -274,7 +282,7 @@ file_descriptor_obj *microbit_file_open(const char *name, uint32_t name_len, boo
274282
if (write) {
275283
if (index != FILE_NOT_FOUND) {
276284
// Free old file
277-
persistent_write_byte_unchecked(&(file_system_chunks[index].marker), FREED_CHUNK);
285+
clear_file(index);
278286
}
279287
index = find_chunk_and_erase();
280288
if (index == FILE_NOT_FOUND) {
@@ -307,14 +315,6 @@ static file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bo
307315
return res;
308316
}
309317

310-
static void clear_file(uint8_t chunk) {
311-
do {
312-
persistent_write_byte_unchecked(&(file_system_chunks[chunk].marker), FREED_CHUNK);
313-
DEBUG(("FILE DEBUG: Freeing chunk %d.\n", chunk));
314-
chunk = file_system_chunks[chunk].next_chunk;
315-
} while (chunk <= chunks_in_file_system);
316-
}
317-
318318
mp_obj_t microbit_remove(mp_obj_t filename) {
319319
mp_uint_t name_len;
320320
const char *name = mp_obj_str_get_data(filename, &name_len);

tests/test_files.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,17 @@ def test_text_file():
8383
os.remove("test2.txt")
8484
assert not os.listdir()
8585

86-
8786
def test_many_files():
88-
for i in range(100):
87+
for i in range(80):
8988
name = "%d.dat" % i
9089
write_data_to_file(name, i*3, 16, 4)
9190
verify_file(name, data_stream(i*3), 16, 4, 'b')
92-
for i in range(100):
91+
for i in range(80):
9392
os.remove("%d.dat" % i)
9493
name = "_%d.dat" % i
9594
write_data_to_file(name, i*3, 16, 4)
9695
verify_file(name, data_stream(i*3), 16, 4, 'b')
97-
for i in range(100):
96+
for i in range(80):
9897
os.remove("_%d.dat" % i)
9998
assert not os.listdir()
10099

tests/test_files2.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ def clear_files():
4949
os.remove(f)
5050

5151
def test_interleaved_small_files():
52-
for i in range(100):
52+
for i in range(80):
5353
name = "%d.dat" % i
5454
write_data_to_file(name, i*3, 16, 6)
55-
for i in range(0, 100, 2):
55+
for i in range(0, 80, 2):
5656
os.remove("%d.dat" % i)
57-
for i in range(100, 150):
57+
for i in range(80, 120):
5858
name = "%d.dat" % i
5959
write_data_to_file(name, i*3, 16, 6)
6060
verify_file(name, data_stream(i*3), 16, 6, 'b')
61-
for i in range(1, 100, 2):
61+
for i in range(1, 80, 2):
6262
os.remove("%d.dat" % i)
63-
for i in range(100, 150):
63+
for i in range(80, 120):
6464
os.remove("%d.dat" % i)
6565
assert not os.listdir()
6666

@@ -70,21 +70,21 @@ def test_interleaved_large_files():
7070
out_buf[i] = 100-i
7171
with open("test1.dat", "wb") as fd1:
7272
with open("test2.dat", "wb") as fd2:
73-
for i in range(90):
73+
for i in range(60):
7474
fd1.write(out_buf)
7575
fd2.write(out_buf)
7676
os.remove("test2.dat")
7777
with open("test3.dat", "wb") as fd3:
78-
for i in range(90):
78+
for i in range(60):
7979
fd3.write(out_buf)
8080
assert sorted(os.listdir()) == [ "test1.dat", "test3.dat" ]
8181
in_buf = bytearray(100)
8282
with open("test1.dat", "rb") as fd:
83-
for i in range(90):
83+
for i in range(60):
8484
fd.readinto(in_buf)
8585
assert in_buf == out_buf
8686
with open("test3.dat", "rb") as fd:
87-
for i in range(90):
87+
for i in range(60):
8888
fd.readinto(in_buf)
8989
assert in_buf == out_buf
9090

tests/test_files3.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@
55
#We don't have space for Beowolf, so here's the next best thing...
66
text = """
77
’Twas brillig, and the slithy toves
8-
Did gyre and gimble in the wabe:
8+
Did gyre and gimble in the wabe:
99
All mimsy were the borogoves,
10-
And the mome raths outgrabe.
10+
And the mome raths outgrabe.
1111
1212
“Beware the Jabberwock, my son!
13-
The jaws that bite, the claws that catch!
13+
The jaws that bite, the claws that catch!
1414
Beware the Jubjub bird, and shun
15-
The frumious Bandersnatch!”
15+
The frumious Bandersnatch!”
1616
1717
He took his vorpal sword in hand;
18-
Long time the manxome foe he sought—
18+
Long time the manxome foe he sought—
1919
So rested he by the Tumtum tree
20-
And stood awhile in thought.
20+
And stood awhile in thought.
2121
2222
And, as in uffish thought he stood,
23-
The Jabberwock, with eyes of flame,
23+
The Jabberwock, with eyes of flame,
2424
Came whiffling through the tulgey wood,
25-
And burbled as it came!
25+
And burbled as it came!
2626
2727
One, two! One, two! And through and through
28-
The vorpal blade went snicker-snack!
28+
The vorpal blade went snicker-snack!
2929
He left it dead, and with its head
30-
He went galumphing back.
30+
He went galumphing back.
3131
3232
“And hast thou slain the Jabberwock?
33-
Come to my arms, my beamish boy!
33+
Come to my arms, my beamish boy!
3434
O frabjous day! Callooh! Callay!”
35-
He chortled in his joy.
35+
He chortled in his joy.
3636
3737
’Twas brillig, and the slithy toves
38-
Did gyre and gimble in the wabe:
38+
Did gyre and gimble in the wabe:
3939
All mimsy were the borogoves,
40-
And the mome raths outgrabe.
40+
And the mome raths outgrabe.
4141
"""
4242

4343
def test_read_while_writing():
@@ -73,11 +73,17 @@ def test_removing_mid_write():
7373
except OSError:
7474
pass
7575

76+
def test_repeated_write():
77+
for i in range(40):
78+
with open("jabbawocky.txt", "w") as j:
79+
j.write(text)
80+
7681
display.clear()
7782
try:
7883
test_read_while_writing()
7984
test_removing_mid_read()
8085
test_removing_mid_write()
86+
test_repeated_write()
8187
print("File test: PASS")
8288
display.show(Image.HAPPY)
8389
except Exception as ae:

0 commit comments

Comments
 (0)