]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
7da2b375 | 3 | // $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | File Utilities | |
7 | ||
8 | CopyFile - Buffered copy of a single file | |
9 | GetLock - dpkg compatible lock file manipulation (fcntl) | |
10 | ||
614adaa0 MV |
11 | Most of this source is placed in the Public Domain, do with it what |
12 | you will | |
7da2b375 | 13 | It was originally written by Jason Gunthorpe <jgg@debian.org>. |
a3a03f5d | 14 | FileFd gzip support added by Martin Pitt <martin.pitt@canonical.com> |
578bfd0a | 15 | |
614adaa0 MV |
16 | The exception is RunScripts() it is under the GPLv2 |
17 | ||
578bfd0a AL |
18 | ##################################################################### */ |
19 | /*}}}*/ | |
20 | // Include Files /*{{{*/ | |
ea542140 DK |
21 | #include <config.h> |
22 | ||
094a497d | 23 | #include <apt-pkg/fileutl.h> |
1cd1c398 | 24 | #include <apt-pkg/strutl.h> |
094a497d | 25 | #include <apt-pkg/error.h> |
b2e465d6 | 26 | #include <apt-pkg/sptr.h> |
468720c5 | 27 | #include <apt-pkg/aptconfiguration.h> |
75ef8f14 | 28 | #include <apt-pkg/configuration.h> |
b2e465d6 | 29 | |
152ab79e | 30 | #include <cstdlib> |
4f333a8b | 31 | #include <cstring> |
3010fb0e | 32 | #include <cstdio> |
4f333a8b | 33 | |
4d055c05 | 34 | #include <iostream> |
578bfd0a | 35 | #include <unistd.h> |
2c206aa4 | 36 | #include <fcntl.h> |
578bfd0a | 37 | #include <sys/stat.h> |
578bfd0a | 38 | #include <sys/types.h> |
cc2313b7 | 39 | #include <sys/time.h> |
1ae93c94 | 40 | #include <sys/wait.h> |
46e39c8e | 41 | #include <dirent.h> |
54676e1a | 42 | #include <signal.h> |
65a1e968 | 43 | #include <errno.h> |
75ef8f14 | 44 | #include <set> |
46e39c8e | 45 | #include <algorithm> |
2cae0ccb | 46 | |
7efb8c8e DK |
47 | #ifdef HAVE_ZLIB |
48 | #include <zlib.h> | |
699b209e | 49 | #endif |
c4997486 DK |
50 | #ifdef HAVE_BZ2 |
51 | #include <bzlib.h> | |
52 | #endif | |
032bd56f | 53 | |
2a79d5b5 | 54 | #ifdef WORDS_BIGENDIAN |
2cae0ccb DK |
55 | #include <inttypes.h> |
56 | #endif | |
ea542140 DK |
57 | |
58 | #include <apti18n.h> | |
578bfd0a AL |
59 | /*}}}*/ |
60 | ||
4d055c05 AL |
61 | using namespace std; |
62 | ||
032bd56f DK |
63 | class FileFdPrivate { |
64 | public: | |
7efb8c8e | 65 | #ifdef HAVE_ZLIB |
032bd56f | 66 | gzFile gz; |
699b209e DK |
67 | #else |
68 | void* gz; | |
c4997486 DK |
69 | #endif |
70 | #ifdef HAVE_BZ2 | |
71 | BZFILE* bz2; | |
72 | #else | |
73 | void* bz2; | |
699b209e | 74 | #endif |
561f860a | 75 | int compressed_fd; |
699b209e DK |
76 | pid_t compressor_pid; |
77 | bool pipe; | |
78 | APT::Configuration::Compressor compressor; | |
52b47296 | 79 | unsigned int openmode; |
1abbc47c | 80 | unsigned long long seekpos; |
c4997486 DK |
81 | FileFdPrivate() : gz(NULL), bz2(NULL), |
82 | compressed_fd(-1), compressor_pid(-1), pipe(false), | |
1abbc47c | 83 | openmode(0), seekpos(0) {}; |
032bd56f DK |
84 | }; |
85 | ||
614adaa0 MV |
86 | // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ |
87 | // --------------------------------------------------------------------- | |
88 | /* */ | |
89 | bool RunScripts(const char *Cnf) | |
90 | { | |
91 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
92 | if (Opts == 0 || Opts->Child == 0) | |
93 | return true; | |
94 | Opts = Opts->Child; | |
95 | ||
96 | // Fork for running the system calls | |
97 | pid_t Child = ExecFork(); | |
98 | ||
99 | // This is the child | |
100 | if (Child == 0) | |
101 | { | |
cfba4f69 MV |
102 | if (_config->FindDir("DPkg::Chroot-Directory","/") != "/") |
103 | { | |
104 | std::cerr << "Chrooting into " | |
105 | << _config->FindDir("DPkg::Chroot-Directory") | |
106 | << std::endl; | |
107 | if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0) | |
108 | _exit(100); | |
109 | } | |
110 | ||
614adaa0 MV |
111 | if (chdir("/tmp/") != 0) |
112 | _exit(100); | |
113 | ||
114 | unsigned int Count = 1; | |
115 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
116 | { | |
117 | if (Opts->Value.empty() == true) | |
118 | continue; | |
119 | ||
120 | if (system(Opts->Value.c_str()) != 0) | |
121 | _exit(100+Count); | |
122 | } | |
123 | _exit(0); | |
124 | } | |
125 | ||
126 | // Wait for the child | |
127 | int Status = 0; | |
128 | while (waitpid(Child,&Status,0) != Child) | |
129 | { | |
130 | if (errno == EINTR) | |
131 | continue; | |
132 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
133 | } | |
134 | ||
135 | // Restore sig int/quit | |
136 | signal(SIGQUIT,SIG_DFL); | |
137 | signal(SIGINT,SIG_DFL); | |
138 | ||
139 | // Check for an error code. | |
140 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
141 | { | |
142 | unsigned int Count = WEXITSTATUS(Status); | |
143 | if (Count > 100) | |
144 | { | |
145 | Count -= 100; | |
146 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
147 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); | |
148 | } | |
149 | ||
150 | return _error->Error("Sub-process returned an error code"); | |
151 | } | |
152 | ||
153 | return true; | |
154 | } | |
155 | /*}}}*/ | |
156 | ||
578bfd0a AL |
157 | // CopyFile - Buffered copy of a file /*{{{*/ |
158 | // --------------------------------------------------------------------- | |
159 | /* The caller is expected to set things so that failure causes erasure */ | |
8b89e57f | 160 | bool CopyFile(FileFd &From,FileFd &To) |
578bfd0a AL |
161 | { |
162 | if (From.IsOpen() == false || To.IsOpen() == false) | |
163 | return false; | |
164 | ||
165 | // Buffered copy between fds | |
b2e465d6 | 166 | SPtrArray<unsigned char> Buf = new unsigned char[64000]; |
650faab0 | 167 | unsigned long long Size = From.Size(); |
b0db36b1 | 168 | while (Size != 0) |
578bfd0a | 169 | { |
650faab0 | 170 | unsigned long long ToRead = Size; |
b0db36b1 AL |
171 | if (Size > 64000) |
172 | ToRead = 64000; | |
173 | ||
4a6d5862 | 174 | if (From.Read(Buf,ToRead) == false || |
b0db36b1 | 175 | To.Write(Buf,ToRead) == false) |
578bfd0a | 176 | return false; |
b0db36b1 AL |
177 | |
178 | Size -= ToRead; | |
578bfd0a AL |
179 | } |
180 | ||
578bfd0a AL |
181 | return true; |
182 | } | |
183 | /*}}}*/ | |
184 | // GetLock - Gets a lock file /*{{{*/ | |
185 | // --------------------------------------------------------------------- | |
186 | /* This will create an empty file of the given name and lock it. Once this | |
187 | is done all other calls to GetLock in any other process will fail with | |
188 | -1. The return result is the fd of the file, the call should call | |
189 | close at some time. */ | |
190 | int GetLock(string File,bool Errors) | |
191 | { | |
f659b39a OS |
192 | // GetLock() is used in aptitude on directories with public-write access |
193 | // Use O_NOFOLLOW here to prevent symlink traversal attacks | |
194 | int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640); | |
578bfd0a AL |
195 | if (FD < 0) |
196 | { | |
b2e465d6 AL |
197 | // Read only .. cant have locking problems there. |
198 | if (errno == EROFS) | |
199 | { | |
200 | _error->Warning(_("Not using locking for read only lock file %s"),File.c_str()); | |
201 | return dup(0); // Need something for the caller to close | |
202 | } | |
203 | ||
578bfd0a | 204 | if (Errors == true) |
b2e465d6 AL |
205 | _error->Errno("open",_("Could not open lock file %s"),File.c_str()); |
206 | ||
207 | // Feh.. We do this to distinguish the lock vs open case.. | |
208 | errno = EPERM; | |
578bfd0a AL |
209 | return -1; |
210 | } | |
b2e465d6 AL |
211 | SetCloseExec(FD,true); |
212 | ||
578bfd0a AL |
213 | // Aquire a write lock |
214 | struct flock fl; | |
c71bc556 AL |
215 | fl.l_type = F_WRLCK; |
216 | fl.l_whence = SEEK_SET; | |
217 | fl.l_start = 0; | |
218 | fl.l_len = 0; | |
578bfd0a AL |
219 | if (fcntl(FD,F_SETLK,&fl) == -1) |
220 | { | |
d89df07a AL |
221 | if (errno == ENOLCK) |
222 | { | |
b2e465d6 AL |
223 | _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str()); |
224 | return dup(0); // Need something for the caller to close | |
d89df07a | 225 | } |
578bfd0a | 226 | if (Errors == true) |
b2e465d6 AL |
227 | _error->Errno("open",_("Could not get lock %s"),File.c_str()); |
228 | ||
229 | int Tmp = errno; | |
578bfd0a | 230 | close(FD); |
b2e465d6 | 231 | errno = Tmp; |
578bfd0a AL |
232 | return -1; |
233 | } | |
234 | ||
235 | return FD; | |
236 | } | |
237 | /*}}}*/ | |
238 | // FileExists - Check if a file exists /*{{{*/ | |
239 | // --------------------------------------------------------------------- | |
36f1098a | 240 | /* Beware: Directories are also files! */ |
578bfd0a AL |
241 | bool FileExists(string File) |
242 | { | |
243 | struct stat Buf; | |
244 | if (stat(File.c_str(),&Buf) != 0) | |
245 | return false; | |
246 | return true; | |
247 | } | |
248 | /*}}}*/ | |
36f1098a DK |
249 | // RealFileExists - Check if a file exists and if it is really a file /*{{{*/ |
250 | // --------------------------------------------------------------------- | |
251 | /* */ | |
252 | bool RealFileExists(string File) | |
253 | { | |
254 | struct stat Buf; | |
255 | if (stat(File.c_str(),&Buf) != 0) | |
256 | return false; | |
257 | return ((Buf.st_mode & S_IFREG) != 0); | |
258 | } | |
259 | /*}}}*/ | |
1cd1c398 DK |
260 | // DirectoryExists - Check if a directory exists and is really one /*{{{*/ |
261 | // --------------------------------------------------------------------- | |
262 | /* */ | |
263 | bool DirectoryExists(string const &Path) | |
264 | { | |
265 | struct stat Buf; | |
266 | if (stat(Path.c_str(),&Buf) != 0) | |
267 | return false; | |
268 | return ((Buf.st_mode & S_IFDIR) != 0); | |
269 | } | |
270 | /*}}}*/ | |
271 | // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/ | |
272 | // --------------------------------------------------------------------- | |
273 | /* This method will create all directories needed for path in good old | |
274 | mkdir -p style but refuses to do this if Parent is not a prefix of | |
275 | this Path. Example: /var/cache/ and /var/cache/apt/archives are given, | |
276 | so it will create apt/archives if /var/cache exists - on the other | |
277 | hand if the parent is /var/lib the creation will fail as this path | |
278 | is not a parent of the path to be generated. */ | |
279 | bool CreateDirectory(string const &Parent, string const &Path) | |
280 | { | |
281 | if (Parent.empty() == true || Path.empty() == true) | |
282 | return false; | |
283 | ||
284 | if (DirectoryExists(Path) == true) | |
285 | return true; | |
286 | ||
287 | if (DirectoryExists(Parent) == false) | |
288 | return false; | |
289 | ||
290 | // we are not going to create directories "into the blue" | |
291 | if (Path.find(Parent, 0) != 0) | |
292 | return false; | |
293 | ||
294 | vector<string> const dirs = VectorizeString(Path.substr(Parent.size()), '/'); | |
295 | string progress = Parent; | |
296 | for (vector<string>::const_iterator d = dirs.begin(); d != dirs.end(); ++d) | |
297 | { | |
298 | if (d->empty() == true) | |
299 | continue; | |
300 | ||
301 | progress.append("/").append(*d); | |
302 | if (DirectoryExists(progress) == true) | |
303 | continue; | |
304 | ||
305 | if (mkdir(progress.c_str(), 0755) != 0) | |
306 | return false; | |
307 | } | |
308 | return true; | |
309 | } | |
310 | /*}}}*/ | |
7753e468 | 311 | // CreateAPTDirectoryIfNeeded - ensure that the given directory exists /*{{{*/ |
b29c3712 DK |
312 | // --------------------------------------------------------------------- |
313 | /* a small wrapper around CreateDirectory to check if it exists and to | |
314 | remove the trailing "/apt/" from the parent directory if needed */ | |
7753e468 | 315 | bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path) |
b29c3712 DK |
316 | { |
317 | if (DirectoryExists(Path) == true) | |
318 | return true; | |
319 | ||
320 | size_t const len = Parent.size(); | |
321 | if (len > 5 && Parent.find("/apt/", len - 6, 5) == len - 5) | |
322 | { | |
323 | if (CreateDirectory(Parent.substr(0,len-5), Path) == true) | |
324 | return true; | |
325 | } | |
326 | else if (CreateDirectory(Parent, Path) == true) | |
327 | return true; | |
328 | ||
329 | return false; | |
330 | } | |
331 | /*}}}*/ | |
46e39c8e MV |
332 | // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/ |
333 | // --------------------------------------------------------------------- | |
334 | /* If an extension is given only files with this extension are included | |
335 | in the returned vector, otherwise every "normal" file is included. */ | |
b39c1859 MV |
336 | std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, |
337 | bool const &SortList, bool const &AllowNoExt) | |
338 | { | |
339 | std::vector<string> ext; | |
340 | ext.reserve(2); | |
341 | if (Ext.empty() == false) | |
342 | ext.push_back(Ext); | |
343 | if (AllowNoExt == true && ext.empty() == false) | |
344 | ext.push_back(""); | |
345 | return GetListOfFilesInDir(Dir, ext, SortList); | |
346 | } | |
347 | std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext, | |
348 | bool const &SortList) | |
349 | { | |
350 | // Attention debuggers: need to be set with the environment config file! | |
351 | bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false); | |
352 | if (Debug == true) | |
353 | { | |
354 | std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl; | |
355 | if (Ext.empty() == true) | |
356 | std::clog << "\tNO extension" << std::endl; | |
357 | else | |
358 | for (std::vector<string>::const_iterator e = Ext.begin(); | |
359 | e != Ext.end(); ++e) | |
360 | std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl; | |
361 | } | |
362 | ||
46e39c8e | 363 | std::vector<string> List; |
36f1098a DK |
364 | |
365 | if (DirectoryExists(Dir.c_str()) == false) | |
366 | { | |
367 | _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str()); | |
368 | return List; | |
369 | } | |
370 | ||
1408e219 | 371 | Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently"); |
46e39c8e MV |
372 | DIR *D = opendir(Dir.c_str()); |
373 | if (D == 0) | |
374 | { | |
375 | _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
376 | return List; | |
377 | } | |
378 | ||
379 | for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) | |
380 | { | |
b39c1859 | 381 | // skip "hidden" files |
46e39c8e MV |
382 | if (Ent->d_name[0] == '.') |
383 | continue; | |
384 | ||
491058e3 DK |
385 | // Make sure it is a file and not something else |
386 | string const File = flCombine(Dir,Ent->d_name); | |
387 | #ifdef _DIRENT_HAVE_D_TYPE | |
388 | if (Ent->d_type != DT_REG) | |
389 | #endif | |
390 | { | |
391 | if (RealFileExists(File.c_str()) == false) | |
392 | { | |
84e254d6 DK |
393 | // do not show ignoration warnings for directories |
394 | if ( | |
395 | #ifdef _DIRENT_HAVE_D_TYPE | |
396 | Ent->d_type == DT_DIR || | |
397 | #endif | |
398 | DirectoryExists(File.c_str()) == true) | |
399 | continue; | |
491058e3 DK |
400 | if (SilentIgnore.Match(Ent->d_name) == false) |
401 | _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str()); | |
402 | continue; | |
403 | } | |
404 | } | |
405 | ||
b39c1859 MV |
406 | // check for accepted extension: |
407 | // no extension given -> periods are bad as hell! | |
408 | // extensions given -> "" extension allows no extension | |
409 | if (Ext.empty() == false) | |
410 | { | |
411 | string d_ext = flExtension(Ent->d_name); | |
412 | if (d_ext == Ent->d_name) // no extension | |
413 | { | |
414 | if (std::find(Ext.begin(), Ext.end(), "") == Ext.end()) | |
415 | { | |
416 | if (Debug == true) | |
417 | std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl; | |
5edc3966 | 418 | if (SilentIgnore.Match(Ent->d_name) == false) |
491058e3 | 419 | _error->Notice(_("Ignoring file '%s' in directory '%s' as it has no filename extension"), Ent->d_name, Dir.c_str()); |
b39c1859 MV |
420 | continue; |
421 | } | |
422 | } | |
423 | else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end()) | |
424 | { | |
425 | if (Debug == true) | |
426 | std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl; | |
1408e219 | 427 | if (SilentIgnore.Match(Ent->d_name) == false) |
491058e3 | 428 | _error->Notice(_("Ignoring file '%s' in directory '%s' as it has an invalid filename extension"), Ent->d_name, Dir.c_str()); |
b39c1859 MV |
429 | continue; |
430 | } | |
431 | } | |
46e39c8e | 432 | |
b39c1859 | 433 | // Skip bad filenames ala run-parts |
46e39c8e MV |
434 | const char *C = Ent->d_name; |
435 | for (; *C != 0; ++C) | |
436 | if (isalpha(*C) == 0 && isdigit(*C) == 0 | |
b39c1859 MV |
437 | && *C != '_' && *C != '-') { |
438 | // no required extension -> dot is a bad character | |
439 | if (*C == '.' && Ext.empty() == false) | |
440 | continue; | |
46e39c8e | 441 | break; |
b39c1859 | 442 | } |
46e39c8e | 443 | |
b39c1859 | 444 | // we don't reach the end of the name -> bad character included |
46e39c8e | 445 | if (*C != 0) |
b39c1859 MV |
446 | { |
447 | if (Debug == true) | |
448 | std::clog << "Bad file: " << Ent->d_name << " → bad character »" | |
449 | << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl; | |
450 | continue; | |
451 | } | |
452 | ||
fbb2c7e0 DK |
453 | // skip filenames which end with a period. These are never valid |
454 | if (*(C - 1) == '.') | |
455 | { | |
456 | if (Debug == true) | |
457 | std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl; | |
458 | continue; | |
459 | } | |
460 | ||
461 | if (Debug == true) | |
462 | std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl; | |
463 | List.push_back(File); | |
464 | } | |
465 | closedir(D); | |
466 | ||
467 | if (SortList == true) | |
468 | std::sort(List.begin(),List.end()); | |
469 | return List; | |
470 | } | |
471 | std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList) | |
472 | { | |
473 | bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false); | |
474 | if (Debug == true) | |
475 | std::clog << "Accept in " << Dir << " all regular files" << std::endl; | |
476 | ||
477 | std::vector<string> List; | |
478 | ||
479 | if (DirectoryExists(Dir.c_str()) == false) | |
480 | { | |
481 | _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str()); | |
482 | return List; | |
483 | } | |
484 | ||
485 | DIR *D = opendir(Dir.c_str()); | |
486 | if (D == 0) | |
487 | { | |
488 | _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); | |
489 | return List; | |
490 | } | |
491 | ||
492 | for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) | |
493 | { | |
494 | // skip "hidden" files | |
495 | if (Ent->d_name[0] == '.') | |
496 | continue; | |
497 | ||
498 | // Make sure it is a file and not something else | |
499 | string const File = flCombine(Dir,Ent->d_name); | |
500 | #ifdef _DIRENT_HAVE_D_TYPE | |
501 | if (Ent->d_type != DT_REG) | |
502 | #endif | |
503 | { | |
504 | if (RealFileExists(File.c_str()) == false) | |
505 | { | |
506 | if (Debug == true) | |
507 | std::clog << "Bad file: " << Ent->d_name << " → it is not a real file" << std::endl; | |
508 | continue; | |
509 | } | |
510 | } | |
511 | ||
512 | // Skip bad filenames ala run-parts | |
513 | const char *C = Ent->d_name; | |
514 | for (; *C != 0; ++C) | |
515 | if (isalpha(*C) == 0 && isdigit(*C) == 0 | |
516 | && *C != '_' && *C != '-' && *C != '.') | |
517 | break; | |
518 | ||
519 | // we don't reach the end of the name -> bad character included | |
520 | if (*C != 0) | |
521 | { | |
522 | if (Debug == true) | |
523 | std::clog << "Bad file: " << Ent->d_name << " → bad character »" << *C << "« in filename" << std::endl; | |
524 | continue; | |
525 | } | |
526 | ||
b39c1859 MV |
527 | // skip filenames which end with a period. These are never valid |
528 | if (*(C - 1) == '.') | |
529 | { | |
530 | if (Debug == true) | |
531 | std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl; | |
46e39c8e | 532 | continue; |
b39c1859 | 533 | } |
46e39c8e | 534 | |
b39c1859 MV |
535 | if (Debug == true) |
536 | std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl; | |
46e39c8e MV |
537 | List.push_back(File); |
538 | } | |
539 | closedir(D); | |
540 | ||
541 | if (SortList == true) | |
542 | std::sort(List.begin(),List.end()); | |
543 | return List; | |
544 | } | |
545 | /*}}}*/ | |
578bfd0a AL |
546 | // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/ |
547 | // --------------------------------------------------------------------- | |
548 | /* We return / on failure. */ | |
549 | string SafeGetCWD() | |
550 | { | |
551 | // Stash the current dir. | |
552 | char S[300]; | |
553 | S[0] = 0; | |
7f25bdff | 554 | if (getcwd(S,sizeof(S)-2) == 0) |
578bfd0a | 555 | return "/"; |
7f25bdff AL |
556 | unsigned int Len = strlen(S); |
557 | S[Len] = '/'; | |
558 | S[Len+1] = 0; | |
578bfd0a AL |
559 | return S; |
560 | } | |
561 | /*}}}*/ | |
2ec858bc MV |
562 | // GetModificationTime - Get the mtime of the given file or -1 on error /*{{{*/ |
563 | // --------------------------------------------------------------------- | |
564 | /* We return / on failure. */ | |
565 | time_t GetModificationTime(string const &Path) | |
566 | { | |
567 | struct stat St; | |
568 | if (stat(Path.c_str(), &St) < 0) | |
569 | return -1; | |
570 | return St.st_mtime; | |
571 | } | |
572 | /*}}}*/ | |
8ce4327b AL |
573 | // flNotDir - Strip the directory from the filename /*{{{*/ |
574 | // --------------------------------------------------------------------- | |
575 | /* */ | |
576 | string flNotDir(string File) | |
577 | { | |
578 | string::size_type Res = File.rfind('/'); | |
579 | if (Res == string::npos) | |
580 | return File; | |
581 | Res++; | |
582 | return string(File,Res,Res - File.length()); | |
583 | } | |
584 | /*}}}*/ | |
d38b7b3d AL |
585 | // flNotFile - Strip the file from the directory name /*{{{*/ |
586 | // --------------------------------------------------------------------- | |
171c45bc | 587 | /* Result ends in a / */ |
d38b7b3d AL |
588 | string flNotFile(string File) |
589 | { | |
590 | string::size_type Res = File.rfind('/'); | |
591 | if (Res == string::npos) | |
171c45bc | 592 | return "./"; |
d38b7b3d AL |
593 | Res++; |
594 | return string(File,0,Res); | |
595 | } | |
596 | /*}}}*/ | |
b2e465d6 AL |
597 | // flExtension - Return the extension for the file /*{{{*/ |
598 | // --------------------------------------------------------------------- | |
599 | /* */ | |
600 | string flExtension(string File) | |
601 | { | |
602 | string::size_type Res = File.rfind('.'); | |
603 | if (Res == string::npos) | |
604 | return File; | |
605 | Res++; | |
606 | return string(File,Res,Res - File.length()); | |
607 | } | |
608 | /*}}}*/ | |
421c8d10 AL |
609 | // flNoLink - If file is a symlink then deref it /*{{{*/ |
610 | // --------------------------------------------------------------------- | |
611 | /* If the name is not a link then the returned path is the input. */ | |
612 | string flNoLink(string File) | |
613 | { | |
614 | struct stat St; | |
615 | if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0) | |
616 | return File; | |
617 | if (stat(File.c_str(),&St) != 0) | |
618 | return File; | |
619 | ||
620 | /* Loop resolving the link. There is no need to limit the number of | |
621 | loops because the stat call above ensures that the symlink is not | |
622 | circular */ | |
623 | char Buffer[1024]; | |
624 | string NFile = File; | |
625 | while (1) | |
626 | { | |
627 | // Read the link | |
628 | int Res; | |
629 | if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 || | |
630 | (unsigned)Res >= sizeof(Buffer)) | |
631 | return File; | |
632 | ||
633 | // Append or replace the previous path | |
634 | Buffer[Res] = 0; | |
635 | if (Buffer[0] == '/') | |
636 | NFile = Buffer; | |
637 | else | |
638 | NFile = flNotFile(NFile) + Buffer; | |
639 | ||
640 | // See if we are done | |
641 | if (lstat(NFile.c_str(),&St) != 0) | |
642 | return File; | |
643 | if (S_ISLNK(St.st_mode) == 0) | |
644 | return NFile; | |
645 | } | |
646 | } | |
647 | /*}}}*/ | |
b2e465d6 AL |
648 | // flCombine - Combine a file and a directory /*{{{*/ |
649 | // --------------------------------------------------------------------- | |
650 | /* If the file is an absolute path then it is just returned, otherwise | |
651 | the directory is pre-pended to it. */ | |
652 | string flCombine(string Dir,string File) | |
653 | { | |
654 | if (File.empty() == true) | |
655 | return string(); | |
656 | ||
657 | if (File[0] == '/' || Dir.empty() == true) | |
658 | return File; | |
659 | if (File.length() >= 2 && File[0] == '.' && File[1] == '/') | |
660 | return File; | |
661 | if (Dir[Dir.length()-1] == '/') | |
662 | return Dir + File; | |
663 | return Dir + '/' + File; | |
664 | } | |
665 | /*}}}*/ | |
3b5421b4 AL |
666 | // SetCloseExec - Set the close on exec flag /*{{{*/ |
667 | // --------------------------------------------------------------------- | |
668 | /* */ | |
669 | void SetCloseExec(int Fd,bool Close) | |
670 | { | |
671 | if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0) | |
672 | { | |
673 | cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl; | |
674 | exit(100); | |
675 | } | |
676 | } | |
677 | /*}}}*/ | |
678 | // SetNonBlock - Set the nonblocking flag /*{{{*/ | |
679 | // --------------------------------------------------------------------- | |
680 | /* */ | |
681 | void SetNonBlock(int Fd,bool Block) | |
682 | { | |
0a8a80e5 AL |
683 | int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK); |
684 | if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0) | |
3b5421b4 AL |
685 | { |
686 | cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl; | |
687 | exit(100); | |
688 | } | |
689 | } | |
690 | /*}}}*/ | |
691 | // WaitFd - Wait for a FD to become readable /*{{{*/ | |
692 | // --------------------------------------------------------------------- | |
b2e465d6 | 693 | /* This waits for a FD to become readable using select. It is useful for |
6d5dd02a AL |
694 | applications making use of non-blocking sockets. The timeout is |
695 | in seconds. */ | |
1084d58a | 696 | bool WaitFd(int Fd,bool write,unsigned long timeout) |
3b5421b4 AL |
697 | { |
698 | fd_set Set; | |
cc2313b7 | 699 | struct timeval tv; |
3b5421b4 AL |
700 | FD_ZERO(&Set); |
701 | FD_SET(Fd,&Set); | |
6d5dd02a AL |
702 | tv.tv_sec = timeout; |
703 | tv.tv_usec = 0; | |
1084d58a | 704 | if (write == true) |
b0db36b1 AL |
705 | { |
706 | int Res; | |
707 | do | |
708 | { | |
709 | Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0)); | |
710 | } | |
711 | while (Res < 0 && errno == EINTR); | |
712 | ||
713 | if (Res <= 0) | |
714 | return false; | |
1084d58a AL |
715 | } |
716 | else | |
717 | { | |
b0db36b1 AL |
718 | int Res; |
719 | do | |
720 | { | |
721 | Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0)); | |
722 | } | |
723 | while (Res < 0 && errno == EINTR); | |
724 | ||
725 | if (Res <= 0) | |
726 | return false; | |
cc2313b7 | 727 | } |
1084d58a | 728 | |
3b5421b4 AL |
729 | return true; |
730 | } | |
731 | /*}}}*/ | |
54676e1a AL |
732 | // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/ |
733 | // --------------------------------------------------------------------- | |
734 | /* This is used if you want to cleanse the environment for the forked | |
735 | child, it fixes up the important signals and nukes all of the fds, | |
736 | otherwise acts like normal fork. */ | |
75ef8f14 | 737 | pid_t ExecFork() |
54676e1a AL |
738 | { |
739 | // Fork off the process | |
740 | pid_t Process = fork(); | |
741 | if (Process < 0) | |
742 | { | |
743 | cerr << "FATAL -> Failed to fork." << endl; | |
744 | exit(100); | |
745 | } | |
746 | ||
747 | // Spawn the subprocess | |
748 | if (Process == 0) | |
749 | { | |
750 | // Setup the signals | |
751 | signal(SIGPIPE,SIG_DFL); | |
752 | signal(SIGQUIT,SIG_DFL); | |
753 | signal(SIGINT,SIG_DFL); | |
754 | signal(SIGWINCH,SIG_DFL); | |
755 | signal(SIGCONT,SIG_DFL); | |
756 | signal(SIGTSTP,SIG_DFL); | |
75ef8f14 MV |
757 | |
758 | set<int> KeepFDs; | |
759 | Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds"); | |
760 | if (Opts != 0 && Opts->Child != 0) | |
761 | { | |
762 | Opts = Opts->Child; | |
763 | for (; Opts != 0; Opts = Opts->Next) | |
764 | { | |
765 | if (Opts->Value.empty() == true) | |
766 | continue; | |
767 | int fd = atoi(Opts->Value.c_str()); | |
768 | KeepFDs.insert(fd); | |
769 | } | |
770 | } | |
771 | ||
54676e1a AL |
772 | // Close all of our FDs - just in case |
773 | for (int K = 3; K != 40; K++) | |
75ef8f14 MV |
774 | { |
775 | if(KeepFDs.find(K) == KeepFDs.end()) | |
007dc9e0 | 776 | fcntl(K,F_SETFD,FD_CLOEXEC); |
75ef8f14 | 777 | } |
54676e1a AL |
778 | } |
779 | ||
780 | return Process; | |
781 | } | |
782 | /*}}}*/ | |
ddc1d8d0 AL |
783 | // ExecWait - Fancy waitpid /*{{{*/ |
784 | // --------------------------------------------------------------------- | |
2c9a72d1 | 785 | /* Waits for the given sub process. If Reap is set then no errors are |
ddc1d8d0 AL |
786 | generated. Otherwise a failed subprocess will generate a proper descriptive |
787 | message */ | |
3826564e | 788 | bool ExecWait(pid_t Pid,const char *Name,bool Reap) |
ddc1d8d0 AL |
789 | { |
790 | if (Pid <= 1) | |
791 | return true; | |
792 | ||
793 | // Wait and collect the error code | |
794 | int Status; | |
795 | while (waitpid(Pid,&Status,0) != Pid) | |
796 | { | |
797 | if (errno == EINTR) | |
798 | continue; | |
799 | ||
800 | if (Reap == true) | |
801 | return false; | |
802 | ||
db0db9fe | 803 | return _error->Error(_("Waited for %s but it wasn't there"),Name); |
ddc1d8d0 AL |
804 | } |
805 | ||
806 | ||
807 | // Check for an error code. | |
808 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
809 | { | |
810 | if (Reap == true) | |
811 | return false; | |
ab7f4d7c | 812 | if (WIFSIGNALED(Status) != 0) |
40e7fe0e | 813 | { |
ab7f4d7c MV |
814 | if( WTERMSIG(Status) == SIGSEGV) |
815 | return _error->Error(_("Sub-process %s received a segmentation fault."),Name); | |
816 | else | |
817 | return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status)); | |
40e7fe0e | 818 | } |
ddc1d8d0 AL |
819 | |
820 | if (WIFEXITED(Status) != 0) | |
b2e465d6 | 821 | return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status)); |
ddc1d8d0 | 822 | |
b2e465d6 | 823 | return _error->Error(_("Sub-process %s exited unexpectedly"),Name); |
ddc1d8d0 AL |
824 | } |
825 | ||
826 | return true; | |
827 | } | |
828 | /*}}}*/ | |
578bfd0a | 829 | |
13d87e2e | 830 | // FileFd::Open - Open a file /*{{{*/ |
578bfd0a AL |
831 | // --------------------------------------------------------------------- |
832 | /* The most commonly used open mode combinations are given with Mode */ | |
52b47296 | 833 | bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, unsigned long const Perms) |
578bfd0a | 834 | { |
257e8d66 DK |
835 | if (Mode == ReadOnlyGzip) |
836 | return Open(FileName, ReadOnly, Gzip, Perms); | |
257e8d66 | 837 | |
468720c5 DK |
838 | if (Compress == Auto && (Mode & WriteOnly) == WriteOnly) |
839 | return _error->Error("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str()); | |
257e8d66 | 840 | |
468720c5 DK |
841 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); |
842 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
843 | if (Compress == Auto) | |
844 | { | |
468720c5 DK |
845 | for (; compressor != compressors.end(); ++compressor) |
846 | { | |
847 | std::string file = std::string(FileName).append(compressor->Extension); | |
848 | if (FileExists(file) == false) | |
849 | continue; | |
850 | FileName = file; | |
468720c5 DK |
851 | break; |
852 | } | |
853 | } | |
854 | else if (Compress == Extension) | |
855 | { | |
52b47296 DK |
856 | std::string::size_type const found = FileName.find_last_of('.'); |
857 | std::string ext; | |
858 | if (found != std::string::npos) | |
859 | { | |
860 | ext = FileName.substr(found); | |
861 | if (ext == ".new" || ext == ".bak") | |
862 | { | |
863 | std::string::size_type const found2 = FileName.find_last_of('.', found - 1); | |
864 | if (found2 != std::string::npos) | |
865 | ext = FileName.substr(found2, found - found2); | |
866 | else | |
867 | ext.clear(); | |
868 | } | |
869 | } | |
aee1aac6 DK |
870 | for (; compressor != compressors.end(); ++compressor) |
871 | if (ext == compressor->Extension) | |
872 | break; | |
873 | // no matching extension - assume uncompressed (imagine files like 'example.org_Packages') | |
874 | if (compressor == compressors.end()) | |
875 | for (compressor = compressors.begin(); compressor != compressors.end(); ++compressor) | |
876 | if (compressor->Name == ".") | |
468720c5 | 877 | break; |
468720c5 | 878 | } |
aee1aac6 | 879 | else |
468720c5 DK |
880 | { |
881 | std::string name; | |
882 | switch (Compress) | |
883 | { | |
aee1aac6 | 884 | case None: name = "."; break; |
468720c5 DK |
885 | case Gzip: name = "gzip"; break; |
886 | case Bzip2: name = "bzip2"; break; | |
887 | case Lzma: name = "lzma"; break; | |
888 | case Xz: name = "xz"; break; | |
aee1aac6 DK |
889 | case Auto: |
890 | case Extension: | |
52b47296 DK |
891 | // Unreachable |
892 | return _error->Error("Opening File %s in None, Auto or Extension should be already handled?!?", FileName.c_str()); | |
468720c5 DK |
893 | } |
894 | for (; compressor != compressors.end(); ++compressor) | |
895 | if (compressor->Name == name) | |
896 | break; | |
aee1aac6 | 897 | if (compressor == compressors.end()) |
468720c5 DK |
898 | return _error->Error("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str()); |
899 | } | |
900 | ||
aee1aac6 DK |
901 | if (compressor == compressors.end()) |
902 | return _error->Error("Can't find a match for specified compressor mode for file %s", FileName.c_str()); | |
903 | return Open(FileName, Mode, *compressor, Perms); | |
904 | } | |
52b47296 | 905 | bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const Perms) |
aee1aac6 DK |
906 | { |
907 | Close(); | |
aee1aac6 DK |
908 | Flags = AutoClose; |
909 | ||
910 | if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0) | |
911 | return _error->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName.c_str()); | |
912 | if ((Mode & ReadWrite) == 0) | |
913 | return _error->Error("No openmode provided in FileFd::Open for %s", FileName.c_str()); | |
468720c5 | 914 | |
257e8d66 DK |
915 | if ((Mode & Atomic) == Atomic) |
916 | { | |
917 | Flags |= Replace; | |
918 | char *name = strdup((FileName + ".XXXXXX").c_str()); | |
919 | TemporaryFileName = string(mktemp(name)); | |
920 | free(name); | |
921 | } | |
922 | else if ((Mode & (Exclusive | Create)) == (Exclusive | Create)) | |
923 | { | |
924 | // for atomic, this will be done by rename in Close() | |
925 | unlink(FileName.c_str()); | |
926 | } | |
927 | if ((Mode & Empty) == Empty) | |
578bfd0a | 928 | { |
257e8d66 DK |
929 | struct stat Buf; |
930 | if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode)) | |
931 | unlink(FileName.c_str()); | |
932 | } | |
c4fc2fd7 | 933 | |
561f860a DK |
934 | int fileflags = 0; |
935 | #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE | |
936 | if_FLAGGED_SET(ReadWrite, O_RDWR); | |
937 | else if_FLAGGED_SET(ReadOnly, O_RDONLY); | |
938 | else if_FLAGGED_SET(WriteOnly, O_WRONLY); | |
4a9db827 | 939 | |
561f860a DK |
940 | if_FLAGGED_SET(Create, O_CREAT); |
941 | if_FLAGGED_SET(Empty, O_TRUNC); | |
942 | if_FLAGGED_SET(Exclusive, O_EXCL); | |
943 | else if_FLAGGED_SET(Atomic, O_EXCL); | |
944 | #undef if_FLAGGED_SET | |
52b47296 | 945 | |
561f860a DK |
946 | if (TemporaryFileName.empty() == false) |
947 | iFd = open(TemporaryFileName.c_str(), fileflags, Perms); | |
468720c5 | 948 | else |
561f860a | 949 | iFd = open(FileName.c_str(), fileflags, Perms); |
468720c5 | 950 | |
b711c01e | 951 | this->FileName = FileName; |
561f860a DK |
952 | if (iFd == -1 || OpenInternDescriptor(Mode, compressor) == false) |
953 | { | |
468720c5 | 954 | if (iFd != -1) |
fc81e8f2 | 955 | { |
561f860a DK |
956 | close (iFd); |
957 | iFd = -1; | |
fc81e8f2 | 958 | } |
561f860a | 959 | return _error->Errno("open",_("Could not open file %s"), FileName.c_str()); |
257e8d66 | 960 | } |
578bfd0a | 961 | |
13d87e2e AL |
962 | SetCloseExec(iFd,true); |
963 | return true; | |
578bfd0a | 964 | } |
257e8d66 DK |
965 | /*}}}*/ |
966 | // FileFd::OpenDescriptor - Open a filedescriptor /*{{{*/ | |
967 | // --------------------------------------------------------------------- | |
968 | /* */ | |
52b47296 | 969 | bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose) |
aee1aac6 DK |
970 | { |
971 | std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors(); | |
972 | std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin(); | |
973 | std::string name; | |
bce778a3 MV |
974 | |
975 | // compat with the old API | |
976 | if (Mode == ReadOnlyGzip && Compress == None) | |
977 | Compress = Gzip; | |
978 | ||
aee1aac6 DK |
979 | switch (Compress) |
980 | { | |
981 | case None: name = "."; break; | |
982 | case Gzip: name = "gzip"; break; | |
983 | case Bzip2: name = "bzip2"; break; | |
984 | case Lzma: name = "lzma"; break; | |
985 | case Xz: name = "xz"; break; | |
986 | case Auto: | |
987 | case Extension: | |
988 | return _error->Error("Opening Fd %d in Auto or Extension compression mode is not supported", Fd); | |
989 | } | |
990 | for (; compressor != compressors.end(); ++compressor) | |
991 | if (compressor->Name == name) | |
992 | break; | |
993 | if (compressor == compressors.end()) | |
994 | return _error->Error("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str()); | |
995 | ||
996 | return OpenDescriptor(Fd, Mode, *compressor, AutoClose); | |
997 | } | |
52b47296 | 998 | bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose) |
144c0969 JAK |
999 | { |
1000 | Close(); | |
1001 | Flags = (AutoClose) ? FileFd::AutoClose : 0; | |
1002 | iFd = Fd; | |
b711c01e | 1003 | this->FileName = ""; |
aee1aac6 | 1004 | if (OpenInternDescriptor(Mode, compressor) == false) |
468720c5 DK |
1005 | { |
1006 | if (AutoClose) | |
1007 | close (iFd); | |
1008 | return _error->Errno("gzdopen",_("Could not open file descriptor %d"), Fd); | |
144c0969 | 1009 | } |
144c0969 | 1010 | return true; |
468720c5 | 1011 | } |
52b47296 | 1012 | bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) |
468720c5 | 1013 | { |
ba667cf7 DK |
1014 | if (d == NULL) |
1015 | { | |
1016 | d = new FileFdPrivate(); | |
1017 | d->openmode = Mode; | |
1018 | d->compressor = compressor; | |
1019 | } | |
561f860a | 1020 | if (compressor.Name == "." || compressor.Binary.empty() == true) |
468720c5 | 1021 | return true; |
7efb8c8e | 1022 | #ifdef HAVE_ZLIB |
aee1aac6 | 1023 | else if (compressor.Name == "gzip") |
468720c5 | 1024 | { |
adbd7eb4 DK |
1025 | if (d->gz != NULL) |
1026 | { | |
1027 | gzclose(d->gz); | |
1028 | d->gz = NULL; | |
1029 | } | |
468720c5 | 1030 | if ((Mode & ReadWrite) == ReadWrite) |
032bd56f | 1031 | d->gz = gzdopen(iFd, "r+"); |
468720c5 | 1032 | else if ((Mode & WriteOnly) == WriteOnly) |
032bd56f | 1033 | d->gz = gzdopen(iFd, "w"); |
468720c5 | 1034 | else |
c4997486 | 1035 | d->gz = gzdopen(iFd, "r"); |
032bd56f | 1036 | if (d->gz == NULL) |
468720c5 | 1037 | return false; |
032bd56f | 1038 | Flags |= Compressed; |
561f860a | 1039 | return true; |
468720c5 | 1040 | } |
699b209e | 1041 | #endif |
c4997486 DK |
1042 | #ifdef HAVE_BZ2 |
1043 | else if (compressor.Name == "bzip2") | |
1044 | { | |
adbd7eb4 DK |
1045 | if (d->bz2 != NULL) |
1046 | { | |
1047 | BZ2_bzclose(d->bz2); | |
1048 | d->bz2 = NULL; | |
1049 | } | |
c4997486 DK |
1050 | if ((Mode & ReadWrite) == ReadWrite) |
1051 | d->bz2 = BZ2_bzdopen(iFd, "r+"); | |
1052 | else if ((Mode & WriteOnly) == WriteOnly) | |
1053 | d->bz2 = BZ2_bzdopen(iFd, "w"); | |
1054 | else | |
1055 | d->bz2 = BZ2_bzdopen(iFd, "r"); | |
1056 | if (d->bz2 == NULL) | |
1057 | return false; | |
1058 | Flags |= Compressed; | |
1059 | return true; | |
1060 | } | |
1061 | #endif | |
1062 | ||
adbd7eb4 DK |
1063 | // collect zombies here in case we reopen |
1064 | if (d->compressor_pid > 0) | |
1065 | ExecWait(d->compressor_pid, "FileFdCompressor", true); | |
561f860a DK |
1066 | |
1067 | if ((Mode & ReadWrite) == ReadWrite) | |
fbb89d94 DK |
1068 | { |
1069 | Flags |= Fail; | |
561f860a | 1070 | return _error->Error("ReadWrite mode is not supported for file %s", FileName.c_str()); |
fbb89d94 | 1071 | } |
561f860a DK |
1072 | |
1073 | bool const Comp = (Mode & WriteOnly) == WriteOnly; | |
561f860a DK |
1074 | if (Comp == false) |
1075 | { | |
adbd7eb4 | 1076 | // Handle 'decompression' of empty files |
561f860a DK |
1077 | struct stat Buf; |
1078 | fstat(iFd, &Buf); | |
1079 | if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false) | |
1080 | return true; | |
1081 | ||
1082 | // We don't need the file open - instead let the compressor open it | |
1083 | // as he properly knows better how to efficiently read from 'his' file | |
1084 | if (FileName.empty() == false) | |
afb093cd | 1085 | { |
561f860a | 1086 | close(iFd); |
afb093cd DK |
1087 | iFd = -1; |
1088 | } | |
561f860a DK |
1089 | } |
1090 | ||
1091 | // Create a data pipe | |
1092 | int Pipe[2] = {-1,-1}; | |
1093 | if (pipe(Pipe) != 0) | |
fbb89d94 DK |
1094 | { |
1095 | Flags |= Fail; | |
561f860a | 1096 | return _error->Errno("pipe",_("Failed to create subprocess IPC")); |
fbb89d94 | 1097 | } |
561f860a DK |
1098 | for (int J = 0; J != 2; J++) |
1099 | SetCloseExec(Pipe[J],true); | |
1100 | ||
1101 | d->compressed_fd = iFd; | |
1102 | d->pipe = true; | |
1103 | ||
1104 | if (Comp == true) | |
1105 | iFd = Pipe[1]; | |
1106 | else | |
1107 | iFd = Pipe[0]; | |
1108 | ||
1109 | // The child.. | |
1110 | d->compressor_pid = ExecFork(); | |
1111 | if (d->compressor_pid == 0) | |
1112 | { | |
1113 | if (Comp == true) | |
1114 | { | |
1115 | dup2(d->compressed_fd,STDOUT_FILENO); | |
1116 | dup2(Pipe[0],STDIN_FILENO); | |
1117 | } | |
1118 | else | |
1119 | { | |
1120 | if (FileName.empty() == true) | |
1121 | dup2(d->compressed_fd,STDIN_FILENO); | |
1122 | dup2(Pipe[1],STDOUT_FILENO); | |
1123 | } | |
0b4895d3 DK |
1124 | int const nullfd = open("/dev/null", O_WRONLY); |
1125 | if (nullfd != -1) | |
1126 | { | |
1127 | dup2(nullfd,STDERR_FILENO); | |
1128 | close(nullfd); | |
1129 | } | |
561f860a DK |
1130 | |
1131 | SetCloseExec(STDOUT_FILENO,false); | |
1132 | SetCloseExec(STDIN_FILENO,false); | |
1133 | ||
1134 | std::vector<char const*> Args; | |
1135 | Args.push_back(compressor.Binary.c_str()); | |
1136 | std::vector<std::string> const * const addArgs = | |
1137 | (Comp == true) ? &(compressor.CompressArgs) : &(compressor.UncompressArgs); | |
1138 | for (std::vector<std::string>::const_iterator a = addArgs->begin(); | |
1139 | a != addArgs->end(); ++a) | |
1140 | Args.push_back(a->c_str()); | |
1141 | if (Comp == false && FileName.empty() == false) | |
1142 | { | |
1143 | Args.push_back("--stdout"); | |
1144 | if (TemporaryFileName.empty() == false) | |
1145 | Args.push_back(TemporaryFileName.c_str()); | |
1146 | else | |
1147 | Args.push_back(FileName.c_str()); | |
1148 | } | |
1149 | Args.push_back(NULL); | |
1150 | ||
1151 | execvp(Args[0],(char **)&Args[0]); | |
1152 | cerr << _("Failed to exec compressor ") << Args[0] << endl; | |
1153 | _exit(100); | |
1154 | } | |
1155 | if (Comp == true) | |
1156 | close(Pipe[0]); | |
468720c5 | 1157 | else |
561f860a | 1158 | close(Pipe[1]); |
afb093cd | 1159 | if ((Comp == true || FileName.empty() == true) && d->compressed_fd != -1) |
561f860a DK |
1160 | close(d->compressed_fd); |
1161 | ||
468720c5 | 1162 | return true; |
144c0969 | 1163 | } |
578bfd0a | 1164 | /*}}}*/ |
8e06abb2 | 1165 | // FileFd::~File - Closes the file /*{{{*/ |
578bfd0a AL |
1166 | // --------------------------------------------------------------------- |
1167 | /* If the proper modes are selected then we close the Fd and possibly | |
1168 | unlink the file on error. */ | |
8e06abb2 | 1169 | FileFd::~FileFd() |
578bfd0a AL |
1170 | { |
1171 | Close(); | |
1172 | } | |
1173 | /*}}}*/ | |
8e06abb2 | 1174 | // FileFd::Read - Read a bit of the file /*{{{*/ |
578bfd0a | 1175 | // --------------------------------------------------------------------- |
b0db36b1 AL |
1176 | /* We are carefull to handle interruption by a signal while reading |
1177 | gracefully. */ | |
650faab0 | 1178 | bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) |
578bfd0a | 1179 | { |
b0db36b1 AL |
1180 | int Res; |
1181 | errno = 0; | |
f604cf55 AL |
1182 | if (Actual != 0) |
1183 | *Actual = 0; | |
699b209e | 1184 | *((char *)To) = '\0'; |
b0db36b1 | 1185 | do |
578bfd0a | 1186 | { |
7efb8c8e | 1187 | #ifdef HAVE_ZLIB |
032bd56f | 1188 | if (d->gz != NULL) |
c4997486 DK |
1189 | Res = gzread(d->gz,To,Size); |
1190 | else | |
1191 | #endif | |
1192 | #ifdef HAVE_BZ2 | |
1193 | if (d->bz2 != NULL) | |
1194 | Res = BZ2_bzread(d->bz2,To,Size); | |
a3a03f5d | 1195 | else |
699b209e | 1196 | #endif |
a3a03f5d | 1197 | Res = read(iFd,To,Size); |
b711c01e | 1198 | |
b0db36b1 AL |
1199 | if (Res < 0) |
1200 | { | |
b711c01e DK |
1201 | if (errno == EINTR) |
1202 | continue; | |
b0db36b1 | 1203 | Flags |= Fail; |
7efb8c8e | 1204 | #ifdef HAVE_ZLIB |
b711c01e DK |
1205 | if (d->gz != NULL) |
1206 | { | |
1207 | int err; | |
1208 | char const * const errmsg = gzerror(d->gz, &err); | |
1209 | if (err != Z_ERRNO) | |
1210 | return _error->Error("gzread: %s (%d: %s)", _("Read error"), err, errmsg); | |
1211 | } | |
c4997486 DK |
1212 | #endif |
1213 | #ifdef HAVE_BZ2 | |
1214 | if (d->bz2 != NULL) | |
1215 | { | |
1216 | int err; | |
1217 | char const * const errmsg = BZ2_bzerror(d->bz2, &err); | |
1218 | if (err != BZ_IO_ERROR) | |
1219 | return _error->Error("BZ2_bzread: %s (%d: %s)", _("Read error"), err, errmsg); | |
1220 | } | |
b711c01e | 1221 | #endif |
b2e465d6 | 1222 | return _error->Errno("read",_("Read error")); |
b0db36b1 | 1223 | } |
578bfd0a | 1224 | |
b0db36b1 AL |
1225 | To = (char *)To + Res; |
1226 | Size -= Res; | |
1abbc47c | 1227 | d->seekpos += Res; |
f604cf55 AL |
1228 | if (Actual != 0) |
1229 | *Actual += Res; | |
b0db36b1 AL |
1230 | } |
1231 | while (Res > 0 && Size > 0); | |
1232 | ||
1233 | if (Size == 0) | |
1234 | return true; | |
1235 | ||
ddc1d8d0 | 1236 | // Eof handling |
f604cf55 | 1237 | if (Actual != 0) |
ddc1d8d0 AL |
1238 | { |
1239 | Flags |= HitEof; | |
1240 | return true; | |
1241 | } | |
1242 | ||
b0db36b1 | 1243 | Flags |= Fail; |
650faab0 | 1244 | return _error->Error(_("read, still have %llu to read but none left"), Size); |
578bfd0a AL |
1245 | } |
1246 | /*}}}*/ | |
032bd56f DK |
1247 | // FileFd::ReadLine - Read a complete line from the file /*{{{*/ |
1248 | // --------------------------------------------------------------------- | |
1249 | /* Beware: This method can be quiet slow for big buffers on UNcompressed | |
1250 | files because of the naive implementation! */ | |
1251 | char* FileFd::ReadLine(char *To, unsigned long long const Size) | |
1252 | { | |
699b209e | 1253 | *To = '\0'; |
7efb8c8e | 1254 | #ifdef HAVE_ZLIB |
032bd56f DK |
1255 | if (d->gz != NULL) |
1256 | return gzgets(d->gz, To, Size); | |
699b209e | 1257 | #endif |
032bd56f DK |
1258 | |
1259 | unsigned long long read = 0; | |
40468850 DK |
1260 | while ((Size - 1) != read) |
1261 | { | |
1262 | unsigned long long done = 0; | |
1263 | if (Read(To + read, 1, &done) == false) | |
1264 | return NULL; | |
1265 | if (done == 0) | |
1266 | break; | |
1267 | if (To[read++] == '\n') | |
1268 | break; | |
1269 | } | |
1270 | if (read == 0) | |
032bd56f | 1271 | return NULL; |
40468850 | 1272 | To[read] = '\0'; |
032bd56f DK |
1273 | return To; |
1274 | } | |
1275 | /*}}}*/ | |
8e06abb2 | 1276 | // FileFd::Write - Write to the file /*{{{*/ |
578bfd0a AL |
1277 | // --------------------------------------------------------------------- |
1278 | /* */ | |
650faab0 | 1279 | bool FileFd::Write(const void *From,unsigned long long Size) |
578bfd0a | 1280 | { |
b0db36b1 AL |
1281 | int Res; |
1282 | errno = 0; | |
1283 | do | |
578bfd0a | 1284 | { |
7efb8c8e | 1285 | #ifdef HAVE_ZLIB |
032bd56f DK |
1286 | if (d->gz != NULL) |
1287 | Res = gzwrite(d->gz,From,Size); | |
a3a03f5d | 1288 | else |
c4997486 DK |
1289 | #endif |
1290 | #ifdef HAVE_BZ2 | |
1291 | if (d->bz2 != NULL) | |
1292 | Res = BZ2_bzwrite(d->bz2,(void*)From,Size); | |
1293 | else | |
699b209e | 1294 | #endif |
a3a03f5d | 1295 | Res = write(iFd,From,Size); |
b0db36b1 AL |
1296 | if (Res < 0 && errno == EINTR) |
1297 | continue; | |
1298 | if (Res < 0) | |
1299 | { | |
1300 | Flags |= Fail; | |
c4997486 DK |
1301 | #ifdef HAVE_ZLIB |
1302 | if (d->gz != NULL) | |
1303 | { | |
1304 | int err; | |
1305 | char const * const errmsg = gzerror(d->gz, &err); | |
1306 | if (err != Z_ERRNO) | |
1307 | return _error->Error("gzwrite: %s (%d: %s)", _("Write error"), err, errmsg); | |
1308 | } | |
1309 | #endif | |
1310 | #ifdef HAVE_BZ2 | |
1311 | if (d->bz2 != NULL) | |
1312 | { | |
1313 | int err; | |
1314 | char const * const errmsg = BZ2_bzerror(d->bz2, &err); | |
1315 | if (err != BZ_IO_ERROR) | |
1316 | return _error->Error("BZ2_bzwrite: %s (%d: %s)", _("Write error"), err, errmsg); | |
1317 | } | |
1318 | #endif | |
b2e465d6 | 1319 | return _error->Errno("write",_("Write error")); |
b0db36b1 AL |
1320 | } |
1321 | ||
1322 | From = (char *)From + Res; | |
1323 | Size -= Res; | |
1abbc47c | 1324 | d->seekpos += Res; |
578bfd0a | 1325 | } |
b0db36b1 | 1326 | while (Res > 0 && Size > 0); |
578bfd0a | 1327 | |
b0db36b1 AL |
1328 | if (Size == 0) |
1329 | return true; | |
1330 | ||
1331 | Flags |= Fail; | |
650faab0 | 1332 | return _error->Error(_("write, still have %llu to write but couldn't"), Size); |
d68d65ad DK |
1333 | } |
1334 | bool FileFd::Write(int Fd, const void *From, unsigned long long Size) | |
1335 | { | |
1336 | int Res; | |
1337 | errno = 0; | |
1338 | do | |
1339 | { | |
1340 | Res = write(Fd,From,Size); | |
1341 | if (Res < 0 && errno == EINTR) | |
1342 | continue; | |
1343 | if (Res < 0) | |
1344 | return _error->Errno("write",_("Write error")); | |
1345 | ||
1346 | From = (char *)From + Res; | |
1347 | Size -= Res; | |
1348 | } | |
1349 | while (Res > 0 && Size > 0); | |
1350 | ||
1351 | if (Size == 0) | |
1352 | return true; | |
1353 | ||
1354 | return _error->Error(_("write, still have %llu to write but couldn't"), Size); | |
578bfd0a AL |
1355 | } |
1356 | /*}}}*/ | |
8e06abb2 | 1357 | // FileFd::Seek - Seek in the file /*{{{*/ |
578bfd0a AL |
1358 | // --------------------------------------------------------------------- |
1359 | /* */ | |
650faab0 | 1360 | bool FileFd::Seek(unsigned long long To) |
578bfd0a | 1361 | { |
c4997486 DK |
1362 | if (d->pipe == true |
1363 | #ifdef HAVE_BZ2 | |
1364 | || d->bz2 != NULL | |
1365 | #endif | |
1366 | ) | |
699b209e | 1367 | { |
1abbc47c DK |
1368 | // Our poor man seeking in pipes is costly, so try to avoid it |
1369 | unsigned long long seekpos = Tell(); | |
1370 | if (seekpos == To) | |
1371 | return true; | |
1372 | else if (seekpos < To) | |
1373 | return Skip(To - seekpos); | |
1374 | ||
561f860a | 1375 | if ((d->openmode & ReadOnly) != ReadOnly) |
fbb89d94 DK |
1376 | { |
1377 | Flags |= Fail; | |
561f860a | 1378 | return _error->Error("Reopen is only implemented for read-only files!"); |
fbb89d94 | 1379 | } |
c4997486 DK |
1380 | #ifdef HAVE_BZ2 |
1381 | if (d->bz2 != NULL) | |
1382 | BZ2_bzclose(d->bz2); | |
1383 | #endif | |
afb093cd DK |
1384 | if (iFd != -1) |
1385 | close(iFd); | |
1386 | iFd = -1; | |
561f860a DK |
1387 | if (TemporaryFileName.empty() == false) |
1388 | iFd = open(TemporaryFileName.c_str(), O_RDONLY); | |
1389 | else if (FileName.empty() == false) | |
1390 | iFd = open(FileName.c_str(), O_RDONLY); | |
1391 | else | |
6fd947bd DK |
1392 | { |
1393 | if (d->compressed_fd > 0) | |
1394 | if (lseek(d->compressed_fd, 0, SEEK_SET) != 0) | |
1395 | iFd = d->compressed_fd; | |
1396 | if (iFd <= 0) | |
fbb89d94 DK |
1397 | { |
1398 | Flags |= Fail; | |
6fd947bd | 1399 | return _error->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!"); |
fbb89d94 | 1400 | } |
6fd947bd | 1401 | } |
561f860a DK |
1402 | |
1403 | if (OpenInternDescriptor(d->openmode, d->compressor) == false) | |
fbb89d94 DK |
1404 | { |
1405 | Flags |= Fail; | |
561f860a | 1406 | return _error->Error("Seek on file %s because it couldn't be reopened", FileName.c_str()); |
fbb89d94 | 1407 | } |
561f860a DK |
1408 | |
1409 | if (To != 0) | |
1410 | return Skip(To); | |
1abbc47c DK |
1411 | |
1412 | d->seekpos = To; | |
561f860a | 1413 | return true; |
699b209e | 1414 | } |
a3a03f5d | 1415 | int res; |
7efb8c8e | 1416 | #ifdef HAVE_ZLIB |
032bd56f DK |
1417 | if (d->gz) |
1418 | res = gzseek(d->gz,To,SEEK_SET); | |
a3a03f5d | 1419 | else |
699b209e | 1420 | #endif |
a3a03f5d | 1421 | res = lseek(iFd,To,SEEK_SET); |
1422 | if (res != (signed)To) | |
578bfd0a AL |
1423 | { |
1424 | Flags |= Fail; | |
650faab0 | 1425 | return _error->Error("Unable to seek to %llu", To); |
578bfd0a | 1426 | } |
1abbc47c DK |
1427 | |
1428 | d->seekpos = To; | |
727f18af AL |
1429 | return true; |
1430 | } | |
1431 | /*}}}*/ | |
1432 | // FileFd::Skip - Seek in the file /*{{{*/ | |
1433 | // --------------------------------------------------------------------- | |
1434 | /* */ | |
650faab0 | 1435 | bool FileFd::Skip(unsigned long long Over) |
727f18af | 1436 | { |
c4997486 DK |
1437 | if (d->pipe == true |
1438 | #ifdef HAVE_BZ2 | |
1439 | || d->bz2 != NULL | |
1440 | #endif | |
1441 | ) | |
40468850 DK |
1442 | { |
1443 | d->seekpos += Over; | |
1444 | char buffer[1024]; | |
1445 | while (Over != 0) | |
1446 | { | |
1447 | unsigned long long toread = std::min((unsigned long long) sizeof(buffer), Over); | |
1448 | if (Read(buffer, toread) == false) | |
fbb89d94 DK |
1449 | { |
1450 | Flags |= Fail; | |
40468850 | 1451 | return _error->Error("Unable to seek ahead %llu",Over); |
fbb89d94 | 1452 | } |
40468850 DK |
1453 | Over -= toread; |
1454 | } | |
1455 | return true; | |
1456 | } | |
1457 | ||
a3a03f5d | 1458 | int res; |
7efb8c8e | 1459 | #ifdef HAVE_ZLIB |
032bd56f DK |
1460 | if (d->gz != NULL) |
1461 | res = gzseek(d->gz,Over,SEEK_CUR); | |
a3a03f5d | 1462 | else |
699b209e | 1463 | #endif |
a3a03f5d | 1464 | res = lseek(iFd,Over,SEEK_CUR); |
1465 | if (res < 0) | |
727f18af AL |
1466 | { |
1467 | Flags |= Fail; | |
650faab0 | 1468 | return _error->Error("Unable to seek ahead %llu",Over); |
727f18af | 1469 | } |
1abbc47c DK |
1470 | d->seekpos = res; |
1471 | ||
6d5dd02a AL |
1472 | return true; |
1473 | } | |
1474 | /*}}}*/ | |
1475 | // FileFd::Truncate - Truncate the file /*{{{*/ | |
1476 | // --------------------------------------------------------------------- | |
1477 | /* */ | |
650faab0 | 1478 | bool FileFd::Truncate(unsigned long long To) |
6d5dd02a | 1479 | { |
c4997486 DK |
1480 | #if defined HAVE_ZLIB || defined HAVE_BZ2 |
1481 | if (d->gz != NULL || d->bz2 != NULL) | |
a3a03f5d | 1482 | { |
1483 | Flags |= Fail; | |
c4997486 | 1484 | return _error->Error("Truncating compressed files is not implemented (%s)", FileName.c_str()); |
a3a03f5d | 1485 | } |
c4997486 | 1486 | #endif |
6d5dd02a AL |
1487 | if (ftruncate(iFd,To) != 0) |
1488 | { | |
1489 | Flags |= Fail; | |
650faab0 | 1490 | return _error->Error("Unable to truncate to %llu",To); |
6d5dd02a AL |
1491 | } |
1492 | ||
578bfd0a AL |
1493 | return true; |
1494 | } | |
1495 | /*}}}*/ | |
7f25bdff AL |
1496 | // FileFd::Tell - Current seek position /*{{{*/ |
1497 | // --------------------------------------------------------------------- | |
1498 | /* */ | |
650faab0 | 1499 | unsigned long long FileFd::Tell() |
7f25bdff | 1500 | { |
1abbc47c DK |
1501 | // In theory, we could just return seekpos here always instead of |
1502 | // seeking around, but not all users of FileFd use always Seek() and co | |
1503 | // so d->seekpos isn't always true and we can just use it as a hint if | |
1504 | // we have nothing else, but not always as an authority… | |
c4997486 DK |
1505 | if (d->pipe == true |
1506 | #ifdef HAVE_BZ2 | |
1507 | || d->bz2 != NULL | |
1508 | #endif | |
1509 | ) | |
1abbc47c DK |
1510 | return d->seekpos; |
1511 | ||
a3a03f5d | 1512 | off_t Res; |
7efb8c8e | 1513 | #ifdef HAVE_ZLIB |
032bd56f DK |
1514 | if (d->gz != NULL) |
1515 | Res = gztell(d->gz); | |
a3a03f5d | 1516 | else |
699b209e | 1517 | #endif |
a3a03f5d | 1518 | Res = lseek(iFd,0,SEEK_CUR); |
7f25bdff | 1519 | if (Res == (off_t)-1) |
fbb89d94 DK |
1520 | { |
1521 | Flags |= Fail; | |
7f25bdff | 1522 | _error->Errno("lseek","Failed to determine the current file position"); |
fbb89d94 | 1523 | } |
1abbc47c | 1524 | d->seekpos = Res; |
7f25bdff AL |
1525 | return Res; |
1526 | } | |
1527 | /*}}}*/ | |
4260fd39 | 1528 | // FileFd::FileSize - Return the size of the file /*{{{*/ |
578bfd0a AL |
1529 | // --------------------------------------------------------------------- |
1530 | /* */ | |
650faab0 | 1531 | unsigned long long FileFd::FileSize() |
578bfd0a AL |
1532 | { |
1533 | struct stat Buf; | |
699b209e | 1534 | if (d->pipe == false && fstat(iFd,&Buf) != 0) |
fbb89d94 DK |
1535 | { |
1536 | Flags |= Fail; | |
44dc669e | 1537 | return _error->Errno("fstat","Unable to determine the file size"); |
fbb89d94 | 1538 | } |
699b209e DK |
1539 | |
1540 | // for compressor pipes st_size is undefined and at 'best' zero | |
1541 | if (d->pipe == true || S_ISFIFO(Buf.st_mode)) | |
1542 | { | |
1543 | // we set it here, too, as we get the info here for free | |
1544 | // in theory the Open-methods should take care of it already | |
1545 | d->pipe = true; | |
1546 | if (stat(FileName.c_str(), &Buf) != 0) | |
fbb89d94 DK |
1547 | { |
1548 | Flags |= Fail; | |
699b209e | 1549 | return _error->Errno("stat","Unable to determine the file size"); |
fbb89d94 | 1550 | } |
699b209e DK |
1551 | } |
1552 | ||
4260fd39 DK |
1553 | return Buf.st_size; |
1554 | } | |
1555 | /*}}}*/ | |
1556 | // FileFd::Size - Return the size of the content in the file /*{{{*/ | |
1557 | // --------------------------------------------------------------------- | |
1558 | /* */ | |
650faab0 | 1559 | unsigned long long FileFd::Size() |
4260fd39 | 1560 | { |
650faab0 | 1561 | unsigned long long size = FileSize(); |
44dc669e | 1562 | |
699b209e DK |
1563 | // for compressor pipes st_size is undefined and at 'best' zero, |
1564 | // so we 'read' the content and 'seek' back - see there | |
c4997486 DK |
1565 | if (d->pipe == true |
1566 | #ifdef HAVE_BZ2 | |
1567 | || (d->bz2 && size > 0) | |
1568 | #endif | |
1569 | ) | |
699b209e | 1570 | { |
1abbc47c | 1571 | unsigned long long const oldSeek = Tell(); |
699b209e DK |
1572 | char ignore[1000]; |
1573 | unsigned long long read = 0; | |
1574 | do { | |
1575 | Read(ignore, sizeof(ignore), &read); | |
699b209e | 1576 | } while(read != 0); |
1abbc47c DK |
1577 | size = Tell(); |
1578 | Seek(oldSeek); | |
699b209e | 1579 | } |
7efb8c8e | 1580 | #ifdef HAVE_ZLIB |
44dc669e | 1581 | // only check gzsize if we are actually a gzip file, just checking for |
032bd56f | 1582 | // "gz" is not sufficient as uncompressed files could be opened with |
44dc669e | 1583 | // gzopen in "direct" mode as well |
699b209e | 1584 | else if (d->gz && !gzdirect(d->gz) && size > 0) |
9c182afa | 1585 | { |
65c72a4b | 1586 | off_t const oldPos = lseek(iFd,0,SEEK_CUR); |
9c182afa MP |
1587 | /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do |
1588 | * this ourselves; the original (uncompressed) file size is the last 32 | |
1589 | * bits of the file */ | |
650faab0 | 1590 | // FIXME: Size for gz-files is limited by 32bit… no largefile support |
9c182afa | 1591 | if (lseek(iFd, -4, SEEK_END) < 0) |
fbb89d94 DK |
1592 | { |
1593 | Flags |= Fail; | |
1594 | return _error->Errno("lseek","Unable to seek to end of gzipped file"); | |
1595 | } | |
f330c0f3 | 1596 | size = 0L; |
9c182afa | 1597 | if (read(iFd, &size, 4) != 4) |
fbb89d94 DK |
1598 | { |
1599 | Flags |= Fail; | |
1600 | return _error->Errno("read","Unable to read original size of gzipped file"); | |
1601 | } | |
f330c0f3 DK |
1602 | |
1603 | #ifdef WORDS_BIGENDIAN | |
2cae0ccb DK |
1604 | uint32_t tmp_size = size; |
1605 | uint8_t const * const p = (uint8_t const * const) &tmp_size; | |
1606 | tmp_size = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; | |
1607 | size = tmp_size; | |
f330c0f3 | 1608 | #endif |
9c182afa | 1609 | |
65c72a4b | 1610 | if (lseek(iFd, oldPos, SEEK_SET) < 0) |
fbb89d94 DK |
1611 | { |
1612 | Flags |= Fail; | |
1613 | return _error->Errno("lseek","Unable to seek in gzipped file"); | |
1614 | } | |
65c72a4b | 1615 | |
9c182afa MP |
1616 | return size; |
1617 | } | |
699b209e | 1618 | #endif |
9c182afa | 1619 | |
44dc669e | 1620 | return size; |
578bfd0a AL |
1621 | } |
1622 | /*}}}*/ | |
76a763e1 DK |
1623 | // FileFd::ModificationTime - Return the time of last touch /*{{{*/ |
1624 | // --------------------------------------------------------------------- | |
1625 | /* */ | |
1626 | time_t FileFd::ModificationTime() | |
1627 | { | |
1628 | struct stat Buf; | |
699b209e | 1629 | if (d->pipe == false && fstat(iFd,&Buf) != 0) |
76a763e1 | 1630 | { |
fbb89d94 | 1631 | Flags |= Fail; |
76a763e1 DK |
1632 | _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); |
1633 | return 0; | |
1634 | } | |
699b209e DK |
1635 | |
1636 | // for compressor pipes st_size is undefined and at 'best' zero | |
1637 | if (d->pipe == true || S_ISFIFO(Buf.st_mode)) | |
1638 | { | |
1639 | // we set it here, too, as we get the info here for free | |
1640 | // in theory the Open-methods should take care of it already | |
1641 | d->pipe = true; | |
1642 | if (stat(FileName.c_str(), &Buf) != 0) | |
1643 | { | |
fbb89d94 | 1644 | Flags |= Fail; |
699b209e DK |
1645 | _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); |
1646 | return 0; | |
1647 | } | |
1648 | } | |
1649 | ||
76a763e1 DK |
1650 | return Buf.st_mtime; |
1651 | } | |
1652 | /*}}}*/ | |
8e06abb2 | 1653 | // FileFd::Close - Close the file if the close flag is set /*{{{*/ |
578bfd0a AL |
1654 | // --------------------------------------------------------------------- |
1655 | /* */ | |
8e06abb2 | 1656 | bool FileFd::Close() |
578bfd0a | 1657 | { |
032bd56f DK |
1658 | if (iFd == -1) |
1659 | return true; | |
1660 | ||
578bfd0a AL |
1661 | bool Res = true; |
1662 | if ((Flags & AutoClose) == AutoClose) | |
d13c2d3f | 1663 | { |
7efb8c8e | 1664 | #ifdef HAVE_ZLIB |
032bd56f DK |
1665 | if (d != NULL && d->gz != NULL) { |
1666 | int const e = gzclose(d->gz); | |
b711c01e | 1667 | // gzdclose() on empty files always fails with "buffer error" here, ignore that |
d13c2d3f | 1668 | if (e != 0 && e != Z_BUF_ERROR) |
3184b4cf | 1669 | Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); |
d13c2d3f | 1670 | } else |
c4997486 DK |
1671 | #endif |
1672 | #ifdef HAVE_BZ2 | |
1673 | if (d != NULL && d->bz2 != NULL) | |
1674 | BZ2_bzclose(d->bz2); | |
1675 | else | |
699b209e | 1676 | #endif |
d13c2d3f | 1677 | if (iFd > 0 && close(iFd) != 0) |
3184b4cf | 1678 | Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); |
d13c2d3f | 1679 | } |
3010fb0e | 1680 | |
62d073d9 | 1681 | if ((Flags & Replace) == Replace && iFd >= 0) { |
3010fb0e | 1682 | if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) |
62d073d9 DK |
1683 | Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str()); |
1684 | ||
fd3b761e | 1685 | FileName = TemporaryFileName; // for the unlink() below. |
257e8d66 | 1686 | TemporaryFileName.clear(); |
3010fb0e | 1687 | } |
62d073d9 DK |
1688 | |
1689 | iFd = -1; | |
1690 | ||
578bfd0a AL |
1691 | if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && |
1692 | FileName.empty() == false) | |
1693 | if (unlink(FileName.c_str()) != 0) | |
62d073d9 | 1694 | Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str()); |
3010fb0e | 1695 | |
032bd56f DK |
1696 | if (d != NULL) |
1697 | { | |
561f860a | 1698 | if (d->compressor_pid > 0) |
52b47296 | 1699 | ExecWait(d->compressor_pid, "FileFdCompressor", true); |
032bd56f DK |
1700 | delete d; |
1701 | d = NULL; | |
1702 | } | |
3010fb0e | 1703 | |
fbb89d94 DK |
1704 | if (Res == false) |
1705 | Flags |= Fail; | |
578bfd0a AL |
1706 | return Res; |
1707 | } | |
1708 | /*}}}*/ | |
b2e465d6 AL |
1709 | // FileFd::Sync - Sync the file /*{{{*/ |
1710 | // --------------------------------------------------------------------- | |
1711 | /* */ | |
1712 | bool FileFd::Sync() | |
1713 | { | |
1714 | #ifdef _POSIX_SYNCHRONIZED_IO | |
1715 | if (fsync(iFd) != 0) | |
fbb89d94 DK |
1716 | { |
1717 | Flags |= Fail; | |
b2e465d6 | 1718 | return _error->Errno("sync",_("Problem syncing the file")); |
fbb89d94 | 1719 | } |
b2e465d6 AL |
1720 | #endif |
1721 | return true; | |
1722 | } | |
1723 | /*}}}*/ | |
699b209e DK |
1724 | |
1725 | gzFile FileFd::gzFd() { return (gzFile) d->gz; } |