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

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