Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion base/export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,9 @@ export
mkdir,
rmdir,
tempdir,
tempfile,
tempname,
mktemp,
mktempdir,
download_file,
realpath,
filemode,
Expand Down
35 changes: 27 additions & 8 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,40 @@ function path_rename(old_pathname::String, new_pathname::String)
run(`mv $old_pathname $new_pathname`)
end

@linux_only function tempdir()
chomp(readall(`mktemp -d`))
tempnam = (OS_NAME == :Windows) ? :_tempnam : :tempnam

function tempname()
d = get(ENV, "TMPDIR", C_NULL) # tempnam ignores TMPDIR on darwin
p = ccall(tempnam, Ptr{Uint8}, (Ptr{Uint8},Ptr{Uint8}), d, "julia")
s = bytestring(p)
c_free(p)
s
end

@osx_only function tempdir()
chomp(readall(`mktemp -d -t tmp`))
tempdir() = dirname(tempname())

# Create and return the name of a temporary file along with an IOStream
@unix_only function mktemp()
b = file_path(tempdir(), "tmpXXXXXX")
p = ccall(:mkstemp, Int32, (Ptr{Uint8}, ), b)
return (b, fdio(p, true))
end

@windows_only function mktemp()
error("not yet implemented")
end

@linux_only function tempfile()
chomp(readall(`mktemp`))
# Create and return the name of a temporary directory
@unix_only function mktempdir()
b = file_path(tempdir(), "tmpXXXXXX")
p = ccall(:mkdtemp, Ptr{Uint8}, (Ptr{Uint8}, ), b)
return bytestring(p)
end

@osx_only function tempfile()
chomp(readall(`mktemp -t tmp`))
@windows_only function mktempdir()
error("not yet implemented")
end

function download_file(url::String)
filename = tempfile()
run(`curl -o $filename $url`)
Expand Down
18 changes: 14 additions & 4 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,20 @@ run(`chmod +w $filename`)
# This section tests temporary file and directory creation. #
#######################################################################

# @assert isdir(tempdir()) == true
# @assert isfile(tempdir()) == false
# @assert isdir(tempfile()) == false
# @assert isfile(tempfile()) == true
# my_tempdir = tempdir()
# @assert isdir(my_tempdir) == true

# path = tempname()
# @assert ispath(path) == false

# (filename, f) = mktemp()
# print(f, "Here is some text")
# close(f)
# @assert isfile(filename) == true
# @assert readall(filename) == "Here is some text"

# dirname = mktempdir()
# @assert isdir(dirname)

############
# Clean up #
Expand Down