]>
jfr.im git - irc/rizon/znc.git/blob - FileUtils.h
2 * Copyright (C) 2004-2011 See the AUTHORS file for details.
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.
12 #include "zncconfig.h"
13 #include "ZNCString.h"
18 #include <sys/fcntl.h>
28 CFile(const CString
& sLongName
);
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);
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;
58 // for gettin file types, using fstat instead
59 static bool FType(const CString
& sFileName
, EFileTypes eType
, bool bUseLstat
= false);
71 // Functions to retrieve file information
74 off_t
GetSize() const;
75 time_t GetATime() const;
76 time_t GetMTime() const;
77 time_t GetCTime() const;
80 static bool Exists(const CString
& sFile
);
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
);
91 // Functions to manipulate the file on the filesystem
94 bool Move(const CString
& sNewFileName
, bool bOverwrite
= false);
95 bool Copy(const CString
& sNewFileName
, bool bOverwrite
= false);
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
);
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
);
115 bool TryExLock(const CString
& sLockFile
, int iFlags
= O_RDWR
| O_CREAT
);
121 CString
GetLongName() const;
122 CString
GetShortName() const;
123 CString
GetDir() const;
125 bool HadError() const { return m_bHadError
; }
126 void ResetError() { m_bHadError
= false; }
128 static void InitHomePath(const CString
& sFallback
);
129 static const CString
& GetHomePath() { return m_sHomePath
; }
132 // fcntl() locking wrapper
133 bool Lock(int iType
, bool bBlocking
);
139 static CString m_sHomePath
;
142 CString m_sLongName
; //!< Absolute filename (m_sPath + "/" + m_sShortName)
143 CString m_sShortName
; //!< Filename alone, without path
146 class CDir
: public vector
<CFile
*> {
149 CDir(const CString
& sDir
) {
151 m_eSortAttr
= CFile::FA_Name
;
157 m_eSortAttr
= CFile::FA_Name
;
165 for (unsigned int a
= 0; a
< size(); a
++) {
172 int Fill(const CString
& sDir
) {
173 return FillByWildcard(sDir
, "*");
176 int FillByWildcard(const CString
& sDir
, const CString
& sWildcard
) {
178 DIR* dir
= opendir((sDir
.empty()) ? "." : sDir
.c_str());
186 while ((de
= readdir(dir
)) != 0) {
187 if ((strcmp(de
->d_name
, ".") == 0) || (strcmp(de
->d_name
, "..") == 0)) {
190 if ((!sWildcard
.empty()) && (!CString(de
->d_name
).WildCmp(sWildcard
))) {
194 CFile
*file
= new CFile(sDir
+ "/" + de
->d_name
/*, this*/); // @todo need to pass pointer to 'this' if we want to do Sort()
202 static unsigned int Chmod(mode_t mode
, const CString
& sWildcard
, const CString
& sDir
= ".") {
204 cDir
.FillByWildcard(sDir
, sWildcard
);
205 return cDir
.Chmod(mode
);
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
)) {
219 static unsigned int Delete(const CString
& sWildcard
, const CString
& sDir
= ".") {
221 cDir
.FillByWildcard(sDir
, sWildcard
);
222 return cDir
.Delete();
225 unsigned int Delete() {
226 unsigned int uRet
= 0;
227 for (unsigned int a
= 0; a
< size(); a
++) {
228 if ((*this)[a
]->Delete()) {
236 CFile::EFileAttr
GetSortAttr() { return m_eSortAttr
; }
237 bool IsDescending() { return m_bDesc
; }
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);
245 static CString
GetCWD() {
247 char * pszCurDir
= getcwd(NULL
, 0);
258 CFile::EFileAttr m_eSortAttr
;
261 #endif // !_FILEUTILS_H