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