]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.h
avoid using global PendingError to avoid failing too often too soon
[apt.git] / apt-pkg / contrib / fileutl.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: fileutl.h,v 1.26 2001/05/07 05:06: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 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 /*}}}*/
21 #ifndef PKGLIB_FILEUTL_H
22 #define PKGLIB_FILEUTL_H
23
24 #include <apt-pkg/macros.h>
25 #include <apt-pkg/aptconfiguration.h>
26
27 #include <string>
28 #include <vector>
29 #include <set>
30 #include <time.h>
31
32 #include <zlib.h>
33
34 #ifndef APT_8_CLEANER_HEADERS
35 using std::string;
36 #endif
37
38 /* Define this for python-apt */
39 #define APT_HAS_GZIP 1
40
41 class FileFdPrivate;
42 class FileFd
43 {
44 protected:
45 int iFd;
46
47 enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
48 HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
49 unsigned long Flags;
50 std::string FileName;
51 std::string TemporaryFileName;
52
53 public:
54 enum OpenMode {
55 ReadOnly = (1 << 0),
56 WriteOnly = (1 << 1),
57 ReadWrite = ReadOnly | WriteOnly,
58
59 Create = (1 << 2),
60 Exclusive = (1 << 3),
61 Atomic = Exclusive | (1 << 4),
62 Empty = (1 << 5),
63
64 WriteEmpty = ReadWrite | Create | Empty,
65 WriteExists = ReadWrite,
66 WriteAny = ReadWrite | Create,
67 WriteTemp = ReadWrite | Create | Exclusive,
68 ReadOnlyGzip,
69 WriteAtomic = ReadWrite | Create | Atomic
70 };
71 enum CompressMode { Auto = 'A', None = 'N', Extension = 'E', Gzip = 'G', Bzip2 = 'B', Lzma = 'L', Xz = 'X' };
72
73 inline bool Read(void *To,unsigned long long Size,bool AllowEof)
74 {
75 unsigned long long Jnk;
76 if (AllowEof)
77 return Read(To,Size,&Jnk);
78 return Read(To,Size);
79 }
80 bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
81 char* ReadLine(char *To, unsigned long long const Size);
82 bool Write(const void *From,unsigned long long Size);
83 bool static Write(int Fd, const void *From, unsigned long long Size);
84 bool Seek(unsigned long long To);
85 bool Skip(unsigned long long To);
86 bool Truncate(unsigned long long To);
87 unsigned long long Tell();
88 // the size of the file content (compressed files will be uncompressed first)
89 unsigned long long Size();
90 // the size of the file itself
91 unsigned long long FileSize();
92 time_t ModificationTime();
93
94 /* You want to use 'unsigned long long' if you are talking about a file
95 to be able to support large files (>2 or >4 GB) properly.
96 This shouldn't happen all to often for the indexes, but deb's might be…
97 And as the auto-conversation converts a 'unsigned long *' to a 'bool'
98 instead of 'unsigned long long *' we need to provide this explicitely -
99 otherwise applications magically start to fail… */
100 bool Read(void *To,unsigned long long Size,unsigned long *Actual) APT_DEPRECATED
101 {
102 unsigned long long R;
103 bool const T = Read(To, Size, &R);
104 *Actual = R;
105 return T;
106 }
107
108 bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
109 bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
110 inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
111 return Open(FileName, Mode, None, AccessMode);
112 };
113 bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
114 bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
115 inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
116 return OpenDescriptor(Fd, Mode, None, AutoClose);
117 };
118 bool Close();
119 bool Sync();
120
121 // Simple manipulators
122 inline int Fd() {return iFd;};
123 inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
124 gzFile gzFd() APT_DEPRECATED APT_PURE;
125
126 inline bool IsOpen() {return iFd >= 0;};
127 inline bool Failed() {return (Flags & Fail) == Fail;};
128 inline void EraseOnFailure() {Flags |= DelOnFail;};
129 inline void OpFail() {Flags |= Fail;};
130 inline bool Eof() {return (Flags & HitEof) == HitEof;};
131 inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
132 inline std::string &Name() {return FileName;};
133
134 FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666);
135 FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666);
136 FileFd();
137 FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None);
138 FileFd(int const Fd, bool const AutoClose);
139 virtual ~FileFd();
140
141 private:
142 FileFdPrivate * d;
143 APT_HIDDEN FileFd & operator=(const FileFd &);
144 APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
145
146 // private helpers to set Fail flag and call _error->Error
147 APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
148 APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
149 };
150
151 bool RunScripts(const char *Cnf);
152 bool CopyFile(FileFd &From,FileFd &To);
153 int GetLock(std::string File,bool Errors = true);
154 bool FileExists(std::string File);
155 bool RealFileExists(std::string File);
156 bool DirectoryExists(std::string const &Path) APT_CONST;
157 bool CreateDirectory(std::string const &Parent, std::string const &Path);
158 time_t GetModificationTime(std::string const &Path);
159 bool Rename(std::string From, std::string To);
160
161 std::string GetTempDir();
162 std::string GetTempDir(std::string const &User);
163 FileFd* GetTempFile(std::string const &Prefix = "",
164 bool ImmediateUnlink = true,
165 FileFd * const TmpFd = NULL);
166
167 /** \brief Ensure the existence of the given Path
168 *
169 * \param Parent directory of the Path directory - a trailing
170 * /apt/ will be removed before CreateDirectory call.
171 * \param Path which should exist after (successful) call
172 */
173 bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
174
175 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
176 bool const &SortList, bool const &AllowNoExt=false);
177 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
178 bool const &SortList);
179 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
180 std::string SafeGetCWD();
181 void SetCloseExec(int Fd,bool Close);
182 void SetNonBlock(int Fd,bool Block);
183 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
184 pid_t ExecFork();
185 pid_t ExecFork(std::set<int> keep_fds);
186 void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
187 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
188
189 // check if the given file starts with a PGP cleartext signature
190 bool StartsWithGPGClearTextSignature(std::string const &FileName);
191
192 /** change file attributes to requested known good values
193 *
194 * The method skips the user:group setting if not root.
195 *
196 * @param requester is printed as functionname in error cases
197 * @param file is the file to be modified
198 * @param user is the (new) owner of the file, e.g. _apt
199 * @param group is the (new) group owning the file, e.g. root
200 * @param mode is the access mode of the file, e.g. 0644
201 */
202 bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode);
203
204 /**
205 * \brief Drop privileges
206 *
207 * Drop the privileges to the user _apt (or the one specified in
208 * APT::Sandbox::User). This does not set the supplementary group
209 * ids up correctly, it only uses the default group. Also prevent
210 * the process from gaining any new privileges afterwards, at least
211 * on Linux.
212 *
213 * \return true on success, false on failure with _error set
214 */
215 bool DropPrivileges();
216
217 // File string manipulators
218 std::string flNotDir(std::string File);
219 std::string flNotFile(std::string File);
220 std::string flNoLink(std::string File);
221 std::string flExtension(std::string File);
222 std::string flCombine(std::string Dir,std::string File);
223
224 /** \brief Takes a file path and returns the absolute path
225 */
226 std::string flAbsPath(std::string File);
227
228 // simple c++ glob
229 std::vector<std::string> Glob(std::string const &pattern, int flags=0);
230
231 /** \brief Popen() implementation that execv() instead of using a shell
232 *
233 * \param Args the execv style command to run
234 * \param FileFd is a referenz to the FileFd to use for input or output
235 * \param Child a reference to the integer that stores the child pid
236 * Note that you must call ExecWait() or similar to cleanup
237 * \param Mode is either FileFd::ReadOnly or FileFd::WriteOnly
238 * \return true on success, false on failure with _error set
239 */
240 bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode);
241
242
243 #endif