Skip to content

Commit 3c94ca3

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

File tree

7 files changed

+12
-16
lines changed

7 files changed

+12
-16
lines changed

+stdlib/+java/set_modtime.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function ok = set_modtime(file, time)
44

55
try
6-
utc = posixtime(datetime(time, 'TimeZone', "UTC"));
6+
utc = posixtime(datetime(time, 'TimeZone', 'UTC'));
77
catch e
88
if ~strcmp(e.identifier, 'Octave:undefined-function')
99
rethrow(e)

+stdlib/+python/set_modtime.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function ok = set_modtime(file, time)
22

3-
utc = posixtime(datetime(time, 'TimeZone', "UTC"));
3+
utc = posixtime(datetime(time, 'TimeZone', 'UTC'));
44

55
try
66
s = py.os.stat(file);

+stdlib/+sys/set_modtime.m

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

33
ok = false;
44

5-
tiso = string(datetime(dt), "yyyy-MM-dd HH:mm:ss");
5+
tiso = string(datetime(dt), 'yyyy-MM-dd HH:mm:ss');
66

77
if ispc()
88
cmd = sprintf('pwsh -c "(Get-Item ''%s'').LastWriteTime = ''%s''"', file, tiso);
99
elseif ismac()
10-
cmd = sprintf('touch -mt %s "%s"', string(dt, "yyyyMMddHHmm"), file);
10+
cmd = sprintf('touch -mt %s "%s"', string(dt, 'yyyyMMddHHmm'), file);
1111
else
12-
cmd = sprintf('touch -t %s "%s"', string(dt, "yyyyMMddHHmm"), file);
12+
cmd = sprintf('touch -t %s "%s"', string(dt, 'yyyyMMddHHmm'), file);
1313
end
1414
% https://man7.org/linux/man-pages/man1/touch.1.html
1515

+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

test/TestHDF5.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function setup_file(tc)
6262
end
6363

6464

65-
methods (Test, TestTags = {'R2017b'})
65+
methods (Test, TestTags = {'R2017a'})
6666

6767
function test_auto_chunk_size(tc)
6868

test/TestTime.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function test_touch_modtime(tc, B_set_modtime)
2828
tc.assertTrue(stdlib.touch(fn))
2929
t0 = stdlib.get_modtime(fn);
3030

31-
ok = stdlib.set_modtime(fn, datetime("tomorrow"), B_set_modtime);
31+
ok = stdlib.set_modtime(fn, datetime('tomorrow'), B_set_modtime);
3232

3333
if ismember(B_set_modtime, stdlib.Backend().select('set_modtime'))
3434
tc.assertTrue(ok)

0 commit comments

Comments
 (0)