]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $
4 /* ######################################################################
8 CopyFile - Buffered copy of a single file
9 GetLock - dpkg compatible lock file manipulation (fcntl)
11 Most of this source is placed in the Public Domain, do with it what
13 It was originally written by Jason Gunthorpe <jgg@debian.org>.
15 The exception is RunScripts() it is under the GPLv2
17 ##################################################################### */
19 // Include Files /*{{{*/
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/sptr.h>
24 #include <apt-pkg/configuration.h>
35 #include <sys/types.h>
47 // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
48 // ---------------------------------------------------------------------
50 bool RunScripts(const char *Cnf
)
52 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
53 if (Opts
== 0 || Opts
->Child
== 0)
57 // Fork for running the system calls
58 pid_t Child
= ExecFork();
63 if (chdir("/tmp/") != 0)
66 unsigned int Count
= 1;
67 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
69 if (Opts
->Value
.empty() == true)
72 if (system(Opts
->Value
.c_str()) != 0)
80 while (waitpid(Child
,&Status
,0) != Child
)
84 return _error
->Errno("waitpid","Couldn't wait for subprocess");
87 // Restore sig int/quit
88 signal(SIGQUIT
,SIG_DFL
);
89 signal(SIGINT
,SIG_DFL
);
91 // Check for an error code.
92 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
94 unsigned int Count
= WEXITSTATUS(Status
);
98 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
99 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
102 return _error
->Error("Sub-process returned an error code");
109 // CopyFile - Buffered copy of a file /*{{{*/
110 // ---------------------------------------------------------------------
111 /* The caller is expected to set things so that failure causes erasure */
112 bool CopyFile(FileFd
&From
,FileFd
&To
)
114 if (From
.IsOpen() == false || To
.IsOpen() == false)
117 // Buffered copy between fds
118 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
119 unsigned long Size
= From
.Size();
122 unsigned long ToRead
= Size
;
126 if (From
.Read(Buf
,ToRead
) == false ||
127 To
.Write(Buf
,ToRead
) == false)
136 // GetLock - Gets a lock file /*{{{*/
137 // ---------------------------------------------------------------------
138 /* This will create an empty file of the given name and lock it. Once this
139 is done all other calls to GetLock in any other process will fail with
140 -1. The return result is the fd of the file, the call should call
141 close at some time. */
142 int GetLock(string File
,bool Errors
)
144 // GetLock() is used in aptitude on directories with public-write access
145 // Use O_NOFOLLOW here to prevent symlink traversal attacks
146 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_NOFOLLOW
,0640);
149 // Read only .. cant have locking problems there.
152 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
153 return dup(0); // Need something for the caller to close
157 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
159 // Feh.. We do this to distinguish the lock vs open case..
163 SetCloseExec(FD
,true);
165 // Aquire a write lock
168 fl
.l_whence
= SEEK_SET
;
171 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
175 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
176 return dup(0); // Need something for the caller to close
179 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
190 // FileExists - Check if a file exists /*{{{*/
191 // ---------------------------------------------------------------------
193 bool FileExists(string File
)
196 if (stat(File
.c_str(),&Buf
) != 0)
201 // DirectoryExists - Check if a directory exists and is really one /*{{{*/
202 // ---------------------------------------------------------------------
204 bool DirectoryExists(string
const &Path
)
207 if (stat(Path
.c_str(),&Buf
) != 0)
209 return ((Buf
.st_mode
& S_IFDIR
) != 0);
212 // CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/
213 // ---------------------------------------------------------------------
214 /* This method will create all directories needed for path in good old
215 mkdir -p style but refuses to do this if Parent is not a prefix of
216 this Path. Example: /var/cache/ and /var/cache/apt/archives are given,
217 so it will create apt/archives if /var/cache exists - on the other
218 hand if the parent is /var/lib the creation will fail as this path
219 is not a parent of the path to be generated. */
220 bool CreateDirectory(string
const &Parent
, string
const &Path
)
222 if (Parent
.empty() == true || Path
.empty() == true)
225 if (DirectoryExists(Path
) == true)
228 if (DirectoryExists(Parent
) == false)
231 // we are not going to create directories "into the blue"
232 if (Path
.find(Parent
, 0) != 0)
235 vector
<string
> const dirs
= VectorizeString(Path
.substr(Parent
.size()), '/');
236 string progress
= Parent
;
237 for (vector
<string
>::const_iterator d
= dirs
.begin(); d
!= dirs
.end(); ++d
)
239 if (d
->empty() == true)
242 progress
.append("/").append(*d
);
243 if (DirectoryExists(progress
) == true)
246 if (mkdir(progress
.c_str(), 0755) != 0)
252 // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/
253 // ---------------------------------------------------------------------
254 /* If an extension is given only files with this extension are included
255 in the returned vector, otherwise every "normal" file is included. */
256 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, string
const &Ext
,
257 bool const &SortList
, bool const &AllowNoExt
)
259 std::vector
<string
> ext
;
261 if (Ext
.empty() == false)
263 if (AllowNoExt
== true && ext
.empty() == false)
265 return GetListOfFilesInDir(Dir
, ext
, SortList
);
267 std::vector
<string
> GetListOfFilesInDir(string
const &Dir
, std::vector
<string
> const &Ext
,
268 bool const &SortList
)
270 // Attention debuggers: need to be set with the environment config file!
271 bool const Debug
= _config
->FindB("Debug::GetListOfFilesInDir", false);
274 std::clog
<< "Accept in " << Dir
<< " only files with the following " << Ext
.size() << " extensions:" << std::endl
;
275 if (Ext
.empty() == true)
276 std::clog
<< "\tNO extension" << std::endl
;
278 for (std::vector
<string
>::const_iterator e
= Ext
.begin();
280 std::clog
<< '\t' << (e
->empty() == true ? "NO" : *e
) << " extension" << std::endl
;
283 std::vector
<string
> List
;
284 DIR *D
= opendir(Dir
.c_str());
287 _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
291 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
293 // skip "hidden" files
294 if (Ent
->d_name
[0] == '.')
297 // check for accepted extension:
298 // no extension given -> periods are bad as hell!
299 // extensions given -> "" extension allows no extension
300 if (Ext
.empty() == false)
302 string d_ext
= flExtension(Ent
->d_name
);
303 if (d_ext
== Ent
->d_name
) // no extension
305 if (std::find(Ext
.begin(), Ext
.end(), "") == Ext
.end())
308 std::clog
<< "Bad file: " << Ent
->d_name
<< " → no extension" << std::endl
;
312 else if (std::find(Ext
.begin(), Ext
.end(), d_ext
) == Ext
.end())
315 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad extension »" << flExtension(Ent
->d_name
) << "«" << std::endl
;
320 // Skip bad filenames ala run-parts
321 const char *C
= Ent
->d_name
;
323 if (isalpha(*C
) == 0 && isdigit(*C
) == 0
324 && *C
!= '_' && *C
!= '-') {
325 // no required extension -> dot is a bad character
326 if (*C
== '.' && Ext
.empty() == false)
331 // we don't reach the end of the name -> bad character included
335 std::clog
<< "Bad file: " << Ent
->d_name
<< " → bad character »"
336 << *C
<< "« in filename (period allowed: " << (Ext
.empty() ? "no" : "yes") << ")" << std::endl
;
340 // skip filenames which end with a period. These are never valid
344 std::clog
<< "Bad file: " << Ent
->d_name
<< " → Period as last character" << std::endl
;
348 // Make sure it is a file and not something else
349 string
const File
= flCombine(Dir
,Ent
->d_name
);
351 if (stat(File
.c_str(),&St
) != 0 || S_ISREG(St
.st_mode
) == 0)
354 std::clog
<< "Bad file: " << Ent
->d_name
<< " → stat says not a good file" << std::endl
;
359 std::clog
<< "Accept file: " << Ent
->d_name
<< " in " << Dir
<< std::endl
;
360 List
.push_back(File
);
364 if (SortList
== true)
365 std::sort(List
.begin(),List
.end());
369 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
370 // ---------------------------------------------------------------------
371 /* We return / on failure. */
374 // Stash the current dir.
377 if (getcwd(S
,sizeof(S
)-2) == 0)
379 unsigned int Len
= strlen(S
);
385 // flNotDir - Strip the directory from the filename /*{{{*/
386 // ---------------------------------------------------------------------
388 string
flNotDir(string File
)
390 string::size_type Res
= File
.rfind('/');
391 if (Res
== string::npos
)
394 return string(File
,Res
,Res
- File
.length());
397 // flNotFile - Strip the file from the directory name /*{{{*/
398 // ---------------------------------------------------------------------
399 /* Result ends in a / */
400 string
flNotFile(string File
)
402 string::size_type Res
= File
.rfind('/');
403 if (Res
== string::npos
)
406 return string(File
,0,Res
);
409 // flExtension - Return the extension for the file /*{{{*/
410 // ---------------------------------------------------------------------
412 string
flExtension(string File
)
414 string::size_type Res
= File
.rfind('.');
415 if (Res
== string::npos
)
418 return string(File
,Res
,Res
- File
.length());
421 // flNoLink - If file is a symlink then deref it /*{{{*/
422 // ---------------------------------------------------------------------
423 /* If the name is not a link then the returned path is the input. */
424 string
flNoLink(string File
)
427 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
429 if (stat(File
.c_str(),&St
) != 0)
432 /* Loop resolving the link. There is no need to limit the number of
433 loops because the stat call above ensures that the symlink is not
441 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
442 (unsigned)Res
>= sizeof(Buffer
))
445 // Append or replace the previous path
447 if (Buffer
[0] == '/')
450 NFile
= flNotFile(NFile
) + Buffer
;
452 // See if we are done
453 if (lstat(NFile
.c_str(),&St
) != 0)
455 if (S_ISLNK(St
.st_mode
) == 0)
460 // flCombine - Combine a file and a directory /*{{{*/
461 // ---------------------------------------------------------------------
462 /* If the file is an absolute path then it is just returned, otherwise
463 the directory is pre-pended to it. */
464 string
flCombine(string Dir
,string File
)
466 if (File
.empty() == true)
469 if (File
[0] == '/' || Dir
.empty() == true)
471 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
473 if (Dir
[Dir
.length()-1] == '/')
475 return Dir
+ '/' + File
;
478 // SetCloseExec - Set the close on exec flag /*{{{*/
479 // ---------------------------------------------------------------------
481 void SetCloseExec(int Fd
,bool Close
)
483 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
485 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
490 // SetNonBlock - Set the nonblocking flag /*{{{*/
491 // ---------------------------------------------------------------------
493 void SetNonBlock(int Fd
,bool Block
)
495 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
496 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
498 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
503 // WaitFd - Wait for a FD to become readable /*{{{*/
504 // ---------------------------------------------------------------------
505 /* This waits for a FD to become readable using select. It is useful for
506 applications making use of non-blocking sockets. The timeout is
508 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
521 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
523 while (Res
< 0 && errno
== EINTR
);
533 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
535 while (Res
< 0 && errno
== EINTR
);
544 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
545 // ---------------------------------------------------------------------
546 /* This is used if you want to cleanse the environment for the forked
547 child, it fixes up the important signals and nukes all of the fds,
548 otherwise acts like normal fork. */
551 // Fork off the process
552 pid_t Process
= fork();
555 cerr
<< "FATAL -> Failed to fork." << endl
;
559 // Spawn the subprocess
563 signal(SIGPIPE
,SIG_DFL
);
564 signal(SIGQUIT
,SIG_DFL
);
565 signal(SIGINT
,SIG_DFL
);
566 signal(SIGWINCH
,SIG_DFL
);
567 signal(SIGCONT
,SIG_DFL
);
568 signal(SIGTSTP
,SIG_DFL
);
571 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
572 if (Opts
!= 0 && Opts
->Child
!= 0)
575 for (; Opts
!= 0; Opts
= Opts
->Next
)
577 if (Opts
->Value
.empty() == true)
579 int fd
= atoi(Opts
->Value
.c_str());
584 // Close all of our FDs - just in case
585 for (int K
= 3; K
!= 40; K
++)
587 if(KeepFDs
.find(K
) == KeepFDs
.end())
588 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
595 // ExecWait - Fancy waitpid /*{{{*/
596 // ---------------------------------------------------------------------
597 /* Waits for the given sub process. If Reap is set then no errors are
598 generated. Otherwise a failed subprocess will generate a proper descriptive
600 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
605 // Wait and collect the error code
607 while (waitpid(Pid
,&Status
,0) != Pid
)
615 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
619 // Check for an error code.
620 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
624 if (WIFSIGNALED(Status
) != 0)
626 if( WTERMSIG(Status
) == SIGSEGV
)
627 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
629 return _error
->Error(_("Sub-process %s received signal %u."),Name
, WTERMSIG(Status
));
632 if (WIFEXITED(Status
) != 0)
633 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
635 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
642 // FileFd::Open - Open a file /*{{{*/
643 // ---------------------------------------------------------------------
644 /* The most commonly used open mode combinations are given with Mode */
645 bool FileFd::Open(string FileName
,OpenMode Mode
, unsigned long Perms
)
652 iFd
= open(FileName
.c_str(),O_RDONLY
);
658 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
659 unlink(FileName
.c_str());
660 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
665 iFd
= open(FileName
.c_str(),O_RDWR
);
669 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
673 unlink(FileName
.c_str());
674 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
679 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
681 this->FileName
= FileName
;
682 SetCloseExec(iFd
,true);
686 // FileFd::~File - Closes the file /*{{{*/
687 // ---------------------------------------------------------------------
688 /* If the proper modes are selected then we close the Fd and possibly
689 unlink the file on error. */
695 // FileFd::Read - Read a bit of the file /*{{{*/
696 // ---------------------------------------------------------------------
697 /* We are carefull to handle interruption by a signal while reading
699 bool FileFd::Read(void *To
,unsigned long Size
,unsigned long *Actual
)
708 Res
= read(iFd
,To
,Size
);
709 if (Res
< 0 && errno
== EINTR
)
714 return _error
->Errno("read",_("Read error"));
717 To
= (char *)To
+ Res
;
722 while (Res
> 0 && Size
> 0);
735 return _error
->Error(_("read, still have %lu to read but none left"),Size
);
738 // FileFd::Write - Write to the file /*{{{*/
739 // ---------------------------------------------------------------------
741 bool FileFd::Write(const void *From
,unsigned long Size
)
747 Res
= write(iFd
,From
,Size
);
748 if (Res
< 0 && errno
== EINTR
)
753 return _error
->Errno("write",_("Write error"));
756 From
= (char *)From
+ Res
;
759 while (Res
> 0 && Size
> 0);
765 return _error
->Error(_("write, still have %lu to write but couldn't"),Size
);
768 // FileFd::Seek - Seek in the file /*{{{*/
769 // ---------------------------------------------------------------------
771 bool FileFd::Seek(unsigned long To
)
773 if (lseek(iFd
,To
,SEEK_SET
) != (signed)To
)
776 return _error
->Error("Unable to seek to %lu",To
);
782 // FileFd::Skip - Seek in the file /*{{{*/
783 // ---------------------------------------------------------------------
785 bool FileFd::Skip(unsigned long Over
)
787 if (lseek(iFd
,Over
,SEEK_CUR
) < 0)
790 return _error
->Error("Unable to seek ahead %lu",Over
);
796 // FileFd::Truncate - Truncate the file /*{{{*/
797 // ---------------------------------------------------------------------
799 bool FileFd::Truncate(unsigned long To
)
801 if (ftruncate(iFd
,To
) != 0)
804 return _error
->Error("Unable to truncate to %lu",To
);
810 // FileFd::Tell - Current seek position /*{{{*/
811 // ---------------------------------------------------------------------
813 unsigned long FileFd::Tell()
815 off_t Res
= lseek(iFd
,0,SEEK_CUR
);
816 if (Res
== (off_t
)-1)
817 _error
->Errno("lseek","Failed to determine the current file position");
821 // FileFd::Size - Return the size of the file /*{{{*/
822 // ---------------------------------------------------------------------
824 unsigned long FileFd::Size()
827 if (fstat(iFd
,&Buf
) != 0)
828 return _error
->Errno("fstat","Unable to determine the file size");
832 // FileFd::Close - Close the file if the close flag is set /*{{{*/
833 // ---------------------------------------------------------------------
838 if ((Flags
& AutoClose
) == AutoClose
)
839 if (iFd
>= 0 && close(iFd
) != 0)
840 Res
&= _error
->Errno("close",_("Problem closing the file"));
843 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
844 FileName
.empty() == false)
845 if (unlink(FileName
.c_str()) != 0)
846 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file"));
850 // FileFd::Sync - Sync the file /*{{{*/
851 // ---------------------------------------------------------------------
855 #ifdef _POSIX_SYNCHRONIZED_IO
857 return _error
->Errno("sync",_("Problem syncing the file"));