]> jfr.im git - irc/rizon/znc.git/blame - FileUtils.h
Write forceserver and webircpassword to conf
[irc/rizon/znc.git] / FileUtils.h
CommitLineData
a09a7e79 1/*
b9b0fd4c 2 * Copyright (C) 2004-2011 See the AUTHORS file for details.
a09a7e79 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 */
6dcacaa7 8
b58cb3c8 9#ifndef _FILEUTILS_H
10#define _FILEUTILS_H
11
3ecbf133 12#include "zncconfig.h"
57f4288c 13#include "ZNCString.h"
b58cb3c8 14#include <dirent.h>
e72c4456 15#include <map>
1761fe71
US
16#include <stdlib.h>
17#include <string.h>
18#include <sys/fcntl.h>
a9f8e655 19#include <unistd.h>
b58cb3c8 20#include <vector>
e72c4456 21
b58cb3c8 22using std::vector;
23using std::map;
24
25class CFile {
26public:
ecf431f2 27 CFile();
beb5b49b 28 CFile(const CString& sLongName);
acd854eb 29 ~CFile();
b58cb3c8 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
9db44ab1 41 void SetFileName(const CString& sLongName);
beb5b49b 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);
b58cb3c8 49
81eef389 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;
b58cb3c8 57
b58cb3c8 58 // for gettin file types, using fstat instead
7999cacf 59 static bool FType(const CString& sFileName, EFileTypes eType, bool bUseLstat = false);
b58cb3c8 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;
fa285b7c 74 off_t GetSize() const;
5eed1f43 75 time_t GetATime() const;
76 time_t GetMTime() const;
77 time_t GetCTime() const;
fa285b7c 78 uid_t GetUID() const;
79 gid_t GetGID() const;
beb5b49b 80 static bool Exists(const CString& sFile);
b58cb3c8 81
fa285b7c 82 static off_t GetSize(const CString& sFile);
5eed1f43 83 static time_t GetATime(const CString& sFile);
84 static time_t GetMTime(const CString& sFile);
85 static time_t GetCTime(const CString& sFile);
fa285b7c 86 static uid_t GetUID(const CString& sFile);
87 static gid_t GetGID(const CString& sFile);
beb5b49b 88 static int GetInfo(const CString& sFile, struct stat& st);
b58cb3c8 89
90 //
91 // Functions to manipulate the file on the filesystem
92 //
f495acd1 93 bool Delete();
fa285b7c 94 bool Move(const CString& sNewFileName, bool bOverwrite = false);
95 bool Copy(const CString& sNewFileName, bool bOverwrite = false);
b58cb3c8 96
beb5b49b 97 static bool Delete(const CString& sFileName);
98 static bool Move(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false);
6be9f989 99 static bool Copy(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite = false);
b58cb3c8 100 bool Chmod(mode_t mode);
beb5b49b 101 static bool Chmod(const CString& sFile, mode_t mode);
4e767b3e 102 bool Seek(off_t uPos);
0a622749 103 bool Truncate();
f618ce2a 104 bool Sync();
6345ce12 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);
b58cb3c8 107 int Read(char *pszBuffer, int iBytes);
c2b8b7c6 108 bool ReadLine(CString & sData, const CString & sDelimiter = "\n");
b7f38c4d 109 bool ReadFile(CString& sData, size_t iMaxSize = 512 * 1024);
b58cb3c8 110 int Write(const char *pszBuffer, u_int iBytes);
beb5b49b 111 int Write(const CString & sData);
b58cb3c8 112 void Close();
ecf431f2 113 void ClearBuffer();
b58cb3c8 114
cc2d84d4 115 bool TryExLock(const CString& sLockFile, int iFlags = O_RDWR | O_CREAT);
12734782 116 bool TryExLock();
33ce80f4 117 bool ExLock();
12734782 118 bool UnLock();
119
ecf431f2 120 bool IsOpen() const;
beb5b49b 121 CString GetLongName() const;
122 CString GetShortName() const;
ecf431f2 123 CString GetDir() const;
b58cb3c8 124
70358cab
US
125 bool HadError() const { return m_bHadError; }
126 void ResetError() { m_bHadError = false; }
127
f9ffe6f4
US
128 static void InitHomePath(const CString& sFallback);
129 static const CString& GetHomePath() { return m_sHomePath; }
130
b58cb3c8 131private:
33ce80f4 132 // fcntl() locking wrapper
133 bool Lock(int iType, bool bBlocking);
12734782 134
f2d7ae1a 135 CString m_sBuffer;
136 int m_iFD;
70358cab 137 bool m_bHadError;
b58cb3c8 138
f9ffe6f4
US
139 static CString m_sHomePath;
140
b58cb3c8 141protected:
3faf0adf 142 CString m_sLongName; //!< Absolute filename (m_sPath + "/" + m_sShortName)
143 CString m_sShortName; //!< Filename alone, without path
b58cb3c8 144};
145
146class CDir : public vector<CFile*> {
147public:
148
beb5b49b 149 CDir(const CString& sDir) {
b58cb3c8 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
acd854eb 160 ~CDir() {
b58cb3c8 161 CleanUp();
162 }
163
acd854eb 164 void CleanUp() {
b58cb3c8 165 for (unsigned int a = 0; a < size(); a++) {
166 delete (*this)[a];
167 }
168
169 clear();
170 }
171
beb5b49b 172 int Fill(const CString& sDir) {
b58cb3c8 173 return FillByWildcard(sDir, "*");
174 }
175
beb5b49b 176 int FillByWildcard(const CString& sDir, const CString& sWildcard) {
b58cb3c8 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 }
0823b27f 190 if ((!sWildcard.empty()) && (!CString(de->d_name).WildCmp(sWildcard))) {
b58cb3c8 191 continue;
192 }
193
3faf0adf 194 CFile *file = new CFile(sDir + "/" + de->d_name/*, this*/); // @todo need to pass pointer to 'this' if we want to do Sort()
b58cb3c8 195 push_back(file);
196 }
197
198 closedir(dir);
199 return size();
200 }
201
beb5b49b 202 static unsigned int Chmod(mode_t mode, const CString& sWildcard, const CString& sDir = ".") {
b58cb3c8 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
13dd3996 219 static unsigned int Delete(const CString& sWildcard, const CString& sDir = ".") {
b58cb3c8 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
c7583c49 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 = "");
00613bc9 242 static CString ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHomeDir = "");
b16e3ebe 243 static bool MakeDir(const CString& sPath, mode_t iMode = 0700);
01bc68b1 244
beb5b49b 245 static CString GetCWD() {
246 CString sRet;
b58cb3c8 247 char * pszCurDir = getcwd(NULL, 0);
248 if (pszCurDir) {
249 sRet = pszCurDir;
250 free(pszCurDir);
251 }
252
253 return sRet;
254 }
255
256private:
257protected:
f2d7ae1a 258 CFile::EFileAttr m_eSortAttr;
259 bool m_bDesc;
b58cb3c8 260};
b58cb3c8 261#endif // !_FILEUTILS_H