1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 - 2012 Grigor Iliev, Benno Senoner * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
* * |
10 |
* This program is distributed in the hope that it will be useful, * |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
* GNU General Public License for more details. * |
14 |
* * |
15 |
* You should have received a copy of the GNU General Public License * |
16 |
* along with this program; if not, write to the Free Software * |
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include "File.h" |
22 |
|
23 |
#include <cstring> |
24 |
#include <errno.h> |
25 |
#include <sstream> |
26 |
#include <sys/stat.h> |
27 |
#include <dirent.h> |
28 |
|
29 |
#include "Exception.h" |
30 |
#include "global_private.h" |
31 |
|
32 |
#if WIN32 |
33 |
#include <windows.h> |
34 |
#else |
35 |
#include <ftw.h> |
36 |
#endif |
37 |
|
38 |
namespace LinuxSampler { |
39 |
#ifdef WIN32 |
40 |
const char File::DirSeparator = '\\'; |
41 |
const char File::PathSeparator = ';'; |
42 |
#else |
43 |
const char File::DirSeparator = '/'; |
44 |
const char File::PathSeparator = ':'; |
45 |
#endif |
46 |
Mutex File::DirectoryWalkerMutex; |
47 |
std::vector<File::DirectoryWalker*> File::DirectoryWalkers; |
48 |
std::string File::DWErrorMsg; |
49 |
|
50 |
File::File(std::string Path) { |
51 |
bExist = !stat(Path.c_str(), &Status); |
52 |
if(!bExist) ErrorMsg = strerror(errno); |
53 |
} |
54 |
|
55 |
bool File::Exist() { |
56 |
return bExist; |
57 |
} |
58 |
|
59 |
std::string File::GetErrorMsg() { |
60 |
return ErrorMsg; |
61 |
} |
62 |
|
63 |
bool File::IsFile() { |
64 |
if(!Exist()) return false; |
65 |
|
66 |
return S_ISREG(Status.st_mode); |
67 |
} |
68 |
|
69 |
bool File::IsDirectory() { |
70 |
if(!Exist()) return false; |
71 |
|
72 |
return S_ISDIR(Status.st_mode); |
73 |
} |
74 |
|
75 |
unsigned long File::GetSize() { |
76 |
if(!Exist()) return 0; |
77 |
|
78 |
return Status.st_size; |
79 |
} |
80 |
|
81 |
FileListPtr File::GetFiles(std::string Dir) { |
82 |
DIR* pDir = opendir(Dir.c_str()); |
83 |
if (pDir == NULL) { |
84 |
std::stringstream ss; |
85 |
ss << "Failed to list the directory content of `"; |
86 |
ss << Dir << "`: " << strerror(errno); |
87 |
throw Exception(ss.str()); |
88 |
} |
89 |
|
90 |
FileListPtr fileList(new std::vector<std::string>); |
91 |
|
92 |
struct dirent* pEnt = readdir(pDir); |
93 |
while (pEnt != NULL) { |
94 |
#ifdef _DIRENT_HAVE_D_TYPE |
95 |
if (pEnt->d_type == DT_REG) { |
96 |
fileList->push_back(std::string(pEnt->d_name)); |
97 |
} |
98 |
#else |
99 |
struct stat s; |
100 |
if (stat((Dir + DirSeparator + pEnt->d_name).c_str(), &s) == 0 && S_ISREG(s.st_mode)) { |
101 |
fileList->push_back(std::string(pEnt->d_name)); |
102 |
} |
103 |
#endif |
104 |
pEnt = readdir(pDir); |
105 |
} |
106 |
|
107 |
if (closedir(pDir)) { |
108 |
std::stringstream ss; |
109 |
ss << "Failed to close directory `" << Dir << "`: "; |
110 |
ss << strerror(errno); |
111 |
throw Exception(ss.str()); |
112 |
} |
113 |
|
114 |
return fileList; |
115 |
} |
116 |
|
117 |
void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) { |
118 |
dmsg(2,("File: WalkDirectoryTree(Dir=%s)\n", Dir.c_str())); |
119 |
File f = File(Dir); |
120 |
if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg()); |
121 |
if(!f.IsDirectory()) throw Exception("The specified path is not a directory: " + Dir); |
122 |
#ifdef WIN32 |
123 |
WalkDirectoryTreeSub(Dir, pWalker); |
124 |
#else |
125 |
DirectoryWalkerMutex.Lock(); |
126 |
DirectoryWalkers.push_back(pWalker); |
127 |
DWErrorMsg = "Failed to process directory tree: " + Dir; |
128 |
|
129 |
if (ftw(Dir.c_str(), FtwCallback, 10)) { |
130 |
DirectoryWalkers.pop_back(); |
131 |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
132 |
throw Exception(DWErrorMsg); |
133 |
} |
134 |
DirectoryWalkers.pop_back(); |
135 |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
136 |
#endif |
137 |
} |
138 |
|
139 |
#ifdef WIN32 |
140 |
void File::WalkDirectoryTreeSub(String Dir, DirectoryWalker* pWalker) { |
141 |
dmsg(2,("File: WalkDirectoryTreeSub(Dir=%s)\n", Dir.c_str())); |
142 |
DWORD attrs = GetFileAttributes(Dir.c_str()); |
143 |
if (attrs == INVALID_FILE_ATTRIBUTES) return; |
144 |
|
145 |
if (attrs & FILE_ATTRIBUTE_DIRECTORY) { |
146 |
pWalker->DirectoryEntry(Dir); |
147 |
|
148 |
std::string::size_type l = Dir.length() - 1; |
149 |
if (Dir[l] == '/') Dir[l] = '\\'; |
150 |
else if (Dir[l] != '\\') Dir += '\\'; |
151 |
|
152 |
WIN32_FIND_DATA fd; |
153 |
HANDLE h = FindFirstFile((Dir + "*").c_str(), &fd); |
154 |
if (h == INVALID_HANDLE_VALUE) return; |
155 |
do { |
156 |
if (strcmp(fd.cFileName, ".") != 0 && |
157 |
strcmp(fd.cFileName, "..") != 0) { |
158 |
WalkDirectoryTreeSub(Dir + fd.cFileName, pWalker); |
159 |
} |
160 |
} while (FindNextFile(h, &fd)); |
161 |
FindClose(h); |
162 |
} else { |
163 |
pWalker->FileEntry(Dir); |
164 |
} |
165 |
} |
166 |
#else |
167 |
int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) { |
168 |
dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath)); |
169 |
try { |
170 |
if (typeflag == FTW_D) DirectoryWalkers.back()->DirectoryEntry(std::string(fpath)); |
171 |
else if (typeflag == FTW_F) DirectoryWalkers.back()->FileEntry(std::string(fpath)); |
172 |
} catch(Exception e) { |
173 |
e.PrintMessage(); |
174 |
DWErrorMsg = e.Message(); |
175 |
return -1; |
176 |
} |
177 |
|
178 |
return 0; |
179 |
} |
180 |
#endif |
181 |
} |