]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
578bfd0a AL |
3 | /* ###################################################################### |
4 | ||
5 | File Utilities | |
6 | ||
7 | CopyFile - Buffered copy of a single file | |
8 | GetLock - dpkg compatible lock file manipulation (fcntl) | |
9 | ||
614adaa0 MV |
10 | Most of this source is placed in the Public Domain, do with it what |
11 | you will | |
7da2b375 | 12 | It was originally written by Jason Gunthorpe <jgg@debian.org>. |
a3a03f5d | 13 | FileFd gzip support added by Martin Pitt <martin.pitt@canonical.com> |
578bfd0a | 14 | |
614adaa0 MV |
15 | The exception is RunScripts() it is under the GPLv2 |
16 | ||
578bfd0a AL |
17 | ##################################################################### */ |
18 | /*}}}*/ | |
19 | // Include Files /*{{{*/ | |
ea542140 DK |
20 | #include <config.h> |
21 | ||
094a497d | 22 | #include <apt-pkg/fileutl.h> |
1cd1c398 | 23 | #include <apt-pkg/strutl.h> |
094a497d | 24 | #include <apt-pkg/error.h> |
b2e465d6 | 25 | #include <apt-pkg/sptr.h> |
468720c5 | 26 | #include <apt-pkg/aptconfiguration.h> |
75ef8f14 | 27 | #include <apt-pkg/configuration.h> |
453b82a3 | 28 | #include <apt-pkg/macros.h> |
b2e465d6 | 29 | |
453b82a3 DK |
30 | #include <ctype.h> |
31 | #include <stdarg.h> | |
32 | #include <stddef.h> | |
33 | #include <sys/select.h> | |
34 | #include <time.h> | |
35 | #include <string> | |
36 | #include <vector> | |
152ab79e | 37 | #include <cstdlib> |
4f333a8b | 38 | #include <cstring> |
3010fb0e | 39 | #include <cstdio> |
4d055c05 | 40 | #include <iostream> |
578bfd0a | 41 | #include <unistd.h> |
2c206aa4 | 42 | #include <fcntl.h> |
578bfd0a | 43 | #include <sys/stat.h> |
cc2313b7 | 44 | #include <sys/time.h> |
1ae93c94 | 45 | #include <sys/wait.h> |
46e39c8e | 46 | #include <dirent.h> |
54676e1a | 47 | #include <signal.h> |
65a1e968 | 48 | #include <errno.h> |
8d01b9d6 | 49 | #include <glob.h> |
fc1a78d8 | 50 | #include <pwd.h> |
3927c6da | 51 | #include <grp.h> |
8d01b9d6 | 52 | |
75ef8f14 | 53 | #include <set> |
46e39c8e | 54 | #include <algorithm> |
98cc7fd2 | 55 | #include <memory> |
2cae0ccb | 56 | |
7efb8c8e DK |
57 | #ifdef HAVE_ZLIB |
58 | #include <zlib.h> | |
699b209e | 59 | #endif |
c4997486 DK |
60 | #ifdef HAVE_BZ2 |
61 | #include <bzlib.h> | |
62 | #endif | |
7f350a37 DK |
63 | #ifdef HAVE_LZMA |
64 | #include <lzma.h> | |
2cae0ccb | 65 | #endif |
e3fbd54c JAK |
66 | #ifdef HAVE_LZ4 |
67 | #include <lz4frame.h> | |
68 | #endif | |
05eab8af AC |
69 | #include <endian.h> |
70 | #include <stdint.h> | |
ea542140 | 71 | |
3927c6da MV |
72 | #if __gnu_linux__ |
73 | #include <sys/prctl.h> | |
74 | #endif | |
75 | ||
ea542140 | 76 | #include <apti18n.h> |
578bfd0a AL |
77 | /*}}}*/ |
78 | ||
4d055c05 AL |
79 | using namespace std; |
80 | ||
40940e63 JAK |
81 | /* Should be a multiple of the common page size (4096) */ |
82 | static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024; | |
83 | ||
614adaa0 MV |
84 | // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ |
85 | // --------------------------------------------------------------------- | |
86 | /* */ | |
87 | bool RunScripts(const char *Cnf) | |
88 | { | |
89 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
90 | if (Opts == 0 || Opts->Child == 0) | |
91 | return true; | |
92 | Opts = Opts->Child; | |
93 | ||
94 | // Fork for running the system calls | |
95 | pid_t Child = ExecFork(); | |
96 | ||
97 | // This is the child | |
98 | if (Child == 0) | |
99 | { | |
cfba4f69 MV |
100 | if (_config->FindDir("DPkg::Chroot-Directory","/") != "/") |
101 | { | |
102 | std::cerr << "Chrooting into " | |
103 | << _config->FindDir("DPkg::Chroot-Directory") | |
104 | << std::endl; | |
105 | if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0) | |
106 | _exit(100); | |
107 | } | |
108 | ||
614adaa0 MV |
109 | if (chdir("/tmp/") != 0) |
110 | _exit(100); | |
111 | ||
112 | unsigned int Count = 1; | |
113 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
114 | { | |
115 | if (Opts->Value.empty() == true) | |
116 | continue; | |
e5b7e019 MV |
117 | |
118 | if(_config->FindB("Debug::RunScripts", false) == true) | |
119 | std::clog << "Running external script: '" | |
120 | << Opts->Value << "'" << std::endl; | |
121 | ||
614adaa0 MV |
122 | if (system(Opts->Value.c_str()) != 0) |
123 | _exit(100+Count); | |
124 | } | |
125 | _exit(0); | |
126 | } | |
127 | ||
128 | // Wait for the child | |
129 | int Status = 0; | |
130 | while (waitpid(Child,&Status,0) != Child) | |
131 | { | |
132 | if (errno == EINTR) | |
133 | continue; | |
134 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
135 | } | |
136 | ||
137 | // Restore sig int/quit | |
138 | signal(SIGQUIT,SIG_DFL); | |
139 | signal(SIGINT,SIG_DFL); | |
140 | ||
141 | // Check for an error code. | |
142 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
143 | { | |
144 | unsigned int Count = WEXITSTATUS(Status); | |
145 | if (Count > 100) | |
146 | { | |
147 | Count -= 100; | |
148 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
149 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); | |
150 | } | |
151 | ||
152 | return _error->Error("Sub-process returned an error code"); | |
153 | } | |
154 | ||
155 | return true; | |
156 | } | |
157 | /*}}}*/ | |
158 | ||
578bfd0a AL |
159 | // CopyFile - Buffered copy of a file /*{{{*/ |
160 | // --------------------------------------------------------------------- | |
161 | /* The caller is expected to set things so that failure causes erasure */ | |
8b89e57f | 162 | bool CopyFile(FileFd &From,FileFd &To) |
578bfd0a | 163 | { |
2128d3fc DK |
164 | if (From.IsOpen() == false || To.IsOpen() == false || |
165 | From.Failed() == true || To.Failed() == true) | |
578bfd0a | 166 | return false; |
e977b8b9 | 167 | |
578bfd0a | 168 | // Buffered copy between fds |
40940e63 | 169 | constexpr size_t BufSize = APT_BUFFER_SIZE; |
0c93e388 | 170 | std::unique_ptr<unsigned char[]> Buf(new unsigned char[BufSize]); |
e977b8b9 DK |
171 | unsigned long long ToRead = 0; |
172 | do { | |
173 | if (From.Read(Buf.get(),BufSize, &ToRead) == false || | |
5df91bc7 | 174 | To.Write(Buf.get(),ToRead) == false) |
578bfd0a | 175 | return false; |
e977b8b9 | 176 | } while (ToRead != 0); |
578bfd0a | 177 | |
ce1f3a2c DK |
178 | return true; |
179 | } | |
180 | /*}}}*/ | |
181 | bool RemoveFile(char const * const Function, std::string const &FileName)/*{{{*/ | |
182 | { | |
183 | if (FileName == "/dev/null") | |
184 | return true; | |
185 | errno = 0; | |
186 | if (unlink(FileName.c_str()) != 0) | |
187 | { | |
188 | if (errno == ENOENT) | |
189 | return true; | |
190 | ||
191 | return _error->WarningE(Function,_("Problem unlinking the file %s"), FileName.c_str()); | |
192 | } | |
e977b8b9 | 193 | return true; |
578bfd0a AL |
194 | } |
195 | /*}}}*/ | |
196 | // GetLock - Gets a lock file /*{{{*/ | |
197 | // --------------------------------------------------------------------- | |
198 | /* This will create an empty file of the given name and lock it. Once this | |
199 | is done all other calls to GetLock in any other process will fail with | |
200 | -1. The return result is the fd of the file, the call should call | |
201 | close at some time. */ | |
202 | int GetLock(string File,bool Errors) | |
203 | { | |
f659b39a OS |
204 | // GetLock() is used in aptitude on directories with public-write access |
205 | // Use O_NOFOLLOW here to prevent symlink traversal attacks | |
206 | int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640); | |
578bfd0a AL |
207 | if (FD < 0) |
208 | { | |
1e3f4083 | 209 | // Read only .. can't have locking problems there. |
b2e465d6 AL |
210 | if (errno == EROFS) |
211 | { | |
212 | _error->Warning(_("Not using locking for read only lock file %s"),File.c_str()); | |
213 | return dup(0); // Need something for the caller to close | |
214 | } | |
215 | ||
578bfd0a | 216 | if (Errors == true) |
b2e465d6 AL |
217 | _error->Errno("open",_("Could not open lock file %s"),File.c_str()); |
218 | ||
219 | // Feh.. We do this to distinguish the lock vs open case.. | |
220 | errno = EPERM; | |
578bfd0a AL |
221 | return -1; |
222 | } | |
b2e465d6 AL |
223 | SetCloseExec(FD,true); |
224 | ||
1e3f4083 | 225 | // Acquire a write lock |
578bfd0a | 226 | struct flock fl; |
c71bc556 AL |
227 | fl.l_type = F_WRLCK; |
228 | fl.l_whence = SEEK_SET; | |
229 | fl.l_start = 0; | |
230 | fl.l_len = 0; | |
578bfd0a AL |
231 | if (fcntl(FD,F_SETLK,&fl) == -1) |
232 | { | |
3d165906 MV |
233 | // always close to not leak resources |
234 | int Tmp = errno; | |
235 | close(FD); | |
236 | errno = Tmp; | |
237 | ||
d89df07a AL |
238 | if (errno == ENOLCK) |
239 | { | |
b2e465d6 AL |
240 | _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str()); |
241 | return dup(0); // Need something for the caller to close | |
3d165906 MV |
242 | } |
243 | ||
578bfd0a | 244 | if (Errors == true) |
b2e465d6 AL |
245 | _error->Errno("open",_("Could not get lock %s"),File.c_str()); |
246 | ||
578bfd0a AL |
247 | return -1; |
248 | } | |
249 | ||
250 | return FD; | |
251 | } | |
252 | /*}}}*/ | |
253 | // FileExists - Check if a file exists /*{{{*/ | |
254 | // --------------------------------------------------------------------- | |
36f1098a | 255 | /* Beware: Directories are also files! */ |
578bfd0a AL |
256 | bool FileExists(string File) |
257 | { | |
258 | struct stat Buf; | |
259 | if (stat(File.c_str(),&Buf) != 0) | |
260 | return false; | |
261 | return true; | |
262 | } | |
263 | /*}}}*/ | |
36f1098a DK |
264 | // RealFileExists - Check if a file exists and if it is really a file /*{{{*/ |
265 | // --------------------------------------------------------------------- | |
266 | /* */ | |
267 | bool RealFileExists(string File) | |
268 | { | |
269 | struct stat Buf; | |
270 | if (stat(File.c_str(),&Buf) != 0) | |
271 | return false; | |
272 | return ((Buf.st_mode & S_IFREG) != 0); | |
273 | } | |
274 | /*}}}*/ | |
1cd1c398 DK |
275 | // DirectoryExists - Check if a directory exists and is really one /*{{{*/ |
276 | // --------------------------------------------------------------------- | |
277 | /* */ | |
278 | bool DirectoryExists(string const &Path) | |
279 | { | |
280 | struct stat Buf; | |
281 | if (stat(Path.c_str(),&Buf) != 0) | |
282 | return false; | |
283 | return ((Buf.st_mode & S_IFDIR) != 0); | |
284 | } | |
285 | /*}}}*/ | |
286 | // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/ | |
287 | // --------------------------------------------------------------------- | |
288 | /* This method will create all directories needed for path in good old | |
289 | mkdir -p style but refuses to do this if Parent is not a prefix of | |
290 | this Path. Example: /var/cache/ and /var/cache/apt/archives are given, | |
291 | so it will create apt/archives if /var/cache exists - on the other | |
292 | hand if the parent is /var/lib the creation will fail as this path | |
293 | is not a parent of the path to be generated. */ | |
294 | bool CreateDirectory(string const &Parent, string const &Path) | |
295 | { | |
296 | if (Parent.empty() == true || Path.empty() == true) | |
297 | return false; | |
298 | ||
299 | if (DirectoryExists(Path) == true) | |
300 | return true; | |
301 | ||
302 | if (DirectoryExists(Parent) == false) | |
303 | return false; | |
304 | ||
305 | // we are not going to create directories "into the blue" | |
9ce3cfc9 | 306 | if (Path.compare(0, Parent.length(), Parent) != 0) |
1cd1c398 DK |
307 | return false; |
308 | ||
309 | vector<string> const dirs = VectorizeString(Path.substr(Parent.size()), '/'); | |
310 | string progress = Parent; | |
311 | for (vector<string>::const_iterator d = dirs.begin(); d != dirs.end(); ++d) | |
312 | { | |
313 | if (d->empty() == true) | |
314 | continue; | |
315 | ||
316 | progress.append("/").append(*d); | |
317 | if (DirectoryExists(progress) == true) | |
318 | continue; | |
319 | ||
320 | if (mkdir(progress.c_str(), 0755) != 0) | |
321 | return false; | |
322 | } | |
323 | return true; | |
324 | } | |
325 | /*}}}*/ | |
7753e468 | 326 | // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/ |
b29c3712 DK |
327 | // --------------------------------------------------------------------- |
328 | /* a small wrapper around CreateDirectory to check if it exists and to | |
329 | remove the trailing "/apt/" from the parent directory if needed */ | |
7753e468 | 330 | bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path) |
b29c3712 DK |
331 | { |
332 | if (DirectoryExists(Path) == true) | |
333 | return true; | |
334 | ||
335 | size_t const len = Parent.size(); | |
336 | if (len > 5 && Parent.find("/apt/", len - 6, 5) == len - 5) | |
337 | { | |
338 | if (CreateDirectory(Parent.substr(0,len-5), Path) == true) | |
339 | return true; | |
340 | } | |
341 | else if (CreateDirectory(Parent, Path) == true) | |
342 | return true; | |
343 | ||
344 | return false; | |
345 | } | |
346 | /*}}}*/ | |
46e39c8e MV |
347 | // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/ |
348 | // --------------------------------------------------------------------- | |
349 | /* If an extension is given only files with this extension are included | |
350 | in the returned vector, otherwise every "normal" file is included. */ | |
b39c1859 MV |
351 | std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, |
352 | bool const &SortList, bool const &AllowNoExt) | |
353 | { | |
354 | std::vector<string> ext; | |
355 | ext.reserve(2); | |
356 | if (Ext.empty() == false) | |
357 | ext.push_back(Ext); | |
358 | if (AllowNoExt == true && ext.empty() == false) | |
359 | ext.push_back(""); | |
360 | return GetListOfFilesInDir(Dir, ext, SortList); | |
361 | } | |
362 | std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext, | |
363 | bool const &SortList) | |
364 | { | |
365 | // Attention debuggers: need to be set with the environment config file! | |
366 | bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false); | |
367 | if (Debug == true) | |
368 | { | |
369 | std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl; | |
370 | if (Ext.empty() == true) | |
371 | std::clog << "\tNO extension" << std::endl; | |
372 | else | |
373 | for (std::vector<string>::const_iterator e = Ext.begin(); | |
374 | e != Ext.end(); ++e) | |
375 | std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl; | |
376 | } | |
377 | ||
46e39c8e | 378 | std::vector<string> List; |
36f1098a | 379 | |
69c2ecbd | 380 | if (DirectoryExists(Dir) == false) |
36f1098a DK |
381 | { |
382 | _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str()); | |
383 | return List; | |
384 | } | |
385 | ||
1408e219 | 386 | Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently"); |
46e39c8e MV |
387 | DIR *D = opendir(Dir.c_str()); |
388 | if (D == 0) | |
389 | { | |
390 | _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
391 | return List; | |
392 | } | |
393 | ||
394 | for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) | |
395 | { | |
b39c1859 | 396 | // skip "hidden" files |
46e39c8e MV |
397 | if (Ent->d_name[0] == '.') |
398 | continue; | |
399 | ||
491058e3 DK |
400 | // Make sure it is a file and not something else |
401 | string const File = flCombine(Dir,Ent->d_name); | |
402 | #ifdef _DIRENT_HAVE_D_TYPE | |
403 | if (Ent->d_type != DT_REG) | |
404 | #endif | |
405 | { | |
69c2ecbd | 406 | if (RealFileExists(File) == false) |
491058e3 | 407 | { |
84e254d6 DK |
408 | // do not show ignoration warnings for directories |
409 | if ( | |
410 | #ifdef _DIRENT_HAVE_D_TYPE | |
411 | Ent->d_type == DT_DIR || | |
412 | #endif | |
69c2ecbd | 413 | DirectoryExists(File) == true) |
84e254d6 | 414 | continue; |
491058e3 DK |
415 | if (SilentIgnore.Match(Ent->d_name) == false) |
416 | _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str()); | |
417 | continue; | |
418 | } | |
419 | } | |
420 | ||
b39c1859 MV |
421 | // check for accepted extension: |
422 | // no extension given -> periods are bad as hell! | |
423 | // extensions given -> "" extension allows no extension | |
424 | if (Ext.empty() == false) | |
425 | { | |
426 | string d_ext = flExtension(Ent->d_name); | |
427 | if (d_ext == Ent->d_name) // no extension | |
428 | { | |
429 | if (std::find(Ext.begin(), Ext.end(), "") == Ext.end()) | |
430 | { | |
431 | if (Debug == true) | |
432 | std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl; | |
5edc3966 | 433 | if (SilentIgnore.Match(Ent->d_name) == false) |
491058e3 | 434 | _error->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent->d_name, Dir.c_str()); |
b39c1859 MV |
435 | continue; |
436 | } | |
437 | } | |
438 | else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end()) | |
439 | { | |
440 | if (Debug == true) | |
441 | std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl; | |
1408e219 | 442 | if (SilentIgnore.Match(Ent->d_name) == false) |
491058e3 | 443 | _error->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent->d_name, Dir.c_str()); |
b39c1859 MV |
444 | continue; |
445 | } | |
446 | } | |
46e39c8e | 447 | |
b39c1859 | 448 | // Skip bad filenames ala run-parts |
46e39c8e MV |
449 | const char *C = Ent->d_name; |
450 | for (; *C != 0; ++C) | |
451 | if (isalpha(*C) == 0 && isdigit(*C) == 0 | |
9d39208a | 452 | && *C != '_' && *C != '-' && *C != ':') { |
b39c1859 MV |
453 | // no required extension -> dot is a bad character |
454 | if (*C == '.' && Ext.empty() == false) | |
455 | continue; | |
46e39c8e | 456 | break; |
b39c1859 | 457 | } |
46e39c8e | 458 | |
b39c1859 | 459 | // we don't reach the end of the name -> bad character included |
46e39c8e | 460 | if (*C != 0) |
b39c1859 MV |
461 | { |
462 | if (Debug == true) | |
463 | std::clog << "Bad file: " << Ent->d_name << " → bad character »" | |
464 | << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl; | |
465 | continue; | |
466 | } | |
467 | ||
fbb2c7e0 DK |
468 | // skip filenames which end with a period. These are never valid |
469 | if (*(C - 1) == '.') | |
470 | { | |
471 | if (Debug == true) | |
472 | std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl; | |
473 | continue; | |
474 | } | |
475 | ||
476 | if (Debug == true) | |
477 | std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl; | |
478 | List.push_back(File); | |
479 | } | |
480 | closedir(D); | |
481 | ||
482 | if (SortList == true) | |
483 | std::sort(List.begin(),List.end()); | |
484 | return List; | |
485 | } | |
486 | std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList) | |
487 | { | |
488 | bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false); | |
489 | if (Debug == true) | |
490 | std::clog << "Accept in " << Dir << " all regular files" << std::endl; | |
491 | ||
492 | std::vector<string> List; | |
493 | ||
69c2ecbd | 494 | if (DirectoryExists(Dir) == false) |
fbb2c7e0 DK |
495 | { |
496 | _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str()); | |
497 | return List; | |
498 | } | |
499 | ||
500 | DIR *D = opendir(Dir.c_str()); | |
501 | if (D == 0) | |
502 | { | |
503 | _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
504 | return List; | |
505 | } | |
506 | ||
507 | for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) | |
508 | { | |
509 | // skip "hidden" files | |
510 | if (Ent->d_name[0] == '.') | |
511 | continue; | |
512 | ||
513 | // Make sure it is a file and not something else | |
514 | string const File = flCombine(Dir,Ent->d_name); | |
515 | #ifdef _DIRENT_HAVE_D_TYPE | |
516 | if (Ent->d_type != DT_REG) | |
517 | #endif | |
518 | { | |
69c2ecbd | 519 | if (RealFileExists(File) == false) |
fbb2c7e0 DK |
520 | { |
521 | if (Debug == true) | |
522 | std::clog << "Bad file: " << Ent->d_name << " → it is not a real file" << std::endl; | |
523 | continue; | |
524 | } | |
525 | } | |
526 | ||
527 | // Skip bad filenames ala run-parts | |
528 | const char *C = Ent->d_name; | |
529 | for (; *C != 0; ++C) | |
530 | if (isalpha(*C) == 0 && isdigit(*C) == 0 | |
531 | && *C != '_' && *C != '-' && *C != '.') | |
532 | break; | |
533 | ||
534 | // we don't reach the end of the name -> bad character included | |
535 | if (*C != 0) | |
536 | { | |
537 | if (Debug == true) | |
538 | std::clog << "Bad file: " << Ent->d_name << " → bad character »" << *C << "« in filename" << std::endl; | |
539 | continue; | |
540 | } | |
541 | ||
b39c1859 MV |
542 | // skip filenames which end with a period. These are never valid |
543 | if (*(C - 1) == '.') | |
544 | { | |
545 | if (Debug == true) | |
546 | std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl; | |
46e39c8e | 547 | continue; |
b39c1859 | 548 | } |
46e39c8e | 549 | |
b39c1859 MV |
550 | if (Debug == true) |
551 | std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl; | |
46e39c8e MV |
552 | List.push_back(File); |
553 | } | |
554 | closedir(D); | |
555 | ||
556 | if (SortList == true) | |
557 | std::sort(List.begin(),List.end()); | |
558 | return List; | |
559 | } | |
560 | /*}}}*/ | |
578bfd0a AL |
561 | // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/ |
562 | // --------------------------------------------------------------------- | |
563 | /* We return / on failure. */ | |
564 | string SafeGetCWD() | |
565 | { | |
566 | // Stash the current dir. | |
567 | char S[300]; | |
568 | S[0] = 0; | |
7f25bdff | 569 | if (getcwd(S,sizeof(S)-2) == 0) |
578bfd0a | 570 | return "/"; |
7f25bdff AL |
571 | unsigned int Len = strlen(S); |
572 | S[Len] = '/'; | |
573 | S[Len+1] = 0; | |
578bfd0a AL |
574 | return S; |
575 | } | |
576 | /*}}}*/ | |
2ec858bc MV |
577 | // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/ |
578 | // --------------------------------------------------------------------- | |
579 | /* We return / on failure. */ | |
580 | time_t GetModificationTime(string const &Path) | |
581 | { | |
582 | struct stat St; | |
583 | if (stat(Path.c_str(), &St) < 0) | |
584 | return -1; | |
585 | return St.st_mtime; | |
586 | } | |
587 | /*}}}*/ | |
8ce4327b AL |
588 | // flNotDir - Strip the directory from the filename /*{{{*/ |
589 | // --------------------------------------------------------------------- | |
590 | /* */ | |
591 | string flNotDir(string File) | |
592 | { | |
593 | string::size_type Res = File.rfind('/'); | |
594 | if (Res == string::npos) | |
595 | return File; | |
596 | Res++; | |
597 | return string(File,Res,Res - File.length()); | |
598 | } | |
599 | /*}}}*/ | |
d38b7b3d AL |
600 | // flNotFile - Strip the file from the directory name /*{{{*/ |
601 | // --------------------------------------------------------------------- | |
171c45bc | 602 | /* Result ends in a / */ |
d38b7b3d AL |
603 | string flNotFile(string File) |
604 | { | |
605 | string::size_type Res = File.rfind('/'); | |
606 | if (Res == string::npos) | |
171c45bc | 607 | return "./"; |
d38b7b3d AL |
608 | Res++; |
609 | return string(File,0,Res); | |
610 | } | |
611 | /*}}}*/ | |
b2e465d6 AL |
612 | // flExtension - Return the extension for the file /*{{{*/ |
613 | // --------------------------------------------------------------------- | |
614 | /* */ | |
615 | string flExtension(string File) | |
616 | { | |
617 | string::size_type Res = File.rfind('.'); | |
618 | if (Res == string::npos) | |
619 | return File; | |
620 | Res++; | |
621 | return string(File,Res,Res - File.length()); | |
622 | } | |
623 | /*}}}*/ | |
421c8d10 AL |
624 | // flNoLink - If file is a symlink then deref it /*{{{*/ |
625 | // --------------------------------------------------------------------- | |
626 | /* If the name is not a link then the returned path is the input. */ | |
627 | string flNoLink(string File) | |
628 | { | |
629 | struct stat St; | |
630 | if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0) | |
631 | return File; | |
632 | if (stat(File.c_str(),&St) != 0) | |
633 | return File; | |
634 | ||
635 | /* Loop resolving the link. There is no need to limit the number of | |
636 | loops because the stat call above ensures that the symlink is not | |
637 | circular */ | |
638 | char Buffer[1024]; | |
639 | string NFile = File; | |
640 | while (1) | |
641 | { | |
642 | // Read the link | |
3286ad13 | 643 | ssize_t Res; |
421c8d10 | 644 | if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 || |
3286ad13 | 645 | (size_t)Res >= sizeof(Buffer)) |
421c8d10 AL |
646 | return File; |
647 | ||
648 | // Append or replace the previous path | |
649 | Buffer[Res] = 0; | |
650 | if (Buffer[0] == '/') | |
651 | NFile = Buffer; | |
652 | else | |
653 | NFile = flNotFile(NFile) + Buffer; | |
654 | ||
655 | // See if we are done | |
656 | if (lstat(NFile.c_str(),&St) != 0) | |
657 | return File; | |
658 | if (S_ISLNK(St.st_mode) == 0) | |
659 | return NFile; | |
660 | } | |
661 | } | |
662 | /*}}}*/ | |
b2e465d6 AL |
663 | // flCombine - Combine a file and a directory /*{{{*/ |
664 | // --------------------------------------------------------------------- | |
665 | /* If the file is an absolute path then it is just returned, otherwise | |
666 | the directory is pre-pended to it. */ | |
667 | string flCombine(string Dir,string File) | |
668 | { | |
669 | if (File.empty() == true) | |
670 | return string(); | |
671 | ||
672 | if (File[0] == '/' || Dir.empty() == true) | |
673 | return File; | |
674 | if (File.length() >= 2 && File[0] == '.' && File[1] == '/') | |
675 | return File; | |
676 | if (Dir[Dir.length()-1] == '/') | |
677 | return Dir + File; | |
678 | return Dir + '/' + File; | |
679 | } | |
680 | /*}}}*/ | |
53ac87ac MV |
681 | // flAbsPath - Return the absolute path of the filename /*{{{*/ |
682 | // --------------------------------------------------------------------- | |
683 | /* */ | |
684 | string flAbsPath(string File) | |
685 | { | |
686 | char *p = realpath(File.c_str(), NULL); | |
687 | if (p == NULL) | |
688 | { | |
95278287 | 689 | _error->Errno("realpath", "flAbsPath on %s failed", File.c_str()); |
53ac87ac MV |
690 | return ""; |
691 | } | |
692 | std::string AbsPath(p); | |
693 | free(p); | |
694 | return AbsPath; | |
695 | } | |
696 | /*}}}*/ | |
3b5421b4 AL |
697 | // SetCloseExec - Set the close on exec flag /*{{{*/ |
698 | // --------------------------------------------------------------------- | |
699 | /* */ | |
700 | void SetCloseExec(int Fd,bool Close) | |
701 | { | |
702 | if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0) | |
703 | { | |
704 | cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl; | |
705 | exit(100); | |
706 | } | |
707 | } | |
708 | /*}}}*/ | |
709 | // SetNonBlock - Set the nonblocking flag /*{{{*/ | |
710 | // --------------------------------------------------------------------- | |
711 | /* */ | |
712 | void SetNonBlock(int Fd,bool Block) | |
713 | { | |
0a8a80e5 AL |
714 | int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK); |
715 | if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0) | |
3b5421b4 AL |
716 | { |
717 | cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl; | |
718 | exit(100); | |
719 | } | |
720 | } | |
721 | /*}}}*/ | |
722 | // WaitFd - Wait for a FD to become readable /*{{{*/ | |
723 | // --------------------------------------------------------------------- | |
b2e465d6 | 724 | /* This waits for a FD to become readable using select. It is useful for |
6d5dd02a AL |
725 | applications making use of non-blocking sockets. The timeout is |
726 | in seconds. */ | |
1084d58a | 727 | bool WaitFd(int Fd,bool write,unsigned long timeout) |
3b5421b4 AL |
728 | { |
729 | fd_set Set; | |
cc2313b7 | 730 | struct timeval tv; |
3b5421b4 AL |
731 | FD_ZERO(&Set); |
732 | FD_SET(Fd,&Set); | |
6d5dd02a AL |
733 | tv.tv_sec = timeout; |
734 | tv.tv_usec = 0; | |
1084d58a | 735 | if (write == true) |
b0db36b1 AL |
736 | { |
737 | int Res; | |
738 | do | |
739 | { | |
740 | Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0)); | |
741 | } | |
742 | while (Res < 0 && errno == EINTR); | |
743 | ||
744 | if (Res <= 0) | |
745 | return false; | |
1084d58a AL |
746 | } |
747 | else | |
748 | { | |
b0db36b1 AL |
749 | int Res; |
750 | do | |
751 | { | |
752 | Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0)); | |
753 | } | |
754 | while (Res < 0 && errno == EINTR); | |
755 | ||
756 | if (Res <= 0) | |
757 | return false; | |
cc2313b7 | 758 | } |
1084d58a | 759 | |
3b5421b4 AL |
760 | return true; |
761 | } | |
762 | /*}}}*/ | |
96ae6de5 | 763 | // MergeKeepFdsFromConfiguration - Merge APT::Keep-Fds configuration /*{{{*/ |
54676e1a | 764 | // --------------------------------------------------------------------- |
96ae6de5 MV |
765 | /* This is used to merge the APT::Keep-Fds with the provided KeepFDs |
766 | * set. | |
767 | */ | |
768 | void MergeKeepFdsFromConfiguration(std::set<int> &KeepFDs) | |
e45c4617 | 769 | { |
e45c4617 MV |
770 | Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds"); |
771 | if (Opts != 0 && Opts->Child != 0) | |
772 | { | |
773 | Opts = Opts->Child; | |
774 | for (; Opts != 0; Opts = Opts->Next) | |
775 | { | |
776 | if (Opts->Value.empty() == true) | |
777 | continue; | |
778 | int fd = atoi(Opts->Value.c_str()); | |
779 | KeepFDs.insert(fd); | |
780 | } | |
781 | } | |
96ae6de5 MV |
782 | } |
783 | /*}}}*/ | |
54676e1a AL |
784 | // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/ |
785 | // --------------------------------------------------------------------- | |
786 | /* This is used if you want to cleanse the environment for the forked | |
787 | child, it fixes up the important signals and nukes all of the fds, | |
788 | otherwise acts like normal fork. */ | |
75ef8f14 | 789 | pid_t ExecFork() |
96ae6de5 MV |
790 | { |
791 | set<int> KeepFDs; | |
792 | // we need to merge the Keep-Fds as external tools like | |
793 | // debconf-apt-progress use it | |
794 | MergeKeepFdsFromConfiguration(KeepFDs); | |
e45c4617 MV |
795 | return ExecFork(KeepFDs); |
796 | } | |
797 | ||
798 | pid_t ExecFork(std::set<int> KeepFDs) | |
54676e1a AL |
799 | { |
800 | // Fork off the process | |
801 | pid_t Process = fork(); | |
802 | if (Process < 0) | |
803 | { | |
804 | cerr << "FATAL -> Failed to fork." << endl; | |
805 | exit(100); | |
806 | } | |
807 | ||
808 | // Spawn the subprocess | |
809 | if (Process == 0) | |
810 | { | |
811 | // Setup the signals | |
812 | signal(SIGPIPE,SIG_DFL); | |
813 | signal(SIGQUIT,SIG_DFL); | |
814 | signal(SIGINT,SIG_DFL); | |
815 | signal(SIGWINCH,SIG_DFL); | |
816 | signal(SIGCONT,SIG_DFL); | |
817 | signal(SIGTSTP,SIG_DFL); | |
75ef8f14 | 818 | |
be4d908f JAK |
819 | DIR *dir = opendir("/proc/self/fd"); |
820 | if (dir != NULL) | |
75ef8f14 | 821 | { |
be4d908f JAK |
822 | struct dirent *ent; |
823 | while ((ent = readdir(dir))) | |
824 | { | |
825 | int fd = atoi(ent->d_name); | |
826 | // If fd > 0, it was a fd number and not . or .. | |
827 | if (fd >= 3 && KeepFDs.find(fd) == KeepFDs.end()) | |
828 | fcntl(fd,F_SETFD,FD_CLOEXEC); | |
829 | } | |
830 | closedir(dir); | |
831 | } else { | |
832 | long ScOpenMax = sysconf(_SC_OPEN_MAX); | |
833 | // Close all of our FDs - just in case | |
834 | for (int K = 3; K != ScOpenMax; K++) | |
835 | { | |
836 | if(KeepFDs.find(K) == KeepFDs.end()) | |
837 | fcntl(K,F_SETFD,FD_CLOEXEC); | |
838 | } | |
75ef8f14 | 839 | } |
54676e1a AL |
840 | } |
841 | ||
842 | return Process; | |
843 | } | |
844 | /*}}}*/ | |
ddc1d8d0 AL |
845 | // ExecWait - Fancy waitpid /*{{{*/ |
846 | // --------------------------------------------------------------------- | |
2c9a72d1 | 847 | /* Waits for the given sub process. If Reap is set then no errors are |
ddc1d8d0 AL |
848 | generated. Otherwise a failed subprocess will generate a proper descriptive |
849 | message */ | |
3826564e | 850 | bool ExecWait(pid_t Pid,const char *Name,bool Reap) |
ddc1d8d0 AL |
851 | { |
852 | if (Pid <= 1) | |
853 | return true; | |
854 | ||
855 | // Wait and collect the error code | |
856 | int Status; | |
857 | while (waitpid(Pid,&Status,0) != Pid) | |
858 | { | |
859 | if (errno == EINTR) | |
860 | continue; | |
861 | ||
862 | if (Reap == true) | |
863 | return false; | |
864 | ||
db0db9fe | 865 | return _error->Error(_("Waited for %s but it wasn't there"),Name); |
ddc1d8d0 AL |
866 | } |
867 | ||
868 | ||
869 | // Check for an error code. | |
870 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
871 | { | |
872 | if (Reap == true) | |
873 | return false; | |
ab7f4d7c | 874 | if (WIFSIGNALED(Status) != 0) |
40e7fe0e | 875 | { |
ab7f4d7c MV |
876 | if( WTERMSIG(Status) == SIGSEGV) |
877 | return _error->Error(_("Sub-process %s received a segmentation fault."),Name); | |
878 | else | |
879 | return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status)); | |
40e7fe0e | 880 | } |
ddc1d8d0 AL |
881 | |
882 | if (WIFEXITED(Status) != 0) | |
b2e465d6 | 883 | return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status)); |
ddc1d8d0 | 884 | |
b2e465d6 | 885 | return _error->Error(_("Sub-process %s exited unexpectedly"),Name); |
ddc1d8d0 AL |
886 | } |
887 | ||
888 | return true; | |
889 | } | |
890 | /*}}}*/ | |
f8aba23f | 891 | // StartsWithGPGClearTextSignature - Check if a file is Pgp/GPG clearsigned /*{{{*/ |
fe5804fc | 892 | bool StartsWithGPGClearTextSignature(string const &FileName) |
0854ad8b MV |
893 | { |
894 | static const char* SIGMSG = "-----BEGIN PGP SIGNED MESSAGE-----\n"; | |
1c89c98a | 895 | char buffer[strlen(SIGMSG)+1]; |
0854ad8b MV |
896 | FILE* gpg = fopen(FileName.c_str(), "r"); |
897 | if (gpg == NULL) | |
898 | return false; | |
899 | ||
900 | char const * const test = fgets(buffer, sizeof(buffer), gpg); | |
901 | fclose(gpg); | |
902 | if (test == NULL || strcmp(buffer, SIGMSG) != 0) | |
903 | return false; | |
904 | ||
905 | return true; | |
906 | } | |
f8aba23f | 907 | /*}}}*/ |
d84da499 DK |
908 | // ChangeOwnerAndPermissionOfFile - set file attributes to requested values /*{{{*/ |
909 | bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode) | |
910 | { | |
911 | if (strcmp(file, "/dev/null") == 0) | |
912 | return true; | |
913 | bool Res = true; | |
914 | if (getuid() == 0 && strlen(user) != 0 && strlen(group) != 0) // if we aren't root, we can't chown, so don't try it | |
915 | { | |
916 | // ensure the file is owned by root and has good permissions | |
917 | struct passwd const * const pw = getpwnam(user); | |
918 | struct group const * const gr = getgrnam(group); | |
34651385 | 919 | if (pw != NULL && gr != NULL && lchown(file, pw->pw_uid, gr->gr_gid) != 0) |
d84da499 DK |
920 | Res &= _error->WarningE(requester, "chown to %s:%s of file %s failed", user, group, file); |
921 | } | |
34651385 DK |
922 | struct stat Buf; |
923 | if (lstat(file, &Buf) != 0 || S_ISLNK(Buf.st_mode)) | |
924 | return Res; | |
d84da499 DK |
925 | if (chmod(file, mode) != 0) |
926 | Res &= _error->WarningE(requester, "chmod 0%o of file %s failed", mode, file); | |
927 | return Res; | |
928 | } | |
929 | /*}}}*/ | |
0854ad8b | 930 | |
38dba8cd | 931 | struct APT_HIDDEN simple_buffer { /*{{{*/ |
5bdba2ca | 932 | size_t buffersize_max = 0; |
38dba8cd JAK |
933 | unsigned long long bufferstart = 0; |
934 | unsigned long long bufferend = 0; | |
5bdba2ca JAK |
935 | char *buffer = nullptr; |
936 | ||
937 | simple_buffer() { | |
938 | reset(4096); | |
939 | } | |
940 | ~simple_buffer() { | |
ab16ead7 | 941 | delete[] buffer; |
5bdba2ca | 942 | } |
38dba8cd | 943 | |
ea58d39e | 944 | const char *get() const { return buffer + bufferstart; } |
38dba8cd | 945 | char *get() { return buffer + bufferstart; } |
f1b9bf7a JAK |
946 | const char *getend() const { return buffer + bufferend; } |
947 | char *getend() { return buffer + bufferend; } | |
ea58d39e | 948 | bool empty() const { return bufferend <= bufferstart; } |
c368b3ab | 949 | bool full() const { return bufferend == buffersize_max; } |
f1b9bf7a | 950 | unsigned long long free() const { return buffersize_max - bufferend; } |
ea58d39e | 951 | unsigned long long size() const { return bufferend-bufferstart; } |
5bdba2ca JAK |
952 | void reset(size_t size) |
953 | { | |
954 | if (size > buffersize_max) { | |
955 | delete[] buffer; | |
956 | buffersize_max = size; | |
957 | buffer = new char[size]; | |
958 | } | |
959 | reset(); | |
960 | } | |
38dba8cd JAK |
961 | void reset() { bufferend = bufferstart = 0; } |
962 | ssize_t read(void *to, unsigned long long requested_size) APT_MUSTCHECK | |
963 | { | |
964 | if (size() < requested_size) | |
965 | requested_size = size(); | |
966 | memcpy(to, buffer + bufferstart, requested_size); | |
967 | bufferstart += requested_size; | |
968 | if (bufferstart == bufferend) | |
969 | bufferstart = bufferend = 0; | |
970 | return requested_size; | |
971 | } | |
c368b3ab JAK |
972 | ssize_t write(const void *from, unsigned long long requested_size) APT_MUSTCHECK |
973 | { | |
47fcfff8 JAK |
974 | if (free() < requested_size) |
975 | requested_size = free(); | |
976 | memcpy(getend(), from, requested_size); | |
c368b3ab JAK |
977 | bufferend += requested_size; |
978 | if (bufferstart == bufferend) | |
979 | bufferstart = bufferend = 0; | |
980 | return requested_size; | |
981 | } | |
38dba8cd JAK |
982 | }; |
983 | /*}}}*/ | |
984 | ||
65ac6aad | 985 | class APT_HIDDEN FileFdPrivate { /*{{{*/ |
88749b5d | 986 | friend class BufferedWriteFileFdPrivate; |
fa89055f DK |
987 | protected: |
988 | FileFd * const filefd; | |
38dba8cd | 989 | simple_buffer buffer; |
fa89055f DK |
990 | int compressed_fd; |
991 | pid_t compressor_pid; | |
992 | bool is_pipe; | |
993 | APT::Configuration::Compressor compressor; | |
994 | unsigned int openmode; | |
995 | unsigned long long seekpos; | |
1d68256d JAK |
996 | public: |
997 | ||
83e22e26 | 998 | explicit FileFdPrivate(FileFd * const pfilefd) : filefd(pfilefd), |
fa89055f DK |
999 | compressed_fd(-1), compressor_pid(-1), is_pipe(false), |
1000 | openmode(0), seekpos(0) {}; | |
1d68256d JAK |
1001 | virtual APT::Configuration::Compressor get_compressor() const |
1002 | { | |
1003 | return compressor; | |
1004 | } | |
1005 | virtual void set_compressor(APT::Configuration::Compressor const &compressor) | |
1006 | { | |
1007 | this->compressor = compressor; | |
1008 | } | |
1009 | virtual unsigned int get_openmode() const | |
1010 | { | |
1011 | return openmode; | |
1012 | } | |
1013 | virtual void set_openmode(unsigned int openmode) | |
1014 | { | |
1015 | this->openmode = openmode; | |
1016 | } | |
1017 | virtual bool get_is_pipe() const | |
1018 | { | |
1019 | return is_pipe; | |
1020 | } | |
1021 | virtual void set_is_pipe(bool is_pipe) | |
1022 | { | |
1023 | this->is_pipe = is_pipe; | |
1024 | } | |
1025 | virtual unsigned long long get_seekpos() const | |
1026 | { | |
1027 | return seekpos; | |
1028 | } | |
1029 | virtual void set_seekpos(unsigned long long seekpos) | |
1030 | { | |
1031 | this->seekpos = seekpos; | |
1032 | } | |
fa89055f DK |
1033 | |
1034 | virtual bool InternalOpen(int const iFd, unsigned int const Mode) = 0; | |
f63123c3 | 1035 | ssize_t InternalRead(void * To, unsigned long long Size) |
fa89055f | 1036 | { |
83e22e26 JAK |
1037 | // Drain the buffer if needed. |
1038 | if (buffer.empty() == false) | |
fa89055f | 1039 | { |
83e22e26 | 1040 | return buffer.read(To, Size); |
fa89055f | 1041 | } |
83e22e26 | 1042 | return InternalUnbufferedRead(To, Size); |
f63123c3 DK |
1043 | } |
1044 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) = 0; | |
1045 | virtual bool InternalReadError() { return filefd->FileFdErrno("read",_("Read error")); } | |
1046 | virtual char * InternalReadLine(char * To, unsigned long long Size) | |
1047 | { | |
1048 | if (unlikely(Size == 0)) | |
1049 | return nullptr; | |
01152444 | 1050 | // Read one byte less than buffer size to have space for trailing 0. |
f63123c3 | 1051 | --Size; |
01152444 | 1052 | |
f63123c3 DK |
1053 | char * const InitialTo = To; |
1054 | ||
01152444 | 1055 | while (Size > 0) { |
83e22e26 | 1056 | if (buffer.empty() == true) |
f63123c3 | 1057 | { |
83e22e26 | 1058 | buffer.reset(); |
f63123c3 | 1059 | unsigned long long actualread = 0; |
72ed5f14 | 1060 | if (filefd->Read(buffer.getend(), buffer.free(), &actualread) == false) |
f63123c3 | 1061 | return nullptr; |
83e22e26 JAK |
1062 | buffer.bufferend = actualread; |
1063 | if (buffer.size() == 0) | |
f63123c3 DK |
1064 | { |
1065 | if (To == InitialTo) | |
1066 | return nullptr; | |
1067 | break; | |
1068 | } | |
1069 | filefd->Flags &= ~FileFd::HitEof; | |
1070 | } | |
1071 | ||
83e22e26 | 1072 | unsigned long long const OutputSize = std::min(Size, buffer.size()); |
b3db9d81 | 1073 | char const * const newline = static_cast<char const * const>(memchr(buffer.get(), '\n', OutputSize)); |
a9024b1b JAK |
1074 | // Read until end of line or up to Size bytes from the buffer. |
1075 | unsigned long long actualread = buffer.read(To, | |
1076 | (newline != nullptr) | |
1077 | ? (newline - buffer.get()) + 1 | |
1078 | : OutputSize); | |
1079 | To += actualread; | |
1080 | Size -= actualread; | |
f63123c3 | 1081 | if (newline != nullptr) |
f63123c3 | 1082 | break; |
01152444 | 1083 | } |
f63123c3 DK |
1084 | *To = '\0'; |
1085 | return InitialTo; | |
fa89055f | 1086 | } |
766761fd JAK |
1087 | virtual bool InternalFlush() |
1088 | { | |
1089 | return true; | |
1090 | } | |
fa89055f DK |
1091 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) = 0; |
1092 | virtual bool InternalWriteError() { return filefd->FileFdErrno("write",_("Write error")); } | |
1093 | virtual bool InternalSeek(unsigned long long const To) | |
1094 | { | |
1095 | // Our poor man seeking is costly, so try to avoid it | |
1096 | unsigned long long const iseekpos = filefd->Tell(); | |
1097 | if (iseekpos == To) | |
1098 | return true; | |
1099 | else if (iseekpos < To) | |
1100 | return filefd->Skip(To - iseekpos); | |
1101 | ||
1102 | if ((openmode & FileFd::ReadOnly) != FileFd::ReadOnly) | |
1103 | return filefd->FileFdError("Reopen is only implemented for read-only files!"); | |
1104 | InternalClose(filefd->FileName); | |
1105 | if (filefd->iFd != -1) | |
1106 | close(filefd->iFd); | |
1107 | filefd->iFd = -1; | |
1108 | if (filefd->TemporaryFileName.empty() == false) | |
1109 | filefd->iFd = open(filefd->TemporaryFileName.c_str(), O_RDONLY); | |
1110 | else if (filefd->FileName.empty() == false) | |
1111 | filefd->iFd = open(filefd->FileName.c_str(), O_RDONLY); | |
1112 | else | |
1113 | { | |
1114 | if (compressed_fd > 0) | |
1115 | if (lseek(compressed_fd, 0, SEEK_SET) != 0) | |
1116 | filefd->iFd = compressed_fd; | |
1117 | if (filefd->iFd < 0) | |
1118 | return filefd->FileFdError("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!"); | |
1119 | } | |
1120 | ||
1121 | if (filefd->OpenInternDescriptor(openmode, compressor) == false) | |
1122 | return filefd->FileFdError("Seek on file %s because it couldn't be reopened", filefd->FileName.c_str()); | |
1123 | ||
83e22e26 | 1124 | buffer.reset(); |
eda0c1ba | 1125 | set_seekpos(0); |
fa89055f DK |
1126 | if (To != 0) |
1127 | return filefd->Skip(To); | |
1128 | ||
1129 | seekpos = To; | |
1130 | return true; | |
1131 | } | |
1132 | virtual bool InternalSkip(unsigned long long Over) | |
1133 | { | |
1134 | unsigned long long constexpr buffersize = 1024; | |
1135 | char buffer[buffersize]; | |
1136 | while (Over != 0) | |
1137 | { | |
1138 | unsigned long long toread = std::min(buffersize, Over); | |
1139 | if (filefd->Read(buffer, toread) == false) | |
1140 | return filefd->FileFdError("Unable to seek ahead %llu",Over); | |
1141 | Over -= toread; | |
1142 | } | |
1143 | return true; | |
1144 | } | |
1145 | virtual bool InternalTruncate(unsigned long long const) | |
1146 | { | |
1147 | return filefd->FileFdError("Truncating compressed files is not implemented (%s)", filefd->FileName.c_str()); | |
1148 | } | |
1149 | virtual unsigned long long InternalTell() | |
1150 | { | |
1151 | // In theory, we could just return seekpos here always instead of | |
1152 | // seeking around, but not all users of FileFd use always Seek() and co | |
1153 | // so d->seekpos isn't always true and we can just use it as a hint if | |
1154 | // we have nothing else, but not always as an authority… | |
83e22e26 | 1155 | return seekpos - buffer.size(); |
fa89055f DK |
1156 | } |
1157 | virtual unsigned long long InternalSize() | |
1158 | { | |
1159 | unsigned long long size = 0; | |
1160 | unsigned long long const oldSeek = filefd->Tell(); | |
1161 | unsigned long long constexpr ignoresize = 1024; | |
1162 | char ignore[ignoresize]; | |
1163 | unsigned long long read = 0; | |
1164 | do { | |
1165 | if (filefd->Read(ignore, ignoresize, &read) == false) | |
1166 | { | |
1167 | filefd->Seek(oldSeek); | |
1168 | return 0; | |
1169 | } | |
1170 | } while(read != 0); | |
1171 | size = filefd->Tell(); | |
1172 | filefd->Seek(oldSeek); | |
1173 | return size; | |
1174 | } | |
1175 | virtual bool InternalClose(std::string const &FileName) = 0; | |
1176 | virtual bool InternalStream() const { return false; } | |
1177 | virtual bool InternalAlwaysAutoClose() const { return true; } | |
1178 | ||
1179 | virtual ~FileFdPrivate() {} | |
1180 | }; | |
1181 | /*}}}*/ | |
cabfb880 | 1182 | class APT_HIDDEN BufferedWriteFileFdPrivate : public FileFdPrivate { /*{{{*/ |
88749b5d JAK |
1183 | protected: |
1184 | FileFdPrivate *wrapped; | |
1185 | simple_buffer writebuffer; | |
1186 | ||
1187 | public: | |
1188 | ||
1189 | explicit BufferedWriteFileFdPrivate(FileFdPrivate *Priv) : | |
1190 | FileFdPrivate(Priv->filefd), wrapped(Priv) {}; | |
1191 | ||
cabfb880 | 1192 | virtual APT::Configuration::Compressor get_compressor() const APT_OVERRIDE |
88749b5d JAK |
1193 | { |
1194 | return wrapped->get_compressor(); | |
1195 | } | |
cabfb880 | 1196 | virtual void set_compressor(APT::Configuration::Compressor const &compressor) APT_OVERRIDE |
88749b5d JAK |
1197 | { |
1198 | return wrapped->set_compressor(compressor); | |
1199 | } | |
cabfb880 | 1200 | virtual unsigned int get_openmode() const APT_OVERRIDE |
88749b5d JAK |
1201 | { |
1202 | return wrapped->get_openmode(); | |
1203 | } | |
cabfb880 | 1204 | virtual void set_openmode(unsigned int openmode) APT_OVERRIDE |
88749b5d JAK |
1205 | { |
1206 | return wrapped->set_openmode(openmode); | |
1207 | } | |
cabfb880 | 1208 | virtual bool get_is_pipe() const APT_OVERRIDE |
88749b5d JAK |
1209 | { |
1210 | return wrapped->get_is_pipe(); | |
1211 | } | |
cabfb880 | 1212 | virtual void set_is_pipe(bool is_pipe) APT_OVERRIDE |
88749b5d JAK |
1213 | { |
1214 | FileFdPrivate::set_is_pipe(is_pipe); | |
1215 | wrapped->set_is_pipe(is_pipe); | |
1216 | } | |
cabfb880 | 1217 | virtual unsigned long long get_seekpos() const APT_OVERRIDE |
88749b5d JAK |
1218 | { |
1219 | return wrapped->get_seekpos(); | |
1220 | } | |
cabfb880 | 1221 | virtual void set_seekpos(unsigned long long seekpos) APT_OVERRIDE |
88749b5d JAK |
1222 | { |
1223 | return wrapped->set_seekpos(seekpos); | |
1224 | } | |
cabfb880 | 1225 | virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE |
88749b5d JAK |
1226 | { |
1227 | if (InternalFlush() == false) | |
1228 | return false; | |
1229 | return wrapped->InternalOpen(iFd, Mode); | |
1230 | } | |
cabfb880 | 1231 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE |
88749b5d JAK |
1232 | { |
1233 | if (InternalFlush() == false) | |
1234 | return -1; | |
1235 | return wrapped->InternalUnbufferedRead(To, Size); | |
1236 | ||
1237 | } | |
cabfb880 | 1238 | virtual bool InternalReadError() APT_OVERRIDE |
88749b5d JAK |
1239 | { |
1240 | return wrapped->InternalReadError(); | |
1241 | } | |
cabfb880 | 1242 | virtual char * InternalReadLine(char * To, unsigned long long Size) APT_OVERRIDE |
88749b5d JAK |
1243 | { |
1244 | if (InternalFlush() == false) | |
1245 | return nullptr; | |
1246 | return wrapped->InternalReadLine(To, Size); | |
1247 | } | |
cabfb880 | 1248 | virtual bool InternalFlush() APT_OVERRIDE |
88749b5d | 1249 | { |
1f5062f6 JAK |
1250 | while (writebuffer.empty() == false) { |
1251 | auto written = wrapped->InternalWrite(writebuffer.get(), | |
1252 | writebuffer.size()); | |
1253 | // Ignore interrupted syscalls | |
1254 | if (written < 0 && errno == EINTR) | |
1255 | continue; | |
1256 | if (written < 0) | |
1996a6a7 | 1257 | return wrapped->InternalWriteError(); |
88749b5d | 1258 | |
1f5062f6 | 1259 | writebuffer.bufferstart += written; |
88749b5d JAK |
1260 | } |
1261 | ||
1262 | writebuffer.reset(); | |
1263 | return true; | |
1264 | } | |
cabfb880 | 1265 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
88749b5d | 1266 | { |
f1828e6b JAK |
1267 | // Optimisation: If the buffer is empty and we have more to write than |
1268 | // would fit in the buffer (or equal number of bytes), write directly. | |
1269 | if (writebuffer.empty() == true && Size >= writebuffer.free()) | |
1270 | return wrapped->InternalWrite(From, Size); | |
1271 | ||
1272 | // Write as much into the buffer as possible and then flush if needed | |
070ed1c9 | 1273 | auto written = writebuffer.write(From, Size); |
88749b5d | 1274 | |
070ed1c9 JAK |
1275 | if (writebuffer.full() && InternalFlush() == false) |
1276 | return -1; | |
88749b5d JAK |
1277 | |
1278 | return written; | |
1279 | } | |
cabfb880 | 1280 | virtual bool InternalWriteError() APT_OVERRIDE |
88749b5d JAK |
1281 | { |
1282 | return wrapped->InternalWriteError(); | |
1283 | } | |
cabfb880 | 1284 | virtual bool InternalSeek(unsigned long long const To) APT_OVERRIDE |
88749b5d JAK |
1285 | { |
1286 | if (InternalFlush() == false) | |
1287 | return false; | |
1288 | return wrapped->InternalSeek(To); | |
1289 | } | |
cabfb880 | 1290 | virtual bool InternalSkip(unsigned long long Over) APT_OVERRIDE |
88749b5d JAK |
1291 | { |
1292 | if (InternalFlush() == false) | |
1293 | return false; | |
1294 | return wrapped->InternalSkip(Over); | |
1295 | } | |
cabfb880 | 1296 | virtual bool InternalTruncate(unsigned long long const Size) APT_OVERRIDE |
88749b5d JAK |
1297 | { |
1298 | if (InternalFlush() == false) | |
1299 | return false; | |
1300 | return wrapped->InternalTruncate(Size); | |
1301 | } | |
cabfb880 | 1302 | virtual unsigned long long InternalTell() APT_OVERRIDE |
88749b5d JAK |
1303 | { |
1304 | if (InternalFlush() == false) | |
1305 | return -1; | |
1306 | return wrapped->InternalTell(); | |
1307 | } | |
cabfb880 | 1308 | virtual unsigned long long InternalSize() APT_OVERRIDE |
88749b5d JAK |
1309 | { |
1310 | if (InternalFlush() == false) | |
1311 | return -1; | |
1312 | return wrapped->InternalSize(); | |
1313 | } | |
cabfb880 | 1314 | virtual bool InternalClose(std::string const &FileName) APT_OVERRIDE |
88749b5d JAK |
1315 | { |
1316 | return wrapped->InternalClose(FileName); | |
1317 | } | |
cabfb880 | 1318 | virtual bool InternalAlwaysAutoClose() const APT_OVERRIDE |
88749b5d JAK |
1319 | { |
1320 | return wrapped->InternalAlwaysAutoClose(); | |
1321 | } | |
1322 | virtual ~BufferedWriteFileFdPrivate() | |
1323 | { | |
1324 | delete wrapped; | |
1325 | } | |
1326 | }; | |
1327 | /*}}}*/ | |
65ac6aad | 1328 | class APT_HIDDEN GzipFileFdPrivate: public FileFdPrivate { /*{{{*/ |
4239dbca | 1329 | #ifdef HAVE_ZLIB |
fa89055f DK |
1330 | public: |
1331 | gzFile gz; | |
cabfb880 | 1332 | virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE |
fa89055f DK |
1333 | { |
1334 | if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite) | |
1335 | gz = gzdopen(iFd, "r+"); | |
1336 | else if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) | |
1337 | gz = gzdopen(iFd, "w"); | |
1338 | else | |
1339 | gz = gzdopen(iFd, "r"); | |
1340 | filefd->Flags |= FileFd::Compressed; | |
1341 | return gz != nullptr; | |
1342 | } | |
cabfb880 | 1343 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1344 | { |
1345 | return gzread(gz, To, Size); | |
1346 | } | |
cabfb880 | 1347 | virtual bool InternalReadError() APT_OVERRIDE |
fa89055f DK |
1348 | { |
1349 | int err; | |
1350 | char const * const errmsg = gzerror(gz, &err); | |
1351 | if (err != Z_ERRNO) | |
1352 | return filefd->FileFdError("gzread: %s (%d: %s)", _("Read error"), err, errmsg); | |
1353 | return FileFdPrivate::InternalReadError(); | |
1354 | } | |
cabfb880 | 1355 | virtual char * InternalReadLine(char * To, unsigned long long Size) APT_OVERRIDE |
fa89055f DK |
1356 | { |
1357 | return gzgets(gz, To, Size); | |
1358 | } | |
cabfb880 | 1359 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1360 | { |
1361 | return gzwrite(gz,From,Size); | |
1362 | } | |
cabfb880 | 1363 | virtual bool InternalWriteError() APT_OVERRIDE |
fa89055f DK |
1364 | { |
1365 | int err; | |
1366 | char const * const errmsg = gzerror(gz, &err); | |
1367 | if (err != Z_ERRNO) | |
1368 | return filefd->FileFdError("gzwrite: %s (%d: %s)", _("Write error"), err, errmsg); | |
1369 | return FileFdPrivate::InternalWriteError(); | |
1370 | } | |
cabfb880 | 1371 | virtual bool InternalSeek(unsigned long long const To) APT_OVERRIDE |
fa89055f DK |
1372 | { |
1373 | off_t const res = gzseek(gz, To, SEEK_SET); | |
1374 | if (res != (off_t)To) | |
1375 | return filefd->FileFdError("Unable to seek to %llu", To); | |
fa89055f | 1376 | seekpos = To; |
83e22e26 | 1377 | buffer.reset(); |
fa89055f DK |
1378 | return true; |
1379 | } | |
cabfb880 | 1380 | virtual bool InternalSkip(unsigned long long Over) APT_OVERRIDE |
fa89055f | 1381 | { |
83e22e26 | 1382 | if (Over >= buffer.size()) |
f63123c3 | 1383 | { |
83e22e26 JAK |
1384 | Over -= buffer.size(); |
1385 | buffer.reset(); | |
f63123c3 DK |
1386 | } |
1387 | else | |
1388 | { | |
83e22e26 | 1389 | buffer.bufferstart += Over; |
f63123c3 DK |
1390 | return true; |
1391 | } | |
1392 | if (Over == 0) | |
1393 | return true; | |
fa89055f DK |
1394 | off_t const res = gzseek(gz, Over, SEEK_CUR); |
1395 | if (res < 0) | |
1396 | return filefd->FileFdError("Unable to seek ahead %llu",Over); | |
1397 | seekpos = res; | |
1398 | return true; | |
1399 | } | |
cabfb880 | 1400 | virtual unsigned long long InternalTell() APT_OVERRIDE |
fa89055f | 1401 | { |
83e22e26 | 1402 | return gztell(gz) - buffer.size(); |
fa89055f | 1403 | } |
cabfb880 | 1404 | virtual unsigned long long InternalSize() APT_OVERRIDE |
fa89055f DK |
1405 | { |
1406 | unsigned long long filesize = FileFdPrivate::InternalSize(); | |
1407 | // only check gzsize if we are actually a gzip file, just checking for | |
1408 | // "gz" is not sufficient as uncompressed files could be opened with | |
1409 | // gzopen in "direct" mode as well | |
1410 | if (filesize == 0 || gzdirect(gz)) | |
1411 | return filesize; | |
1412 | ||
1413 | off_t const oldPos = lseek(filefd->iFd, 0, SEEK_CUR); | |
1414 | /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do | |
1415 | * this ourselves; the original (uncompressed) file size is the last 32 | |
1416 | * bits of the file */ | |
1417 | // FIXME: Size for gz-files is limited by 32bit… no largefile support | |
1418 | if (lseek(filefd->iFd, -4, SEEK_END) < 0) | |
1419 | { | |
1420 | filefd->FileFdErrno("lseek","Unable to seek to end of gzipped file"); | |
1421 | return 0; | |
1422 | } | |
1423 | uint32_t size = 0; | |
1424 | if (read(filefd->iFd, &size, 4) != 4) | |
1425 | { | |
1426 | filefd->FileFdErrno("read","Unable to read original size of gzipped file"); | |
1427 | return 0; | |
1428 | } | |
1429 | size = le32toh(size); | |
1430 | ||
1431 | if (lseek(filefd->iFd, oldPos, SEEK_SET) < 0) | |
1432 | { | |
1433 | filefd->FileFdErrno("lseek","Unable to seek in gzipped file"); | |
1434 | return 0; | |
1435 | } | |
1436 | return size; | |
1437 | } | |
cabfb880 | 1438 | virtual bool InternalClose(std::string const &FileName) APT_OVERRIDE |
fa89055f DK |
1439 | { |
1440 | if (gz == nullptr) | |
1441 | return true; | |
1442 | int const e = gzclose(gz); | |
1443 | gz = nullptr; | |
1444 | // gzdclose() on empty files always fails with "buffer error" here, ignore that | |
1445 | if (e != 0 && e != Z_BUF_ERROR) | |
1446 | return _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); | |
1447 | return true; | |
1448 | } | |
1449 | ||
11755147 | 1450 | explicit GzipFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), gz(nullptr) {} |
fa89055f | 1451 | virtual ~GzipFileFdPrivate() { InternalClose(""); } |
4239dbca | 1452 | #endif |
fa89055f DK |
1453 | }; |
1454 | /*}}}*/ | |
65ac6aad | 1455 | class APT_HIDDEN Bz2FileFdPrivate: public FileFdPrivate { /*{{{*/ |
4239dbca | 1456 | #ifdef HAVE_BZ2 |
fa89055f DK |
1457 | BZFILE* bz2; |
1458 | public: | |
cabfb880 | 1459 | virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE |
fa89055f DK |
1460 | { |
1461 | if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite) | |
1462 | bz2 = BZ2_bzdopen(iFd, "r+"); | |
1463 | else if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) | |
1464 | bz2 = BZ2_bzdopen(iFd, "w"); | |
1465 | else | |
1466 | bz2 = BZ2_bzdopen(iFd, "r"); | |
1467 | filefd->Flags |= FileFd::Compressed; | |
1468 | return bz2 != nullptr; | |
1469 | } | |
cabfb880 | 1470 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1471 | { |
1472 | return BZ2_bzread(bz2, To, Size); | |
1473 | } | |
cabfb880 | 1474 | virtual bool InternalReadError() APT_OVERRIDE |
fa89055f DK |
1475 | { |
1476 | int err; | |
1477 | char const * const errmsg = BZ2_bzerror(bz2, &err); | |
1478 | if (err != BZ_IO_ERROR) | |
1479 | return filefd->FileFdError("BZ2_bzread: %s %s (%d: %s)", filefd->FileName.c_str(), _("Read error"), err, errmsg); | |
1480 | return FileFdPrivate::InternalReadError(); | |
1481 | } | |
cabfb880 | 1482 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1483 | { |
1484 | return BZ2_bzwrite(bz2, (void*)From, Size); | |
1485 | } | |
cabfb880 | 1486 | virtual bool InternalWriteError() APT_OVERRIDE |
fa89055f DK |
1487 | { |
1488 | int err; | |
1489 | char const * const errmsg = BZ2_bzerror(bz2, &err); | |
1490 | if (err != BZ_IO_ERROR) | |
1491 | return filefd->FileFdError("BZ2_bzwrite: %s %s (%d: %s)", filefd->FileName.c_str(), _("Write error"), err, errmsg); | |
1492 | return FileFdPrivate::InternalWriteError(); | |
1493 | } | |
cabfb880 DK |
1494 | virtual bool InternalStream() const APT_OVERRIDE { return true; } |
1495 | virtual bool InternalClose(std::string const &) APT_OVERRIDE | |
fa89055f DK |
1496 | { |
1497 | if (bz2 == nullptr) | |
1498 | return true; | |
1499 | BZ2_bzclose(bz2); | |
1500 | bz2 = nullptr; | |
1501 | return true; | |
1502 | } | |
1503 | ||
11755147 | 1504 | explicit Bz2FileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), bz2(nullptr) {} |
fa89055f | 1505 | virtual ~Bz2FileFdPrivate() { InternalClose(""); } |
4239dbca | 1506 | #endif |
e3fbd54c JAK |
1507 | }; |
1508 | /*}}}*/ | |
1509 | class APT_HIDDEN Lz4FileFdPrivate: public FileFdPrivate { /*{{{*/ | |
e3fbd54c JAK |
1510 | static constexpr unsigned long long LZ4_HEADER_SIZE = 19; |
1511 | static constexpr unsigned long long LZ4_FOOTER_SIZE = 4; | |
1512 | #ifdef HAVE_LZ4 | |
1513 | LZ4F_decompressionContext_t dctx; | |
1514 | LZ4F_compressionContext_t cctx; | |
1515 | LZ4F_errorCode_t res; | |
1516 | FileFd backend; | |
1517 | simple_buffer lz4_buffer; | |
1518 | // Count of bytes that the decompressor expects to read next, or buffer size. | |
40940e63 | 1519 | size_t next_to_load = APT_BUFFER_SIZE; |
e3fbd54c | 1520 | public: |
cabfb880 | 1521 | virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE |
e3fbd54c JAK |
1522 | { |
1523 | if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite) | |
1524 | return _error->Error("lz4 only supports write or read mode"); | |
1525 | ||
1526 | if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) { | |
1527 | res = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION); | |
40940e63 | 1528 | lz4_buffer.reset(LZ4F_compressBound(APT_BUFFER_SIZE, nullptr) |
e3fbd54c JAK |
1529 | + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE); |
1530 | } else { | |
1531 | res = LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION); | |
40940e63 | 1532 | lz4_buffer.reset(APT_BUFFER_SIZE); |
e3fbd54c JAK |
1533 | } |
1534 | ||
1535 | filefd->Flags |= FileFd::Compressed; | |
1536 | ||
1537 | if (LZ4F_isError(res)) | |
1538 | return false; | |
1539 | ||
1540 | unsigned int flags = (Mode & (FileFd::WriteOnly|FileFd::ReadOnly)); | |
6f35be91 | 1541 | if (backend.OpenDescriptor(iFd, flags, FileFd::None, true) == false) |
e3fbd54c JAK |
1542 | return false; |
1543 | ||
1544 | // Write the file header | |
1545 | if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) | |
1546 | { | |
1547 | res = LZ4F_compressBegin(cctx, lz4_buffer.buffer, lz4_buffer.buffersize_max, nullptr); | |
1548 | if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false) | |
1549 | return false; | |
1550 | } | |
1551 | ||
1552 | return true; | |
1553 | } | |
cabfb880 | 1554 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE |
e3fbd54c JAK |
1555 | { |
1556 | /* Keep reading as long as the compressor still wants to read */ | |
1557 | while (next_to_load) { | |
1558 | // Fill compressed buffer; | |
1559 | if (lz4_buffer.empty()) { | |
1560 | unsigned long long read; | |
1561 | /* Reset - if LZ4 decompressor wants to read more, allocate more */ | |
1562 | lz4_buffer.reset(next_to_load); | |
1563 | if (backend.Read(lz4_buffer.getend(), lz4_buffer.free(), &read) == false) | |
1564 | return -1; | |
1565 | lz4_buffer.bufferend += read; | |
1566 | ||
1567 | /* Expected EOF */ | |
1568 | if (read == 0) { | |
1569 | res = -1; | |
1570 | return filefd->FileFdError("LZ4F: %s %s", | |
1571 | filefd->FileName.c_str(), | |
1572 | _("Unexpected end of file")), -1; | |
1573 | } | |
1574 | } | |
1575 | // Drain compressed buffer as far as possible. | |
1576 | size_t in = lz4_buffer.size(); | |
1577 | size_t out = Size; | |
1578 | ||
1579 | res = LZ4F_decompress(dctx, To, &out, lz4_buffer.get(), &in, nullptr); | |
1580 | if (LZ4F_isError(res)) | |
1581 | return -1; | |
1582 | ||
1583 | next_to_load = res; | |
1584 | lz4_buffer.bufferstart += in; | |
1585 | ||
1586 | if (out != 0) | |
1587 | return out; | |
1588 | } | |
1589 | ||
1590 | return 0; | |
1591 | } | |
cabfb880 | 1592 | virtual bool InternalReadError() APT_OVERRIDE |
e3fbd54c JAK |
1593 | { |
1594 | char const * const errmsg = LZ4F_getErrorName(res); | |
1595 | ||
1596 | return filefd->FileFdError("LZ4F: %s %s (%zu: %s)", filefd->FileName.c_str(), _("Read error"), res, errmsg); | |
1597 | } | |
cabfb880 | 1598 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
e3fbd54c | 1599 | { |
40940e63 | 1600 | unsigned long long const towrite = std::min(APT_BUFFER_SIZE, Size); |
e3fbd54c JAK |
1601 | |
1602 | res = LZ4F_compressUpdate(cctx, | |
1603 | lz4_buffer.buffer, lz4_buffer.buffersize_max, | |
1604 | From, towrite, nullptr); | |
1605 | ||
1606 | if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false) | |
1607 | return -1; | |
1608 | ||
1609 | return towrite; | |
1610 | } | |
cabfb880 | 1611 | virtual bool InternalWriteError() APT_OVERRIDE |
e3fbd54c JAK |
1612 | { |
1613 | char const * const errmsg = LZ4F_getErrorName(res); | |
1614 | ||
1615 | return filefd->FileFdError("LZ4F: %s %s (%zu: %s)", filefd->FileName.c_str(), _("Write error"), res, errmsg); | |
1616 | } | |
cabfb880 | 1617 | virtual bool InternalStream() const APT_OVERRIDE { return true; } |
e3fbd54c | 1618 | |
cabfb880 | 1619 | virtual bool InternalFlush() APT_OVERRIDE |
e3fbd54c JAK |
1620 | { |
1621 | return backend.Flush(); | |
1622 | } | |
1623 | ||
cabfb880 | 1624 | virtual bool InternalClose(std::string const &) APT_OVERRIDE |
e3fbd54c JAK |
1625 | { |
1626 | /* Reset variables */ | |
1627 | res = 0; | |
40940e63 | 1628 | next_to_load = APT_BUFFER_SIZE; |
e3fbd54c JAK |
1629 | |
1630 | if (cctx != nullptr) | |
1631 | { | |
f43dd439 DK |
1632 | if (filefd->Failed() == false) |
1633 | { | |
1634 | res = LZ4F_compressEnd(cctx, lz4_buffer.buffer, lz4_buffer.buffersize_max, nullptr); | |
1635 | if (LZ4F_isError(res) || backend.Write(lz4_buffer.buffer, res) == false) | |
1636 | return false; | |
1637 | if (!backend.Flush()) | |
1638 | return false; | |
1639 | } | |
e3fbd54c JAK |
1640 | if (!backend.Close()) |
1641 | return false; | |
1642 | ||
1643 | res = LZ4F_freeCompressionContext(cctx); | |
1644 | cctx = nullptr; | |
1645 | } | |
1646 | ||
1647 | if (dctx != nullptr) | |
1648 | { | |
1649 | res = LZ4F_freeDecompressionContext(dctx); | |
1650 | dctx = nullptr; | |
1651 | } | |
6f35be91 DK |
1652 | if (backend.IsOpen()) |
1653 | { | |
1654 | backend.Close(); | |
1655 | filefd->iFd = -1; | |
1656 | } | |
e3fbd54c JAK |
1657 | |
1658 | return LZ4F_isError(res) == false; | |
1659 | } | |
1660 | ||
1661 | explicit Lz4FileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), dctx(nullptr), cctx(nullptr) {} | |
1662 | virtual ~Lz4FileFdPrivate() { | |
1663 | InternalClose(""); | |
1664 | } | |
1665 | #endif | |
fa89055f DK |
1666 | }; |
1667 | /*}}}*/ | |
65ac6aad | 1668 | class APT_HIDDEN LzmaFileFdPrivate: public FileFdPrivate { /*{{{*/ |
4239dbca | 1669 | #ifdef HAVE_LZMA |
fa89055f DK |
1670 | struct LZMAFILE { |
1671 | FILE* file; | |
f43dd439 | 1672 | FileFd * const filefd; |
fa89055f DK |
1673 | uint8_t buffer[4096]; |
1674 | lzma_stream stream; | |
1675 | lzma_ret err; | |
1676 | bool eof; | |
1677 | bool compressing; | |
1678 | ||
f43dd439 | 1679 | LZMAFILE(FileFd * const fd) : file(nullptr), filefd(fd), eof(false), compressing(false) { buffer[0] = '\0'; } |
fa89055f DK |
1680 | ~LZMAFILE() |
1681 | { | |
f43dd439 | 1682 | if (compressing == true && filefd->Failed() == false) |
fa89055f DK |
1683 | { |
1684 | size_t constexpr buffersize = sizeof(buffer)/sizeof(buffer[0]); | |
1685 | while(true) | |
1686 | { | |
1687 | stream.avail_out = buffersize; | |
1688 | stream.next_out = buffer; | |
1689 | err = lzma_code(&stream, LZMA_FINISH); | |
1690 | if (err != LZMA_OK && err != LZMA_STREAM_END) | |
1691 | { | |
1692 | _error->Error("~LZMAFILE: Compress finalisation failed"); | |
1693 | break; | |
1694 | } | |
1695 | size_t const n = buffersize - stream.avail_out; | |
1696 | if (n && fwrite(buffer, 1, n, file) != n) | |
1697 | { | |
1698 | _error->Errno("~LZMAFILE",_("Write error")); | |
1699 | break; | |
1700 | } | |
1701 | if (err == LZMA_STREAM_END) | |
1702 | break; | |
1703 | } | |
1704 | } | |
1705 | lzma_end(&stream); | |
1706 | fclose(file); | |
1707 | } | |
1708 | }; | |
1709 | LZMAFILE* lzma; | |
7a68effc DK |
1710 | static uint32_t findXZlevel(std::vector<std::string> const &Args) |
1711 | { | |
1712 | for (auto a = Args.rbegin(); a != Args.rend(); ++a) | |
1713 | if (a->empty() == false && (*a)[0] == '-' && (*a)[1] != '-') | |
1714 | { | |
1715 | auto const number = a->find_last_of("0123456789"); | |
1716 | if (number == std::string::npos) | |
1717 | continue; | |
1718 | auto const extreme = a->find("e", number); | |
1719 | uint32_t level = (extreme != std::string::npos) ? LZMA_PRESET_EXTREME : 0; | |
1720 | switch ((*a)[number]) | |
1721 | { | |
1722 | case '0': return level | 0; | |
1723 | case '1': return level | 1; | |
1724 | case '2': return level | 2; | |
1725 | case '3': return level | 3; | |
1726 | case '4': return level | 4; | |
1727 | case '5': return level | 5; | |
1728 | case '6': return level | 6; | |
1729 | case '7': return level | 7; | |
1730 | case '8': return level | 8; | |
1731 | case '9': return level | 9; | |
1732 | } | |
1733 | } | |
1734 | return 6; | |
1735 | } | |
fa89055f | 1736 | public: |
cabfb880 | 1737 | virtual bool InternalOpen(int const iFd, unsigned int const Mode) APT_OVERRIDE |
fa89055f DK |
1738 | { |
1739 | if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite) | |
1740 | return filefd->FileFdError("ReadWrite mode is not supported for lzma/xz files %s", filefd->FileName.c_str()); | |
1741 | ||
1742 | if (lzma == nullptr) | |
f43dd439 | 1743 | lzma = new LzmaFileFdPrivate::LZMAFILE(filefd); |
fa89055f DK |
1744 | if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) |
1745 | lzma->file = fdopen(iFd, "w"); | |
1746 | else | |
1747 | lzma->file = fdopen(iFd, "r"); | |
1748 | filefd->Flags |= FileFd::Compressed; | |
1749 | if (lzma->file == nullptr) | |
1750 | return false; | |
1751 | ||
fa89055f DK |
1752 | lzma_stream tmp_stream = LZMA_STREAM_INIT; |
1753 | lzma->stream = tmp_stream; | |
1754 | ||
1755 | if ((Mode & FileFd::WriteOnly) == FileFd::WriteOnly) | |
1756 | { | |
7a68effc | 1757 | uint32_t const xzlevel = findXZlevel(compressor.CompressArgs); |
fa89055f DK |
1758 | if (compressor.Name == "xz") |
1759 | { | |
885a1ffd | 1760 | if (lzma_easy_encoder(&lzma->stream, xzlevel, LZMA_CHECK_CRC64) != LZMA_OK) |
fa89055f DK |
1761 | return false; |
1762 | } | |
1763 | else | |
1764 | { | |
1765 | lzma_options_lzma options; | |
1766 | lzma_lzma_preset(&options, xzlevel); | |
1767 | if (lzma_alone_encoder(&lzma->stream, &options) != LZMA_OK) | |
1768 | return false; | |
1769 | } | |
1770 | lzma->compressing = true; | |
1771 | } | |
1772 | else | |
1773 | { | |
7a68effc | 1774 | uint64_t const memlimit = UINT64_MAX; |
fa89055f DK |
1775 | if (compressor.Name == "xz") |
1776 | { | |
1777 | if (lzma_auto_decoder(&lzma->stream, memlimit, 0) != LZMA_OK) | |
1778 | return false; | |
1779 | } | |
1780 | else | |
1781 | { | |
1782 | if (lzma_alone_decoder(&lzma->stream, memlimit) != LZMA_OK) | |
1783 | return false; | |
1784 | } | |
1785 | lzma->compressing = false; | |
1786 | } | |
1787 | return true; | |
1788 | } | |
cabfb880 | 1789 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1790 | { |
1791 | ssize_t Res; | |
1792 | if (lzma->eof == true) | |
1793 | return 0; | |
1794 | ||
1795 | lzma->stream.next_out = (uint8_t *) To; | |
1796 | lzma->stream.avail_out = Size; | |
1797 | if (lzma->stream.avail_in == 0) | |
1798 | { | |
1799 | lzma->stream.next_in = lzma->buffer; | |
1800 | lzma->stream.avail_in = fread(lzma->buffer, 1, sizeof(lzma->buffer)/sizeof(lzma->buffer[0]), lzma->file); | |
1801 | } | |
1802 | lzma->err = lzma_code(&lzma->stream, LZMA_RUN); | |
1803 | if (lzma->err == LZMA_STREAM_END) | |
1804 | { | |
1805 | lzma->eof = true; | |
1806 | Res = Size - lzma->stream.avail_out; | |
1807 | } | |
1808 | else if (lzma->err != LZMA_OK) | |
1809 | { | |
1810 | Res = -1; | |
1811 | errno = 0; | |
1812 | } | |
1813 | else | |
1814 | { | |
1815 | Res = Size - lzma->stream.avail_out; | |
1816 | if (Res == 0) | |
1817 | { | |
1818 | // lzma run was okay, but produced no output… | |
1819 | Res = -1; | |
1820 | errno = EINTR; | |
1821 | } | |
1822 | } | |
1823 | return Res; | |
1824 | } | |
cabfb880 | 1825 | virtual bool InternalReadError() APT_OVERRIDE |
fa89055f DK |
1826 | { |
1827 | return filefd->FileFdError("lzma_read: %s (%d)", _("Read error"), lzma->err); | |
1828 | } | |
cabfb880 | 1829 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
fa89055f | 1830 | { |
9a63c3f4 | 1831 | ssize_t Res; |
fa89055f DK |
1832 | lzma->stream.next_in = (uint8_t *)From; |
1833 | lzma->stream.avail_in = Size; | |
1834 | lzma->stream.next_out = lzma->buffer; | |
1835 | lzma->stream.avail_out = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]); | |
1836 | lzma->err = lzma_code(&lzma->stream, LZMA_RUN); | |
1837 | if (lzma->err != LZMA_OK) | |
1838 | return -1; | |
1839 | size_t const n = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]) - lzma->stream.avail_out; | |
1840 | size_t const m = (n == 0) ? 0 : fwrite(lzma->buffer, 1, n, lzma->file); | |
1841 | if (m != n) | |
9a63c3f4 CW |
1842 | { |
1843 | Res = -1; | |
1844 | errno = 0; | |
1845 | } | |
fa89055f | 1846 | else |
9a63c3f4 CW |
1847 | { |
1848 | Res = Size - lzma->stream.avail_in; | |
1849 | if (Res == 0) | |
1850 | { | |
1851 | // lzma run was okay, but produced no output… | |
1852 | Res = -1; | |
1853 | errno = EINTR; | |
1854 | } | |
1855 | } | |
1856 | return Res; | |
fa89055f | 1857 | } |
cabfb880 | 1858 | virtual bool InternalWriteError() APT_OVERRIDE |
fa89055f DK |
1859 | { |
1860 | return filefd->FileFdError("lzma_write: %s (%d)", _("Write error"), lzma->err); | |
1861 | } | |
cabfb880 DK |
1862 | virtual bool InternalStream() const APT_OVERRIDE { return true; } |
1863 | virtual bool InternalClose(std::string const &) APT_OVERRIDE | |
fa89055f DK |
1864 | { |
1865 | delete lzma; | |
1866 | lzma = nullptr; | |
1867 | return true; | |
1868 | } | |
1869 | ||
11755147 | 1870 | explicit LzmaFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd), lzma(nullptr) {} |
fa89055f | 1871 | virtual ~LzmaFileFdPrivate() { InternalClose(""); } |
4239dbca | 1872 | #endif |
fa89055f DK |
1873 | }; |
1874 | /*}}}*/ | |
65ac6aad | 1875 | class APT_HIDDEN PipedFileFdPrivate: public FileFdPrivate /*{{{*/ |
fa89055f DK |
1876 | /* if we don't have a specific class dealing with library calls, we (un)compress |
1877 | by executing a specified binary and pipe in/out what we need */ | |
1878 | { | |
1879 | public: | |
cabfb880 | 1880 | virtual bool InternalOpen(int const, unsigned int const Mode) APT_OVERRIDE |
fa89055f DK |
1881 | { |
1882 | // collect zombies here in case we reopen | |
1883 | if (compressor_pid > 0) | |
1884 | ExecWait(compressor_pid, "FileFdCompressor", true); | |
1885 | ||
1886 | if ((Mode & FileFd::ReadWrite) == FileFd::ReadWrite) | |
1887 | return filefd->FileFdError("ReadWrite mode is not supported for file %s", filefd->FileName.c_str()); | |
cc9745a0 DK |
1888 | if (compressor.Binary == "false") |
1889 | return filefd->FileFdError("libapt has inbuilt support for the %s compression," | |
1890 | " but was forced to ignore it in favor of an external binary – which isn't installed.", compressor.Name.c_str()); | |
4239dbca | 1891 | |
fa89055f DK |
1892 | bool const Comp = (Mode & FileFd::WriteOnly) == FileFd::WriteOnly; |
1893 | if (Comp == false) | |
1894 | { | |
1895 | // Handle 'decompression' of empty files | |
1896 | struct stat Buf; | |
1897 | fstat(filefd->iFd, &Buf); | |
1898 | if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false) | |
1899 | return true; | |
1900 | ||
1901 | // We don't need the file open - instead let the compressor open it | |
1902 | // as he properly knows better how to efficiently read from 'his' file | |
1903 | if (filefd->FileName.empty() == false) | |
1904 | { | |
1905 | close(filefd->iFd); | |
1906 | filefd->iFd = -1; | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | // Create a data pipe | |
1911 | int Pipe[2] = {-1,-1}; | |
1912 | if (pipe(Pipe) != 0) | |
1913 | return filefd->FileFdErrno("pipe",_("Failed to create subprocess IPC")); | |
1914 | for (int J = 0; J != 2; J++) | |
1915 | SetCloseExec(Pipe[J],true); | |
1916 | ||
1917 | compressed_fd = filefd->iFd; | |
1d68256d | 1918 | set_is_pipe(true); |
fa89055f DK |
1919 | |
1920 | if (Comp == true) | |
1921 | filefd->iFd = Pipe[1]; | |
1922 | else | |
1923 | filefd->iFd = Pipe[0]; | |
1924 | ||
1925 | // The child.. | |
1926 | compressor_pid = ExecFork(); | |
1927 | if (compressor_pid == 0) | |
1928 | { | |
1929 | if (Comp == true) | |
1930 | { | |
1931 | dup2(compressed_fd,STDOUT_FILENO); | |
1932 | dup2(Pipe[0],STDIN_FILENO); | |
1933 | } | |
1934 | else | |
1935 | { | |
1936 | if (compressed_fd != -1) | |
1937 | dup2(compressed_fd,STDIN_FILENO); | |
1938 | dup2(Pipe[1],STDOUT_FILENO); | |
1939 | } | |
1940 | int const nullfd = open("/dev/null", O_WRONLY); | |
1941 | if (nullfd != -1) | |
1942 | { | |
1943 | dup2(nullfd,STDERR_FILENO); | |
1944 | close(nullfd); | |
1945 | } | |
1946 | ||
1947 | SetCloseExec(STDOUT_FILENO,false); | |
1948 | SetCloseExec(STDIN_FILENO,false); | |
1949 | ||
1950 | std::vector<char const*> Args; | |
1951 | Args.push_back(compressor.Binary.c_str()); | |
1952 | std::vector<std::string> const * const addArgs = | |
1953 | (Comp == true) ? &(compressor.CompressArgs) : &(compressor.UncompressArgs); | |
1954 | for (std::vector<std::string>::const_iterator a = addArgs->begin(); | |
1955 | a != addArgs->end(); ++a) | |
1956 | Args.push_back(a->c_str()); | |
1957 | if (Comp == false && filefd->FileName.empty() == false) | |
1958 | { | |
1959 | // commands not needing arguments, do not need to be told about using standard output | |
1960 | // in reality, only testcases with tools like cat, rev, rot13, … are able to trigger this | |
1961 | if (compressor.CompressArgs.empty() == false && compressor.UncompressArgs.empty() == false) | |
1962 | Args.push_back("--stdout"); | |
1963 | if (filefd->TemporaryFileName.empty() == false) | |
1964 | Args.push_back(filefd->TemporaryFileName.c_str()); | |
1965 | else | |
1966 | Args.push_back(filefd->FileName.c_str()); | |
1967 | } | |
1968 | Args.push_back(NULL); | |
1969 | ||
1970 | execvp(Args[0],(char **)&Args[0]); | |
1971 | cerr << _("Failed to exec compressor ") << Args[0] << endl; | |
1972 | _exit(100); | |
1973 | } | |
1974 | if (Comp == true) | |
1975 | close(Pipe[0]); | |
1976 | else | |
1977 | close(Pipe[1]); | |
1978 | ||
1979 | return true; | |
1980 | } | |
cabfb880 | 1981 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1982 | { |
1983 | return read(filefd->iFd, To, Size); | |
1984 | } | |
cabfb880 | 1985 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
fa89055f DK |
1986 | { |
1987 | return write(filefd->iFd, From, Size); | |
1988 | } | |
cabfb880 | 1989 | virtual bool InternalClose(std::string const &) APT_OVERRIDE |
fa89055f DK |
1990 | { |
1991 | bool Ret = true; | |
bdc42211 DK |
1992 | if (filefd->iFd != -1) |
1993 | { | |
1994 | close(filefd->iFd); | |
1995 | filefd->iFd = -1; | |
1996 | } | |
fa89055f DK |
1997 | if (compressor_pid > 0) |
1998 | Ret &= ExecWait(compressor_pid, "FileFdCompressor", true); | |
1999 | compressor_pid = -1; | |
2000 | return Ret; | |
2001 | } | |
11755147 | 2002 | explicit PipedFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd) {} |
fa89055f DK |
2003 | virtual ~PipedFileFdPrivate() { InternalClose(""); } |
2004 | }; | |
2005 | /*}}}*/ | |
65ac6aad | 2006 | class APT_HIDDEN DirectFileFdPrivate: public FileFdPrivate /*{{{*/ |
fa89055f DK |
2007 | { |
2008 | public: | |
cabfb880 DK |
2009 | virtual bool InternalOpen(int const, unsigned int const) APT_OVERRIDE { return true; } |
2010 | virtual ssize_t InternalUnbufferedRead(void * const To, unsigned long long const Size) APT_OVERRIDE | |
fa89055f DK |
2011 | { |
2012 | return read(filefd->iFd, To, Size); | |
2013 | } | |
cabfb880 | 2014 | virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) APT_OVERRIDE |
fa89055f | 2015 | { |
f63123c3 | 2016 | // files opened read+write are strange and only really "supported" for direct files |
83e22e26 | 2017 | if (buffer.size() != 0) |
f63123c3 | 2018 | { |
83e22e26 JAK |
2019 | lseek(filefd->iFd, -buffer.size(), SEEK_CUR); |
2020 | buffer.reset(); | |
f63123c3 | 2021 | } |
fa89055f DK |
2022 | return write(filefd->iFd, From, Size); |
2023 | } | |
cabfb880 | 2024 | virtual bool InternalSeek(unsigned long long const To) APT_OVERRIDE |
fa89055f DK |
2025 | { |
2026 | off_t const res = lseek(filefd->iFd, To, SEEK_SET); | |
2027 | if (res != (off_t)To) | |
2028 | return filefd->FileFdError("Unable to seek to %llu", To); | |
2029 | seekpos = To; | |
83e22e26 | 2030 | buffer.reset(); |
fa89055f DK |
2031 | return true; |
2032 | } | |
cabfb880 | 2033 | virtual bool InternalSkip(unsigned long long Over) APT_OVERRIDE |
fa89055f | 2034 | { |
83e22e26 | 2035 | if (Over >= buffer.size()) |
f63123c3 | 2036 | { |
83e22e26 JAK |
2037 | Over -= buffer.size(); |
2038 | buffer.reset(); | |
f63123c3 DK |
2039 | } |
2040 | else | |
2041 | { | |
83e22e26 | 2042 | buffer.bufferstart += Over; |
f63123c3 DK |
2043 | return true; |
2044 | } | |
2045 | if (Over == 0) | |
2046 | return true; | |
fa89055f DK |
2047 | off_t const res = lseek(filefd->iFd, Over, SEEK_CUR); |
2048 | if (res < 0) | |
2049 | return filefd->FileFdError("Unable to seek ahead %llu",Over); | |
2050 | seekpos = res; | |
2051 | return true; | |
2052 | } | |
cabfb880 | 2053 | virtual bool InternalTruncate(unsigned long long const To) APT_OVERRIDE |
fa89055f | 2054 | { |
83e22e26 | 2055 | if (buffer.size() != 0) |
f63123c3 DK |
2056 | { |
2057 | unsigned long long const seekpos = lseek(filefd->iFd, 0, SEEK_CUR); | |
83e22e26 JAK |
2058 | if ((seekpos - buffer.size()) >= To) |
2059 | buffer.reset(); | |
f63123c3 | 2060 | else if (seekpos >= To) |
83e22e26 | 2061 | buffer.bufferend = (To - seekpos) + buffer.bufferstart; |
f63123c3 | 2062 | else |
83e22e26 | 2063 | buffer.reset(); |
f63123c3 | 2064 | } |
fa89055f DK |
2065 | if (ftruncate(filefd->iFd, To) != 0) |
2066 | return filefd->FileFdError("Unable to truncate to %llu",To); | |
2067 | return true; | |
2068 | } | |
cabfb880 | 2069 | virtual unsigned long long InternalTell() APT_OVERRIDE |
fa89055f | 2070 | { |
83e22e26 | 2071 | return lseek(filefd->iFd,0,SEEK_CUR) - buffer.size(); |
fa89055f | 2072 | } |
cabfb880 | 2073 | virtual unsigned long long InternalSize() APT_OVERRIDE |
fa89055f DK |
2074 | { |
2075 | return filefd->FileSize(); | |
2076 | } | |
cabfb880 DK |
2077 | virtual bool InternalClose(std::string const &) APT_OVERRIDE { return true; } |
2078 | virtual bool InternalAlwaysAutoClose() const APT_OVERRIDE { return false; } | |
fa89055f | 2079 | |
11755147 | 2080 | explicit DirectFileFdPrivate(FileFd * const filefd) : FileFdPrivate(filefd) {} |
fa89055f | 2081 | virtual ~DirectFileFdPrivate() { InternalClose(""); } |
4239dbca DK |
2082 | }; |
2083 | /*}}}*/ | |
6c55f07a DK |
2084 | // FileFd Constructors /*{{{*/ |
2085 | FileFd::FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode) : iFd(-1), Flags(0), d(NULL) | |
2086 | { | |
2087 | Open(FileName,Mode, None, AccessMode); | |
2088 | } | |
2089 | FileFd::FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode) : iFd(-1), Flags(0), d(NULL) | |
2090 | { | |
2091 | Open(FileName,Mode, Compress, AccessMode); | |
2092 | } | |
2093 | FileFd::FileFd() : iFd(-1), Flags(AutoClose), d(NULL) {} | |
2094 | FileFd::FileFd(int const Fd, unsigned int const Mode, CompressMode Compress) : iFd(-1), Flags(0), d(NULL) | |
2095 | { | |
2096 | OpenDescriptor(Fd, Mode, Compress); | |
2097 | } | |
2098 | FileFd::FileFd(int const Fd, bool const AutoClose) : iFd(-1), Flags(0), d(NULL) | |
2099 | { | |
2100 | OpenDescriptor(Fd, ReadWrite, None, AutoClose); | |
2101 | } | |
2102 | /*}}}*/ | |
13d87e2e | 2103 | // FileFd::Open - Open a file /*{{{*/ |
578bfd0a AL |
2104 | // --------------------------------------------------------------------- |
2105 | /* The most commonly used open mode combinations are given with Mode */ | |
e5f3f8c1 | 2106 | bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, unsigned long const AccessMode) |
578bfd0a | 2107 | { |
257e8d66 | 2108 | if (Mode == ReadOnlyGzip) |
e5f3f8c1 | 2109 | return Open(FileName, ReadOnly, Gzip, AccessMode); |
257e8d66 | 2110 | |
468720c5 | 2111 | if (Compress == Auto && (Mode & WriteOnly) == WriteOnly) |
ae635e3c | 2112 | return FileFdError("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str()); |
257e8d66 | 2113 | |
468720c5 DK |
2114 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); |
2115 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
2116 | if (Compress == Auto) | |
2117 | { | |
468720c5 DK |
2118 | for (; compressor != compressors.end(); ++compressor) |
2119 | { | |
e788a834 | 2120 | std::string file = FileName + compressor->Extension; |
468720c5 DK |
2121 | if (FileExists(file) == false) |
2122 | continue; | |
2123 | FileName = file; | |
468720c5 DK |
2124 | break; |
2125 | } | |
2126 | } | |
2127 | else if (Compress == Extension) | |
2128 | { | |
52b47296 DK |
2129 | std::string::size_type const found = FileName.find_last_of('.'); |
2130 | std::string ext; | |
2131 | if (found != std::string::npos) | |
2132 | { | |
2133 | ext = FileName.substr(found); | |
2134 | if (ext == ".new" || ext == ".bak") | |
2135 | { | |
2136 | std::string::size_type const found2 = FileName.find_last_of('.', found - 1); | |
2137 | if (found2 != std::string::npos) | |
2138 | ext = FileName.substr(found2, found - found2); | |
2139 | else | |
2140 | ext.clear(); | |
2141 | } | |
2142 | } | |
aee1aac6 DK |
2143 | for (; compressor != compressors.end(); ++compressor) |
2144 | if (ext == compressor->Extension) | |
2145 | break; | |
2146 | // no matching extension - assume uncompressed (imagine files like 'example.org_Packages') | |
2147 | if (compressor == compressors.end()) | |
2148 | for (compressor = compressors.begin(); compressor != compressors.end(); ++compressor) | |
2149 | if (compressor->Name == ".") | |
468720c5 | 2150 | break; |
468720c5 | 2151 | } |
aee1aac6 | 2152 | else |
468720c5 DK |
2153 | { |
2154 | std::string name; | |
2155 | switch (Compress) | |
2156 | { | |
aee1aac6 | 2157 | case None: name = "."; break; |
468720c5 DK |
2158 | case Gzip: name = "gzip"; break; |
2159 | case Bzip2: name = "bzip2"; break; | |
2160 | case Lzma: name = "lzma"; break; | |
2161 | case Xz: name = "xz"; break; | |
e3fbd54c | 2162 | case Lz4: name = "lz4"; break; |
aee1aac6 DK |
2163 | case Auto: |
2164 | case Extension: | |
52b47296 | 2165 | // Unreachable |
ae635e3c | 2166 | return FileFdError("Opening File %s in None, Auto or Extension should be already handled?!?", FileName.c_str()); |
468720c5 DK |
2167 | } |
2168 | for (; compressor != compressors.end(); ++compressor) | |
2169 | if (compressor->Name == name) | |
2170 | break; | |
aee1aac6 | 2171 | if (compressor == compressors.end()) |
ae635e3c | 2172 | return FileFdError("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str()); |
468720c5 DK |
2173 | } |
2174 | ||
aee1aac6 | 2175 | if (compressor == compressors.end()) |
ae635e3c | 2176 | return FileFdError("Can't find a match for specified compressor mode for file %s", FileName.c_str()); |
e5f3f8c1 | 2177 | return Open(FileName, Mode, *compressor, AccessMode); |
aee1aac6 | 2178 | } |
e5f3f8c1 | 2179 | bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const AccessMode) |
aee1aac6 DK |
2180 | { |
2181 | Close(); | |
aee1aac6 DK |
2182 | Flags = AutoClose; |
2183 | ||
2184 | if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0) | |
ae635e3c | 2185 | return FileFdError("ReadOnly mode for %s doesn't accept additional flags!", FileName.c_str()); |
aee1aac6 | 2186 | if ((Mode & ReadWrite) == 0) |
ae635e3c | 2187 | return FileFdError("No openmode provided in FileFd::Open for %s", FileName.c_str()); |
468720c5 | 2188 | |
cd46d4eb DK |
2189 | unsigned int OpenMode = Mode; |
2190 | if (FileName == "/dev/null") | |
2191 | OpenMode = OpenMode & ~(Atomic | Exclusive | Create | Empty); | |
2192 | ||
2193 | if ((OpenMode & Atomic) == Atomic) | |
257e8d66 DK |
2194 | { |
2195 | Flags |= Replace; | |
257e8d66 | 2196 | } |
cd46d4eb | 2197 | else if ((OpenMode & (Exclusive | Create)) == (Exclusive | Create)) |
257e8d66 DK |
2198 | { |
2199 | // for atomic, this will be done by rename in Close() | |
ce1f3a2c | 2200 | RemoveFile("FileFd::Open", FileName); |
257e8d66 | 2201 | } |
cd46d4eb | 2202 | if ((OpenMode & Empty) == Empty) |
578bfd0a | 2203 | { |
257e8d66 DK |
2204 | struct stat Buf; |
2205 | if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode)) | |
ce1f3a2c | 2206 | RemoveFile("FileFd::Open", FileName); |
257e8d66 | 2207 | } |
c4fc2fd7 | 2208 | |
561f860a | 2209 | int fileflags = 0; |
cd46d4eb | 2210 | #define if_FLAGGED_SET(FLAG, MODE) if ((OpenMode & FLAG) == FLAG) fileflags |= MODE |
561f860a DK |
2211 | if_FLAGGED_SET(ReadWrite, O_RDWR); |
2212 | else if_FLAGGED_SET(ReadOnly, O_RDONLY); | |
2213 | else if_FLAGGED_SET(WriteOnly, O_WRONLY); | |
4a9db827 | 2214 | |
561f860a DK |
2215 | if_FLAGGED_SET(Create, O_CREAT); |
2216 | if_FLAGGED_SET(Empty, O_TRUNC); | |
2217 | if_FLAGGED_SET(Exclusive, O_EXCL); | |
561f860a | 2218 | #undef if_FLAGGED_SET |
52b47296 | 2219 | |
cd46d4eb | 2220 | if ((OpenMode & Atomic) == Atomic) |
7335eebe AGM |
2221 | { |
2222 | char *name = strdup((FileName + ".XXXXXX").c_str()); | |
2223 | ||
dc545c0b | 2224 | if((iFd = mkstemp(name)) == -1) |
7335eebe AGM |
2225 | { |
2226 | free(name); | |
98b69f9d | 2227 | return FileFdErrno("mkstemp", "Could not create temporary file for %s", FileName.c_str()); |
7335eebe AGM |
2228 | } |
2229 | ||
2230 | TemporaryFileName = string(name); | |
7335eebe | 2231 | free(name); |
dc545c0b | 2232 | |
230e69d7 DK |
2233 | // umask() will always set the umask and return the previous value, so |
2234 | // we first set the umask and then reset it to the old value | |
2235 | mode_t const CurrentUmask = umask(0); | |
2236 | umask(CurrentUmask); | |
2237 | // calculate the actual file permissions (just like open/creat) | |
2238 | mode_t const FilePermissions = (AccessMode & ~CurrentUmask); | |
2239 | ||
2240 | if(fchmod(iFd, FilePermissions) == -1) | |
dc545c0b | 2241 | return FileFdErrno("fchmod", "Could not change permissions for temporary file %s", TemporaryFileName.c_str()); |
7335eebe | 2242 | } |
468720c5 | 2243 | else |
230e69d7 | 2244 | iFd = open(FileName.c_str(), fileflags, AccessMode); |
468720c5 | 2245 | |
b711c01e | 2246 | this->FileName = FileName; |
cd46d4eb | 2247 | if (iFd == -1 || OpenInternDescriptor(OpenMode, compressor) == false) |
561f860a | 2248 | { |
468720c5 | 2249 | if (iFd != -1) |
fc81e8f2 | 2250 | { |
561f860a DK |
2251 | close (iFd); |
2252 | iFd = -1; | |
fc81e8f2 | 2253 | } |
ae635e3c | 2254 | return FileFdErrno("open",_("Could not open file %s"), FileName.c_str()); |
257e8d66 | 2255 | } |
578bfd0a | 2256 | |
13d87e2e AL |
2257 | SetCloseExec(iFd,true); |
2258 | return true; | |
578bfd0a | 2259 | } |
257e8d66 DK |
2260 | /*}}}*/ |
2261 | // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/ | |
52b47296 | 2262 | bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose) |
aee1aac6 DK |
2263 | { |
2264 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); | |
2265 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
2266 | std::string name; | |
bce778a3 MV |
2267 | |
2268 | // compat with the old API | |
2269 | if (Mode == ReadOnlyGzip && Compress == None) | |
2270 | Compress = Gzip; | |
2271 | ||
aee1aac6 DK |
2272 | switch (Compress) |
2273 | { | |
2274 | case None: name = "."; break; | |
2275 | case Gzip: name = "gzip"; break; | |
2276 | case Bzip2: name = "bzip2"; break; | |
2277 | case Lzma: name = "lzma"; break; | |
2278 | case Xz: name = "xz"; break; | |
e3fbd54c | 2279 | case Lz4: name = "lz4"; break; |
aee1aac6 DK |
2280 | case Auto: |
2281 | case Extension: | |
f97bb523 DK |
2282 | if (AutoClose == true && Fd != -1) |
2283 | close(Fd); | |
ae635e3c | 2284 | return FileFdError("Opening Fd %d in Auto or Extension compression mode is not supported", Fd); |
aee1aac6 DK |
2285 | } |
2286 | for (; compressor != compressors.end(); ++compressor) | |
2287 | if (compressor->Name == name) | |
2288 | break; | |
2289 | if (compressor == compressors.end()) | |
f97bb523 DK |
2290 | { |
2291 | if (AutoClose == true && Fd != -1) | |
2292 | close(Fd); | |
ae635e3c | 2293 | return FileFdError("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str()); |
f97bb523 | 2294 | } |
aee1aac6 DK |
2295 | return OpenDescriptor(Fd, Mode, *compressor, AutoClose); |
2296 | } | |
52b47296 | 2297 | bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose) |
144c0969 JAK |
2298 | { |
2299 | Close(); | |
2300 | Flags = (AutoClose) ? FileFd::AutoClose : 0; | |
84baaae9 | 2301 | iFd = Fd; |
b711c01e | 2302 | this->FileName = ""; |
84baaae9 | 2303 | if (OpenInternDescriptor(Mode, compressor) == false) |
468720c5 | 2304 | { |
f97bb523 | 2305 | if (iFd != -1 && ( |
84baaae9 | 2306 | (Flags & Compressed) == Compressed || |
f97bb523 DK |
2307 | AutoClose == true)) |
2308 | { | |
468720c5 | 2309 | close (iFd); |
f97bb523 DK |
2310 | iFd = -1; |
2311 | } | |
2312 | return FileFdError(_("Could not open file descriptor %d"), Fd); | |
144c0969 | 2313 | } |
144c0969 | 2314 | return true; |
468720c5 | 2315 | } |
52b47296 | 2316 | bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) |
468720c5 | 2317 | { |
84baaae9 DK |
2318 | if (iFd == -1) |
2319 | return false; | |
ff477ee1 | 2320 | |
fa89055f DK |
2321 | if (d != nullptr) |
2322 | d->InternalClose(FileName); | |
2323 | ||
2324 | if (d == nullptr) | |
2325 | { | |
2326 | if (false) | |
2327 | /* dummy so that the rest can be 'else if's */; | |
2328 | #define APT_COMPRESS_INIT(NAME, CONSTRUCTOR) \ | |
2329 | else if (compressor.Name == NAME) \ | |
2330 | d = new CONSTRUCTOR(this) | |
69d6988a | 2331 | #ifdef HAVE_ZLIB |
fa89055f | 2332 | APT_COMPRESS_INIT("gzip", GzipFileFdPrivate); |
69d6988a DK |
2333 | #endif |
2334 | #ifdef HAVE_BZ2 | |
fa89055f | 2335 | APT_COMPRESS_INIT("bzip2", Bz2FileFdPrivate); |
69d6988a | 2336 | #endif |
7f350a37 | 2337 | #ifdef HAVE_LZMA |
fa89055f DK |
2338 | APT_COMPRESS_INIT("xz", LzmaFileFdPrivate); |
2339 | APT_COMPRESS_INIT("lzma", LzmaFileFdPrivate); | |
7f350a37 | 2340 | #endif |
e3fbd54c JAK |
2341 | #ifdef HAVE_LZ4 |
2342 | APT_COMPRESS_INIT("lz4", Lz4FileFdPrivate); | |
2343 | #endif | |
69d6988a | 2344 | #undef APT_COMPRESS_INIT |
fa89055f DK |
2345 | else if (compressor.Name == "." || compressor.Binary.empty() == true) |
2346 | d = new DirectFileFdPrivate(this); | |
2347 | else | |
2348 | d = new PipedFileFdPrivate(this); | |
69d6988a | 2349 | |
88749b5d JAK |
2350 | if (Mode & BufferedWrite) |
2351 | d = new BufferedWriteFileFdPrivate(d); | |
2352 | ||
1d68256d JAK |
2353 | d->set_openmode(Mode); |
2354 | d->set_compressor(compressor); | |
fa89055f | 2355 | if ((Flags & AutoClose) != AutoClose && d->InternalAlwaysAutoClose()) |
84baaae9 DK |
2356 | { |
2357 | // Need to duplicate fd here or gz/bz2 close for cleanup will close the fd as well | |
2358 | int const internFd = dup(iFd); | |
2359 | if (internFd == -1) | |
2360 | return FileFdErrno("OpenInternDescriptor", _("Could not open file descriptor %d"), iFd); | |
2361 | iFd = internFd; | |
2362 | } | |
561f860a | 2363 | } |
fa89055f | 2364 | return d->InternalOpen(iFd, Mode); |
144c0969 | 2365 | } |
578bfd0a | 2366 | /*}}}*/ |
8e06abb2 | 2367 | // FileFd::~File - Closes the file /*{{{*/ |
578bfd0a AL |
2368 | // --------------------------------------------------------------------- |
2369 | /* If the proper modes are selected then we close the Fd and possibly | |
2370 | unlink the file on error. */ | |
8e06abb2 | 2371 | FileFd::~FileFd() |
578bfd0a AL |
2372 | { |
2373 | Close(); | |
500400fe | 2374 | if (d != NULL) |
fa89055f | 2375 | d->InternalClose(FileName); |
96ab3c6f MV |
2376 | delete d; |
2377 | d = NULL; | |
578bfd0a AL |
2378 | } |
2379 | /*}}}*/ | |
8e06abb2 | 2380 | // FileFd::Read - Read a bit of the file /*{{{*/ |
578bfd0a | 2381 | // --------------------------------------------------------------------- |
1e3f4083 | 2382 | /* We are careful to handle interruption by a signal while reading |
b0db36b1 | 2383 | gracefully. */ |
650faab0 | 2384 | bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) |
578bfd0a | 2385 | { |
fa89055f DK |
2386 | if (d == nullptr) |
2387 | return false; | |
39e77e45 | 2388 | ssize_t Res = 1; |
b0db36b1 | 2389 | errno = 0; |
f604cf55 AL |
2390 | if (Actual != 0) |
2391 | *Actual = 0; | |
699b209e | 2392 | *((char *)To) = '\0'; |
39e77e45 | 2393 | while (Res > 0 && Size > 0) |
578bfd0a | 2394 | { |
fa89055f | 2395 | Res = d->InternalRead(To, Size); |
b711c01e | 2396 | |
b0db36b1 AL |
2397 | if (Res < 0) |
2398 | { | |
b711c01e | 2399 | if (errno == EINTR) |
c4b113e6 DK |
2400 | { |
2401 | // trick the while-loop into running again | |
2402 | Res = 1; | |
2403 | errno = 0; | |
b711c01e | 2404 | continue; |
c4b113e6 | 2405 | } |
fa89055f | 2406 | return d->InternalReadError(); |
b0db36b1 | 2407 | } |
578bfd0a | 2408 | |
b0db36b1 AL |
2409 | To = (char *)To + Res; |
2410 | Size -= Res; | |
ff477ee1 | 2411 | if (d != NULL) |
1d68256d | 2412 | d->set_seekpos(d->get_seekpos() + Res); |
f604cf55 AL |
2413 | if (Actual != 0) |
2414 | *Actual += Res; | |
b0db36b1 | 2415 | } |
b0db36b1 AL |
2416 | |
2417 | if (Size == 0) | |
2418 | return true; | |
2419 | ||
ddc1d8d0 | 2420 | // Eof handling |
f604cf55 | 2421 | if (Actual != 0) |
ddc1d8d0 AL |
2422 | { |
2423 | Flags |= HitEof; | |
2424 | return true; | |
2425 | } | |
ae635e3c DK |
2426 | |
2427 | return FileFdError(_("read, still have %llu to read but none left"), Size); | |
578bfd0a AL |
2428 | } |
2429 | /*}}}*/ | |
032bd56f DK |
2430 | // FileFd::ReadLine - Read a complete line from the file /*{{{*/ |
2431 | // --------------------------------------------------------------------- | |
fa89055f | 2432 | /* Beware: This method can be quite slow for big buffers on UNcompressed |
032bd56f DK |
2433 | files because of the naive implementation! */ |
2434 | char* FileFd::ReadLine(char *To, unsigned long long const Size) | |
2435 | { | |
699b209e | 2436 | *To = '\0'; |
fa89055f DK |
2437 | if (d == nullptr) |
2438 | return nullptr; | |
2439 | return d->InternalReadLine(To, Size); | |
032bd56f DK |
2440 | } |
2441 | /*}}}*/ | |
766761fd JAK |
2442 | // FileFd::Flush - Flush the file /*{{{*/ |
2443 | bool FileFd::Flush() | |
2444 | { | |
2445 | if (d == nullptr) | |
2446 | return true; | |
2447 | ||
2448 | return d->InternalFlush(); | |
2449 | } | |
2450 | /*}}}*/ | |
8e06abb2 | 2451 | // FileFd::Write - Write to the file /*{{{*/ |
650faab0 | 2452 | bool FileFd::Write(const void *From,unsigned long long Size) |
578bfd0a | 2453 | { |
fa89055f DK |
2454 | if (d == nullptr) |
2455 | return false; | |
5df91bc7 | 2456 | ssize_t Res = 1; |
b0db36b1 | 2457 | errno = 0; |
5df91bc7 | 2458 | while (Res > 0 && Size > 0) |
578bfd0a | 2459 | { |
fa89055f | 2460 | Res = d->InternalWrite(From, Size); |
9a63c3f4 | 2461 | |
b0db36b1 | 2462 | if (Res < 0) |
9a63c3f4 CW |
2463 | { |
2464 | if (errno == EINTR) | |
2465 | { | |
2466 | // trick the while-loop into running again | |
2467 | Res = 1; | |
2468 | errno = 0; | |
2469 | continue; | |
2470 | } | |
fa89055f | 2471 | return d->InternalWriteError(); |
9a63c3f4 | 2472 | } |
fa89055f | 2473 | |
cf4ff3b7 | 2474 | From = (char const *)From + Res; |
b0db36b1 | 2475 | Size -= Res; |
ff477ee1 | 2476 | if (d != NULL) |
1d68256d | 2477 | d->set_seekpos(d->get_seekpos() + Res); |
578bfd0a | 2478 | } |
fa89055f | 2479 | |
b0db36b1 AL |
2480 | if (Size == 0) |
2481 | return true; | |
ae635e3c DK |
2482 | |
2483 | return FileFdError(_("write, still have %llu to write but couldn't"), Size); | |
d68d65ad DK |
2484 | } |
2485 | bool FileFd::Write(int Fd, const void *From, unsigned long long Size) | |
2486 | { | |
5df91bc7 | 2487 | ssize_t Res = 1; |
d68d65ad | 2488 | errno = 0; |
5df91bc7 | 2489 | while (Res > 0 && Size > 0) |
d68d65ad DK |
2490 | { |
2491 | Res = write(Fd,From,Size); | |
2492 | if (Res < 0 && errno == EINTR) | |
2493 | continue; | |
2494 | if (Res < 0) | |
2495 | return _error->Errno("write",_("Write error")); | |
2496 | ||
cf4ff3b7 | 2497 | From = (char const *)From + Res; |
d68d65ad DK |
2498 | Size -= Res; |
2499 | } | |
d68d65ad DK |
2500 | |
2501 | if (Size == 0) | |
2502 | return true; | |
2503 | ||
2504 | return _error->Error(_("write, still have %llu to write but couldn't"), Size); | |
578bfd0a AL |
2505 | } |
2506 | /*}}}*/ | |
8e06abb2 | 2507 | // FileFd::Seek - Seek in the file /*{{{*/ |
650faab0 | 2508 | bool FileFd::Seek(unsigned long long To) |
578bfd0a | 2509 | { |
fa89055f DK |
2510 | if (d == nullptr) |
2511 | return false; | |
bb93178b | 2512 | Flags &= ~HitEof; |
fa89055f | 2513 | return d->InternalSeek(To); |
727f18af AL |
2514 | } |
2515 | /*}}}*/ | |
fa89055f | 2516 | // FileFd::Skip - Skip over data in the file /*{{{*/ |
650faab0 | 2517 | bool FileFd::Skip(unsigned long long Over) |
727f18af | 2518 | { |
fa89055f DK |
2519 | if (d == nullptr) |
2520 | return false; | |
2521 | return d->InternalSkip(Over); | |
6d5dd02a AL |
2522 | } |
2523 | /*}}}*/ | |
fa89055f | 2524 | // FileFd::Truncate - Truncate the file /*{{{*/ |
650faab0 | 2525 | bool FileFd::Truncate(unsigned long long To) |
6d5dd02a | 2526 | { |
fa89055f DK |
2527 | if (d == nullptr) |
2528 | return false; | |
ad5051ef DK |
2529 | // truncating /dev/null is always successful - as we get an error otherwise |
2530 | if (To == 0 && FileName == "/dev/null") | |
2531 | return true; | |
fa89055f | 2532 | return d->InternalTruncate(To); |
578bfd0a AL |
2533 | } |
2534 | /*}}}*/ | |
7f25bdff AL |
2535 | // FileFd::Tell - Current seek position /*{{{*/ |
2536 | // --------------------------------------------------------------------- | |
2537 | /* */ | |
650faab0 | 2538 | unsigned long long FileFd::Tell() |
7f25bdff | 2539 | { |
fa89055f DK |
2540 | if (d == nullptr) |
2541 | return false; | |
2542 | off_t const Res = d->InternalTell(); | |
7f25bdff | 2543 | if (Res == (off_t)-1) |
ae635e3c | 2544 | FileFdErrno("lseek","Failed to determine the current file position"); |
1d68256d | 2545 | d->set_seekpos(Res); |
7f25bdff AL |
2546 | return Res; |
2547 | } | |
2548 | /*}}}*/ | |
8190b07a | 2549 | static bool StatFileFd(char const * const msg, int const iFd, std::string const &FileName, struct stat &Buf, FileFdPrivate * const d) /*{{{*/ |
578bfd0a | 2550 | { |
1d68256d | 2551 | bool ispipe = (d != NULL && d->get_is_pipe() == true); |
6008b79a DK |
2552 | if (ispipe == false) |
2553 | { | |
2554 | if (fstat(iFd,&Buf) != 0) | |
8190b07a DK |
2555 | // higher-level code will generate more meaningful messages, |
2556 | // even translated this would be meaningless for users | |
2557 | return _error->Errno("fstat", "Unable to determine %s for fd %i", msg, iFd); | |
003c40d3 DK |
2558 | if (FileName.empty() == false) |
2559 | ispipe = S_ISFIFO(Buf.st_mode); | |
6008b79a | 2560 | } |
699b209e DK |
2561 | |
2562 | // for compressor pipes st_size is undefined and at 'best' zero | |
6008b79a | 2563 | if (ispipe == true) |
699b209e DK |
2564 | { |
2565 | // we set it here, too, as we get the info here for free | |
2566 | // in theory the Open-methods should take care of it already | |
ff477ee1 | 2567 | if (d != NULL) |
1d68256d | 2568 | d->set_is_pipe(true); |
699b209e | 2569 | if (stat(FileName.c_str(), &Buf) != 0) |
8190b07a DK |
2570 | return _error->Errno("fstat", "Unable to determine %s for file %s", msg, FileName.c_str()); |
2571 | } | |
2572 | return true; | |
2573 | } | |
2574 | /*}}}*/ | |
2575 | // FileFd::FileSize - Return the size of the file /*{{{*/ | |
2576 | unsigned long long FileFd::FileSize() | |
2577 | { | |
2578 | struct stat Buf; | |
2579 | if (StatFileFd("file size", iFd, FileName, Buf, d) == false) | |
2580 | { | |
2581 | Flags |= Fail; | |
2582 | return 0; | |
699b209e | 2583 | } |
4260fd39 DK |
2584 | return Buf.st_size; |
2585 | } | |
2586 | /*}}}*/ | |
8190b07a DK |
2587 | // FileFd::ModificationTime - Return the time of last touch /*{{{*/ |
2588 | time_t FileFd::ModificationTime() | |
2589 | { | |
2590 | struct stat Buf; | |
2591 | if (StatFileFd("modification time", iFd, FileName, Buf, d) == false) | |
2592 | { | |
2593 | Flags |= Fail; | |
2594 | return 0; | |
2595 | } | |
2596 | return Buf.st_mtime; | |
2597 | } | |
2598 | /*}}}*/ | |
4260fd39 | 2599 | // FileFd::Size - Return the size of the content in the file /*{{{*/ |
650faab0 | 2600 | unsigned long long FileFd::Size() |
4260fd39 | 2601 | { |
fa89055f DK |
2602 | if (d == nullptr) |
2603 | return false; | |
2604 | return d->InternalSize(); | |
578bfd0a AL |
2605 | } |
2606 | /*}}}*/ | |
8e06abb2 | 2607 | // FileFd::Close - Close the file if the close flag is set /*{{{*/ |
578bfd0a AL |
2608 | // --------------------------------------------------------------------- |
2609 | /* */ | |
8e06abb2 | 2610 | bool FileFd::Close() |
578bfd0a | 2611 | { |
f43dd439 | 2612 | if (Failed() == false && Flush() == false) |
766761fd | 2613 | return false; |
032bd56f DK |
2614 | if (iFd == -1) |
2615 | return true; | |
2616 | ||
578bfd0a AL |
2617 | bool Res = true; |
2618 | if ((Flags & AutoClose) == AutoClose) | |
d13c2d3f | 2619 | { |
500400fe DK |
2620 | if ((Flags & Compressed) != Compressed && iFd > 0 && close(iFd) != 0) |
2621 | Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); | |
2da8aae5 JAK |
2622 | } |
2623 | ||
2624 | if (d != NULL) | |
2625 | { | |
fa89055f | 2626 | Res &= d->InternalClose(FileName); |
2da8aae5 JAK |
2627 | delete d; |
2628 | d = NULL; | |
d13c2d3f | 2629 | } |
3010fb0e | 2630 | |
d3aac32e | 2631 | if ((Flags & Replace) == Replace) { |
fc5db01b | 2632 | if (Failed() == false && rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) |
62d073d9 DK |
2633 | Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str()); |
2634 | ||
fd3b761e | 2635 | FileName = TemporaryFileName; // for the unlink() below. |
257e8d66 | 2636 | TemporaryFileName.clear(); |
3010fb0e | 2637 | } |
62d073d9 DK |
2638 | |
2639 | iFd = -1; | |
2640 | ||
578bfd0a AL |
2641 | if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && |
2642 | FileName.empty() == false) | |
ce1f3a2c | 2643 | Res &= RemoveFile("FileFd::Close", FileName); |
3010fb0e | 2644 | |
fbb89d94 DK |
2645 | if (Res == false) |
2646 | Flags |= Fail; | |
578bfd0a AL |
2647 | return Res; |
2648 | } | |
2649 | /*}}}*/ | |
b2e465d6 AL |
2650 | // FileFd::Sync - Sync the file /*{{{*/ |
2651 | // --------------------------------------------------------------------- | |
2652 | /* */ | |
2653 | bool FileFd::Sync() | |
2654 | { | |
b2e465d6 | 2655 | if (fsync(iFd) != 0) |
ae635e3c DK |
2656 | return FileFdErrno("sync",_("Problem syncing the file")); |
2657 | return true; | |
2658 | } | |
2659 | /*}}}*/ | |
2660 | // FileFd::FileFdErrno - set Fail and call _error->Errno *{{{*/ | |
2661 | bool FileFd::FileFdErrno(const char *Function, const char *Description,...) | |
2662 | { | |
2663 | Flags |= Fail; | |
2664 | va_list args; | |
2665 | size_t msgSize = 400; | |
2666 | int const errsv = errno; | |
2667 | while (true) | |
fbb89d94 | 2668 | { |
ae635e3c DK |
2669 | va_start(args,Description); |
2670 | if (_error->InsertErrno(GlobalError::ERROR, Function, Description, args, errsv, msgSize) == false) | |
2671 | break; | |
2672 | va_end(args); | |
fbb89d94 | 2673 | } |
ae635e3c DK |
2674 | return false; |
2675 | } | |
2676 | /*}}}*/ | |
2677 | // FileFd::FileFdError - set Fail and call _error->Error *{{{*/ | |
2678 | bool FileFd::FileFdError(const char *Description,...) { | |
2679 | Flags |= Fail; | |
2680 | va_list args; | |
2681 | size_t msgSize = 400; | |
2682 | while (true) | |
2683 | { | |
2684 | va_start(args,Description); | |
2685 | if (_error->Insert(GlobalError::ERROR, Description, args, msgSize) == false) | |
2686 | break; | |
2687 | va_end(args); | |
2688 | } | |
2689 | return false; | |
b2e465d6 AL |
2690 | } |
2691 | /*}}}*/ | |
fa89055f | 2692 | gzFile FileFd::gzFd() { /*{{{*/ |
7f350a37 | 2693 | #ifdef HAVE_ZLIB |
fa89055f DK |
2694 | GzipFileFdPrivate * const gzipd = dynamic_cast<GzipFileFdPrivate*>(d); |
2695 | if (gzipd == nullptr) | |
2696 | return nullptr; | |
2697 | else | |
2698 | return gzipd->gz; | |
7f350a37 | 2699 | #else |
fa89055f | 2700 | return nullptr; |
7f350a37 DK |
2701 | #endif |
2702 | } | |
fa89055f | 2703 | /*}}}*/ |
8d01b9d6 | 2704 | |
f8aba23f | 2705 | // Glob - wrapper around "glob()" /*{{{*/ |
8d01b9d6 MV |
2706 | std::vector<std::string> Glob(std::string const &pattern, int flags) |
2707 | { | |
2708 | std::vector<std::string> result; | |
2709 | glob_t globbuf; | |
ec4835a1 ÁGM |
2710 | int glob_res; |
2711 | unsigned int i; | |
8d01b9d6 MV |
2712 | |
2713 | glob_res = glob(pattern.c_str(), flags, NULL, &globbuf); | |
2714 | ||
2715 | if (glob_res != 0) | |
2716 | { | |
2717 | if(glob_res != GLOB_NOMATCH) { | |
2718 | _error->Errno("glob", "Problem with glob"); | |
2719 | return result; | |
2720 | } | |
2721 | } | |
2722 | ||
2723 | // append results | |
2724 | for(i=0;i<globbuf.gl_pathc;i++) | |
2725 | result.push_back(string(globbuf.gl_pathv[i])); | |
2726 | ||
2727 | globfree(&globbuf); | |
2728 | return result; | |
2729 | } | |
2730 | /*}}}*/ | |
f8aba23f | 2731 | std::string GetTempDir() /*{{{*/ |
68e01721 MV |
2732 | { |
2733 | const char *tmpdir = getenv("TMPDIR"); | |
2734 | ||
2735 | #ifdef P_tmpdir | |
2736 | if (!tmpdir) | |
2737 | tmpdir = P_tmpdir; | |
2738 | #endif | |
2739 | ||
68e01721 | 2740 | struct stat st; |
0d303f17 | 2741 | if (!tmpdir || strlen(tmpdir) == 0 || // tmpdir is set |
dd6da7d2 DK |
2742 | stat(tmpdir, &st) != 0 || (st.st_mode & S_IFDIR) == 0) // exists and is directory |
2743 | tmpdir = "/tmp"; | |
2744 | else if (geteuid() != 0 && // root can do everything anyway | |
2745 | faccessat(-1, tmpdir, R_OK | W_OK | X_OK, AT_EACCESS | AT_SYMLINK_NOFOLLOW) != 0) // current user has rwx access to directory | |
68e01721 MV |
2746 | tmpdir = "/tmp"; |
2747 | ||
2748 | return string(tmpdir); | |
dd6da7d2 DK |
2749 | } |
2750 | std::string GetTempDir(std::string const &User) | |
2751 | { | |
2752 | // no need/possibility to drop privs | |
2753 | if(getuid() != 0 || User.empty() || User == "root") | |
2754 | return GetTempDir(); | |
2755 | ||
2756 | struct passwd const * const pw = getpwnam(User.c_str()); | |
2757 | if (pw == NULL) | |
2758 | return GetTempDir(); | |
2759 | ||
226c0f64 DK |
2760 | gid_t const old_euid = geteuid(); |
2761 | gid_t const old_egid = getegid(); | |
dd6da7d2 DK |
2762 | if (setegid(pw->pw_gid) != 0) |
2763 | _error->Errno("setegid", "setegid %u failed", pw->pw_gid); | |
2764 | if (seteuid(pw->pw_uid) != 0) | |
2765 | _error->Errno("seteuid", "seteuid %u failed", pw->pw_uid); | |
2766 | ||
2767 | std::string const tmp = GetTempDir(); | |
2768 | ||
226c0f64 DK |
2769 | if (seteuid(old_euid) != 0) |
2770 | _error->Errno("seteuid", "seteuid %u failed", old_euid); | |
2771 | if (setegid(old_egid) != 0) | |
2772 | _error->Errno("setegid", "setegid %u failed", old_egid); | |
dd6da7d2 DK |
2773 | |
2774 | return tmp; | |
68e01721 | 2775 | } |
f8aba23f | 2776 | /*}}}*/ |
c9443c01 | 2777 | FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * const TmpFd) /*{{{*/ |
0d29b9d4 MV |
2778 | { |
2779 | char fn[512]; | |
c9443c01 | 2780 | FileFd * const Fd = TmpFd == NULL ? new FileFd() : TmpFd; |
0d29b9d4 | 2781 | |
c9443c01 DK |
2782 | std::string const tempdir = GetTempDir(); |
2783 | snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", | |
0d29b9d4 | 2784 | tempdir.c_str(), Prefix.c_str()); |
c9443c01 | 2785 | int const fd = mkstemp(fn); |
0d29b9d4 MV |
2786 | if(ImmediateUnlink) |
2787 | unlink(fn); | |
c9443c01 | 2788 | if (fd < 0) |
0d29b9d4 MV |
2789 | { |
2790 | _error->Errno("GetTempFile",_("Unable to mkstemp %s"), fn); | |
2791 | return NULL; | |
2792 | } | |
c9443c01 | 2793 | if (!Fd->OpenDescriptor(fd, FileFd::ReadWrite, FileFd::None, true)) |
0d29b9d4 MV |
2794 | { |
2795 | _error->Errno("GetTempFile",_("Unable to write to %s"),fn); | |
2796 | return NULL; | |
2797 | } | |
0d29b9d4 MV |
2798 | return Fd; |
2799 | } | |
f8aba23f DK |
2800 | /*}}}*/ |
2801 | bool Rename(std::string From, std::string To) /*{{{*/ | |
c1409d1b MV |
2802 | { |
2803 | if (rename(From.c_str(),To.c_str()) != 0) | |
2804 | { | |
2805 | _error->Error(_("rename failed, %s (%s -> %s)."),strerror(errno), | |
2806 | From.c_str(),To.c_str()); | |
2807 | return false; | |
f8aba23f | 2808 | } |
c1409d1b MV |
2809 | return true; |
2810 | } | |
f8aba23f DK |
2811 | /*}}}*/ |
2812 | bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode)/*{{{*/ | |
7ad2a347 MV |
2813 | { |
2814 | int fd; | |
2815 | if (Mode != FileFd::ReadOnly && Mode != FileFd::WriteOnly) | |
2816 | return _error->Error("Popen supports ReadOnly (x)or WriteOnly mode only"); | |
2817 | ||
2818 | int Pipe[2] = {-1, -1}; | |
2819 | if(pipe(Pipe) != 0) | |
7ad2a347 | 2820 | return _error->Errno("pipe", _("Failed to create subprocess IPC")); |
5e49cbb7 | 2821 | |
7ad2a347 MV |
2822 | std::set<int> keep_fds; |
2823 | keep_fds.insert(Pipe[0]); | |
2824 | keep_fds.insert(Pipe[1]); | |
2825 | Child = ExecFork(keep_fds); | |
2826 | if(Child < 0) | |
2827 | return _error->Errno("fork", "Failed to fork"); | |
2828 | if(Child == 0) | |
2829 | { | |
2830 | if(Mode == FileFd::ReadOnly) | |
2831 | { | |
2832 | close(Pipe[0]); | |
2833 | fd = Pipe[1]; | |
2834 | } | |
2835 | else if(Mode == FileFd::WriteOnly) | |
2836 | { | |
2837 | close(Pipe[1]); | |
2838 | fd = Pipe[0]; | |
2839 | } | |
2840 | ||
2841 | if(Mode == FileFd::ReadOnly) | |
2842 | { | |
2843 | dup2(fd, 1); | |
2844 | dup2(fd, 2); | |
2845 | } else if(Mode == FileFd::WriteOnly) | |
2846 | dup2(fd, 0); | |
2847 | ||
2848 | execv(Args[0], (char**)Args); | |
2849 | _exit(100); | |
2850 | } | |
2851 | if(Mode == FileFd::ReadOnly) | |
2852 | { | |
2853 | close(Pipe[1]); | |
2854 | fd = Pipe[0]; | |
8f5b67ae DK |
2855 | } |
2856 | else if(Mode == FileFd::WriteOnly) | |
7ad2a347 MV |
2857 | { |
2858 | close(Pipe[0]); | |
2859 | fd = Pipe[1]; | |
2860 | } | |
8f5b67ae DK |
2861 | else |
2862 | return _error->Error("Popen supports ReadOnly (x)or WriteOnly mode only"); | |
7ad2a347 MV |
2863 | Fd.OpenDescriptor(fd, Mode, FileFd::None, true); |
2864 | ||
2865 | return true; | |
2866 | } | |
f8aba23f DK |
2867 | /*}}}*/ |
2868 | bool DropPrivileges() /*{{{*/ | |
fc1a78d8 | 2869 | { |
8f45798d DK |
2870 | if(_config->FindB("Debug::NoDropPrivs", false) == true) |
2871 | return true; | |
2872 | ||
2873 | #if __gnu_linux__ | |
2874 | #if defined(PR_SET_NO_NEW_PRIVS) && ( PR_SET_NO_NEW_PRIVS != 38 ) | |
2875 | #error "PR_SET_NO_NEW_PRIVS is defined, but with a different value than expected!" | |
2876 | #endif | |
2877 | // see prctl(2), needs linux3.5 at runtime - magic constant to avoid it at buildtime | |
2878 | int ret = prctl(38, 1, 0, 0, 0); | |
2879 | // ignore EINVAL - kernel is too old to understand the option | |
2880 | if(ret < 0 && errno != EINVAL) | |
2881 | _error->Warning("PR_SET_NO_NEW_PRIVS failed with %i", ret); | |
2882 | #endif | |
2883 | ||
990dd78a DK |
2884 | // empty setting disables privilege dropping - this also ensures |
2885 | // backward compatibility, see bug #764506 | |
2886 | const std::string toUser = _config->Find("APT::Sandbox::User"); | |
514a25cb | 2887 | if (toUser.empty() || toUser == "root") |
990dd78a DK |
2888 | return true; |
2889 | ||
ebca2f25 DK |
2890 | // a lot can go wrong trying to drop privileges completely, |
2891 | // so ideally we would like to verify that we have done it – | |
2892 | // but the verify asks for too much in case of fakeroot (and alike) | |
2893 | // [Specific checks can be overridden with dedicated options] | |
2894 | bool const VerifySandboxing = _config->FindB("APT::Sandbox::Verify", false); | |
2895 | ||
f1e3c8f0 | 2896 | // uid will be 0 in the end, but gid might be different anyway |
8f45798d DK |
2897 | uid_t const old_uid = getuid(); |
2898 | gid_t const old_gid = getgid(); | |
fc1a78d8 | 2899 | |
5f2047ec JAK |
2900 | if (old_uid != 0) |
2901 | return true; | |
3927c6da | 2902 | |
b8dae9a1 | 2903 | struct passwd *pw = getpwnam(toUser.c_str()); |
fc1a78d8 | 2904 | if (pw == NULL) |
b8dae9a1 | 2905 | return _error->Error("No user %s, can not drop rights", toUser.c_str()); |
3927c6da | 2906 | |
f1e3c8f0 | 2907 | // Do not change the order here, it might break things |
5a326439 | 2908 | // Get rid of all our supplementary groups first |
3b084f06 | 2909 | if (setgroups(1, &pw->pw_gid)) |
3927c6da MV |
2910 | return _error->Errno("setgroups", "Failed to setgroups"); |
2911 | ||
5a326439 JAK |
2912 | // Now change the group ids to the new user |
2913 | #ifdef HAVE_SETRESGID | |
2914 | if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) | |
2915 | return _error->Errno("setresgid", "Failed to set new group ids"); | |
2916 | #else | |
3927c6da | 2917 | if (setegid(pw->pw_gid) != 0) |
5f2047ec JAK |
2918 | return _error->Errno("setegid", "Failed to setegid"); |
2919 | ||
fc1a78d8 MV |
2920 | if (setgid(pw->pw_gid) != 0) |
2921 | return _error->Errno("setgid", "Failed to setgid"); | |
5a326439 | 2922 | #endif |
5f2047ec | 2923 | |
5a326439 JAK |
2924 | // Change the user ids to the new user |
2925 | #ifdef HAVE_SETRESUID | |
2926 | if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) | |
2927 | return _error->Errno("setresuid", "Failed to set new user ids"); | |
2928 | #else | |
fc1a78d8 MV |
2929 | if (setuid(pw->pw_uid) != 0) |
2930 | return _error->Errno("setuid", "Failed to setuid"); | |
5f2047ec JAK |
2931 | if (seteuid(pw->pw_uid) != 0) |
2932 | return _error->Errno("seteuid", "Failed to seteuid"); | |
5a326439 | 2933 | #endif |
5f2047ec | 2934 | |
ebca2f25 DK |
2935 | // disabled by default as fakeroot doesn't implement getgroups currently (#806521) |
2936 | if (VerifySandboxing == true || _config->FindB("APT::Sandbox::Verify::Groups", false) == true) | |
2937 | { | |
2938 | // Verify that the user isn't still in any supplementary groups | |
2939 | long const ngroups_max = sysconf(_SC_NGROUPS_MAX); | |
2940 | std::unique_ptr<gid_t[]> gidlist(new gid_t[ngroups_max]); | |
2941 | if (unlikely(gidlist == NULL)) | |
2942 | return _error->Error("Allocation of a list of size %lu for getgroups failed", ngroups_max); | |
2943 | ssize_t gidlist_nr; | |
2944 | if ((gidlist_nr = getgroups(ngroups_max, gidlist.get())) < 0) | |
2945 | return _error->Errno("getgroups", "Could not get new groups (%lu)", ngroups_max); | |
2946 | for (ssize_t i = 0; i < gidlist_nr; ++i) | |
2947 | if (gidlist[i] != pw->pw_gid) | |
2948 | return _error->Error("Could not switch group, user %s is still in group %d", toUser.c_str(), gidlist[i]); | |
2949 | } | |
2950 | ||
2951 | // enabled by default as all fakeroot-lookalikes should fake that accordingly | |
2952 | if (VerifySandboxing == true || _config->FindB("APT::Sandbox::Verify::IDs", true) == true) | |
2953 | { | |
2954 | // Verify that gid, egid, uid, and euid changed | |
2955 | if (getgid() != pw->pw_gid) | |
2956 | return _error->Error("Could not switch group"); | |
2957 | if (getegid() != pw->pw_gid) | |
2958 | return _error->Error("Could not switch effective group"); | |
2959 | if (getuid() != pw->pw_uid) | |
2960 | return _error->Error("Could not switch user"); | |
2961 | if (geteuid() != pw->pw_uid) | |
2962 | return _error->Error("Could not switch effective user"); | |
5f2047ec | 2963 | |
550ab420 | 2964 | #ifdef HAVE_GETRESUID |
ebca2f25 DK |
2965 | // verify that the saved set-user-id was changed as well |
2966 | uid_t ruid = 0; | |
2967 | uid_t euid = 0; | |
2968 | uid_t suid = 0; | |
2969 | if (getresuid(&ruid, &euid, &suid)) | |
2970 | return _error->Errno("getresuid", "Could not get saved set-user-ID"); | |
2971 | if (suid != pw->pw_uid) | |
2972 | return _error->Error("Could not switch saved set-user-ID"); | |
550ab420 JAK |
2973 | #endif |
2974 | ||
2975 | #ifdef HAVE_GETRESGID | |
ebca2f25 DK |
2976 | // verify that the saved set-group-id was changed as well |
2977 | gid_t rgid = 0; | |
2978 | gid_t egid = 0; | |
2979 | gid_t sgid = 0; | |
2980 | if (getresgid(&rgid, &egid, &sgid)) | |
2981 | return _error->Errno("getresuid", "Could not get saved set-group-ID"); | |
2982 | if (sgid != pw->pw_gid) | |
2983 | return _error->Error("Could not switch saved set-group-ID"); | |
550ab420 | 2984 | #endif |
ebca2f25 | 2985 | } |
550ab420 | 2986 | |
ebca2f25 DK |
2987 | // disabled as fakeroot doesn't forbid (by design) (re)gaining root from unprivileged |
2988 | if (VerifySandboxing == true || _config->FindB("APT::Sandbox::Verify::Regain", false) == true) | |
2989 | { | |
2990 | // Check that uid and gid changes do not work anymore | |
2991 | if (pw->pw_gid != old_gid && (setgid(old_gid) != -1 || setegid(old_gid) != -1)) | |
2992 | return _error->Error("Could restore a gid to root, privilege dropping did not work"); | |
bdc00df5 | 2993 | |
ebca2f25 DK |
2994 | if (pw->pw_uid != old_uid && (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) |
2995 | return _error->Error("Could restore a uid to root, privilege dropping did not work"); | |
2996 | } | |
bdc00df5 | 2997 | |
fc1a78d8 MV |
2998 | return true; |
2999 | } | |
f8aba23f | 3000 | /*}}}*/ |