File: FileSystemWritableFileStream.js

package info (click to toggle)
firefox-esr 78.15.0esr-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,301,156 kB
  • sloc: cpp: 5,665,905; javascript: 4,798,386; ansic: 2,878,233; python: 977,004; asm: 270,347; xml: 181,456; java: 111,756; sh: 72,926; makefile: 21,819; perl: 13,380; cs: 4,725; yacc: 4,565; objc: 3,026; pascal: 1,787; lex: 1,720; ada: 1,681; exp: 505; php: 436; lisp: 260; awk: 152; ruby: 103; csh: 80; sed: 53; sql: 45
file content (117 lines) | stat: -rw-r--r-- 4,435 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'trunc_shrink', root);
  const stream = await handle.createWritable();

  await stream.write('1234567890');
  await stream.truncate(5);
  await stream.close();

  assert_equals(await getFileContents(handle), '12345');
  assert_equals(await getFileSize(handle), 5);
}, 'truncate() to shrink a file');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'trunc_grow', root);
  const stream = await handle.createWritable();

  await stream.write('abc');
  await stream.truncate(5);
  await stream.close();

  assert_equals(await getFileContents(handle), 'abc\0\0');
  assert_equals(await getFileSize(handle), 5);
}, 'truncate() to grow a file');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'parent_dir', root);
  const file_name = 'create_writable_fails_when_dir_removed.txt';
  const handle = await createEmptyFile(t, file_name, dir);

  await root.removeEntry('parent_dir', {recursive: true});
  await promise_rejects_dom(t, 'NotFoundError', handle.createWritable());
}, 'createWritable() fails when parent directory is removed');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'parent_dir', root);
  const file_name = 'write_fails_when_dir_removed.txt';
  const handle = await createEmptyFile(t, file_name, dir);
  const stream = await handle.createWritable();

  await root.removeEntry('parent_dir', {recursive: true});
  await promise_rejects_dom(t, 'NotFoundError', stream.write('foo'));
}, 'write() fails when parent directory is removed');

directory_test(async (t, root) => {
  const dir = await createDirectory(t, 'parent_dir', root);
  const file_name = 'truncate_fails_when_dir_removed.txt';
  const handle = await createEmptyFile(t, file_name, dir);
  const stream = await handle.createWritable();

  await root.removeEntry('parent_dir', {recursive: true});
  await promise_rejects_dom(t, 'NotFoundError', stream.truncate(0));
}, 'truncate() fails when parent directory is removed');

directory_test(async (t, root) => {
  const handle = await createFileWithContents(
      t, 'atomic_file_is_copied.txt', 'fooks', root);
  const stream = await handle.createWritable({keepExistingData: true});

  await stream.write('bar');
  await stream.close();
  assert_equals(await getFileContents(handle), 'barks');
  assert_equals(await getFileSize(handle), 5);
}, 'createWritable({keepExistingData: true}): atomic writable file stream initialized with source contents');

directory_test(async (t, root) => {
  const handle = await createFileWithContents(
      t, 'atomic_file_is_not_copied.txt', 'very long string', root);
  const stream = await handle.createWritable({keepExistingData: false});

  await stream.write('bar');
  assert_equals(await getFileContents(handle), 'very long string');
  await stream.close();
  assert_equals(await getFileContents(handle), 'bar');
  assert_equals(await getFileSize(handle), 3);
}, 'createWritable({keepExistingData: false}): atomic writable file stream initialized with empty file');

directory_test(async (t, root) => {
  const handle = await createFileWithContents(
      t, 'trunc_smaller_offset.txt', '1234567890', root);
  const stream = await handle.createWritable({keepExistingData: true});

  await stream.truncate(5);
  await stream.write('abc');
  await stream.close();

  assert_equals(await getFileContents(handle), 'abc45');
  assert_equals(await getFileSize(handle), 5);
}, 'cursor position: truncate size > offset');

directory_test(async (t, root) => {
  const handle = await createFileWithContents(
      t, 'trunc_bigger_offset.txt', '1234567890', root);
  const stream = await handle.createWritable({keepExistingData: true});

  await stream.seek(6);
  await stream.truncate(5);
  await stream.write('abc');
  await stream.close();

  assert_equals(await getFileContents(handle), '12345abc');
  assert_equals(await getFileSize(handle), 8);
}, 'cursor position: truncate size < offset');

directory_test(async (t, root) => {
  const handle = await createEmptyFile(t, 'contents', root);
  const stream = await handle.createWritable();

  stream.write('abc');
  stream.write('def');
  stream.truncate(9);
  stream.seek(0);
  stream.write('xyz');
  await stream.close();

  assert_equals(await getFileContents(handle), 'xyzdef\0\0\0');
  assert_equals(await getFileSize(handle), 9);
}, 'commands are queued');