1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2008 Grigor Iliev, Benno Senoner * |
* Copyright (C) 2008 - 2012 Grigor Iliev, Benno Senoner * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
23 |
#include <cstring> |
#include <cstring> |
24 |
#include <errno.h> |
#include <errno.h> |
25 |
#include <sstream> |
#include <sstream> |
26 |
|
#include <sys/stat.h> |
27 |
|
#include <dirent.h> |
28 |
|
|
29 |
#include "Exception.h" |
#include "Exception.h" |
30 |
|
#include "Path.h" |
31 |
#include "global_private.h" |
#include "global_private.h" |
32 |
|
|
33 |
#if WIN32 |
#if WIN32 |
34 |
|
#include <windows.h> |
35 |
#else |
#else |
|
#include <dirent.h> |
|
36 |
#include <ftw.h> |
#include <ftw.h> |
37 |
#endif |
#endif |
38 |
|
|
39 |
namespace LinuxSampler { |
namespace LinuxSampler { |
40 |
|
#ifdef WIN32 |
41 |
#if WIN32 |
const char File::DirSeparator = '\\'; |
42 |
char File::DirSeparator = '\\'; |
const char File::PathSeparator = ';'; |
43 |
#else |
#else |
44 |
char File::DirSeparator = '/'; |
const char File::DirSeparator = '/'; |
45 |
|
const char File::PathSeparator = ':'; |
46 |
#endif |
#endif |
47 |
|
Mutex File::DirectoryWalkerMutex; |
|
Mutex File::DirectoryWalkerMutex = Mutex(); |
|
48 |
std::vector<File::DirectoryWalker*> File::DirectoryWalkers; |
std::vector<File::DirectoryWalker*> File::DirectoryWalkers; |
49 |
std::string File::DWErrorMsg; |
std::string File::DWErrorMsg; |
50 |
|
|
51 |
|
File::File(const Path& path) { |
52 |
|
bExist = !stat(path.toNativeFSPath().c_str(), &Status); |
53 |
|
if (!bExist) ErrorMsg = strerror(errno); |
54 |
|
} |
55 |
|
|
56 |
File::File(std::string Path) { |
File::File(std::string Path) { |
|
#if WIN32 |
|
|
|
|
|
#else |
|
57 |
bExist = !stat(Path.c_str(), &Status); |
bExist = !stat(Path.c_str(), &Status); |
58 |
if(!bExist) ErrorMsg = strerror(errno); |
if(!bExist) ErrorMsg = strerror(errno); |
|
#endif |
|
59 |
} |
} |
60 |
|
|
61 |
bool File::Exist() { |
bool File::Exist() { |
69 |
bool File::IsFile() { |
bool File::IsFile() { |
70 |
if(!Exist()) return false; |
if(!Exist()) return false; |
71 |
|
|
|
#if WIN32 |
|
|
|
|
|
#else |
|
72 |
return S_ISREG(Status.st_mode); |
return S_ISREG(Status.st_mode); |
|
#endif |
|
73 |
} |
} |
74 |
|
|
75 |
bool File::IsDirectory() { |
bool File::IsDirectory() { |
76 |
if(!Exist()) return false; |
if(!Exist()) return false; |
77 |
|
|
|
#if WIN32 |
|
|
|
|
|
#else |
|
78 |
return S_ISDIR(Status.st_mode); |
return S_ISDIR(Status.st_mode); |
|
#endif |
|
79 |
} |
} |
80 |
|
|
81 |
unsigned long File::GetSize() { |
unsigned long File::GetSize() { |
82 |
if(!Exist()) return 0; |
if(!Exist()) return 0; |
83 |
|
|
84 |
#if WIN32 |
return Status.st_size; |
|
|
|
|
#else |
|
|
return Status.st_size; |
|
|
#endif |
|
85 |
} |
} |
86 |
|
|
87 |
FileListPtr File::GetFiles(std::string Dir) { |
FileListPtr File::GetFiles(std::string Dir) { |
|
#if WIN32 |
|
|
|
|
|
#else |
|
88 |
DIR* pDir = opendir(Dir.c_str()); |
DIR* pDir = opendir(Dir.c_str()); |
89 |
if (pDir == NULL) { |
if (pDir == NULL) { |
90 |
std::stringstream ss; |
std::stringstream ss; |
97 |
|
|
98 |
struct dirent* pEnt = readdir(pDir); |
struct dirent* pEnt = readdir(pDir); |
99 |
while (pEnt != NULL) { |
while (pEnt != NULL) { |
100 |
if (pEnt->d_type != DT_REG) { |
#ifdef _DIRENT_HAVE_D_TYPE |
101 |
pEnt = readdir(pDir); |
if (pEnt->d_type == DT_REG) { |
|
continue; |
|
|
} |
|
|
|
|
102 |
fileList->push_back(std::string(pEnt->d_name)); |
fileList->push_back(std::string(pEnt->d_name)); |
103 |
|
} |
104 |
|
#else |
105 |
|
struct stat s; |
106 |
|
if (stat((Dir + DirSeparator + pEnt->d_name).c_str(), &s) == 0 && S_ISREG(s.st_mode)) { |
107 |
|
fileList->push_back(std::string(pEnt->d_name)); |
108 |
|
} |
109 |
|
#endif |
110 |
pEnt = readdir(pDir); |
pEnt = readdir(pDir); |
111 |
} |
} |
112 |
|
|
118 |
} |
} |
119 |
|
|
120 |
return fileList; |
return fileList; |
|
#endif |
|
121 |
} |
} |
122 |
|
|
123 |
void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) { |
void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) { |
125 |
File f = File(Dir); |
File f = File(Dir); |
126 |
if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg()); |
if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg()); |
127 |
if(!f.IsDirectory()) throw Exception("The specified path is not a directory: " + Dir); |
if(!f.IsDirectory()) throw Exception("The specified path is not a directory: " + Dir); |
128 |
|
#ifdef WIN32 |
129 |
|
WalkDirectoryTreeSub(Dir, pWalker); |
130 |
|
#else |
131 |
DirectoryWalkerMutex.Lock(); |
DirectoryWalkerMutex.Lock(); |
132 |
DirectoryWalkers.push_back(pWalker); |
DirectoryWalkers.push_back(pWalker); |
133 |
DWErrorMsg = "Failed to process directory tree: " + Dir; |
DWErrorMsg = "Failed to process directory tree: " + Dir; |
134 |
|
|
|
#if WIN32 |
|
|
|
|
|
#else |
|
135 |
if (ftw(Dir.c_str(), FtwCallback, 10)) { |
if (ftw(Dir.c_str(), FtwCallback, 10)) { |
136 |
DirectoryWalkers.pop_back(); |
DirectoryWalkers.pop_back(); |
137 |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
138 |
throw Exception(DWErrorMsg); |
throw Exception(DWErrorMsg); |
139 |
} |
} |
|
#endif |
|
140 |
DirectoryWalkers.pop_back(); |
DirectoryWalkers.pop_back(); |
141 |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
142 |
|
#endif |
143 |
} |
} |
144 |
|
|
145 |
#if WIN32 |
#ifdef WIN32 |
146 |
|
void File::WalkDirectoryTreeSub(String Dir, DirectoryWalker* pWalker) { |
147 |
|
dmsg(2,("File: WalkDirectoryTreeSub(Dir=%s)\n", Dir.c_str())); |
148 |
|
DWORD attrs = GetFileAttributes(Dir.c_str()); |
149 |
|
if (attrs == INVALID_FILE_ATTRIBUTES) return; |
150 |
|
|
151 |
|
if (attrs & FILE_ATTRIBUTE_DIRECTORY) { |
152 |
|
pWalker->DirectoryEntry(Dir); |
153 |
|
|
154 |
|
std::string::size_type l = Dir.length() - 1; |
155 |
|
if (Dir[l] == '/') Dir[l] = '\\'; |
156 |
|
else if (Dir[l] != '\\') Dir += '\\'; |
157 |
|
|
158 |
|
WIN32_FIND_DATA fd; |
159 |
|
HANDLE h = FindFirstFile((Dir + "*").c_str(), &fd); |
160 |
|
if (h == INVALID_HANDLE_VALUE) return; |
161 |
|
do { |
162 |
|
if (strcmp(fd.cFileName, ".") != 0 && |
163 |
|
strcmp(fd.cFileName, "..") != 0) { |
164 |
|
WalkDirectoryTreeSub(Dir + fd.cFileName, pWalker); |
165 |
|
} |
166 |
|
} while (FindNextFile(h, &fd)); |
167 |
|
FindClose(h); |
168 |
|
} else { |
169 |
|
pWalker->FileEntry(Dir); |
170 |
|
} |
171 |
|
} |
172 |
#else |
#else |
173 |
int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) { |
int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) { |
174 |
dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath)); |
dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath)); |
182 |
} |
} |
183 |
|
|
184 |
return 0; |
return 0; |
185 |
}; |
} |
186 |
#endif |
#endif |
187 |
} |
} |