Skip to content

Commit bd063ae

Browse files
Merge pull request #47 from JuliaIO/sk/open_read_write
2 parents 2725e1f + 4048684 commit bd063ae

File tree

1 file changed

+71
-95
lines changed

1 file changed

+71
-95
lines changed

src/Tar.jl

Lines changed: 71 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,36 @@ include("header.jl")
1818
include("create.jl")
1919
include("extract.jl")
2020

21+
## some API utilities ##
22+
2123
const true_predicate = _ -> true
2224

23-
open_read(f::Function, tarball::AbstractString) = open(f, tarball)
24-
open_read(f::Function, tarball::IO) = f(tarball)
25+
open_read(f::Function, file::AbstractString) = open(f, file)
26+
open_read(f::Function, file::IO) = f(file)
2527

26-
function open_write(f::Function, tarball::AbstractString)
27-
try open(f, tarball, write=true)
28+
function open_write(f::Function, file::AbstractString)
29+
try open(f, file, write=true)
30+
catch
31+
rm(file, force=true)
32+
rethrow()
33+
end
34+
return file
35+
end
36+
function open_write(f::Function, file::Nothing)
37+
file, io = mktemp()
38+
try f(io)
2839
catch
29-
rm(tarball, force=true)
40+
close(io)
41+
rm(file, force=true)
3042
rethrow()
3143
end
44+
close(io)
45+
return file
46+
end
47+
function open_write(f::Function, file::IO)
48+
f(file)
49+
return file
3250
end
33-
open_write(f::Function, tarball::IO) = f(tarball)
3451

3552
## official API: create, list, extract
3653

@@ -55,32 +72,20 @@ will be included in the archive.
5572
function create(
5673
predicate::Function,
5774
dir::AbstractString,
58-
tarball::Union{AbstractString, IO},
75+
tarball::Union{AbstractString, IO, Nothing} = nothing,
5976
)
60-
create_dir_check(dir)
77+
check_create_dir(dir)
6178
open_write(tarball) do tar
6279
create_tarball(predicate, tar, dir)
6380
end
64-
return tarball
65-
end
66-
67-
function create(predicate::Function, dir::AbstractString)
68-
create_dir_check(dir)
69-
tarball, tar = mktemp()
70-
try create_tarball(predicate, tar, dir)
71-
catch
72-
close(tar)
73-
rm(tarball, force=true)
74-
rethrow()
75-
end
76-
close(tar)
77-
return tarball
7881
end
7982

80-
create(dir::AbstractString, tarball::Union{AbstractString, IO}) =
83+
function create(
84+
dir::AbstractString,
85+
tarball::Union{AbstractString, IO, Nothing} = nothing,
86+
)
8187
create(true_predicate, dir, tarball)
82-
create(dir::AbstractString) =
83-
create(true_predicate, dir)
88+
end
8489

8590
"""
8691
list(tarball; [ strict = true ]) -> Vector{Header}
@@ -134,46 +139,35 @@ what is extracted during the extraction process.
134139
function extract(
135140
predicate::Function,
136141
tarball::Union{AbstractString, IO},
137-
dir::AbstractString,
142+
dir::Union{AbstractString, Nothing} = nothing,
138143
)
139-
extract_tarball_check(tarball)
140-
extract_dir_check(dir)
144+
check_extract_tarball(tarball)
145+
check_extract_dir(dir)
141146
open_read(tarball) do tar
142-
if ispath(dir)
147+
if dir !== nothing && ispath(dir)
143148
extract_tarball(predicate, tar, dir)
144149
else
145-
mkdir(dir)
146-
extract_tarball_cleanup_dir(predicate, tar, dir)
150+
if dir === nothing
151+
dir = mktempdir()
152+
else
153+
mkdir(dir)
154+
end
155+
try extract_tarball(predicate, tar, dir)
156+
catch
157+
chmod(dir, 0o700, recursive=true)
158+
rm(dir, force=true, recursive=true)
159+
rethrow()
160+
end
147161
end
148162
end
149-
return dir
163+
return dir::AbstractString
150164
end
151165

152-
function extract(predicate::Function, tarball::Union{AbstractString, IO})
153-
extract_tarball_check(tarball)
154-
open_read(tarball) do tar
155-
dir = mktempdir()
156-
extract_tarball_cleanup_dir(predicate, tar, dir)
157-
return dir
158-
end
159-
end
160-
161-
extract(tarball::Union{AbstractString, IO}, dir::AbstractString) =
162-
extract(true_predicate, tarball, dir)
163-
extract(tarball::Union{AbstractString, IO}) =
164-
extract(true_predicate, tarball)
165-
166-
function extract_tarball_cleanup_dir(
167-
predicate::Function,
168-
tar::IO,
169-
dir::AbstractString,
166+
function extract(
167+
tarball::Union{AbstractString, IO},
168+
dir::Union{AbstractString, Nothing} = nothing,
170169
)
171-
try extract_tarball(predicate, tar, dir)
172-
catch
173-
chmod(dir, 0o700, recursive=true)
174-
rm(dir, force=true, recursive=true)
175-
rethrow()
176-
end
170+
extract(true_predicate, tarball, dir)
177171
end
178172

179173
"""
@@ -202,48 +196,23 @@ record what content is encountered during the rewrite process.
202196
function rewrite(
203197
predicate::Function,
204198
old_tarball::Union{AbstractString, IO},
205-
new_tarball::Union{AbstractString, IO},
199+
new_tarball::Union{AbstractString, IO, Nothing} = nothing,
206200
)
207-
old_tarball = rewrite_old_tarball_check(old_tarball)
201+
old_tarball = check_rewrite_old_tarball(old_tarball)
208202
open_read(old_tarball) do old_tar
209203
open_write(new_tarball) do new_tar
210204
rewrite_tarball(predicate, old_tar, new_tar)
211205
end
212206
end
213-
return new_tarball
214207
end
215208

