ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2014 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _FILEUTILS_H
18 #define _FILEUTILS_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/ZNCString.h>
22 #include <dirent.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/fcntl.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <vector>
29 
30 class CFile {
31 public:
32  CFile();
33  CFile(const CString& sLongName);
34  ~CFile();
35 
36  enum EFileTypes {
44  };
45 
46  void SetFileName(const CString& sLongName);
47  static bool IsReg(const CString& sLongName, bool bUseLstat = false);
48  static bool IsDir(const CString& sLongName, bool bUseLstat = false);
49  static bool IsChr(const CString& sLongName, bool bUseLstat = false);
50  static bool IsBlk(const CString& sLongName, bool bUseLstat = false);
51  static bool IsFifo(const CString& sLongName, bool bUseLstat = false);
52  static bool IsLnk(const CString& sLongName, bool bUseLstat = true);
53  static bool IsSock(const CString& sLongName, bool bUseLstat = false);
54 
55  bool IsReg(bool bUseLstat = false) const;
56  bool IsDir(bool bUseLstat = false) const;
57  bool IsChr(bool bUseLstat = false) const;
58  bool IsBlk(bool bUseLstat = false) const;
59  bool IsFifo(bool bUseLstat = false) const;
60  bool IsLnk(bool bUseLstat = true) const;
61  bool IsSock(bool bUseLstat = false) const;
62 
63  // for gettin file types, using fstat instead
64  static bool FType(const CString& sFileName, EFileTypes eType, bool bUseLstat = false);
65 
66  enum EFileAttr {
73  };
74 
75  //
76  // Functions to retrieve file information
77  //
78  bool Exists() const;
79  off_t GetSize() const;
80  time_t GetATime() const;
81  time_t GetMTime() const;
82  time_t GetCTime() const;
83  uid_t GetUID() const;
84  gid_t GetGID() const;
85  static bool Exists(const CString& sFile);
86 
87  static off_t GetSize(const CString& sFile);
88  static time_t GetATime(const CString& sFile);
89  static time_t GetMTime(const CString& sFile);
90  static time_t GetCTime(const CString& sFile);
91  static uid_t GetUID(const CString& sFile);
92  static gid_t GetGID(const CString& sFile);
93  static int GetInfo(const CString& sFile, struct stat& st);
94 
95  //
96  // Functions to manipulate the file on the filesystem
97  //
98  bool Delete();
99  bool Move(const CString& sNewFileName, bool bOverwrite = false);
100  bool Copy(const CString& sNewFileName, bool bOverwrite = false);
101 
102  static bool Delete(const CString& sFileName);
103  static bool Move(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false);
104  static bool Copy(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false);
105  bool Chmod(mode_t mode);
106  static bool Chmod(const CString& sFile, mode_t mode);
107  bool Seek(off_t uPos);
108  bool Truncate();
109  bool Sync();
110  bool Open(const CString& sFileName, int iFlags = O_RDONLY, mode_t iMode = 0644);
111  bool Open(int iFlags = O_RDONLY, mode_t iMode = 0644);
112  ssize_t Read(char *pszBuffer, int iBytes);
113  bool ReadLine(CString & sData, const CString & sDelimiter = "\n");
114  bool ReadFile(CString& sData, size_t iMaxSize = 512 * 1024);
115  ssize_t Write(const char *pszBuffer, size_t iBytes);
116  ssize_t Write(const CString & sData);
117  void Close();
118  void ClearBuffer();
119 
120  bool TryExLock(const CString& sLockFile, int iFlags = O_RDWR | O_CREAT);
121  bool TryExLock();
122  bool ExLock();
123  bool UnLock();
124 
125  bool IsOpen() const;
126  CString GetLongName() const;
127  CString GetShortName() const;
128  CString GetDir() const;
129 
130  bool HadError() const { return m_bHadError; }
131  void ResetError() { m_bHadError = false; }
132 
133  static void InitHomePath(const CString& sFallback);
134  static const CString& GetHomePath() { return m_sHomePath; }
135 
136 private:
137  // fcntl() locking wrapper
138  bool Lock(short iType, bool bBlocking);
139 
140  CString m_sBuffer;
141  int m_iFD;
142  bool m_bHadError;
143 
144  static CString m_sHomePath;
145 
146 protected:
149 };
150 
151 class CDir : public std::vector<CFile*> {
152 public:
153 
154  CDir(const CString& sDir) {
155  m_bDesc = false;
157  Fill(sDir);
158  }
159 
160  CDir() {
161  m_bDesc = false;
163  }
164 
165  ~CDir() {
166  CleanUp();
167  }
168 
169  void CleanUp() {
170  for (unsigned int a = 0; a < size(); a++) {
171  delete (*this)[a];
172  }
173 
174  clear();
175  }
176 
177  size_t Fill(const CString& sDir) {
178  return FillByWildcard(sDir, "*");
179  }
180 
181  size_t FillByWildcard(const CString& sDir, const CString& sWildcard) {
182  CleanUp();
183  DIR* dir = opendir((sDir.empty()) ? "." : sDir.c_str());
184 
185  if (!dir) {
186  return 0;
187  }
188 
189  struct dirent * de;
190 
191  while ((de = readdir(dir)) != 0) {
192  if ((strcmp(de->d_name, ".") == 0) || (strcmp(de->d_name, "..") == 0)) {
193  continue;
194  }
195  if ((!sWildcard.empty()) && (!CString(de->d_name).WildCmp(sWildcard))) {
196  continue;
197  }
198 
199  CFile *file = new CFile(sDir.TrimSuffix_n("/") + "/" + de->d_name/*, this*/); // @todo need to pass pointer to 'this' if we want to do Sort()
200  push_back(file);
201  }
202 
203  closedir(dir);
204  return size();
205  }
206 
207  static unsigned int Chmod(mode_t mode, const CString& sWildcard, const CString& sDir = ".") {
208  CDir cDir;
209  cDir.FillByWildcard(sDir, sWildcard);
210  return cDir.Chmod(mode);
211  }
212 
213  unsigned int Chmod(mode_t mode) {
214  unsigned int uRet = 0;
215  for (unsigned int a = 0; a < size(); a++) {
216  if ((*this)[a]->Chmod(mode)) {
217  uRet++;
218  }
219  }
220 
221  return uRet;
222  }
223 
224  static unsigned int Delete(const CString& sWildcard, const CString& sDir = ".") {
225  CDir cDir;
226  cDir.FillByWildcard(sDir, sWildcard);
227  return cDir.Delete();
228  }
229 
230  unsigned int Delete() {
231  unsigned int uRet = 0;
232  for (unsigned int a = 0; a < size(); a++) {
233  if ((*this)[a]->Delete()) {
234  uRet++;
235  }
236  }
237 
238  return uRet;
239  }
240 
242  bool IsDescending() { return m_bDesc; }
243 
244  // Check if sPath + "/" + sAdd (~/ is handled) is an absolute path which
245  // resides under sPath. Returns absolute path on success, else "".
246  static CString CheckPathPrefix(const CString& sPath, const CString& sAdd, const CString& sHomeDir = "");
247  static CString ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHomeDir = "");
248  static bool MakeDir(const CString& sPath, mode_t iMode = 0700);
249 
250  static CString GetCWD() {
251  CString sRet;
252  char * pszCurDir = getcwd(NULL, 0);
253  if (pszCurDir) {
254  sRet = pszCurDir;
255  free(pszCurDir);
256  }
257 
258  return sRet;
259  }
260 
261 private:
262 protected:
264  bool m_bDesc;
265 };
266 #endif // !_FILEUTILS_H
static bool IsChr(const CString &sLongName, bool bUseLstat=false)
time_t GetATime() const
bool TryExLock()
static CString CheckPathPrefix(const CString &sPath, const CString &sAdd, const CString &sHomeDir="")
static CString ChangeDir(const CString &sPath, const CString &sAdd, const CString &sHomeDir="")
Definition: FileUtils.h:67
bool m_bDesc
Definition: FileUtils.h:264
bool Truncate()
static bool MakeDir(const CString &sPath, mode_t iMode=0700)
CString TrimSuffix_n(const CString &sSuffix) const
Trim a given suffix.
static bool IsFifo(const CString &sLongName, bool bUseLstat=false)
bool Exists() const
static bool IsLnk(const CString &sLongName, bool bUseLstat=true)
Definition: FileUtils.h:41
bool Delete()
static unsigned int Delete(const CString &sWildcard, const CString &sDir=".")
Definition: FileUtils.h:224
Definition: FileUtils.h:69
Definition: FileUtils.h:68
bool Sync()
EFileAttr
Definition: FileUtils.h:66
static bool IsBlk(const CString &sLongName, bool bUseLstat=false)
static unsigned int Chmod(mode_t mode, const CString &sWildcard, const CString &sDir=".")
Definition: FileUtils.h:207
bool IsOpen() const
CString m_sShortName
Filename alone, without path.
Definition: FileUtils.h:148
bool Seek(off_t uPos)
time_t GetCTime() const
CString m_sLongName
Absolute filename (m_sPath + &quot;/&quot; + m_sShortName)
Definition: FileUtils.h:147
static void InitHomePath(const CString &sFallback)
void ClearBuffer()
Definition: FileUtils.h:37
uid_t GetUID() const
static bool FType(const CString &sFileName, EFileTypes eType, bool bUseLstat=false)
bool HadError() const
Definition: FileUtils.h:130
CDir()
Definition: FileUtils.h:160
bool ReadLine(CString &sData, const CString &sDelimiter="\n")
static bool IsReg(const CString &sLongName, bool bUseLstat=false)
Definition: FileUtils.h:30
bool Chmod(mode_t mode)
bool ExLock()
Definition: FileUtils.h:39
Definition: FileUtils.h:151
Definition: FileUtils.h:38
String class that is used inside ZNC.
Definition: ZNCString.h:67
unsigned int Delete()
Definition: FileUtils.h:230
static CString GetCWD()
Definition: FileUtils.h:250
static const CString & GetHomePath()
Definition: FileUtils.h:134
void ResetError()
Definition: FileUtils.h:131
size_t FillByWildcard(const CString &sDir, const CString &sWildcard)
Definition: FileUtils.h:181
static int GetInfo(const CString &sFile, struct stat &st)
Definition: FileUtils.h:71
~CDir()
Definition: FileUtils.h:165
bool UnLock()
CFile::EFileAttr GetSortAttr()
Definition: FileUtils.h:241
CString GetDir() const
static bool WildCmp(const CString &sWild, const CString &sString)
Do a wildcard comparision between two strings.
static bool IsSock(const CString &sLongName, bool bUseLstat=false)
Definition: FileUtils.h:70
size_t Fill(const CString &sDir)
Definition: FileUtils.h:177
bool Open(const CString &sFileName, int iFlags=O_RDONLY, mode_t iMode=0644)
static bool IsDir(const CString &sLongName, bool bUseLstat=false)
CString GetLongName() const
Definition: FileUtils.h:40
bool ReadFile(CString &sData, size_t iMaxSize=512 *1024)
EFileTypes
Definition: FileUtils.h:36
void Close()
off_t GetSize() const
void CleanUp()
Definition: FileUtils.h:169
CString GetShortName() const
bool Move(const CString &sNewFileName, bool bOverwrite=false)
CFile::EFileAttr m_eSortAttr
Definition: FileUtils.h:263
ssize_t Write(const char *pszBuffer, size_t iBytes)
Definition: FileUtils.h:43
CDir(const CString &sDir)
Definition: FileUtils.h:154
time_t GetMTime() const
bool IsDescending()
Definition: FileUtils.h:242
gid_t GetGID() const
bool Copy(const CString &sNewFileName, bool bOverwrite=false)
unsigned int Chmod(mode_t mode)
Definition: FileUtils.h:213
ssize_t Read(char *pszBuffer, int iBytes)
Definition: FileUtils.h:72
Definition: FileUtils.h:42
void SetFileName(const CString &sLongName)