apt: apt-pkg/contrib/fileutl.h Source File

apt 2.3.15 commandline package manager

fileutl.h
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 File Utilities
6
7 CopyFile - Buffered copy of a single file
8 GetLock - dpkg compatible lock file manipulation (fcntl)
9 FileExists - Returns true if the file exists
10 SafeGetCWD - Returns the CWD in a string with overrun protection
11
12 The file class is a handy abstraction for various functions+classes
13 that need to accept filenames.
14
15 This source is placed in the Public Domain, do with it what you will
16 It was originally written by Jason Gunthorpe.
17
18 ##################################################################### */
19 /*}}}*/
20#ifndef PKGLIB_FILEUTL_H
21#define PKGLIB_FILEUTL_H
22
23#include <apt-pkg/aptconfiguration.h>
24#include <apt-pkg/macros.h>
25
26#include <set>
27#include <string>
28#include <vector>
29#include <time.h>
30
31#include <zlib.h>
32
33
34/* Define this for python-apt */
35#define APT_HAS_GZIP 1
36
37class FileFdPrivate;
38 class APT_PUBLIC FileFd
39{
40 friend class FileFdPrivate;
41 friend class GzipFileFdPrivate;
42 friend class Bz2FileFdPrivate;
43 friend class LzmaFileFdPrivate;
44 friend class Lz4FileFdPrivate;
45 friend class ZstdFileFdPrivate;
46 friend class DirectFileFdPrivate;
47 friend class PipedFileFdPrivate;
48 protected:
49 int iFd;
50
51 enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
52 HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
53 unsigned long Flags;
54 std::string FileName;
55 std::string TemporaryFileName;
56
57 public:
58 enum OpenMode {
59 ReadOnly = (1 << 0),
60 WriteOnly = (1 << 1),
61 ReadWrite = ReadOnly | WriteOnly,
62
63 Create = (1 << 2),
64 Exclusive = (1 << 3),
65 Atomic = Exclusive | (1 << 4),
66 Empty = (1 << 5),
67 BufferedWrite = (1 << 6),
68
69 WriteEmpty = ReadWrite | Create | Empty,
70 WriteExists = ReadWrite,
71 WriteAny = ReadWrite | Create,
72 WriteTemp = ReadWrite | Create | Exclusive,
73 ReadOnlyGzip,
74 WriteAtomic = ReadWrite | Create | Atomic
75 };
76 enum CompressMode
77 {
78 Auto = 'A',
79 None = 'N',
80 Extension = 'E',
81 Gzip = 'G',
82 Bzip2 = 'B',
83 Lzma = 'L',
84 Xz = 'X',
85 Lz4 = '4',
86 Zstd = 'Z'
87 };
88
89 inline bool Read(void *To,unsigned long long Size,bool AllowEof)
90 {
91 unsigned long long Jnk;
92 if (AllowEof)
93 return Read(To,Size,&Jnk);
94 return Read(To,Size);
95 }
96 bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
97 bool static Read(int const Fd, void *To, unsigned long long Size, unsigned long long * const Actual = 0);
109 char* ReadLine(char *To, unsigned long long const Size);
119 bool ReadLine(std::string &To);
120 bool Flush();
121 bool Write(const void *From,unsigned long long Size);
122 bool static Write(int Fd, const void *From, unsigned long long Size);
123 bool Seek(unsigned long long To);
124 bool Skip(unsigned long long To);
125 bool Truncate(unsigned long long To);
126 unsigned long long Tell();
127 // the size of the file content (compressed files will be uncompressed first)
128 unsigned long long Size();
129 // the size of the file itself
130 unsigned long long FileSize();
131 time_t ModificationTime();
132
133 bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
134 bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
135 inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
136 return Open(FileName, Mode, None, AccessMode);
137 };
138 bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
139 bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
140 inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
141 return OpenDescriptor(Fd, Mode, None, AutoClose);
142 };
143 bool Close();
144 bool Sync();
145
146 // Simple manipulators
147 inline int Fd() {return iFd;};
148 inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
149
150 inline bool IsOpen() {return iFd >= 0;};
151 inline bool Failed() {return (Flags & Fail) == Fail;};
152 inline void EraseOnFailure() {Flags |= DelOnFail;};
153 inline void OpFail() {Flags |= Fail;};
154 inline bool Eof() {return (Flags & HitEof) == HitEof;};
155 inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
156 inline std::string &Name() {return FileName;};
157 inline void SetFileName(std::string const &name) { FileName = name; };
158
159 FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666);
160 FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666);
161 FileFd();
162 FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None);
163 FileFd(int const Fd, bool const AutoClose);
164 virtual ~FileFd();
165
166 private:
167 FileFdPrivate * d;
168 APT_HIDDEN FileFd(const FileFd &);
169 APT_HIDDEN FileFd & operator=(const FileFd &);
170 APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
171
172 // private helpers to set Fail flag and call _error->Error
173 APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
174 APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
175};
176
177APT_PUBLIC bool RunScripts(const char *Cnf);
178APT_PUBLIC bool CopyFile(FileFd &From,FileFd &To);
179APT_PUBLIC bool RemoveFile(char const * const Function, std::string const &FileName);
180APT_PUBLIC bool RemoveFileAt(char const * const Function, int const dirfd, std::string const &FileName);
181APT_PUBLIC int GetLock(std::string File,bool Errors = true);
182APT_PUBLIC bool FileExists(std::string File);
183APT_PUBLIC bool RealFileExists(std::string File);
184APT_PUBLIC bool DirectoryExists(std::string const &Path);
185APT_PUBLIC bool CreateDirectory(std::string const &Parent, std::string const &Path);
186APT_PUBLIC time_t GetModificationTime(std::string const &Path);
187APT_PUBLIC bool Rename(std::string From, std::string To);
188
189APT_PUBLIC std::string GetTempDir();
190APT_PUBLIC std::string GetTempDir(std::string const &User);
191APT_PUBLIC FileFd* GetTempFile(std::string const &Prefix = "",
192 bool ImmediateUnlink = true,
193 FileFd * const TmpFd = NULL);
194
195// FIXME: GetTempFile should always return a buffered file
196APT_HIDDEN FileFd* GetTempFile(std::string const &Prefix,
197 bool ImmediateUnlink ,
198 FileFd * const TmpFd,
199 bool Buffered);
200
207APT_PUBLIC bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
208
209APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
210 bool const &SortList, bool const &AllowNoExt=false);
211APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
212 bool const &SortList);
213APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
214APT_PUBLIC std::string SafeGetCWD();
215APT_PUBLIC void SetCloseExec(int Fd,bool Close);
216APT_PUBLIC void SetNonBlock(int Fd,bool Block);
217APT_PUBLIC bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
218APT_PUBLIC pid_t ExecFork();
219APT_PUBLIC pid_t ExecFork(std::set<int> keep_fds);
220APT_PUBLIC void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
221APT_PUBLIC bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
222
223// check if the given file starts with a PGP cleartext signature
224APT_PUBLIC bool StartsWithGPGClearTextSignature(std::string const &FileName);
225
236APT_PUBLIC bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode);
237
249APT_PUBLIC bool DropPrivileges();
250
251// File string manipulators
252APT_PUBLIC std::string flNotDir(std::string File);
253APT_PUBLIC std::string flNotFile(std::string File);
254APT_PUBLIC std::string flNoLink(std::string File);
255APT_PUBLIC std::string flExtension(std::string File);
256APT_PUBLIC std::string flCombine(std::string Dir,std::string File);
257
260APT_PUBLIC std::string flAbsPath(std::string File);
262APT_HIDDEN std::string flNormalize(std::string file);
263
264// simple c++ glob
265APT_PUBLIC std::vector<std::string> Glob(std::string const &pattern, int flags=0);
266
279APT_PUBLIC bool Popen(const char *Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool CaptureStderr = true, bool Sandbox = false);
280
281APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd);
282
283APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode);
284
285#endif
Bz2FileFdPrivate
Definition: fileutl.cc:1560
DirectFileFdPrivate
Definition: fileutl.cc:2316
FileFdPrivate
Definition: fileutl.cc:1091
FileFd
Definition: fileutl.h:39
GzipFileFdPrivate
Definition: fileutl.cc:1433
Lz4FileFdPrivate
Definition: fileutl.cc:1614
LzmaFileFdPrivate
Definition: fileutl.cc:1984
PipedFileFdPrivate
Definition: fileutl.cc:2186
ZstdFileFdPrivate
Definition: fileutl.cc:1774
APT::Configuration::Compressor
Representation of supported compressors.
Definition: aptconfiguration.h:95