]>
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 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe <jgg@debian.org>.
14 ##################################################################### */
16 // Include Files /*{{{*/
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/sptr.h>
20 #include <apt-pkg/configuration.h>
29 #include <sys/types.h>
39 // CopyFile - Buffered copy of a file /*{{{*/
40 // ---------------------------------------------------------------------
41 /* The caller is expected to set things so that failure causes erasure */
42 bool CopyFile(FileFd
&From
,FileFd
&To
)
44 if (From
.IsOpen() == false || To
.IsOpen() == false)
47 // Buffered copy between fds
48 SPtrArray
<unsigned char> Buf
= new unsigned char[64000];
49 unsigned long Size
= From
.Size();
52 unsigned long ToRead
= Size
;
56 if (From
.Read(Buf
,ToRead
) == false ||
57 To
.Write(Buf
,ToRead
) == false)
66 // GetLock - Gets a lock file /*{{{*/
67 // ---------------------------------------------------------------------
68 /* This will create an empty file of the given name and lock it. Once this
69 is done all other calls to GetLock in any other process will fail with
70 -1. The return result is the fd of the file, the call should call
71 close at some time. */
72 int GetLock(string File
,bool Errors
)
74 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,0640);
77 // Read only .. cant have locking problems there.
80 _error
->Warning(_("Not using locking for read only lock file %s"),File
.c_str());
81 return dup(0); // Need something for the caller to close
85 _error
->Errno("open",_("Could not open lock file %s"),File
.c_str());
87 // Feh.. We do this to distinguish the lock vs open case..
91 SetCloseExec(FD
,true);
93 // Aquire a write lock
96 fl
.l_whence
= SEEK_SET
;
99 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
103 _error
->Warning(_("Not using locking for nfs mounted lock file %s"),File
.c_str());
104 return dup(0); // Need something for the caller to close
107 _error
->Errno("open",_("Could not get lock %s"),File
.c_str());
118 // FileExists - Check if a file exists /*{{{*/
119 // ---------------------------------------------------------------------
121 bool FileExists(string File
)
124 if (stat(File
.c_str(),&Buf
) != 0)
129 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
130 // ---------------------------------------------------------------------
131 /* We return / on failure. */
134 // Stash the current dir.
137 if (getcwd(S
,sizeof(S
)-2) == 0)
139 unsigned int Len
= strlen(S
);
145 // flNotDir - Strip the directory from the filename /*{{{*/
146 // ---------------------------------------------------------------------
148 string
flNotDir(string File
)
150 string::size_type Res
= File
.rfind('/');
151 if (Res
== string::npos
)
154 return string(File
,Res
,Res
- File
.length());
157 // flNotFile - Strip the file from the directory name /*{{{*/
158 // ---------------------------------------------------------------------
159 /* Result ends in a / */
160 string
flNotFile(string File
)
162 string::size_type Res
= File
.rfind('/');
163 if (Res
== string::npos
)
166 return string(File
,0,Res
);
169 // flExtension - Return the extension for the file /*{{{*/
170 // ---------------------------------------------------------------------
172 string
flExtension(string File
)
174 string::size_type Res
= File
.rfind('.');
175 if (Res
== string::npos
)
178 return string(File
,Res
,Res
- File
.length());
181 // flNoLink - If file is a symlink then deref it /*{{{*/
182 // ---------------------------------------------------------------------
183 /* If the name is not a link then the returned path is the input. */
184 string
flNoLink(string File
)
187 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
189 if (stat(File
.c_str(),&St
) != 0)
192 /* Loop resolving the link. There is no need to limit the number of
193 loops because the stat call above ensures that the symlink is not
201 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
202 (unsigned)Res
>= sizeof(Buffer
))
205 // Append or replace the previous path
207 if (Buffer
[0] == '/')
210 NFile
= flNotFile(NFile
) + Buffer
;
212 // See if we are done
213 if (lstat(NFile
.c_str(),&St
) != 0)
215 if (S_ISLNK(St
.st_mode
) == 0)
220 // flCombine - Combine a file and a directory /*{{{*/
221 // ---------------------------------------------------------------------
222 /* If the file is an absolute path then it is just returned, otherwise
223 the directory is pre-pended to it. */
224 string
flCombine(string Dir
,string File
)
226 if (File
.empty() == true)
229 if (File
[0] == '/' || Dir
.empty() == true)
231 if (File
.length() >= 2 && File
[0] == '.' && File
[1] == '/')
233 if (Dir
[Dir
.length()-1] == '/')
235 return Dir
+ '/' + File
;
238 // SetCloseExec - Set the close on exec flag /*{{{*/
239 // ---------------------------------------------------------------------
241 void SetCloseExec(int Fd
,bool Close
)
243 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
245 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
250 // SetNonBlock - Set the nonblocking flag /*{{{*/
251 // ---------------------------------------------------------------------
253 void SetNonBlock(int Fd
,bool Block
)
255 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
256 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
258 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
263 // WaitFd - Wait for a FD to become readable /*{{{*/
264 // ---------------------------------------------------------------------
265 /* This waits for a FD to become readable using select. It is useful for
266 applications making use of non-blocking sockets. The timeout is
268 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
281 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
283 while (Res
< 0 && errno
== EINTR
);
293 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
295 while (Res
< 0 && errno
== EINTR
);
304 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
305 // ---------------------------------------------------------------------
306 /* This is used if you want to cleanse the environment for the forked
307 child, it fixes up the important signals and nukes all of the fds,
308 otherwise acts like normal fork. */
311 // Fork off the process
312 pid_t Process
= fork();
315 cerr
<< "FATAL -> Failed to fork." << endl
;
319 // Spawn the subprocess
323 signal(SIGPIPE
,SIG_DFL
);
324 signal(SIGQUIT
,SIG_DFL
);
325 signal(SIGINT
,SIG_DFL
);
326 signal(SIGWINCH
,SIG_DFL
);
327 signal(SIGCONT
,SIG_DFL
);
328 signal(SIGTSTP
,SIG_DFL
);
331 Configuration::Item
const *Opts
= _config
->Tree("APT::Keep-Fds");
332 if (Opts
!= 0 && Opts
->Child
!= 0)
335 for (; Opts
!= 0; Opts
= Opts
->Next
)
337 if (Opts
->Value
.empty() == true)
339 int fd
= atoi(Opts
->Value
.c_str());
344 // Close all of our FDs - just in case
345 for (int K
= 3; K
!= 40; K
++)
347 if(KeepFDs
.find(K
) == KeepFDs
.end())
348 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
355 // ExecWait - Fancy waitpid /*{{{*/
356 // ---------------------------------------------------------------------
357 /* Waits for the given sub process. If Reap is set then no errors are
358 generated. Otherwise a failed subprocess will generate a proper descriptive
360 bool ExecWait(pid_t Pid
,const char *Name
,bool Reap
)
365 // Wait and collect the error code
367 while (waitpid(Pid
,&Status
,0) != Pid
)
375 return _error
->Error(_("Waited for %s but it wasn't there"),Name
);
379 // Check for an error code.
380 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
384 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
385 return _error
->Error(_("Sub-process %s received a segmentation fault."),Name
);
387 if (WIFEXITED(Status
) != 0)
388 return _error
->Error(_("Sub-process %s returned an error code (%u)"),Name
,WEXITSTATUS(Status
));
390 return _error
->Error(_("Sub-process %s exited unexpectedly"),Name
);
397 // FileFd::Open - Open a file /*{{{*/
398 // ---------------------------------------------------------------------
399 /* The most commonly used open mode combinations are given with Mode */
400 bool FileFd::Open(string FileName
,OpenMode Mode
, unsigned long Perms
)
407 iFd
= open(FileName
.c_str(),O_RDONLY
);
413 if (lstat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
414 unlink(FileName
.c_str());
415 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
420 iFd
= open(FileName
.c_str(),O_RDWR
);
424 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
428 unlink(FileName
.c_str());
429 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_EXCL
,Perms
);
434 return _error
->Errno("open",_("Could not open file %s"),FileName
.c_str());
436 this->FileName
= FileName
;
437 SetCloseExec(iFd
,true);
441 // FileFd::~File - Closes the file /*{{{*/
442 // ---------------------------------------------------------------------
443 /* If the proper modes are selected then we close the Fd and possibly
444 unlink the file on error. */
450 // FileFd::Read - Read a bit of the file /*{{{*/
451 // ---------------------------------------------------------------------
452 /* We are carefull to handle interruption by a signal while reading
454 bool FileFd::Read(void *To
,unsigned long Size
,unsigned long *Actual
)
463 Res
= read(iFd
,To
,Size
);
464 if (Res
< 0 && errno
== EINTR
)
469 return _error
->Errno("read",_("Read error"));
472 To
= (char *)To
+ Res
;
477 while (Res
> 0 && Size
> 0);
490 return _error
->Error(_("read, still have %lu to read but none left"),Size
);
493 // FileFd::Write - Write to the file /*{{{*/
494 // ---------------------------------------------------------------------
496 bool FileFd::Write(const void *From
,unsigned long Size
)
502 Res
= write(iFd
,From
,Size
);
503 if (Res
< 0 && errno
== EINTR
)
508 return _error
->Errno("write",_("Write error"));
511 From
= (char *)From
+ Res
;
514 while (Res
> 0 && Size
> 0);
520 return _error
->Error(_("write, still have %lu to write but couldn't"),Size
);
523 // FileFd::Seek - Seek in the file /*{{{*/
524 // ---------------------------------------------------------------------
526 bool FileFd::Seek(unsigned long To
)
528 if (lseek(iFd
,To
,SEEK_SET
) != (signed)To
)
531 return _error
->Error("Unable to seek to %lu",To
);
537 // FileFd::Skip - Seek in the file /*{{{*/
538 // ---------------------------------------------------------------------
540 bool FileFd::Skip(unsigned long Over
)
542 if (lseek(iFd
,Over
,SEEK_CUR
) < 0)
545 return _error
->Error("Unable to seek ahead %lu",Over
);
551 // FileFd::Truncate - Truncate the file /*{{{*/
552 // ---------------------------------------------------------------------
554 bool FileFd::Truncate(unsigned long To
)
556 if (ftruncate(iFd
,To
) != 0)
559 return _error
->Error("Unable to truncate to %lu",To
);
565 // FileFd::Tell - Current seek position /*{{{*/
566 // ---------------------------------------------------------------------
568 unsigned long FileFd::Tell()
570 off_t Res
= lseek(iFd
,0,SEEK_CUR
);
571 if (Res
== (off_t
)-1)
572 _error
->Errno("lseek","Failed to determine the current file position");
576 // FileFd::Size - Return the size of the file /*{{{*/
577 // ---------------------------------------------------------------------
579 unsigned long FileFd::Size()
582 if (fstat(iFd
,&Buf
) != 0)
583 return _error
->Errno("fstat","Unable to determine the file size");
587 // FileFd::Close - Close the file if the close flag is set /*{{{*/
588 // ---------------------------------------------------------------------
593 if ((Flags
& AutoClose
) == AutoClose
)
594 if (iFd
>= 0 && close(iFd
) != 0)
595 Res
&= _error
->Errno("close",_("Problem closing the file"));
598 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
599 FileName
.empty() == false)
600 if (unlink(FileName
.c_str()) != 0)
601 Res
&= _error
->WarningE("unlnk",_("Problem unlinking the file"));
605 // FileFd::Sync - Sync the file /*{{{*/
606 // ---------------------------------------------------------------------
610 #ifdef _POSIX_SYNCHRONIZED_IO
612 return _error
->Errno("sync",_("Problem syncing the file"));