Tag file can read from unseekable objects
[ntk/apt.git] / apt-pkg / contrib / fileutl.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: fileutl.cc,v 1.38 2001/04/22 05:42:52 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 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe.
13
14 ##################################################################### */
15 /*}}}*/
16 // Include Files /*{{{*/
17 #ifdef __GNUG__
18 #pragma implementation "apt-pkg/fileutl.h"
19 #endif
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/sptr.h>
23
24 #include <apti18n.h>
25
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/wait.h>
32 #include <signal.h>
33 #include <errno.h>
34 /*}}}*/
35
36 // CopyFile - Buffered copy of a file /*{{{*/
37 // ---------------------------------------------------------------------
38 /* The caller is expected to set things so that failure causes erasure */
39 bool CopyFile(FileFd &From,FileFd &To)
40 {
41 if (From.IsOpen() == false || To.IsOpen() == false)
42 return false;
43
44 // Buffered copy between fds
45 SPtrArray<unsigned char> Buf = new unsigned char[64000];
46 unsigned long Size = From.Size();
47 while (Size != 0)
48 {
49 unsigned long ToRead = Size;
50 if (Size > 64000)
51 ToRead = 64000;
52
53 if (From.Read(Buf,ToRead) == false ||
54 To.Write(Buf,ToRead) == false)
55 return false;
56
57 Size -= ToRead;
58 }
59
60 return true;
61 }
62 /*}}}*/
63 // GetLock - Gets a lock file /*{{{*/
64 // ---------------------------------------------------------------------
65 /* This will create an empty file of the given name and lock it. Once this
66 is done all other calls to GetLock in any other process will fail with
67 -1. The return result is the fd of the file, the call should call
68 close at some time. */
69 int GetLock(string File,bool Errors)
70 {
71 int FD = open(File.c_str(),O_RDWR | O_CREAT | O_TRUNC,0640);
72 if (FD < 0)
73 {
74 // Read only .. cant have locking problems there.
75 if (errno == EROFS)
76 {
77 _error->Warning(_("Not using locking for read only lock file %s"),File.c_str());
78 return dup(0); // Need something for the caller to close
79 }
80
81 if (Errors == true)
82 _error->Errno("open",_("Could not open lock file %s"),File.c_str());
83
84 // Feh.. We do this to distinguish the lock vs open case..
85 errno = EPERM;
86 return -1;
87 }
88 SetCloseExec(FD,true);
89
90 // Aquire a write lock
91 struct flock fl;
92 fl.l_type = F_WRLCK;
93 fl.l_whence = SEEK_SET;
94 fl.l_start = 0;
95 fl.l_len = 0;
96 if (fcntl(FD,F_SETLK,&fl) == -1)
97 {
98 if (errno == ENOLCK)
99 {
100 _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str());
101 return dup(0); // Need something for the caller to close
102 }
103 if (Errors == true)
104 _error->Errno("open",_("Could not get lock %s"),File.c_str());
105
106 int Tmp = errno;
107 close(FD);
108 errno = Tmp;
109 return -1;
110 }
111
112 return FD;
113 }
114 /*}}}*/
115 // FileExists - Check if a file exists /*{{{*/
116 // ---------------------------------------------------------------------
117 /* */
118 bool FileExists(string File)
119 {
120 struct stat Buf;
121 if (stat(File.c_str(),&Buf) != 0)
122 return false;
123 return true;
124 }
125 /*}}}*/
126 // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
127 // ---------------------------------------------------------------------
128 /* We return / on failure. */
129 string SafeGetCWD()
130 {
131 // Stash the current dir.
132 char S[300];
133 S[0] = 0;
134 if (getcwd(S,sizeof(S)-2) == 0)
135 return "/";
136 unsigned int Len = strlen(S);
137 S[Len] = '/';
138 S[Len+1] = 0;
139 return S;
140 }
141 /*}}}*/
142 // flNotDir - Strip the directory from the filename /*{{{*/
143 // ---------------------------------------------------------------------
144 /* */
145 string flNotDir(string File)
146 {
147 string::size_type Res = File.rfind('/');
148 if (Res == string::npos)
149 return File;
150 Res++;
151 return string(File,Res,Res - File.length());
152 }
153 /*}}}*/
154 // flNotFile - Strip the file from the directory name /*{{{*/
155 // ---------------------------------------------------------------------
156 /* */
157 string flNotFile(string File)
158 {
159 string::size_type Res = File.rfind('/');
160 if (Res == string::npos)
161 return File;
162 Res++;
163 return string(File,0,Res);
164 }
165 /*}}}*/
166 // flExtension - Return the extension for the file /*{{{*/
167 // ---------------------------------------------------------------------
168 /* */
169 string flExtension(string File)
170 {
171 string::size_type Res = File.rfind('.');
172 if (Res == string::npos)
173 return File;
174 Res++;
175 return string(File,Res,Res - File.length());
176 }
177 /*}}}*/
178 // flNoLink - If file is a symlink then deref it /*{{{*/
179 // ---------------------------------------------------------------------
180 /* If the name is not a link then the returned path is the input. */
181 string flNoLink(string File)
182 {
183 struct stat St;
184 if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0)
185 return File;
186 if (stat(File.c_str(),&St) != 0)
187 return File;
188
189 /* Loop resolving the link. There is no need to limit the number of
190 loops because the stat call above ensures that the symlink is not
191 circular */
192 char Buffer[1024];
193 string NFile = File;
194 while (1)
195 {
196 // Read the link
197 int Res;
198 if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 ||
199 (unsigned)Res >= sizeof(Buffer))
200 return File;
201
202 // Append or replace the previous path
203 Buffer[Res] = 0;
204 if (Buffer[0] == '/')
205 NFile = Buffer;
206 else
207 NFile = flNotFile(NFile) + Buffer;
208
209 // See if we are done
210 if (lstat(NFile.c_str(),&St) != 0)
211 return File;
212 if (S_ISLNK(St.st_mode) == 0)
213 return NFile;
214 }
215 }
216 /*}}}*/
217 // flCombine - Combine a file and a directory /*{{{*/
218 // ---------------------------------------------------------------------
219 /* If the file is an absolute path then it is just returned, otherwise
220 the directory is pre-pended to it. */
221 string flCombine(string Dir,string File)
222 {
223 if (File.empty() == true)
224 return string();
225
226 if (File[0] == '/' || Dir.empty() == true)
227 return File;
228 if (File.length() >= 2 && File[0] == '.' && File[1] == '/')
229 return File;
230 if (Dir[Dir.length()-1] == '/')
231 return Dir + File;
232 return Dir + '/' + File;
233 }
234 /*}}}*/
235 // SetCloseExec - Set the close on exec flag /*{{{*/
236 // ---------------------------------------------------------------------
237 /* */
238 void SetCloseExec(int Fd,bool Close)
239 {
240 if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0)
241 {
242 cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl;
243 exit(100);
244 }
245 }
246 /*}}}*/
247 // SetNonBlock - Set the nonblocking flag /*{{{*/
248 // ---------------------------------------------------------------------
249 /* */
250 void SetNonBlock(int Fd,bool Block)
251 {
252 int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK);
253 if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0)
254 {
255 cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl;
256 exit(100);
257 }
258 }
259 /*}}}*/
260 // WaitFd - Wait for a FD to become readable /*{{{*/
261 // ---------------------------------------------------------------------
262 /* This waits for a FD to become readable using select. It is useful for
263 applications making use of non-blocking sockets. The timeout is
264 in seconds. */
265 bool WaitFd(int Fd,bool write,unsigned long timeout)
266 {
267 fd_set Set;
268 struct timeval tv;
269 FD_ZERO(&Set);
270 FD_SET(Fd,&Set);
271 tv.tv_sec = timeout;
272 tv.tv_usec = 0;
273 if (write == true)
274 {
275 int Res;
276 do
277 {
278 Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0));
279 }
280 while (Res < 0 && errno == EINTR);
281
282 if (Res <= 0)
283 return false;
284 }
285 else
286 {
287 int Res;
288 do
289 {
290 Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0));
291 }
292 while (Res < 0 && errno == EINTR);
293
294 if (Res <= 0)
295 return false;
296 }
297
298 return true;
299 }
300 /*}}}*/
301 // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
302 // ---------------------------------------------------------------------
303 /* This is used if you want to cleanse the environment for the forked
304 child, it fixes up the important signals and nukes all of the fds,
305 otherwise acts like normal fork. */
306 int ExecFork()
307 {
308 // Fork off the process
309 pid_t Process = fork();
310 if (Process < 0)
311 {
312 cerr << "FATAL -> Failed to fork." << endl;
313 exit(100);
314 }
315
316 // Spawn the subprocess
317 if (Process == 0)
318 {
319 // Setup the signals
320 signal(SIGPIPE,SIG_DFL);
321 signal(SIGQUIT,SIG_DFL);
322 signal(SIGINT,SIG_DFL);
323 signal(SIGWINCH,SIG_DFL);
324 signal(SIGCONT,SIG_DFL);
325 signal(SIGTSTP,SIG_DFL);
326
327 // Close all of our FDs - just in case
328 for (int K = 3; K != 40; K++)
329 fcntl(K,F_SETFD,FD_CLOEXEC);
330 }
331
332 return Process;
333 }
334 /*}}}*/
335 // ExecWait - Fancy waitpid /*{{{*/
336 // ---------------------------------------------------------------------
337 /* Waits for the given sub process. If Reap is set the no errors are
338 generated. Otherwise a failed subprocess will generate a proper descriptive
339 message */
340 bool ExecWait(int Pid,const char *Name,bool Reap)
341 {
342 if (Pid <= 1)
343 return true;
344
345 // Wait and collect the error code
346 int Status;
347 while (waitpid(Pid,&Status,0) != Pid)
348 {
349 if (errno == EINTR)
350 continue;
351
352 if (Reap == true)
353 return false;
354
355 return _error->Error(_("Waited, for %s but it wasn't there"),Name);
356 }
357
358
359 // Check for an error code.
360 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
361 {
362 if (Reap == true)
363 return false;
364 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
365 return _error->Error(_("Sub-process %s received a segmentation fault."),Name);
366
367 if (WIFEXITED(Status) != 0)
368 return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status));
369
370 return _error->Error(_("Sub-process %s exited unexpectedly"),Name);
371 }
372
373 return true;
374 }
375 /*}}}*/
376
377 // FileFd::Open - Open a file /*{{{*/
378 // ---------------------------------------------------------------------
379 /* The most commonly used open mode combinations are given with Mode */
380 bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
381 {
382 Close();
383 Flags = AutoClose;
384 switch (Mode)
385 {
386 case ReadOnly:
387 iFd = open(FileName.c_str(),O_RDONLY);
388 break;
389
390 case WriteEmpty:
391 {
392 struct stat Buf;
393 if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
394 unlink(FileName.c_str());
395 iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
396 break;
397 }
398
399 case WriteExists:
400 iFd = open(FileName.c_str(),O_RDWR);
401 break;
402
403 case WriteAny:
404 iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms);
405 break;
406
407 case WriteTemp:
408 unlink(FileName.c_str());
409 iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
410 break;
411 }
412
413 if (iFd < 0)
414 return _error->Errno("open",_("Could not open file %s"),FileName.c_str());
415
416 this->FileName = FileName;
417 SetCloseExec(iFd,true);
418 return true;
419 }
420 /*}}}*/
421 // FileFd::~File - Closes the file /*{{{*/
422 // ---------------------------------------------------------------------
423 /* If the proper modes are selected then we close the Fd and possibly
424 unlink the file on error. */
425 FileFd::~FileFd()
426 {
427 Close();
428 }
429 /*}}}*/
430 // FileFd::Read - Read a bit of the file /*{{{*/
431 // ---------------------------------------------------------------------
432 /* We are carefull to handle interruption by a signal while reading
433 gracefully. */
434 bool FileFd::Read(void *To,unsigned long Size,unsigned long *Actual)
435 {
436 int Res;
437 errno = 0;
438 if (Actual != 0)
439 *Actual = 0;
440
441 do
442 {
443 Res = read(iFd,To,Size);
444 if (Res < 0 && errno == EINTR)
445 continue;
446 if (Res < 0)
447 {
448 Flags |= Fail;
449 return _error->Errno("read",_("Read error"));
450 }
451
452 To = (char *)To + Res;
453 Size -= Res;
454 if (Actual != 0)
455 *Actual += Res;
456 }
457 while (Res > 0 && Size > 0);
458
459 if (Size == 0)
460 return true;
461
462 // Eof handling
463 if (Actual != 0)
464 {
465 Flags |= HitEof;
466 return true;
467 }
468
469 Flags |= Fail;
470 return _error->Error(_("read, still have %lu to read but none left"),Size);
471 }
472 /*}}}*/
473 // FileFd::Write - Write to the file /*{{{*/
474 // ---------------------------------------------------------------------
475 /* */
476 bool FileFd::Write(const void *From,unsigned long Size)
477 {
478 int Res;
479 errno = 0;
480 do
481 {
482 Res = write(iFd,From,Size);
483 if (Res < 0 && errno == EINTR)
484 continue;
485 if (Res < 0)
486 {
487 Flags |= Fail;
488 return _error->Errno("write",_("Write error"));
489 }
490
491 From = (char *)From + Res;
492 Size -= Res;
493 }
494 while (Res > 0 && Size > 0);
495
496 if (Size == 0)
497 return true;
498
499 Flags |= Fail;
500 return _error->Error(_("write, still have %lu to write but couldn't"),Size);
501 }
502 /*}}}*/
503 // FileFd::Seek - Seek in the file /*{{{*/
504 // ---------------------------------------------------------------------
505 /* */
506 bool FileFd::Seek(unsigned long To)
507 {
508 if (lseek(iFd,To,SEEK_SET) != (signed)To)
509 {
510 Flags |= Fail;
511 return _error->Error("Unable to seek to %lu",To);
512 }
513
514 return true;
515 }
516 /*}}}*/
517 // FileFd::Skip - Seek in the file /*{{{*/
518 // ---------------------------------------------------------------------
519 /* */
520 bool FileFd::Skip(unsigned long Over)
521 {
522 if (lseek(iFd,Over,SEEK_CUR) < 0)
523 {
524 Flags |= Fail;
525 return _error->Error("Unable to seek ahead %lu",Over);
526 }
527
528 return true;
529 }
530 /*}}}*/
531 // FileFd::Truncate - Truncate the file /*{{{*/
532 // ---------------------------------------------------------------------
533 /* */
534 bool FileFd::Truncate(unsigned long To)
535 {
536 if (ftruncate(iFd,To) != 0)
537 {
538 Flags |= Fail;
539 return _error->Error("Unable to truncate to %lu",To);
540 }
541
542 return true;
543 }
544 /*}}}*/
545 // FileFd::Tell - Current seek position /*{{{*/
546 // ---------------------------------------------------------------------
547 /* */
548 unsigned long FileFd::Tell()
549 {
550 off_t Res = lseek(iFd,0,SEEK_CUR);
551 if (Res == (off_t)-1)
552 _error->Errno("lseek","Failed to determine the current file position");
553 return Res;
554 }
555 /*}}}*/
556 // FileFd::Size - Return the size of the file /*{{{*/
557 // ---------------------------------------------------------------------
558 /* */
559 unsigned long FileFd::Size()
560 {
561 struct stat Buf;
562 if (fstat(iFd,&Buf) != 0)
563 return _error->Errno("fstat","Unable to determine the file size");
564 return Buf.st_size;
565 }
566 /*}}}*/
567 // FileFd::Close - Close the file if the close flag is set /*{{{*/
568 // ---------------------------------------------------------------------
569 /* */
570 bool FileFd::Close()
571 {
572 bool Res = true;
573 if ((Flags & AutoClose) == AutoClose)
574 if (iFd >= 0 && close(iFd) != 0)
575 Res &= _error->Errno("close",_("Problem closing the file"));
576 iFd = -1;
577
578 if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
579 FileName.empty() == false)
580 if (unlink(FileName.c_str()) != 0)
581 Res &= _error->WarningE("unlnk",_("Problem unlinking the file"));
582 return Res;
583 }
584 /*}}}*/
585 // FileFd::Sync - Sync the file /*{{{*/
586 // ---------------------------------------------------------------------
587 /* */
588 bool FileFd::Sync()
589 {
590 #ifdef _POSIX_SYNCHRONIZED_IO
591 if (fsync(iFd) != 0)
592 return _error->Errno("sync",_("Problem syncing the file"));
593 #endif
594 return true;
595 }
596 /*}}}*/