]> jfr.im git - irc/rizon/znc.git/blob - FileUtils.h
Write forceserver and webircpassword to conf
[irc/rizon/znc.git] / FileUtils.h
1 /*
2 * Copyright (C) 2004-2011 See the AUTHORS file for details.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 */
8
9 #ifndef _FILEUTILS_H
10 #define _FILEUTILS_H
11
12 #include "zncconfig.h"
13 #include "ZNCString.h"
14 #include <dirent.h>
15 #include <map>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/fcntl.h>
19 #include <unistd.h>
20 #include <vector>
21
22 using std::vector;
23 using std::map;
24
25 class CFile {
26 public:
27 CFile();
28 CFile(const CString& sLongName);
29 ~CFile();
30
31 enum EFileTypes {
32 FT_REGULAR,
33 FT_DIRECTORY,
34 FT_CHARACTER,
35 FT_BLOCK,
36 FT_FIFO,
37 FT_LINK,
38 FT_SOCK
39 };
40
41 void SetFileName(const CString& sLongName);
42 static bool IsReg(const CString& sLongName, bool bUseLstat = false);
43 static bool IsDir(const CString& sLongName, bool bUseLstat = false);
44 static bool IsChr(const CString& sLongName, bool bUseLstat = false);
45 static bool IsBlk(const CString& sLongName, bool bUseLstat = false);
46 static bool IsFifo(const CString& sLongName, bool bUseLstat = false);
47 static bool IsLnk(const CString& sLongName, bool bUseLstat = true);
48 static bool IsSock(const CString& sLongName, bool bUseLstat = false);
49
50 bool IsReg(bool bUseLstat = false) const;
51 bool IsDir(bool bUseLstat = false) const;
52 bool IsChr(bool bUseLstat = false) const;
53 bool IsBlk(bool bUseLstat = false) const;
54 bool IsFifo(bool bUseLstat = false) const;
55 bool IsLnk(bool bUseLstat = true) const;
56 bool IsSock(bool bUseLstat = false) const;
57
58 // for gettin file types, using fstat instead
59 static bool FType(const CString& sFileName, EFileTypes eType, bool bUseLstat = false);
60
61 enum EFileAttr {
62 FA_Name,
63 FA_Size,
64 FA_ATime,
65 FA_MTime,
66 FA_CTime,
67 FA_UID
68 };
69
70 //
71 // Functions to retrieve file information
72 //
73 bool Exists() const;
74 off_t GetSize() const;
75 time_t GetATime() const;
76 time_t GetMTime() const;
77 time_t GetCTime() const;
78 uid_t GetUID() const;
79 gid_t GetGID() const;
80 static bool Exists(const CString& sFile);
81
82 static off_t GetSize(const CString& sFile);
83 static time_t GetATime(const CString& sFile);
84 static time_t GetMTime(const CString& sFile);
85 static time_t GetCTime(const CString& sFile);
86 static uid_t GetUID(const CString& sFile);
87 static gid_t GetGID(const CString& sFile);
88 static int GetInfo(const CString& sFile, struct stat& st);
89
90 //
91 // Functions to manipulate the file on the filesystem
92 //
93 bool Delete();
94 bool Move(const CString& sNewFileName, bool bOverwrite = false);
95 bool Copy(const CString& sNewFileName, bool bOverwrite = false);
96
97 static bool Delete(const CString& sFileName);
98 static bool Move(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false);
99 static bool Copy(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false);
100 bool Chmod(mode_t mode);
101 static bool Chmod(const CString& sFile, mode_t mode);
102 bool Seek(off_t uPos);
103 bool Truncate();
104 bool Sync();
105 bool Open(const CString& sFileName, int iFlags = O_RDONLY, mode_t iMode = 0644);
106 bool Open(int iFlags = O_RDONLY, mode_t iMode = 0644);
107 int Read(char *pszBuffer, int iBytes);
108 bool ReadLine(CString & sData, const CString & sDelimiter = "\n");
109 bool ReadFile(CString& sData, size_t iMaxSize = 512 * 1024);
110 int Write(const char *pszBuffer, u_int iBytes);
111 int Write(const CString & sData);
112 void Close();
113 void ClearBuffer();
114
115 bool TryExLock(const CString& sLockFile, int iFlags = O_RDWR | O_CREAT);
116 bool TryExLock();
117 bool ExLock();
118 bool UnLock();
119
120 bool IsOpen() const;
121 CString GetLongName() const;
122 CString GetShortName() const;
123 CString GetDir() const;
124
125 bool HadError() const { return m_bHadError; }
126 void ResetError() { m_bHadError = false; }
127
128 static void InitHomePath(const CString& sFallback);
129 static const CString& GetHomePath() { return m_sHomePath; }
130
131 private:
132 // fcntl() locking wrapper
133 bool Lock(int iType, bool bBlocking);
134
135 CString m_sBuffer;
136 int m_iFD;
137 bool m_bHadError;
138
139 static CString m_sHomePath;
140
141 protected:
142 CString m_sLongName; //!< Absolute filename (m_sPath + "/" + m_sShortName)
143 CString m_sShortName; //!< Filename alone, without path
144 };
145
146 class CDir : public vector<CFile*> {
147 public:
148
149 CDir(const CString& sDir) {
150 m_bDesc = false;
151 m_eSortAttr = CFile::FA_Name;
152 Fill(sDir);
153 }
154
155 CDir() {
156 m_bDesc = false;
157 m_eSortAttr = CFile::FA_Name;
158 }
159
160 ~CDir() {
161 CleanUp();
162 }
163
164 void CleanUp() {
165 for (unsigned int a = 0; a < size(); a++) {
166 delete (*this)[a];
167 }
168
169 clear();
170 }
171
172 int Fill(const CString& sDir) {
173 return FillByWildcard(sDir, "*");
174 }
175
176 int FillByWildcard(const CString& sDir, const CString& sWildcard) {
177 CleanUp();
178 DIR* dir = opendir((sDir.empty()) ? "." : sDir.c_str());
179
180 if (!dir) {
181 return 0;
182 }
183
184 struct dirent * de;
185
186 while ((de = readdir(dir)) != 0) {
187 if ((strcmp(de->d_name, ".") == 0) || (strcmp(de->d_name, "..") == 0)) {
188 continue;
189 }
190 if ((!sWildcard.empty()) && (!CString(de->d_name).WildCmp(sWildcard))) {
191 continue;
192 }
193
194 CFile *file = new CFile(sDir + "/" + de->d_name/*, this*/); // @todo need to pass pointer to 'this' if we want to do Sort()
195 push_back(file);
196 }
197
198 closedir(dir);
199 return size();
200 }
201
202 static unsigned int Chmod(mode_t mode, const CString& sWildcard, const CString& sDir = ".") {
203 CDir cDir;
204 cDir.FillByWildcard(sDir, sWildcard);
205 return cDir.Chmod(mode);
206 }
207
208 unsigned int Chmod(mode_t mode) {
209 unsigned int uRet = 0;
210 for (unsigned int a = 0; a < size(); a++) {
211 if ((*this)[a]->Chmod(mode)) {
212 uRet++;
213 }
214 }
215
216 return uRet;
217 }
218
219 static unsigned int Delete(const CString& sWildcard, const CString& sDir = ".") {
220 CDir cDir;
221 cDir.FillByWildcard(sDir, sWildcard);
222 return cDir.Delete();
223 }
224
225 unsigned int Delete() {
226 unsigned int uRet = 0;
227 for (unsigned int a = 0; a < size(); a++) {
228 if ((*this)[a]->Delete()) {
229 uRet++;
230 }
231 }
232
233 return uRet;
234 }
235
236 CFile::EFileAttr GetSortAttr() { return m_eSortAttr; }
237 bool IsDescending() { return m_bDesc; }
238
239 // Check if sPath + "/" + sAdd (~/ is handled) is an absolute path which
240 // resides under sPath. Returns absolute path on success, else "".
241 static CString CheckPathPrefix(const CString& sPath, const CString& sAdd, const CString& sHomeDir = "");
242 static CString ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHomeDir = "");
243 static bool MakeDir(const CString& sPath, mode_t iMode = 0700);
244
245 static CString GetCWD() {
246 CString sRet;
247 char * pszCurDir = getcwd(NULL, 0);
248 if (pszCurDir) {
249 sRet = pszCurDir;
250 free(pszCurDir);
251 }
252
253 return sRet;
254 }
255
256 private:
257 protected:
258 CFile::EFileAttr m_eSortAttr;
259 bool m_bDesc;
260 };
261 #endif // !_FILEUTILS_H