nspkg / engine / package_fetch.v
145 lines · 141 sloc · 4.91 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// cache selection, output order and signature downloads follow pacman util.c/dload.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7fn writable_cache_directory(path string) bool {
8 info := os.stat(path) or { return false }
9 return info.mode & 0o170000 == 0o040000 && info.mode & 0o222 != 0 && C.access(unsafe { &char(path.str) }, C.W_OK) == 0
10}
11
12pub fn (mut handle Handle) cache_directory() !string {
13 handle.check()!
14 for directory in handle.opts.lists[.cache_dirs] {
15 info := os.stat(directory) or {
16 handle.log(.warning, localized_message('no %s cache exists, creating...\n', directory))
17 make_directory_mode(directory, 0o755) or {
18 handle.diagnostics << 'could not create cache ${directory}: ${err.msg()}'
19 continue
20 }
21 if writable_cache_directory(directory) {
22 handle.log(.debug, 'using cachedir: ${directory}\n')
23 return directory
24 }
25 continue
26 }
27 if info.mode & 0o170000 != 0o040000 {
28 handle.log(.debug, 'skipping cachedir, not a directory: ${directory}\n')
29 } else if C.faccessat(C.AT_FDCWD, unsafe { &char(directory.str) }, C.W_OK, C.AT_SYMLINK_NOFOLLOW) != 0 {
30 reason := os.last_error().msg()
31 handle.log(.debug, '"${directory}" is not writable: ${reason}\n')
32 handle.log(.debug, 'skipping cachedir, not writable: ${directory}\n')
33 } else if info.mode & 0o222 == 0 {
34 handle.log(.debug, 'skipping cachedir, no write bits set: ${directory}\n')
35 } else {
36 handle.log(.debug, 'using cachedir: ${directory}\n')
37 return directory
38 }
39 }
40 // the source's inverted stat test ignores a valid TMPDIR; check it explicitly.
41 temporary := os.getenv('TMPDIR')
42 fallback := if temporary != '' && writable_cache_directory(temporary) {
43 temporary
44 } else {
45 '/tmp'
46 }
47 if !writable_cache_directory(fallback) {
48 return handle.fail(.retrieve_prepare, 'no writable package cache')
49 }
50 handle.add_string_list(.cache_dirs, fallback)!
51 directory := handle.opts.lists[.cache_dirs].last()
52 handle.diagnostics << 'could not find or create package cache, using ${directory}'
53 handle.log(.debug, 'using cachedir: ${directory}\n')
54 handle.log(.warning, localized_message("couldn't find or create package cache, using %s instead\n", directory))
55 return directory
56}
57
58pub fn (mut handle Handle) fetch_package_urls(urls []string) ![]string {
59 handle.begin_retrieval()!
60 defer { handle.retrieval_active = false }
61 directory := handle.cache_directory()!
62 level := u32(handle.integer_option(.remote_file_siglevel)!)
63 mut cached_paths := []string{}
64 mut pending_names := []string{}
65 mut requests := []DownloadRequest{}
66 mut temporary_paths := []string{}
67 mut by_name := map[string]string{}
68 mut success := false
69 defer {
70 if !success {
71 for path in temporary_paths {
72 handle.remove_temporary_file(path)
73 handle.remove_temporary_file(path + '.sig')
74 handle.remove_temporary_file(path + '.sig.part')
75 }
76 }
77 }
78 for input in urls {
79 url := c_string_prefix(input)
80 if url == '' { return handle.fail(.wrong_args, 'empty package URL') }
81 slash := url.last_index('/') or { -1 }
82 basename := if slash >= 0 { url[slash + 1..] } else { '' }
83 if basename != '' {
84 cached := handle.find_cached_file(basename)!
85 if cached.found {
86 signature := if level & 1 != 0 {
87 handle.find_cached_file(basename + '.sig')!
88 } else {
89 CachedFile{ found: true }
90 }
91 if signature.found {
92 cached_paths << cached.path
93 continue
94 }
95 }
96 }
97 usable := slash >= 0 && basename != '' && basename.contains('.pkg')
98 mut name := basename
99 if !usable {
100 path := handle.temporary_download_name(directory)!
101 temporary_paths << path
102 name = path[directory.len..]
103 if voidptr(handle.fetch_callback) != unsafe { nil } {
104 handle.remove_temporary_file(path)
105 }
106 }
107 if existing := by_name[name] {
108 if existing != url {
109 return handle.fail(.wrong_args, 'different URLs share a package filename')
110 }
111 } else {
112 by_name[name] = url
113 requests << DownloadRequest{
114 url: url
115 filename: name
116 allow_resume: usable
117 unlink_on_fail: !usable
118 temporary: !usable
119 download_signature: level & 1 != 0
120 signature_optional: level & 2 != 0
121 }
122 }
123 pending_names << name
124 }
125 if requests.len > 0 {
126 handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_start, packages: requests.len })
127 handle.download_files(requests, directory) or {
128 handle.log(.warning, localized_message('failed to retrieve some files\n'))
129 handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_failed })
130 return handle.fail(.retrieve, err.msg())
131 }
132 handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_done })
133 for name in pending_names {
134 file := handle.find_cached_file(name)!
135 if !file.found {
136 handle.log(.warning, localized_message('download completed successfully but no file in the cache\n'))
137 return handle.fail(.retrieve, 'download completed but ${name} is not in the cache')
138 }
139 cached_paths << file.path
140 }
141 }
142 success = true
143 handle.check()!
144 return cached_paths
145}
146