]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: fileutl.cc,v 1.31 1999/09/30 06:30:34 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.
14 ##################################################################### */
16 // Include Files /*{{{*/
18 #pragma implementation "apt-pkg/fileutl.h"
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/error.h>
25 #include <sys/fcntl.h>
26 #include <sys/types.h>
33 // CopyFile - Buffered copy of a file /*{{{*/
34 // ---------------------------------------------------------------------
35 /* The caller is expected to set things so that failure causes erasure */
36 bool CopyFile(FileFd
&From
,FileFd
&To
)
38 if (From
.IsOpen() == false || To
.IsOpen() == false)
41 // Buffered copy between fds
42 unsigned char *Buf
= new unsigned char[64000];
43 unsigned long Size
= From
.Size();
46 unsigned long ToRead
= Size
;
50 if (From
.Read(Buf
,ToRead
) == false ||
51 To
.Write(Buf
,ToRead
) == false)
64 // GetLock - Gets a lock file /*{{{*/
65 // ---------------------------------------------------------------------
66 /* This will create an empty file of the given name and lock it. Once this
67 is done all other calls to GetLock in any other process will fail with
68 -1. The return result is the fd of the file, the call should call
69 close at some time. */
70 int GetLock(string File
,bool Errors
)
72 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,0640);
76 _error
->Errno("open","Could not open lock file %s",File
.c_str());
80 // Aquire a write lock
83 fl
.l_whence
= SEEK_SET
;
86 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
90 _error
->Warning("Not using locking for nfs mounted lock file %s",File
.c_str());
94 _error
->Errno("open","Could not get lock %s",File
.c_str());
102 // FileExists - Check if a file exists /*{{{*/
103 // ---------------------------------------------------------------------
105 bool FileExists(string File
)
108 if (stat(File
.c_str(),&Buf
) != 0)
113 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
114 // ---------------------------------------------------------------------
115 /* We return / on failure. */
118 // Stash the current dir.
121 if (getcwd(S
,sizeof(S
)-2) == 0)
123 unsigned int Len
= strlen(S
);
129 // flNotDir - Strip the directory from the filename /*{{{*/
130 // ---------------------------------------------------------------------
132 string
flNotDir(string File
)
134 string::size_type Res
= File
.rfind('/');
135 if (Res
== string::npos
)
138 return string(File
,Res
,Res
- File
.length());
141 // flNotFile - Strip the file from the directory name /*{{{*/
142 // ---------------------------------------------------------------------
144 string
flNotFile(string File
)
146 string::size_type Res
= File
.rfind('/');
147 if (Res
== string::npos
)
150 return string(File
,0,Res
);
153 // flNoLink - If file is a symlink then deref it /*{{{*/
154 // ---------------------------------------------------------------------
155 /* If the name is not a link then the returned path is the input. */
156 string
flNoLink(string File
)
159 if (lstat(File
.c_str(),&St
) != 0 || S_ISLNK(St
.st_mode
) == 0)
161 if (stat(File
.c_str(),&St
) != 0)
164 /* Loop resolving the link. There is no need to limit the number of
165 loops because the stat call above ensures that the symlink is not
173 if ((Res
= readlink(NFile
.c_str(),Buffer
,sizeof(Buffer
))) <= 0 ||
174 (unsigned)Res
>= sizeof(Buffer
))
177 // Append or replace the previous path
179 if (Buffer
[0] == '/')
182 NFile
= flNotFile(NFile
) + Buffer
;
184 // See if we are done
185 if (lstat(NFile
.c_str(),&St
) != 0)
187 if (S_ISLNK(St
.st_mode
) == 0)
192 // SetCloseExec - Set the close on exec flag /*{{{*/
193 // ---------------------------------------------------------------------
195 void SetCloseExec(int Fd
,bool Close
)
197 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
199 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
204 // SetNonBlock - Set the nonblocking flag /*{{{*/
205 // ---------------------------------------------------------------------
207 void SetNonBlock(int Fd
,bool Block
)
209 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
210 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
212 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
217 // WaitFd - Wait for a FD to become readable /*{{{*/
218 // ---------------------------------------------------------------------
219 /* This waits for a FD to become readable using select. It is usefull for
220 applications making use of non-blocking sockets. The timeout is
222 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
235 Res
= select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0));
237 while (Res
< 0 && errno
== EINTR
);
247 Res
= select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0));
249 while (Res
< 0 && errno
== EINTR
);
258 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
259 // ---------------------------------------------------------------------
260 /* This is used if you want to cleanse the environment for the forked
261 child, it fixes up the important signals and nukes all of the fds,
262 otherwise acts like normal fork. */
265 // Fork off the process
266 pid_t Process
= fork();
269 cerr
<< "FATAL -> Failed to fork." << endl
;
273 // Spawn the subprocess
277 signal(SIGPIPE
,SIG_DFL
);
278 signal(SIGQUIT
,SIG_DFL
);
279 signal(SIGINT
,SIG_DFL
);
280 signal(SIGWINCH
,SIG_DFL
);
281 signal(SIGCONT
,SIG_DFL
);
282 signal(SIGTSTP
,SIG_DFL
);
284 // Close all of our FDs - just in case
285 for (int K
= 3; K
!= 40; K
++)
286 fcntl(K
,F_SETFD
,FD_CLOEXEC
);
292 // ExecWait - Fancy waitpid /*{{{*/
293 // ---------------------------------------------------------------------
294 /* Waits for the given sub process. If Reap is set the no errors are
295 generated. Otherwise a failed subprocess will generate a proper descriptive
297 bool ExecWait(int Pid
,const char *Name
,bool Reap
)
302 // Wait and collect the error code
304 while (waitpid(Pid
,&Status
,0) != Pid
)
312 return _error
->Error("Waited, for %s but it wasn't there",Name
);
316 // Check for an error code.
317 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
321 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
322 return _error
->Error("Sub-process %s recieved a segmentation fault.",Name
);
324 if (WIFEXITED(Status
) != 0)
325 return _error
->Error("Sub-process %s returned an error code (%u)",Name
,WEXITSTATUS(Status
));
327 return _error
->Error("Sub-process %s exited unexpectedly",Name
);
334 // FileFd::Open - Open a file /*{{{*/
335 // ---------------------------------------------------------------------
336 /* The most commonly used open mode combinations are given with Mode */
337 bool FileFd::Open(string FileName
,OpenMode Mode
, unsigned long Perms
)
344 iFd
= open(FileName
.c_str(),O_RDONLY
);
350 if (stat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
351 unlink(FileName
.c_str());
352 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
357 iFd
= open(FileName
.c_str(),O_RDWR
);
361 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
366 return _error
->Errno("open","Could not open file %s",FileName
.c_str());
368 this->FileName
= FileName
;
369 SetCloseExec(iFd
,true);
373 // FileFd::~File - Closes the file /*{{{*/
374 // ---------------------------------------------------------------------
375 /* If the proper modes are selected then we close the Fd and possibly
376 unlink the file on error. */
382 // FileFd::Read - Read a bit of the file /*{{{*/
383 // ---------------------------------------------------------------------
384 /* We are carefull to handle interruption by a signal while reading
386 bool FileFd::Read(void *To
,unsigned long Size
,bool AllowEof
)
392 Res
= read(iFd
,To
,Size
);
393 if (Res
< 0 && errno
== EINTR
)
398 return _error
->Errno("read","Read error");
401 To
= (char *)To
+ Res
;
404 while (Res
> 0 && Size
> 0);
410 if (AllowEof
== true)
417 return _error
->Error("read, still have %u to read but none left",Size
);
420 // FileFd::Write - Write to the file /*{{{*/
421 // ---------------------------------------------------------------------
423 bool FileFd::Write(const void *From
,unsigned long Size
)
429 Res
= write(iFd
,From
,Size
);
430 if (Res
< 0 && errno
== EINTR
)
435 return _error
->Errno("write","Write error");
438 From
= (char *)From
+ Res
;
441 while (Res
> 0 && Size
> 0);
447 return _error
->Error("write, still have %u to write but couldn't",Size
);
450 // FileFd::Seek - Seek in the file /*{{{*/
451 // ---------------------------------------------------------------------
453 bool FileFd::Seek(unsigned long To
)
455 if (lseek(iFd
,To
,SEEK_SET
) != (signed)To
)
458 return _error
->Error("Unable to seek to %u",To
);
464 // FileFd::Skip - Seek in the file /*{{{*/
465 // ---------------------------------------------------------------------
467 bool FileFd::Skip(unsigned long Over
)
469 if (lseek(iFd
,Over
,SEEK_CUR
) < 0)
472 return _error
->Error("Unable to seek ahead %u",Over
);
478 // FileFd::Truncate - Truncate the file /*{{{*/
479 // ---------------------------------------------------------------------
481 bool FileFd::Truncate(unsigned long To
)
483 if (ftruncate(iFd
,To
) != 0)
486 return _error
->Error("Unable to truncate to %u",To
);
492 // FileFd::Tell - Current seek position /*{{{*/
493 // ---------------------------------------------------------------------
495 unsigned long FileFd::Tell()
497 off_t Res
= lseek(iFd
,0,SEEK_CUR
);
498 if (Res
== (off_t
)-1)
499 _error
->Errno("lseek","Failed to determine the current file position");
503 // FileFd::Size - Return the size of the file /*{{{*/
504 // ---------------------------------------------------------------------
506 unsigned long FileFd::Size()
509 if (fstat(iFd
,&Buf
) != 0)
510 return _error
->Errno("fstat","Unable to determine the file size");
514 // FileFd::Close - Close the file if the close flag is set /*{{{*/
515 // ---------------------------------------------------------------------
520 if ((Flags
& AutoClose
) == AutoClose
)
521 if (iFd
>= 0 && close(iFd
) != 0)
522 Res
&= _error
->Errno("close","Problem closing the file");
525 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
526 FileName
.empty() == false)
527 if (unlink(FileName
.c_str()) != 0)
528 Res
&= _error
->Warning("unlnk","Problem unlinking the file");