44 strings left
[ntk/apt.git] / apt-pkg / contrib / fileutl.h
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
233b185f 3// $Id: fileutl.h,v 1.26 2001/05/07 05:06:52 jgg Exp $
578bfd0a
AL
4/* ######################################################################
5
6 File Utilities
7
8 CopyFile - Buffered copy of a single file
9 GetLock - dpkg compatible lock file manipulation (fcntl)
10 FileExists - Returns true if the file exists
11 SafeGetCWD - Returns the CWD in a string with overrun protection
12
13 The file class is a handy abstraction for various functions+classes
14 that need to accept filenames.
15
16 This source is placed in the Public Domain, do with it what you will
17 It was originally written by Jason Gunthorpe.
18
19 ##################################################################### */
20 /*}}}*/
578bfd0a
AL
21#ifndef PKGLIB_FILEUTL_H
22#define PKGLIB_FILEUTL_H
23
6c139d6e 24
578bfd0a
AL
25#include <string>
26
233b185f
AL
27using std::string;
28
8e06abb2 29class FileFd
578bfd0a
AL
30{
31 protected:
32 int iFd;
33
ddc1d8d0
AL
34 enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
35 HitEof = (1<<3)};
578bfd0a
AL
36 unsigned long Flags;
37 string FileName;
38
39 public:
f08fcf34 40 enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp};
578bfd0a 41
f604cf55
AL
42 inline bool Read(void *To,unsigned long Size,bool AllowEof)
43 {
44 unsigned long Jnk;
45 if (AllowEof)
46 return Read(To,Size,&Jnk);
47 return Read(To,Size);
48 }
49 bool Read(void *To,unsigned long Size,unsigned long *Actual = 0);
a05599f1 50 bool Write(const void *From,unsigned long Size);
578bfd0a 51 bool Seek(unsigned long To);
727f18af 52 bool Skip(unsigned long To);
6d5dd02a 53 bool Truncate(unsigned long To);
7f25bdff 54 unsigned long Tell();
578bfd0a 55 unsigned long Size();
13d87e2e 56 bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
578bfd0a 57 bool Close();
b2e465d6
AL
58 bool Sync();
59
578bfd0a
AL
60 // Simple manipulators
61 inline int Fd() {return iFd;};
869f717a 62 inline void Fd(int fd) {iFd = fd;};
578bfd0a
AL
63 inline bool IsOpen() {return iFd >= 0;};
64 inline bool Failed() {return (Flags & Fail) == Fail;};
65 inline void EraseOnFailure() {Flags |= DelOnFail;};
66 inline void OpFail() {Flags |= Fail;};
ddc1d8d0 67 inline bool Eof() {return (Flags & HitEof) == HitEof;};
c7b5ce1c
AL
68 inline string &Name() {return FileName;};
69
13d87e2e
AL
70 FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
71 Flags(0)
72 {
73 Open(FileName,Mode,Perms);
74 };
36375005 75 FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose) {};
8e06abb2
AL
76 FileFd(int Fd,bool) : iFd(Fd), Flags(0) {};
77 virtual ~FileFd();
578bfd0a
AL
78};
79
614adaa0 80bool RunScripts(const char *Cnf);
8b89e57f 81bool CopyFile(FileFd &From,FileFd &To);
578bfd0a
AL
82int GetLock(string File,bool Errors = true);
83bool FileExists(string File);
84string SafeGetCWD();
3b5421b4
AL
85void SetCloseExec(int Fd,bool Close);
86void SetNonBlock(int Fd,bool Block);
1084d58a 87bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
75ef8f14 88pid_t ExecFork();
3826564e 89bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
578bfd0a 90
8ce4327b
AL
91// File string manipulators
92string flNotDir(string File);
d38b7b3d 93string flNotFile(string File);
421c8d10 94string flNoLink(string File);
b2e465d6
AL
95string flExtension(string File);
96string flCombine(string Dir,string File);
8ce4327b 97
578bfd0a 98#endif