forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.jl
1722 lines (1580 loc) · 60.3 KB
/
file.jl
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
#############################################
# Create some temporary files & directories #
#############################################
starttime = time_ns() / 1e9
pwd_ = pwd()
dir = mktempdir()
file = joinpath(dir, "afile.txt")
# like touch, but lets the operating system update the timestamp
# for greater precision on some platforms (windows)
@test close(open(file,"w")) === nothing
subdir = joinpath(dir, "adir")
mkdir(subdir)
subdir2 = joinpath(dir, "adir2")
mkdir(subdir2)
@test_throws Base._UVError("mkdir($(repr(file)); mode=0o777)", Base.UV_EEXIST) mkdir(file)
let err = nothing
try
mkdir(file)
catch err
io = IOBuffer()
showerror(io, err)
@test startswith(String(take!(io)), "IOError: mkdir") && err.code == Base.UV_EEXIST
end
end
if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
dirlink = joinpath(dir, "dirlink")
symlink(subdir, dirlink)
@test stat(dirlink) == stat(subdir)
@test readdir(dirlink) == readdir(subdir)
# relative link
relsubdirlink = joinpath(subdir, "rel_subdirlink")
reldir = joinpath("..", "adir2")
symlink(reldir, relsubdirlink)
@test stat(relsubdirlink) == stat(subdir2)
@test readdir(relsubdirlink) == readdir(subdir2)
# creation of symlink to directory that does not yet exist
new_dir = joinpath(subdir, "new_dir")
foo_file = joinpath(subdir, "new_dir", "foo")
nedlink = joinpath(subdir, "nonexistent_dirlink")
symlink("new_dir", nedlink; dir_target=true)
try
readdir(nedlink)
@test false
catch e
@test isa(e, Base.IOError)
# It's surprisingly difficult to know what numeric value this will be across platforms
# so we'll just check the string representation instead. :(
@test endswith(e.msg, "(ENOENT)")
end
mkdir(new_dir)
touch(foo_file)
@test readdir(new_dir) == readdir(nedlink)
rm(foo_file)
rm(new_dir)
rm(nedlink)
end
if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
link = joinpath(dir, "afilesymlink.txt")
symlink(file, link)
@test stat(file) == stat(link)
# relative link
rellink = joinpath(subdir, "rel_afilesymlink.txt")
relfile = joinpath("..", "afile.txt")
symlink(relfile, rellink)
@test stat(rellink) == stat(file)
end
@testset "hardlink" begin
link = joinpath(dir, "afilehardlink.txt")
hardlink(file, link)
@test stat(file) == stat(link)
# when the destination exists
@test_throws Base.IOError hardlink(file, link)
rm(link)
# the source file does not exist
missing_file = joinpath(dir, "for-sure-missing-file.txt")
@test_throws Base.IOError hardlink(missing_file, link)
end
using Random
@testset "that temp names are actually unique" begin
temps = [tempname(cleanup=false) for _ = 1:100]
@test allunique(temps)
temps = map(1:100) do _
path, io = mktemp(cleanup=false)
close(io)
return path
end
@test allunique(temps)
foreach(temps) do path
rm(path, force=true)
end
end
@testset "tempname with parent" begin
t = tempname()
@test dirname(t) == tempdir()
mktempdir() do d
t = tempname(d)
@test dirname(t) == d
end
@test_throws ArgumentError tempname(randstring())
# 38873: check that `TMPDIR` being set does not
# override the parent argument to `tempname`.
mktempdir() do d
withenv("TMPDIR"=>tempdir()) do
t = tempname(d)
@test dirname(t) == d
end
end
@test_throws ArgumentError tempname(randstring())
end
child_eval(code::String) = eval(Meta.parse(readchomp(`$(Base.julia_cmd()) -E $code`)))
@testset "mktemp/dir basic cleanup" begin
# mktemp without cleanup
t = child_eval("t = mktemp(cleanup=false)[1]; @assert isfile(t); t")
@test isfile(t)
rm(t, force=true)
@test !ispath(t)
# mktemp with cleanup
t = child_eval("t = mktemp()[1]; @assert isfile(t); t")
@test !ispath(t)
# mktempdir without cleanup
t = child_eval("t = mktempdir(cleanup=false); touch(joinpath(t, \"file.txt\")); t")
@test isfile("$t/file.txt")
rm(t, recursive=true, force=true)
@test !ispath(t)
# mktempdir with cleanup
t = child_eval("t = mktempdir(); touch(joinpath(t, \"file.txt\")); t")
@test !ispath(t)
# tempname without cleanup
t = child_eval("t = tempname(cleanup=false); touch(t); t")
@test isfile(t)
rm(t, force=true)
@test !ispath(t)
# tempname with cleanup
t = child_eval("t = tempname(); touch(t); t")
@test !ispath(t)
end
import Base.Filesystem: TEMP_CLEANUP_MIN, TEMP_CLEANUP_MAX, TEMP_CLEANUP
function with_temp_cleanup(f::Function, n::Int)
SAVE_TEMP_CLEANUP_MIN = TEMP_CLEANUP_MIN[]
SAVE_TEMP_CLEANUP_MAX = TEMP_CLEANUP_MAX[]
SAVE_TEMP_CLEANUP = copy(TEMP_CLEANUP)
empty!(TEMP_CLEANUP)
TEMP_CLEANUP_MIN[] = n
TEMP_CLEANUP_MAX[] = n
try f()
finally
Sys.iswindows() && GC.gc(true)
for t in keys(TEMP_CLEANUP)
rm(t, recursive=true, force=true)
end
copy!(TEMP_CLEANUP, SAVE_TEMP_CLEANUP)
TEMP_CLEANUP_MAX[] = SAVE_TEMP_CLEANUP_MAX
TEMP_CLEANUP_MIN[] = SAVE_TEMP_CLEANUP_MIN
end
end
function mktempfile(; cleanup=true)
(file, io) = mktemp(cleanup=cleanup)
Sys.iswindows() && close(io)
return file
end
@testset "mktemp/dir cleanup list purging" begin
n = 12 # cleanup min & max
@assert n % 2 == n % 3 == 0 # otherwise tests won't work
with_temp_cleanup(n) do
@test length(TEMP_CLEANUP) == 0
@test TEMP_CLEANUP_MAX[] == n
# for n mktemps, no purging is triggered
temps = String[]
for i = 1:n
t = i % 2 == 0 ? mktempfile() : mktempdir()
push!(temps, t)
@test ispath(t)
@test length(TEMP_CLEANUP) == i
@test TEMP_CLEANUP_MAX[] == n
# delete 1/3 of the temp paths
i % 3 == 0 && rm(t, recursive=true, force=true)
end
# without cleanup no purge is triggered
t = mktempdir(cleanup=false)
@test isdir(t)
@test length(TEMP_CLEANUP) == n
@test TEMP_CLEANUP_MAX[] == n
rm(t, recursive=true, force=true)
# purge triggered by next mktemp with cleanup
t = mktempfile()
push!(temps, t)
n′ = 2n÷3 + 1
@test 2n′ > n
@test isfile(t)
@test length(TEMP_CLEANUP) == n′
@test TEMP_CLEANUP_MAX[] == 2n′
# remove all temp files
for t in temps
rm(t, recursive=true, force=true)
end
# for n′ mktemps, no purging is triggered
for i = 1:n′
t = i % 2 == 0 ? mktempfile() : mktempdir()
push!(temps, t)
@test ispath(t)
@test length(TEMP_CLEANUP) == n′ + i
@test TEMP_CLEANUP_MAX[] == 2n′
# delete 2/3 of the temp paths
i % 3 != 0 && rm(t, recursive=true, force=true)
end
# without cleanup no purge is triggered
t = mktempfile(cleanup=false)
@test isfile(t)
@test length(TEMP_CLEANUP) == 2n′
@test TEMP_CLEANUP_MAX[] == 2n′
rm(t, force=true)
# purge triggered by next mktemp
t = mktempdir()
push!(temps, t)
n′′ = n′÷3 + 1
@test 2n′′ < n
@test isdir(t)
@test length(TEMP_CLEANUP) == n′′
@test TEMP_CLEANUP_MAX[] == n
end
end
no_error_logging(f::Function) =
Base.CoreLogging.with_logger(f, Base.CoreLogging.NullLogger())
@testset "hof mktemp/dir when cleanup is prevented" begin
d = mktempdir()
with_temp_cleanup(3) do
@test length(TEMP_CLEANUP) == 0
@test TEMP_CLEANUP_MAX[] == 3
local t, f
temps = String[]
# mktemp is normally cleaned up on completion
mktemp(d) do path, _
@test isfile(path)
t = path
end
@test !ispath(t)
@test length(TEMP_CLEANUP) == 0
@test TEMP_CLEANUP_MAX[] == 3
# mktemp when cleanup is prevented
no_error_logging() do
mktemp(d) do path, _
@test isfile(path)
f = open(path) # make undeletable on Windows
chmod(d, 0o400) # make undeletable on UNIX
t = path
end
end
chmod(d, 0o700)
close(f)
@test isfile(t)
@test length(TEMP_CLEANUP) == 1
@test TEMP_CLEANUP_MAX[] == 3
push!(temps, t)
# mktempdir is normally cleaned up on completion
mktempdir(d) do path
@test isdir(path)
t = path
end
@test !ispath(t)
@test length(TEMP_CLEANUP) == 1
@test TEMP_CLEANUP_MAX[] == 3
# mktempdir when cleanup is prevented
no_error_logging() do
mktempdir(d) do path
@test isdir(path)
# make undeletable on Windows:
f = open(joinpath(path, "file.txt"), "w+")
chmod(d, 0o400) # make undeletable on UNIX
t = path
end
end
chmod(d, 0o700)
close(f)
@test isdir(t)
@test length(TEMP_CLEANUP) == 2
@test TEMP_CLEANUP_MAX[] == 3
push!(temps, t)
# make one more temp file
t = mktemp()[1]
@test isfile(t)
@test length(TEMP_CLEANUP) == 3
@test TEMP_CLEANUP_MAX[] == 3
# nothing has been deleted yet
for t in temps
@test ispath(t)
end
# another temp file triggers purge
t = mktempdir()
@test isdir(t)
@test length(TEMP_CLEANUP) == 2
@test TEMP_CLEANUP_MAX[] == 4
# now all the temps are gone
for t in temps
@test !ispath(t)
end
end
end
@testset "mktempdir() permissions correction" begin
# Test that `mktempdir()` with children with weird permissions get cleared
# before we would delete the directory
local temp_dir_path
mktempdir() do dir
temp_dir_path = dir
mkdir(joinpath(dir, "foo"))
touch(joinpath(dir, "foo", "bar"))
chmod(joinpath(dir, "foo"), 0o444)
@test isdir(temp_dir_path)
end
@test !isdir(temp_dir_path)
end
#######################################################################
# This section tests some of the features of the stat-based file info #
#######################################################################
@test !isfile(Base.Filesystem.StatStruct())
@test isdir(dir)
@test !isfile(dir)
@test !islink(dir)
@test !isdir(file)
@test isfile(file)
@test !islink(file)
@test filemode(file) & 0o444 > 0 # readable
@test filemode(file) & 0o222 > 0 # writable
@test chmod(file, filemode(file) & 0o7555) == file
@test filemode(file) & 0o222 == 0
chmod(file, filemode(file) | 0o222)
@test filemode(file) & 0o111 == 0
@test filesize(file) == 0
# issue #26685
@test !isfile("http://google.com")
if Sys.iswindows()
permissions = 0o444
@test filemode(dir) & 0o777 != permissions
@test filemode(subdir) & 0o777 != permissions
@test filemode(file) & 0o777 != permissions
chmod(dir, permissions, recursive=true)
@test filemode(dir) & 0o777 == permissions
@test filemode(subdir) & 0o777 == permissions
@test filemode(file) & 0o777 == permissions
chmod(dir, 0o666, recursive=true) # Reset permissions in case someone wants to use these later
else
function get_umask()
umask = ccall(:umask, UInt32, (UInt32,), 0)
ccall(:umask, UInt32, (UInt32,), umask)
return umask
end
mktempdir() do tmpdir
umask = get_umask()
tmpfile=joinpath(tmpdir, "tempfile.txt")
tmpfile2=joinpath(tmpdir, "tempfile2.txt")
touch(tmpfile)
cp(tmpfile, tmpfile2)
@test filemode(tmpfile) & (~umask) == filemode(tmpfile2)
rm(tmpfile2)
chmod(tmpfile, 0o777)
cp(tmpfile, tmpfile2)
@test filemode(tmpfile) & (~umask) == filemode(tmpfile2)
rm(tmpfile2)
chmod(tmpfile, 0o707)
cp(tmpfile, tmpfile2)
@test filemode(tmpfile) & (~umask) == filemode(tmpfile2)
rm(tmpfile2)
linkfile=joinpath(dir, "tempfile.txt")
symlink(tmpfile, linkfile)
permissions=0o776
@test filemode(dir) & 0o777 != permissions
@test filemode(subdir) & 0o777 != permissions
@test filemode(file) & 0o777 != permissions
@test filemode(linkfile) & 0o777 != permissions
@test filemode(tmpfile) & 0o777 != permissions
chmod(dir, permissions, recursive=true)
@test filemode(dir) & 0o777 == permissions
@test filemode(subdir) & 0o777 == permissions
@test filemode(file) & 0o777 == permissions
@test lstat(link).mode & 0o777 != permissions # Symbolic links are not modified.
@test filemode(linkfile) & 0o777 != permissions # Symbolic links are not followed.
@test filemode(tmpfile) & 0o777 != permissions
rm(linkfile)
end
end
function test_stat_error(stat::Function, pth)
if stat === lstat && !(pth isa AbstractString)
return # no lstat for fd handles
end
ex = try; stat(pth); false; catch ex; ex; end::Base.IOError
@test ex.code == (pth isa AbstractString ? Base.UV_EACCES : Base.UV_EBADF)
pth isa AbstractString || (pth = Base.INVALID_OS_HANDLE)
@test startswith(ex.msg, "stat($(repr(pth)))")
nothing
end
@testset "stat errors" begin # PR 32031
mktempdir() do dir
cd(dir) do
touch("afile")
try
# remove permission to access this folder
# to cause cause EACCESS-denied errors
@static if Sys.iswindows()
@test ccall((:ImpersonateAnonymousToken, "Advapi32.dll"), stdcall, Cint, (Libc.WindowsRawSocket,),
ccall(:GetCurrentThread, Libc.WindowsRawSocket, ())) != 0
else
chmod(dir, 0o000)
end
for pth in ("afile",
joinpath("afile", "not_file"),
SubString(joinpath(dir, "afile")),
Base.RawFD(-1),
-1)
test_stat_error(stat, pth)
test_stat_error(lstat, pth)
end
finally
# restore permissions
# to let us cleanup afile
@static if Sys.iswindows()
ccall((:RevertToSelf, "advapi32.dll"), stdcall, Cint, ()) == 0 && exit(1)
else
chmod(dir, 0o777)
end
end
end
end
end
# On windows the filesize of a folder is the accumulation of all the contained
# files and is thus zero in this case.
if Sys.iswindows()
@test filesize(dir) == 0
else
@test filesize(dir) > 0
end
# We need both: one to check passed time, one to comapare file's mtime()
nowtime = time_ns() / 1e9
nowwall = time()
# Allow 10s skew in addition to the time it took us to actually execute this code
let skew = 10 + (nowtime - starttime)
mfile = mtime(file)
mdir = mtime(dir)
@test abs(nowwall - mfile) <= skew && abs(nowwall - mdir) <= skew && abs(mfile - mdir) <= skew
end
#@test Int(time()) >= Int(mtime(file)) >= Int(mtime(dir)) >= 0 # 1 second accuracy should be sufficient
# test links
if Sys.isunix()
@test islink(link) == true
@test readlink(link) == file
end
if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
@test islink(dirlink) == true
@test isdir(dirlink) == true
@test readlink(dirlink) == subdir * (Sys.iswindows() ? "\\" : "")
end
# rm recursive TODO add links
newfile = joinpath(dir, "bfile.txt")
mv(file, newfile)
file = newfile
c_tmpdir = mktempdir()
c_subdir = joinpath(c_tmpdir, "c_subdir")
mkdir(c_subdir)
c_file = joinpath(c_tmpdir, "cfile.txt")
cp(newfile, c_file)
@test isdir(c_subdir)
@test isfile(c_file)
@test_throws Base.IOError rm(c_tmpdir)
@test_throws Base.IOError rm(c_tmpdir, force=true)
# create temp dir in specific directory
d_tmpdir = mktempdir(c_tmpdir)
@test isdir(d_tmpdir)
@test Base.samefile(dirname(d_tmpdir), c_tmpdir)
# create temp file in specific directory
d_tmpfile,f = mktemp(c_tmpdir)
close(f)
@test isfile(d_tmpfile)
@test Base.samefile(dirname(d_tmpfile), c_tmpdir)
rm(c_tmpdir, recursive=true)
@test !isdir(c_tmpdir)
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir)
@test rm(c_tmpdir, force=true) === nothing
@test_throws Base._UVError("unlink($(repr(c_tmpdir)))", Base.UV_ENOENT) rm(c_tmpdir, recursive=true)
@test rm(c_tmpdir, force=true, recursive=true) === nothing
# Some operations can return multiple different error codes depending on the system environment.
function throws_matching_exception(f::Function, acceptable_exceptions::AbstractVector)
try
f()
@error "No exception was thrown."
return false
catch ex
if ex in acceptable_exceptions
return true
else
@error "The thrown exception is not in the list of acceptable exceptions" acceptable_exceptions exception=(ex, catch_backtrace())
return false
end
end
end
function throws_matching_uv_error(f::Function, pfx::AbstractString, codes::AbstractVector{<:Integer})
acceptable_exceptions = multiple_uv_errors(pfx, codes)
return throws_matching_exception(f, acceptable_exceptions)
end
function multiple_uv_errors(pfx::AbstractString, codes::AbstractVector{<:Integer})
return [Base._UVError(pfx, code) for code in codes]
end
if !Sys.iswindows()
# chown will give an error if the user does not have permissions to change files
uid = Libc.geteuid()
@test stat(file).uid == uid
@test uid == Libc.getuid()
if uid == 0 # root user
chown(file, -2, -1) # Change the file owner to nobody
@test stat(file).uid != 0
chown(file, 0, -2) # Change the file group to nogroup (and owner back to root)
@test stat(file).gid != 0
@test stat(file).uid == 0
@test chown(file, -1, 0) == file
@test stat(file).gid == 0
@test stat(file).uid == 0
else
@test throws_matching_uv_error("chown($(repr(file)), -2, -1)", [Base.UV_EPERM, Base.UV_EINVAL]) do
chown(file, -2, -1) # Non-root user cannot change ownership to another user
end
@test throws_matching_uv_error("chown($(repr(file)), -1, -2)", [Base.UV_EPERM, Base.UV_EINVAL]) do
chown(file, -1, -2) # Non-root user cannot change group to a group they are not a member of (eg: nogroup)
end
end
else
# test that chown doesn't cause any errors for Windows
@test chown(file, -2, -2) == file
end
##############
# mark/reset #
##############
s = open(file, "w")
write(s, "Marked!\n")
write(s, "Hello world!\n")
write(s, "Goodbye world!\n")
close(s)
s = open(file)
mark(s)
str = readline(s)
@test startswith(str, "Marked!")
@test ismarked(s)
reset(s)
@test !ismarked(s)
str = readline(s)
@test startswith(str, "Marked!")
mark(s)
@test readline(s) == "Hello world!"
@test ismarked(s)
unmark(s)
@test !ismarked(s)
@test_throws ArgumentError reset(s)
@test !unmark(s)
@test !ismarked(s)
close(s)
#######################################################################
# This section tests temporary file and directory creation. #
#######################################################################
@testset "quoting filenames" begin
@test try
open("this file is not expected to exist")
false
catch e
isa(e, SystemError) || rethrow()
@test e.errnum == 2
@test startswith(sprint(showerror, e), "SystemError: opening file \"this file is not expected to exist\"")
true
end
end
@testset "tempdir" begin
my_tempdir = tempdir()
@test isdir(my_tempdir)
@test my_tempdir[end] != '/'
@test my_tempdir[end] != '\\'
var = Sys.iswindows() ? "TMP" : "TMPDIR"
PATH_PREFIX = Sys.iswindows() ? "C:\\" : "/tmp/" * "x"^255 # we want a long path on UNIX so that we test buffer resizing in `tempdir`
# Warning: On Windows uv_os_tmpdir internally calls GetTempPathW. The max string length for
# GetTempPathW is 261 (including the implied trailing backslash), not the typical length 259.
# We thus use 260 (with implied trailing slash backlash this then gives 261 chars)
# NOTE: not the actual max path on UNIX, but true in the Windows case for this function.
# NOTE: we subtract 9 to account for i = 0:9.
MAX_PATH = (Sys.iswindows() ? 260 - length(PATH_PREFIX) : 255) - 9
for i = 0:9
local tmp = joinpath(PATH_PREFIX, "x"^MAX_PATH * "123456789"[1:i])
@test withenv(var => tmp) do
tempdir()
end == tmp
end
end
let path = tempname()
# issue #9053
@test !ispath(path)
end
(p, f) = mktemp()
print(f, "Here is some text")
close(f)
@test isfile(p) == true
@test read(p, String) == "Here is some text"
rm(p)
let
tmp_path = mktemp() do p, io
@test isfile(p)
print(io, "鴨かも?")
p
end
@test tmp_path != ""
@test !isfile(tmp_path)
end
let
tmpdir = mktempdir() do d
@test isdir(d)
d
end
@test tmpdir != ""
@test !isdir(tmpdir)
end
emptyfile = joinpath(dir, "empty")
touch(emptyfile)
emptyf = open(emptyfile)
@test isempty(readlines(emptyf, keep=true))
close(emptyf)
rm(emptyfile)
########################################################################################
## This section tests cp & mv(rename) files, directories, absolute and relative links. #
########################################################################################
function check_dir(orig_path::AbstractString, copied_path::AbstractString, follow_symlinks::Bool)
isdir(orig_path) || throw(ArgumentError("'$orig_path' is not a directory."))
# copied_path must also be a dir.
@test isdir(copied_path)
readir_orig = readdir(orig_path)
readir_copied = readdir(copied_path)
@test readir_orig == readir_copied
# check recursive
for name in readir_orig
@test name in readir_copied
check_cp(joinpath(orig_path, name), joinpath(copied_path, name), follow_symlinks)
end
end
function check_cp(orig_path::AbstractString, copied_path::AbstractString, follow_symlinks::Bool)
if islink(orig_path)
if !follow_symlinks
# copied_path must be a link
@test islink(copied_path)
readlink_orig = readlink(orig_path)
# copied_path must have the same link value:
# this is true for absolute and relative links
@test readlink_orig == readlink(copied_path)
if isabspath(readlink_orig)
@test isabspath(readlink(copied_path))
end
else
# copied_path may not be a link if follow_symlinks=true
@test islink(orig_path) == !islink(copied_path)
if isdir(orig_path)
check_dir(orig_path, copied_path, follow_symlinks)
else
# copied_path must also be a file.
@test isfile(copied_path)
# copied_path must have same content
@test read(orig_path, String) == read(copied_path, String)
end
end
elseif isdir(orig_path)
check_cp_main(orig_path, copied_path, follow_symlinks)
else
# copied_path must also be a file.
@test isfile(copied_path)
# copied_path must have same content
@test read(orig_path, String) == read(copied_path, String)
end
end
function check_cp_main(orig::AbstractString, copied::AbstractString, follow_symlinks::Bool)
if isdir(orig)
check_dir(orig, copied, follow_symlinks)
else
check_cp(orig, copied, follow_symlinks)
end
end
function cp_and_test(src::AbstractString, dst::AbstractString, follow_symlinks::Bool)
cp(src, dst; follow_symlinks=follow_symlinks)
check_cp_main(src, dst, follow_symlinks)
end
## cp ----------------------------------------------------
# issue #8698
# Test copy file
let
afile = joinpath(dir, "a.txt")
touch(afile)
af = open(afile, "r+")
write(af, "This is indeed a test")
bfile = joinpath(dir, "b.txt")
cp(afile, bfile)
cfile = joinpath(dir, "c.txt")
write(cfile, "This is longer than the contents of afile")
cp(afile, cfile; force=true)
a_stat = stat(afile)
b_stat = stat(bfile)
c_stat = stat(cfile)
@test a_stat.mode == b_stat.mode
@test a_stat.size == b_stat.size
@test a_stat.size == c_stat.size
@test parse(Int, split(sprint(show, a_stat),"mode: ")[2][1:8]) == a_stat.mode
close(af)
rm(afile)
rm(bfile)
rm(cfile)
end
## mv ----------------------------------------------------
mktempdir() do tmpdir
# rename file
file = joinpath(tmpdir, "afile.txt")
close(open(file, "w")) # like touch, but lets the operating system update
files_stat = stat(file)
# the timestamp for greater precision on some platforms (windows)
newfile = joinpath(tmpdir, "bfile.txt")
mv(file, newfile)
newfile_stat = stat(newfile)
@test !ispath(file)
@test isfile(newfile)
@test Base.samefile(files_stat, newfile_stat)
file = newfile
# Test renaming directories
a_tmpdir = mktempdir()
b_tmpdir = joinpath(tmpdir, "b_tmpdir")
# grab a_tmpdir's file info before renaming
a_stat = stat(a_tmpdir)
# rename, then make sure b_tmpdir does exist and a_tmpdir doesn't
mv(a_tmpdir, b_tmpdir)
@test isdir(b_tmpdir)
@test !ispath(a_tmpdir)
# get b_tmpdir's file info and compare with a_tmpdir
b_stat = stat(b_tmpdir)
@test Base.samefile(a_stat, b_stat)
rm(b_tmpdir)
end
# issue #10506 #10434
## Tests for directories and links to directories
if !Sys.iswindows() || Sys.windows_version() >= Sys.WINDOWS_VISTA_VER
function setup_dirs(tmpdir)
srcdir = joinpath(tmpdir, "src")
hidden_srcdir = joinpath(tmpdir, ".hidden_srcdir")
hidden_srcsubdir = joinpath(hidden_srcdir, ".hidden_srcsubdir")
srcdir_cp = joinpath(tmpdir, "srcdir_cp")
mkdir(srcdir)
mkdir(hidden_srcdir)
mkdir(hidden_srcsubdir)
abs_dirlink = joinpath(tmpdir, "abs_dirlink")
symlink(abspath(srcdir), abs_dirlink)
cd(tmpdir)
rel_dirlink = "rel_dirlink"
symlink("src", rel_dirlink)
cd(pwd_)
cfile = joinpath(srcdir, "c.txt")
file_txt = "This is some text with unicode - 这是一个文件"
write(cfile, file_txt)
hidden_cfile = joinpath(hidden_srcsubdir, "c.txt")
write(hidden_cfile, file_txt)
abs_dirlink_cp = joinpath(tmpdir, "abs_dirlink_cp")
hidden_srcsubdir_cp = joinpath(tmpdir, ".hidden_srcsubdir_cp")
path_rel_dirlink = joinpath(tmpdir, rel_dirlink)
path_rel_dirlink_cp = joinpath(tmpdir, "rel_dirlink_cp")
test_src_paths = [srcdir, hidden_srcsubdir, abs_dirlink, path_rel_dirlink]
test_cp_paths = [srcdir_cp, hidden_srcsubdir_cp, abs_dirlink_cp, path_rel_dirlink_cp]
return test_src_paths, test_cp_paths
end
function cp_follow_symlinks_false_check(s, d; force=false)
cp(s, d; force=force, follow_symlinks=false)
@test isdir(s) == isdir(d)
@test islink(s) == islink(d)
islink(s) && @test readlink(s) == readlink(d)
islink(s) && @test isabspath(readlink(s)) == isabspath(readlink(d))
# all should contain 1 file named "c.txt"
@test "c.txt" in readdir(d)
@test length(readdir(d)) == 1
end
function mv_check(s, d, d_mv; force=true)
# setup dest
cp(s, d; force=true, follow_symlinks=false)
stat_d = stat(d)
# mv(rename) dst to dst_mv
mv(d, d_mv; force=force)
stat_d_mv = stat(d_mv)
# make sure d does not exist anymore
@test !ispath(d)
# compare s, with d_mv
@test isdir(s) == isdir(d_mv)
@test islink(s) == islink(d_mv)
islink(s) && @test readlink(s) == readlink(d_mv)
islink(s) && @test isabspath(readlink(s)) == isabspath(readlink(d_mv))
# all should contain 1 file named "c.txt"
@test "c.txt" in readdir(d_mv)
@test length(readdir(d_mv)) == 1
# d => d_mv same file/dir
@test Base.samefile(stat_d, stat_d_mv)
end
## Test require `force=true` (remove destination first) for existing
# directories and existing links to directories
# cp ----------------------------------------------------
mktempdir() do tmpdir
# Setup new copies for the test
maindir1 = joinpath(tmpdir, "maindir1")
maindir2 = joinpath(tmpdir, "maindir2")
mkdir(maindir1)
mkdir(maindir2)
test_src_paths1, test_new_paths1 = setup_dirs(maindir1)
test_src_paths2, test_new_paths2 = setup_dirs(maindir2)
for (s, d) in zip(test_src_paths1, test_new_paths1)
cp_follow_symlinks_false_check(s, d)
end
for (s, d) in zip(test_src_paths2, test_new_paths2)
cp_follow_symlinks_false_check(s, d)
end
# Test require `force=true`
for s in test_src_paths1
for d in test_new_paths2
@test_throws ArgumentError cp(s, d; force=false)
@test_throws ArgumentError cp(s, d; force=false, follow_symlinks=true)
end
end
# Test remove the existing path first and copy
# need to use here the test_src_paths2:
# otherwise ArgumentError: 'src' and 'dst' refer to the same file/dir.
for (s, d) in zip(test_src_paths2, test_new_paths1)
cp_follow_symlinks_false_check(s, d; force=true)
end
# Test remove the existing path first and copy an empty dir
emptydir = joinpath(maindir1, "emptydir")
mkdir(emptydir)
for d in test_new_paths1
cp(emptydir, d; force=true, follow_symlinks=false)
# Expect no link because a dir is copied (follow_symlinks=false does not effect this)
@test isdir(d) && !islink(d)
# none should contain any file
@test isempty(readdir(d))
end
end
# mv ----------------------------------------------------
mktempdir() do tmpdir
# Setup new copies for the test
maindir1 = joinpath(tmpdir, "maindir1")
maindir2 = joinpath(tmpdir, "maindir2")
mkdir(maindir1)
mkdir(maindir2)
test_src_paths1, test_new_paths1 = setup_dirs(maindir1)
test_src_paths2, test_new_paths2 = setup_dirs(maindir2)
for (s, d) in zip(test_src_paths1, test_new_paths1)
cp_follow_symlinks_false_check(s, d; force=true)
end
for (s, d) in zip(test_src_paths2, test_new_paths2)
cp_follow_symlinks_false_check(s, d; force=true)
end
# Test require `force=true`
for s in test_src_paths1
for d in test_new_paths2
@test_throws ArgumentError mv(s, d; force=false)
end
end
# Test remove the existing path first and move
# need to use here the test_src_paths2:
# otherwise ArgumentError: 'src' and 'dst' refer to the same file/dir.This is not supported.
for (s, d) in zip(test_src_paths2, test_new_paths1)
d_mv = joinpath(dirname(d), "$(basename(d))_mv")
mv_check(s, d, d_mv; force=true)
end
end
# Test full: absolute and relative directory links
# cp / mv ----------------------------------------------------
mktempdir() do tmpdir
maindir = joinpath(tmpdir, "mytestdir")
mkdir(maindir)
targetdir = abspath(joinpath(maindir, "targetdir"))
mkdir(targetdir)
subdir1 = joinpath(maindir, "subdir1")
mkdir(subdir1)
cfile = abspath(joinpath(maindir, "c.txt"))
write(cfile, "This is c.txt - 这是一个文件")
write(abspath(joinpath(targetdir, "file1.txt")),
"This is file1.txt - 这是一个文件")
abs_dl = joinpath(maindir, "abs_linkto_targetdir")
symlink(targetdir, abs_dl)
# Setup relative links
cd(subdir1)
rel_dl = "rel_linkto_targetdir"
rel_dir = joinpath("..", "targetdir")
symlink(rel_dir, rel_dl)
cd(pwd_)
# TEST: Directory with links: Test each option
maindir_new = joinpath(dirname(maindir),"maindir_new")
maindir_new_keepsym = joinpath(dirname(maindir),"maindir_new_keepsym")
cp_and_test(maindir, maindir_new, true)
cp_and_test(maindir, maindir_new_keepsym, false)
# mv ----------------------------------------------------
# move the 3 maindirs
for d in [maindir_new, maindir_new_keepsym, maindir]
d_mv = joinpath(dirname(d), "$(basename(d))_mv")
mv(d, d_mv; force=true)
end
end
# issue ----------------------------------------------------
# Check for issue when: (src == dst) or when one is a link to the other
# https://github.com/JuliaLang/julia/pull/11172#issuecomment-100391076
mktempdir() do tmpdir
test_src_paths1, test_new_paths1 = setup_dirs(tmpdir)
dirs = [joinpath(tmpdir, "src"), joinpath(tmpdir, "abs_dirlink"), joinpath(tmpdir, "rel_dirlink")]
for src in dirs
for dst in dirs
# cptree
@test_throws ArgumentError Base.cptree(src, dst; force=true, follow_symlinks=false)
@test_throws ArgumentError Base.cptree(src, dst; force=true, follow_symlinks=true)
# cp
@test_throws ArgumentError cp(src, dst; force=true, follow_symlinks=false)
@test_throws ArgumentError cp(src, dst; force=true, follow_symlinks=true)
# mv
@test_throws ArgumentError mv(src, dst; force=true)
end
end
end