216209
function rewrite(
217-
predicate::Function,
218210
old_tarball::Union{AbstractString, IO},
219-
)
220-
old_tarball = rewrite_old_tarball_check(old_tarball)
221-
open_read(old_tarball) do old_tar
222-
new_tarball, new_tar = mktemp()
223-
try rewrite_tarball(predicate, old_tar, new_tar)
224-
catch
225-
close(new_tar)
226-
rm(new_tarball, force=true)
227-
rethrow()
228-
end
229-
close(new_tar)
230-
return new_tarball
231-
end
232-
end
233-
234-
function rewrite(
235-
old_tarball::Union{AbstractString, IO},
236-
new_tarball::Union{AbstractString, IO},
211+
new_tarball::Union{AbstractString, IO, Nothing} = nothing,
237212
)
238213
rewrite(true_predicate, old_tarball, new_tarball)
239214
end
240215

241-
function rewrite(
242-
old_tarball::Union{AbstractString, IO},
243-
)
244-
rewrite(true_predicate, old_tarball)
245-
end
246-
247216
"""
248217
tree_hash([ predicate, ] tarball;
249218
[ algorithm = "git-sha1", ]
@@ -305,7 +274,7 @@ function tree_hash(
305274
algorithm == "git-sha256" ? SHA.SHA256_CTX :
306275
error("invalid tree hashing algorithm: $algorithm")
307276

308-
tree_hash_tarball_check(tarball)
277+
check_tree_hash_tarball(tarball)
309278
open_read(tarball) do tar
310279
git_tree_hash(predicate, tar, HashType, skip_empty)
311280
end
@@ -316,32 +285,37 @@ function tree_hash(
316285
algorithm::AbstractString = "git-sha1",
317286
skip_empty::Bool = false,
318287
)
319-
tree_hash(true_predicate, tarball, algorithm=algorithm, skip_empty=skip_empty)
288+
tree_hash(
289+
true_predicate,
290+
tarball,
291+
algorithm = algorithm,
292+
skip_empty = skip_empty,
293+
)
320294
end
321295

322296
## error checking utility functions
323297

324-
create_dir_check(dir::AbstractString) =
298+
check_create_dir(dir::AbstractString) =
325299
isdir(dir) || error("""
326300
not a directory: $dir
327301
USAGE: create([predicate,] dir, [tarball])
328302
""")
329303

330-
list_tarball_check(tarball::AbstractString) =
304+
check_list_tarball(tarball::AbstractString) =
331305
isfile(tarball) || error("""
332306
not a file: $tarball
333307
USAGE: list(tarball)
334308
""")
335309

336-
extract_tarball_check(tarball::AbstractString) =
310+
check_extract_tarball(tarball::AbstractString) =
337311
isfile(tarball) || error("""
338312
not a file: $tarball
339313
USAGE: extract([predicate,] tarball, [dir])
340314
""")
341315

342-
extract_tarball_check(tarball::IO) = nothing
316+
check_extract_tarball(tarball::IO) = nothing
343317

344-
function extract_dir_check(dir::AbstractString)
318+
function check_extract_dir(dir::AbstractString)
345319
st = stat(dir)
346320
ispath(st) && !isdir(st) &&
347321
error("""
@@ -355,21 +329,23 @@ function extract_dir_check(dir::AbstractString)
355329
""")
356330
end
357331

358-
rewrite_old_tarball_check(tarball::AbstractString) =
332+
check_extract_dir(dir::Nothing) = nothing
333+
334+
check_rewrite_old_tarball(tarball::AbstractString) =
359335
isfile(tarball) ? tarball : rror("""
360336
not a file: $tarball
361337
USAGE: rewrite([predicate,] old_tarball, [new_tarball])
362338
""")
363339

364-
rewrite_old_tarball_check(tarball::IO) =
340+
check_rewrite_old_tarball(tarball::IO) =
365341
applicable(seek, tarball, 0) ? tarball : IOBuffer(read(tarball))
366342

367-
tree_hash_tarball_check(tarball::AbstractString) =
343+
check_tree_hash_tarball(tarball::AbstractString) =
368344
isfile(tarball) || error("""
369345
not a file: $tarball
370346
USAGE: tree_hash([predicate,] tarball)
371347
""")
372348

373-
tree_hash_tarball_check(tarball::IO) = nothing
349+
check_tree_hash_tarball(tarball::IO) = nothing
374350

375351
end # module

0 commit comments

Comments
 (0)