]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: fileutl.cc,v 1.21 1999/02/16 04:18:35 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>
31 // CopyFile - Buffered copy of a file /*{{{*/
32 // ---------------------------------------------------------------------
33 /* The caller is expected to set things so that failure causes erasure */
34 bool CopyFile(FileFd
&From
,FileFd
&To
)
36 if (From
.IsOpen() == false || To
.IsOpen() == false)
39 // Buffered copy between fds
40 unsigned char *Buf
= new unsigned char[64000];
42 while ((Size
= read(From
.Fd(),Buf
,64000)) > 0)
44 if (To
.Write(Buf
,Size
) == false)
55 // GetLock - Gets a lock file /*{{{*/
56 // ---------------------------------------------------------------------
57 /* This will create an empty file of the given name and lock it. Once this
58 is done all other calls to GetLock in any other process will fail with
59 -1. The return result is the fd of the file, the call should call
60 close at some time. */
61 int GetLock(string File
,bool Errors
)
63 int FD
= open(File
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,0640);
67 _error
->Errno("open","Could not open lock file %s",File
.c_str());
71 // Aquire a write lock
74 fl
.l_whence
= SEEK_SET
;
77 if (fcntl(FD
,F_SETLK
,&fl
) == -1)
80 _error
->Errno("open","Could not get lock %s",File
.c_str());
88 // FileExists - Check if a file exists /*{{{*/
89 // ---------------------------------------------------------------------
91 bool FileExists(string File
)
94 if (stat(File
.c_str(),&Buf
) != 0)
99 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
100 // ---------------------------------------------------------------------
101 /* We return / on failure. */
104 // Stash the current dir.
107 if (getcwd(S
,sizeof(S
)-2) == 0)
109 unsigned int Len
= strlen(S
);
115 // flNotDir - Strip the directory from the filename /*{{{*/
116 // ---------------------------------------------------------------------
118 string
flNotDir(string File
)
120 string::size_type Res
= File
.rfind('/');
121 if (Res
== string::npos
)
124 return string(File
,Res
,Res
- File
.length());
127 // flNotFile - Strip the file from the directory name /*{{{*/
128 // ---------------------------------------------------------------------
130 string
flNotFile(string File
)
132 string::size_type Res
= File
.rfind('/');
133 if (Res
== string::npos
)
136 return string(File
,0,Res
);
139 // SetCloseExec - Set the close on exec flag /*{{{*/
140 // ---------------------------------------------------------------------
142 void SetCloseExec(int Fd
,bool Close
)
144 if (fcntl(Fd
,F_SETFD
,(Close
== false)?0:FD_CLOEXEC
) != 0)
146 cerr
<< "FATAL -> Could not set close on exec " << strerror(errno
) << endl
;
151 // SetNonBlock - Set the nonblocking flag /*{{{*/
152 // ---------------------------------------------------------------------
154 void SetNonBlock(int Fd
,bool Block
)
156 int Flags
= fcntl(Fd
,F_GETFL
) & (~O_NONBLOCK
);
157 if (fcntl(Fd
,F_SETFL
,Flags
| ((Block
== false)?0:O_NONBLOCK
)) != 0)
159 cerr
<< "FATAL -> Could not set non-blocking flag " << strerror(errno
) << endl
;
164 // WaitFd - Wait for a FD to become readable /*{{{*/
165 // ---------------------------------------------------------------------
166 /* This waits for a FD to become readable using select. It is usefull for
167 applications making use of non-blocking sockets. */
168 bool WaitFd(int Fd
,bool write
,unsigned long timeout
)
174 tv
.tv_sec
= timeout
/ 1000000;
175 tv
.tv_usec
= timeout
% 1000000;
178 if (select(Fd
+1,0,&Set
,0,(timeout
!= 0?&tv
:0)) <= 0)
183 if (select(Fd
+1,&Set
,0,0,(timeout
!= 0?&tv
:0)) <= 0)
191 // FileFd::FileFd - Open a file /*{{{*/
192 // ---------------------------------------------------------------------
193 /* The most commonly used open mode combinations are given with Mode */
194 FileFd::FileFd(string FileName
,OpenMode Mode
, unsigned long Perms
)
200 iFd
= open(FileName
.c_str(),O_RDONLY
);
206 if (stat(FileName
.c_str(),&Buf
) == 0 && S_ISLNK(Buf
.st_mode
))
207 unlink(FileName
.c_str());
208 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
| O_TRUNC
,Perms
);
213 iFd
= open(FileName
.c_str(),O_RDWR
);
217 iFd
= open(FileName
.c_str(),O_RDWR
| O_CREAT
,Perms
);
222 _error
->Errno("open","Could not open file %s",FileName
.c_str());
225 this->FileName
= FileName
;
226 SetCloseExec(iFd
,true);
230 // FileFd::~File - Closes the file /*{{{*/
231 // ---------------------------------------------------------------------
232 /* If the proper modes are selected then we close the Fd and possibly
233 unlink the file on error. */
239 // FileFd::Read - Read a bit of the file /*{{{*/
240 // ---------------------------------------------------------------------
242 bool FileFd::Read(void *To
,unsigned long Size
)
244 if (read(iFd
,To
,Size
) != (signed)Size
)
247 return _error
->Errno("read","Read error");
253 // FileFd::Write - Write to the file /*{{{*/
254 // ---------------------------------------------------------------------
256 bool FileFd::Write(const void *From
,unsigned long Size
)
258 if (write(iFd
,From
,Size
) != (signed)Size
)
261 return _error
->Errno("write","Write error");
267 // FileFd::Seek - Seek in the file /*{{{*/
268 // ---------------------------------------------------------------------
270 bool FileFd::Seek(unsigned long To
)
272 if (lseek(iFd
,To
,SEEK_SET
) != (signed)To
)
275 return _error
->Error("Unable to seek to %u",To
);
281 // FileFd::Tell - Current seek position /*{{{*/
282 // ---------------------------------------------------------------------
284 unsigned long FileFd::Tell()
286 off_t Res
= lseek(iFd
,0,SEEK_CUR
);
287 if (Res
== (off_t
)-1)
288 _error
->Errno("lseek","Failed to determine the current file position");
292 // FileFd::Size - Return the size of the file /*{{{*/
293 // ---------------------------------------------------------------------
295 unsigned long FileFd::Size()
298 if (fstat(iFd
,&Buf
) != 0)
299 return _error
->Errno("fstat","Unable to determine the file size");
303 // FileFd::Close - Close the file if the close flag is set /*{{{*/
304 // ---------------------------------------------------------------------
309 if ((Flags
& AutoClose
) == AutoClose
)
310 if (iFd
>= 0 && close(iFd
) != 0)
311 Res
&= _error
->Errno("close","Problem closing the file");
314 if ((Flags
& Fail
) == Fail
&& (Flags
& DelOnFail
) == DelOnFail
&&
315 FileName
.empty() == false)
316 if (unlink(FileName
.c_str()) != 0)
317 Res
&= _error
->Warning("unlnk","Problem unlinking the file");