apt 2.9.14 commandline package manager

fileutl.h
1// -*- mode: cpp; mode: fold -*-
2// SPDX-License-Identifier: GPL-2.0+
3// Description /*{{{*/
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 file had this historic note, but now includes further changes
17 under the GPL-2.0+:
18
19 This source is placed in the Public Domain, do with it what you will
20 It was originally written by Jason Gunthorpe.
21
22 ##################################################################### */
23 /*}}}*/
24#ifndef PKGLIB_FILEUTL_H
25#define PKGLIB_FILEUTL_H
26
27#include <apt-pkg/aptconfiguration.h>
28#include <apt-pkg/macros.h>
29
30#include <ctime>
31#include <set>
32#include <memory>
33#include <string>
34#include <vector>
35#include <sys/stat.h>
36
37/* Define this for python-apt */
38#define APT_HAS_GZIP 1
39
40 class FileFdPrivate;
41 class APT_PUBLIC FileFd
42{
43 friend class FileFdPrivate;
44 friend class GzipFileFdPrivate;
45 friend class Bz2FileFdPrivate;
46 friend class LzmaFileFdPrivate;
47 friend class Lz4FileFdPrivate;
48 friend class ZstdFileFdPrivate;
49 friend class DirectFileFdPrivate;
50 friend class PipedFileFdPrivate;
51 protected:
52 int iFd;
53
54 enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
55 HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
56 unsigned long Flags;
57 std::string FileName;
58 std::string TemporaryFileName;
59
60 public:
61 enum OpenMode {
62 ReadOnly = (1 << 0),
63 WriteOnly = (1 << 1),
64 ReadWrite = ReadOnly | WriteOnly,
65
66 Create = (1 << 2),
67 Exclusive = (1 << 3),
68 Atomic = Exclusive | (1 << 4),
69 Empty = (1 << 5),
70 BufferedWrite = (1 << 6),
71
72 WriteEmpty = ReadWrite | Create | Empty,
73 WriteExists = ReadWrite,
74 WriteAny = ReadWrite | Create,
75 WriteTemp = ReadWrite | Create | Exclusive,
76 ReadOnlyGzip,
77 WriteAtomic = ReadWrite | Create | Atomic
78 };
79 enum CompressMode
80 {
81 Auto = 'A',
82 None = 'N',
83 Extension = 'E',
84 Gzip = 'G',
85 Bzip2 = 'B',
86 Lzma = 'L',
87 Xz = 'X',
88 Lz4 = '4',
89 Zstd = 'Z'
90 };
91
92 inline bool Read(void *To,unsigned long long Size,bool AllowEof)
93 {
94 unsigned long long Jnk;
95 if (AllowEof)
96 return Read(To,Size,&Jnk);
97 return Read(To,Size);
98 }
99 bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
100 bool static Read(int const Fd, void *To, unsigned long long Size, unsigned long long * const Actual = 0);
112 char* ReadLine(char *To, unsigned long long const Size);
122 bool ReadLine(std::string &To);
123 bool Flush();
124 bool Write(const void *From,unsigned long long Size);
125 bool static Write(int Fd, const void *From, unsigned long long Size);
126 bool Seek(unsigned long long To);
127 bool Skip(unsigned long long To);
128 bool Truncate(unsigned long long To);
129 unsigned long long Tell();
130 // the size of the file content (compressed files will be uncompressed first)
131 unsigned long long Size();
132 // the size of the file itself
133 unsigned long long FileSize();
134 time_t ModificationTime();
135
136 bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const AccessMode = 0666);
137 bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const AccessMode = 0666);
138 inline bool Open(std::string FileName,unsigned int const Mode, unsigned long const AccessMode = 0666) {
139 return Open(std::move(FileName), Mode, None, AccessMode);
140 };
141 bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
142 bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
143 inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
144 return OpenDescriptor(Fd, Mode, None, AutoClose);
145 };
146 bool Close();
147 bool Sync();
148
149 // Simple manipulators
150 inline int Fd() {return iFd;};
151 inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
152
153 inline bool IsOpen() {return iFd >= 0;};
154 inline bool Failed() {return (Flags & Fail) == Fail;};
155 inline void EraseOnFailure() {Flags |= DelOnFail;};
156 inline void OpFail() {Flags |= Fail;};
157 inline bool Eof() {return (Flags & HitEof) == HitEof;};
158 inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
159 inline std::string &Name() {return FileName;};
160 inline void SetFileName(std::string const &name) { FileName = name; };
161
162 FileFd(std::string FileName,unsigned int const Mode,unsigned long AccessMode = 0666);
163 FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long AccessMode = 0666);
164 FileFd();
165 FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None);
166 FileFd(int const Fd, bool const AutoClose);
167 virtual ~FileFd();
168
169 private:
170 FileFdPrivate * d;
171 APT_HIDDEN FileFd(const FileFd &);
172 APT_HIDDEN FileFd & operator=(const FileFd &);
173 APT_HIDDEN bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
174
175 // private helpers to set Fail flag and call _error->Error
176 APT_HIDDEN bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD;
177 APT_HIDDEN bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD;
178};
179
180APT_PUBLIC bool RunScripts(const char *Cnf);
181APT_PUBLIC bool CopyFile(FileFd &From,FileFd &To);
182APT_PUBLIC bool RemoveFile(char const * const Function, std::string const &FileName);
183APT_PUBLIC bool RemoveFileAt(char const * const Function, int const dirfd, std::string const &FileName);
184APT_PUBLIC int GetLock(std::string File,bool Errors = true);
185APT_PUBLIC bool FileExists(std::string File);
186APT_PUBLIC bool RealFileExists(std::string File);
187APT_PUBLIC bool DirectoryExists(std::string const &Path);
188APT_PUBLIC bool CreateDirectory(std::string const &Parent, std::string const &Path);
189APT_PUBLIC time_t GetModificationTime(std::string const &Path);
190APT_PUBLIC bool Rename(std::string From, std::string To);
191
192APT_PUBLIC std::string GetTempDir();
193APT_PUBLIC std::string GetTempDir(std::string const &User);
194APT_PUBLIC FileFd* GetTempFile(std::string const &Prefix = "",
195 bool ImmediateUnlink = true,
196 FileFd * const TmpFd = NULL);
197
198// FIXME: GetTempFile should always return a buffered file
199APT_HIDDEN FileFd* GetTempFile(std::string const &Prefix,
200 bool ImmediateUnlink ,
201 FileFd * const TmpFd,
202 bool Buffered);
203
210APT_PUBLIC bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
211
212APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
213 bool const &SortList, bool const &AllowNoExt=false);
214APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
215 bool const &SortList);
216APT_PUBLIC std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
217APT_PUBLIC std::string SafeGetCWD();
218APT_PUBLIC void SetCloseExec(int Fd,bool Close);
219APT_PUBLIC void SetNonBlock(int Fd,bool Block);
220APT_PUBLIC bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
221APT_PUBLIC pid_t ExecFork();
222APT_PUBLIC pid_t ExecFork(std::set<int> keep_fds);
223APT_PUBLIC void MergeKeepFdsFromConfiguration(std::set<int> &keep_fds);
224APT_PUBLIC bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
225
226// check if the given file starts with a PGP cleartext signature
227APT_PUBLIC bool StartsWithGPGClearTextSignature(std::string const &FileName);
228
239APT_PUBLIC bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * const file, char const * const user, char const * const group, mode_t const mode);
240
252APT_PUBLIC bool DropPrivileges();
253
254// File string manipulators
255APT_PUBLIC std::string flNotDir(std::string File);
256APT_PUBLIC std::string flNotFile(std::string File);
257APT_PUBLIC std::string flNoLink(std::string File);
258APT_PUBLIC std::string flExtension(std::string File);
259APT_PUBLIC std::string flCombine(std::string Dir,std::string File);
260
263APT_PUBLIC std::string flAbsPath(std::string File);
265APT_HIDDEN std::string flNormalize(std::string file);
266
267// simple c++ glob
268APT_PUBLIC std::vector<std::string> Glob(std::string const &pattern, int flags=0);
269
282APT_PUBLIC bool Popen(const char *Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool CaptureStderr = true, bool Sandbox = false);
283
284APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd);
285
286APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode);
287
288
289 namespace {
290 struct FILEFcloseDeleter {
291 void operator()(FILE *p) {
292 fclose(p);
293 }
294 };
295 struct FILEPcloseDeleter {
296 void operator()(FILE *p) {
297 pclose(p);
298 }
299 };
300
301 [[maybe_unused]] std::unique_ptr<FILE, FILEFcloseDeleter> make_unique_FILE(const char *const filename, char const *const mode)
302 {
303 return {fopen(filename, mode), {}};
304 }
305 [[maybe_unused]] std::unique_ptr<FILE, FILEFcloseDeleter> make_unique_FILE(std::string const &filename, char const *const mode)
306 {
307 return make_unique_FILE(filename.c_str(), mode);
308 }
309
310 [[maybe_unused]] std::unique_ptr<FILE, FILEPcloseDeleter> make_unique_popen(const char *program, char const *const mode)
311 {
312 return {popen(program, mode), {}};
313 }
314}
315
316#endif
Bz2FileFdPrivate
Definition fileutl.cc:1563
DirectFileFdPrivate
Definition fileutl.cc:2318
FileFdPrivate
Definition fileutl.cc:1096
FileFd
Definition fileutl.h:42
GzipFileFdPrivate
Definition fileutl.cc:1436
Lz4FileFdPrivate
Definition fileutl.cc:1617
LzmaFileFdPrivate
Definition fileutl.cc:1987
PipedFileFdPrivate
Definition fileutl.cc:2188
ZstdFileFdPrivate
Definition fileutl.cc:1777
APT::Configuration::Compressor
Representation of supported compressors.
Definition aptconfiguration.h:95