Skip to content

Commit c8a8f9c

Browse files
committed
touch: safer approach
1 parent 141540c commit c8a8f9c

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

+stdlib/touch.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
function ok = touch(file)
44

5-
ok = stdlib.exists(file);
6-
7-
if ~ok
8-
fid = fopen(file, "w");
9-
ok = fid > 0 && fclose(fid) == 0;
10-
end
5+
fid = fopen(file, 'a');
6+
ok = fid > 0 && fclose(fid) == 0;
117

128
if ok
13-
ok = stdlib.set_modtime(file, datetime("now"));
9+
ok = stdlib.set_modtime(file, datetime('now'));
1410
end
1511

1612
end

test/TestDisk.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function test_is_mount(tc, B_ps)
8989

9090

9191
function test_hard_link_count(tc, B_jps)
92-
fn = "test_hard_link_count.txt";
92+
fn = 'test_hard_link_count.txt';
9393
tc.assertTrue(stdlib.touch(fn))
9494

9595
[i, b] = stdlib.hard_link_count(fn, B_jps);
@@ -136,7 +136,7 @@ function test_is_dev_drive(tc, B_ps)
136136
function test_remove_file(tc)
137137
tc.assumeFalse(stdlib.matlabOlderThan('R2018a'), 'test shaky on Matlab < R2018a')
138138

139-
f = "test_remove.tmp";
139+
f = 'test_remove.tmp';
140140

141141
tc.verifyFalse(stdlib.remove(f), "should not succeed at removing non-existant path")
142142

0 commit comments

Comments
 (0)