]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
Add a GetListOfFilesInDir() helper method which replaces the old
[apt.git] / apt-pkg / contrib / fileutl.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $
4 /* ######################################################################
5
6 File Utilities
7
8 CopyFile - Buffered copy of a single file
9 GetLock - dpkg compatible lock file manipulation (fcntl)
10
11 Most of this source is placed in the Public Domain, do with it what
12 you will
13 It was originally written by Jason Gunthorpe <jgg@debian.org>.
14
15 The exception is RunScripts() it is under the GPLv2
16
17 ##################################################################### */
18 /*}}}*/
19 // Include Files /*{{{*/
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/sptr.h>
23 #include <apt-pkg/configuration.h>
24
25 #include <apti18n.h>
26
27 #include <cstdlib>
28 #include <cstring>
29
30 #include <iostream>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/wait.h>
37 #include <dirent.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <set>
41 #include <algorithm>
42 /*}}}*/
43
44 using namespace std;
45
46 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
47 // ---------------------------------------------------------------------
48 /* */
49 bool RunScripts(const char *Cnf)
50 {
51 Configuration::Item const *Opts = _config->Tree(Cnf);
52 if (Opts == 0 || Opts->Child == 0)
53 return true;
54 Opts = Opts->Child;
55
56 // Fork for running the system calls
57 pid_t Child = ExecFork();
58
59 // This is the child
60 if (Child == 0)
61 {
62 if (chdir("/tmp/") != 0)
63 _exit(100);
64
65 unsigned int Count = 1;
66 for (; Opts != 0; Opts = Opts->Next, Count++)
67 {
68 if (Opts->Value.empty() == true)
69 continue;
70
71 if (system(Opts->Value.c_str()) != 0)
72 _exit(100+Count);
73 }
74 _exit(0);
75 }
76
77 // Wait for the child
78 int Status = 0;
79 while (waitpid(Child,&Status,0) != Child)
80 {
81 if (errno == EINTR)
82 continue;
83 return _error->Errno("waitpid","Couldn't wait for subprocess");
84 }
85
86 // Restore sig int/quit
87 signal(SIGQUIT,SIG_DFL);
88 signal(SIGINT,SIG_DFL);
89
90 // Check for an error code.
91 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
92 {
93 unsigned int Count = WEXITSTATUS(Status);
94 if (Count > 100)
95 {
96 Count -= 100;
97 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
98 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
99 }
100
101 return _error->Error("Sub-process returned an error code");
102 }
103
104 return true;
105 }
106 /*}}}*/
107
108 // CopyFile - Buffered copy of a file /*{{{*/
109 // ---------------------------------------------------------------------
110 /* The caller is expected to set things so that failure causes erasure */
111 bool CopyFile(FileFd &From,FileFd &To)
112 {
113 if (From.IsOpen() == false || To.IsOpen() == false)
114 return false;
115
116 // Buffered copy between fds
117 SPtrArray<unsigned char> Buf = new unsigned char[64000];
118 unsigned long Size = From.Size();
119 while (Size != 0)
120 {
121 unsigned long ToRead = Size;
122 if (Size > 64000)
123 ToRead = 64000;
124
125 if (From.Read(Buf,ToRead) == false ||
126 To.Write(Buf,ToRead) == false)
127 return false;
128
129 Size -= ToRead;
130 }
131
132 return true;
133 }
134 /*}}}*/
135 // GetLock - Gets a lock file /*{{{*/
136 // ---------------------------------------------------------------------
137 /* This will create an empty file of the given name and lock it. Once this
138 is done all other calls to GetLock in any other process will fail with
139 -1. The return result is the fd of the file, the call should call
140 close at some time. */
141 int GetLock(string File,bool Errors)
142 {
143 // GetLock() is used in aptitude on directories with public-write access
144 // Use O_NOFOLLOW here to prevent symlink traversal attacks
145 int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640);
146 if (FD < 0)
147 {
148 // Read only .. cant have locking problems there.
149 if (errno == EROFS)
150 {
151 _error->Warning(_("Not using locking for read only lock file %s"),File.c_str());
152 return dup(0); // Need something for the caller to close
153 }
154
155 if (Errors == true)
156 _error->Errno("open",_("Could not open lock file %s"),File.c_str());
157
158 // Feh.. We do this to distinguish the lock vs open case..
159 errno = EPERM;
160 return -1;
161 }
162 SetCloseExec(FD,true);
163
164 // Aquire a write lock
165 struct flock fl;
166 fl.l_type = F_WRLCK;
167 fl.l_whence = SEEK_SET;
168 fl.l_start = 0;
169 fl.l_len = 0;
170 if (fcntl(FD,F_SETLK,&fl) == -1)
171 {
172 if (errno == ENOLCK)
173 {
174 _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str());
175 return dup(0); // Need something for the caller to close
176 }
177 if (Errors == true)
178 _error->Errno("open",_("Could not get lock %s"),File.c_str());
179
180 int Tmp = errno;
181 close(FD);
182 errno = Tmp;
183 return -1;
184 }
185
186 return FD;
187 }
188 /*}}}*/
189 // FileExists - Check if a file exists /*{{{*/
190 // ---------------------------------------------------------------------
191 /* */
192 bool FileExists(string File)
193 {
194 struct stat Buf;
195 if (stat(File.c_str(),&Buf) != 0)
196 return false;
197 return true;
198 }
199 /*}}}*/
200 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
201 // ---------------------------------------------------------------------
202 /* If an extension is given only files with this extension are included
203 in the returned vector, otherwise every "normal" file is included. */
204 std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
205 bool const &SortList) {
206 std::vector<string> List;
207 DIR *D = opendir(Dir.c_str());
208 if (D == 0) {
209 _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
210 return List;
211 }
212
213 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) {
214 if (Ent->d_name[0] == '.')
215 continue;
216
217 if (Ext.empty() == false && flExtension(Ent->d_name) != Ext)
218 continue;
219
220 // Skip bad file names ala run-parts
221 const char *C = Ent->d_name;
222 for (; *C != 0; ++C)
223 if (isalpha(*C) == 0 && isdigit(*C) == 0
224 && *C != '_' && *C != '-' && *C != '.')
225 break;
226
227 if (*C != 0)
228 continue;
229
230 // Make sure it is a file and not something else
231 string const File = flCombine(Dir,Ent->d_name);
232 struct stat St;
233 if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
234 continue;
235
236 List.push_back(File);
237 }
238 closedir(D);
239
240 if (SortList == true)
241 std::sort(List.begin(),List.end());
242 return List;
243 }
244 /*}}}*/
245 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
246 // ---------------------------------------------------------------------
247 /* We return / on failure. */
248 string SafeGetCWD()
249 {
250 // Stash the current dir.
251 char S[300];
252 S[0] = 0;
253 if (getcwd(S,sizeof(S)-2) == 0)
254 return "/";
255 unsigned int Len = strlen(S);
256 S[Len] = '/';
257 S[Len+1] = 0;
258 return S;
259 }
260 /*}}}*/
261 // flNotDir - Strip the directory from the filename /*{{{*/
262 // ---------------------------------------------------------------------
263 /* */
264 string flNotDir(string File)
265 {
266 string::size_type Res = File.rfind('/');
267 if (Res == string::npos)
268 return File;
269 Res++;
270 return string(File,Res,Res - File.length());
271 }
272 /*}}}*/
273 // flNotFile - Strip the file from the directory name /*{{{*/
274 // ---------------------------------------------------------------------
275 /* Result ends in a / */
276 string flNotFile(string File)
277 {
278 string::size_type Res = File.rfind('/');
279 if (Res == string::npos)
280 return "./";
281 Res++;
282 return string(File,0,Res);
283 }
284 /*}}}*/
285 // flExtension - Return the extension for the file /*{{{*/
286 // ---------------------------------------------------------------------
287 /* */
288 string flExtension(string File)
289 {
290 string::size_type Res = File.rfind('.');
291 if (Res == string::npos)
292 return File;
293 Res++;
294 return string(File,Res,Res - File.length());
295 }
296 /*}}}*/
297 // flNoLink - If file is a symlink then deref it /*{{{*/
298 // ---------------------------------------------------------------------
299 /* If the name is not a link then the returned path is the input. */
300 string flNoLink(string File)
301 {
302 struct stat St;
303 if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0)
304 return File;
305 if (stat(File.c_str(),&St) != 0)
306 return File;
307
308 /* Loop resolving the link. There is no need to limit the number of
309 loops because the stat call above ensures that the symlink is not
310 circular */
311 char Buffer[1024];
312 string NFile = File;
313 while (1)
314 {
315 // Read the link
316 int Res;
317 if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 ||
318 (unsigned)Res >= sizeof(Buffer))
319 return File;
320
321 // Append or replace the previous path
322 Buffer[Res] = 0;
323 if (Buffer[0] == '/')
324 NFile = Buffer;
325 else
326 NFile = flNotFile(NFile) + Buffer;
327
328 // See if we are done
329 if (lstat(NFile.c_str(),&St) != 0)
330 return File;
331 if (S_ISLNK(St.st_mode) == 0)
332 return NFile;
333 }
334 }
335 /*}}}*/
336 // flCombine - Combine a file and a directory /*{{{*/
337 // ---------------------------------------------------------------------
338 /* If the file is an absolute path then it is just returned, otherwise
339 the directory is pre-pended to it. */
340 string flCombine(string Dir,string File)
341 {
342 if (File.empty() == true)
343 return string();
344
345 if (File[0] == '/' || Dir.empty() == true)
346 return File;
347 if (File.length() >= 2 && File[0] == '.' && File[1] == '/')
348 return File;
349 if (Dir[Dir.length()-1] == '/')
350 return Dir + File;
351 return Dir + '/' + File;
352 }
353 /*}}}*/
354 // SetCloseExec - Set the close on exec flag /*{{{*/
355 // ---------------------------------------------------------------------
356 /* */
357 void SetCloseExec(int Fd,bool Close)
358 {
359 if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0)
360 {
361 cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl;
362 exit(100);
363 }
364 }
365 /*}}}*/
366 // SetNonBlock - Set the nonblocking flag /*{{{*/
367 // ---------------------------------------------------------------------
368 /* */
369 void SetNonBlock(int Fd,bool Block)
370 {
371 int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK);
372 if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0)
373 {
374 cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl;
375 exit(100);
376 }
377 }
378 /*}}}*/
379 // WaitFd - Wait for a FD to become readable /*{{{*/
380 // ---------------------------------------------------------------------
381 /* This waits for a FD to become readable using select. It is useful for
382 applications making use of non-blocking sockets. The timeout is
383 in seconds. */
384 bool WaitFd(int Fd,bool write,unsigned long timeout)
385 {
386 fd_set Set;
387 struct timeval tv;
388 FD_ZERO(&Set);
389 FD_SET(Fd,&Set);
390 tv.tv_sec = timeout;
391 tv.tv_usec = 0;
392 if (write == true)
393 {
394 int Res;
395 do
396 {
397 Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0));
398 }
399 while (Res < 0 && errno == EINTR);
400
401 if (Res <= 0)
402 return false;
403 }
404 else
405 {
406 int Res;
407 do
408 {
409 Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0));
410 }
411 while (Res < 0 && errno == EINTR);
412
413 if (Res <= 0)
414 return false;
415 }
416
417 return true;
418 }
419 /*}}}*/
420 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
421 // ---------------------------------------------------------------------
422 /* This is used if you want to cleanse the environment for the forked
423 child, it fixes up the important signals and nukes all of the fds,
424 otherwise acts like normal fork. */
425 pid_t ExecFork()
426 {
427 // Fork off the process
428 pid_t Process = fork();
429 if (Process < 0)
430 {
431 cerr << "FATAL -> Failed to fork." << endl;
432 exit(100);
433 }
434
435 // Spawn the subprocess
436 if (Process == 0)
437 {
438 // Setup the signals
439 signal(SIGPIPE,SIG_DFL);
440 signal(SIGQUIT,SIG_DFL);
441 signal(SIGINT,SIG_DFL);
442 signal(SIGWINCH,SIG_DFL);
443 signal(SIGCONT,SIG_DFL);
444 signal(SIGTSTP,SIG_DFL);
445
446 set<int> KeepFDs;
447 Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds");
448 if (Opts != 0 && Opts->Child != 0)
449 {
450 Opts = Opts->Child;
451 for (; Opts != 0; Opts = Opts->Next)
452 {
453 if (Opts->Value.empty() == true)
454 continue;
455 int fd = atoi(Opts->Value.c_str());
456 KeepFDs.insert(fd);
457 }
458 }
459
460 // Close all of our FDs - just in case
461 for (int K = 3; K != 40; K++)
462 {
463 if(KeepFDs.find(K) == KeepFDs.end())
464 fcntl(K,F_SETFD,FD_CLOEXEC);
465 }
466 }
467
468 return Process;
469 }
470 /*}}}*/
471 // ExecWait - Fancy waitpid /*{{{*/
472 // ---------------------------------------------------------------------
473 /* Waits for the given sub process. If Reap is set then no errors are
474 generated. Otherwise a failed subprocess will generate a proper descriptive
475 message */
476 bool ExecWait(pid_t Pid,const char *Name,bool Reap)
477 {
478 if (Pid <= 1)
479 return true;
480
481 // Wait and collect the error code
482 int Status;
483 while (waitpid(Pid,&Status,0) != Pid)
484 {
485 if (errno == EINTR)
486 continue;
487
488 if (Reap == true)
489 return false;
490
491 return _error->Error(_("Waited for %s but it wasn't there"),Name);
492 }
493
494
495 // Check for an error code.
496 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
497 {
498 if (Reap == true)
499 return false;
500 if (WIFSIGNALED(Status) != 0)
501 {
502 if( WTERMSIG(Status) == SIGSEGV)
503 return _error->Error(_("Sub-process %s received a segmentation fault."),Name);
504 else
505 return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status));
506 }
507
508 if (WIFEXITED(Status) != 0)
509 return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status));
510
511 return _error->Error(_("Sub-process %s exited unexpectedly"),Name);
512 }
513
514 return true;
515 }
516 /*}}}*/
517
518 // FileFd::Open - Open a file /*{{{*/
519 // ---------------------------------------------------------------------
520 /* The most commonly used open mode combinations are given with Mode */
521 bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
522 {
523 Close();
524 Flags = AutoClose;
525 switch (Mode)
526 {
527 case ReadOnly:
528 iFd = open(FileName.c_str(),O_RDONLY);
529 break;
530
531 case WriteEmpty:
532 {
533 struct stat Buf;
534 if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
535 unlink(FileName.c_str());
536 iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
537 break;
538 }
539
540 case WriteExists:
541 iFd = open(FileName.c_str(),O_RDWR);
542 break;
543
544 case WriteAny:
545 iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms);
546 break;
547
548 case WriteTemp:
549 unlink(FileName.c_str());
550 iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
551 break;
552 }
553
554 if (iFd < 0)
555 return _error->Errno("open",_("Could not open file %s"),FileName.c_str());
556
557 this->FileName = FileName;
558 SetCloseExec(iFd,true);
559 return true;
560 }
561 /*}}}*/
562 // FileFd::~File - Closes the file /*{{{*/
563 // ---------------------------------------------------------------------
564 /* If the proper modes are selected then we close the Fd and possibly
565 unlink the file on error. */
566 FileFd::~FileFd()
567 {
568 Close();
569 }
570 /*}}}*/
571 // FileFd::Read - Read a bit of the file /*{{{*/
572 // ---------------------------------------------------------------------
573 /* We are carefull to handle interruption by a signal while reading
574 gracefully. */
575 bool FileFd::Read(void *To,unsigned long Size,unsigned long *Actual)
576 {
577 int Res;
578 errno = 0;
579 if (Actual != 0)
580 *Actual = 0;
581
582 do
583 {
584 Res = read(iFd,To,Size);
585 if (Res < 0 && errno == EINTR)
586 continue;
587 if (Res < 0)
588 {
589 Flags |= Fail;
590 return _error->Errno("read",_("Read error"));
591 }
592
593 To = (char *)To + Res;
594 Size -= Res;
595 if (Actual != 0)
596 *Actual += Res;
597 }
598 while (Res > 0 && Size > 0);
599
600 if (Size == 0)
601 return true;
602
603 // Eof handling
604 if (Actual != 0)
605 {
606 Flags |= HitEof;
607 return true;
608 }
609
610 Flags |= Fail;
611 return _error->Error(_("read, still have %lu to read but none left"),Size);
612 }
613 /*}}}*/
614 // FileFd::Write - Write to the file /*{{{*/
615 // ---------------------------------------------------------------------
616 /* */
617 bool FileFd::Write(const void *From,unsigned long Size)
618 {
619 int Res;
620 errno = 0;
621 do
622 {
623 Res = write(iFd,From,Size);
624 if (Res < 0 && errno == EINTR)
625 continue;
626 if (Res < 0)
627 {
628 Flags |= Fail;
629 return _error->Errno("write",_("Write error"));
630 }
631
632 From = (char *)From + Res;
633 Size -= Res;
634 }
635 while (Res > 0 && Size > 0);
636
637 if (Size == 0)
638 return true;
639
640 Flags |= Fail;
641 return _error->Error(_("write, still have %lu to write but couldn't"),Size);
642 }
643 /*}}}*/
644 // FileFd::Seek - Seek in the file /*{{{*/
645 // ---------------------------------------------------------------------
646 /* */
647 bool FileFd::Seek(unsigned long To)
648 {
649 if (lseek(iFd,To,SEEK_SET) != (signed)To)
650 {
651 Flags |= Fail;
652 return _error->Error("Unable to seek to %lu",To);
653 }
654
655 return true;
656 }
657 /*}}}*/
658 // FileFd::Skip - Seek in the file /*{{{*/
659 // ---------------------------------------------------------------------
660 /* */
661 bool FileFd::Skip(unsigned long Over)
662 {
663 if (lseek(iFd,Over,SEEK_CUR) < 0)
664 {
665 Flags |= Fail;
666 return _error->Error("Unable to seek ahead %lu",Over);
667 }
668
669 return true;
670 }
671 /*}}}*/
672 // FileFd::Truncate - Truncate the file /*{{{*/
673 // ---------------------------------------------------------------------
674 /* */
675 bool FileFd::Truncate(unsigned long To)
676 {
677 if (ftruncate(iFd,To) != 0)
678 {
679 Flags |= Fail;
680 return _error->Error("Unable to truncate to %lu",To);
681 }
682
683 return true;
684 }
685 /*}}}*/
686 // FileFd::Tell - Current seek position /*{{{*/
687 // ---------------------------------------------------------------------
688 /* */
689 unsigned long FileFd::Tell()
690 {
691 off_t Res = lseek(iFd,0,SEEK_CUR);
692 if (Res == (off_t)-1)
693 _error->Errno("lseek","Failed to determine the current file position");
694 return Res;
695 }
696 /*}}}*/
697 // FileFd::Size - Return the size of the file /*{{{*/
698 // ---------------------------------------------------------------------
699 /* */
700 unsigned long FileFd::Size()
701 {
702 struct stat Buf;
703 if (fstat(iFd,&Buf) != 0)
704 return _error->Errno("fstat","Unable to determine the file size");
705 return Buf.st_size;
706 }
707 /*}}}*/
708 // FileFd::Close - Close the file if the close flag is set /*{{{*/
709 // ---------------------------------------------------------------------
710 /* */
711 bool FileFd::Close()
712 {
713 bool Res = true;
714 if ((Flags & AutoClose) == AutoClose)
715 if (iFd >= 0 && close(iFd) != 0)
716 Res &= _error->Errno("close",_("Problem closing the file"));
717 iFd = -1;
718
719 if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
720 FileName.empty() == false)
721 if (unlink(FileName.c_str()) != 0)
722 Res &= _error->WarningE("unlnk",_("Problem unlinking the file"));
723 return Res;
724 }
725 /*}}}*/
726 // FileFd::Sync - Sync the file /*{{{*/
727 // ---------------------------------------------------------------------
728 /* */
729 bool FileFd::Sync()
730 {
731 #ifdef _POSIX_SYNCHRONIZED_IO
732 if (fsync(iFd) != 0)
733 return _error->Errno("sync",_("Problem syncing the file"));
734 #endif
735 return true;
736 }
737 /*}}}*/