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 "global_private.h" |
#include "global_private.h" |
31 |
|
|
32 |
#if WIN32 |
#if WIN32 |
33 |
|
#include <windows.h> |
34 |
#else |
#else |
|
#include <dirent.h> |
|
35 |
#include <ftw.h> |
#include <ftw.h> |
36 |
#endif |
#endif |
37 |
|
|
38 |
namespace LinuxSampler { |
namespace LinuxSampler { |
39 |
|
#ifdef WIN32 |
40 |
#if WIN32 |
const char File::DirSeparator = '\\'; |
41 |
char File::DirSeparator = '\\'; |
const char File::PathSeparator = ';'; |
42 |
#else |
#else |
43 |
char File::DirSeparator = '/'; |
const char File::DirSeparator = '/'; |
44 |
|
const char File::PathSeparator = ':'; |
45 |
#endif |
#endif |
46 |
|
Mutex File::DirectoryWalkerMutex; |
|
Mutex File::DirectoryWalkerMutex = Mutex(); |
|
47 |
std::vector<File::DirectoryWalker*> File::DirectoryWalkers; |
std::vector<File::DirectoryWalker*> File::DirectoryWalkers; |
48 |
std::string File::DWErrorMsg; |
std::string File::DWErrorMsg; |
49 |
|
|
50 |
File::File(std::string Path) { |
File::File(std::string Path) { |
|
#if WIN32 |
|
|
|
|
|
#else |
|
51 |
bExist = !stat(Path.c_str(), &Status); |
bExist = !stat(Path.c_str(), &Status); |
52 |
if(!bExist) ErrorMsg = strerror(errno); |
if(!bExist) ErrorMsg = strerror(errno); |
|
#endif |
|
53 |
} |
} |
54 |
|
|
55 |
bool File::Exist() { |
bool File::Exist() { |
63 |
bool File::IsFile() { |
bool File::IsFile() { |
64 |
if(!Exist()) return false; |
if(!Exist()) return false; |
65 |
|
|
|
#if WIN32 |
|
|
|
|
|
#else |
|
66 |
return S_ISREG(Status.st_mode); |
return S_ISREG(Status.st_mode); |
|
#endif |
|
67 |
} |
} |
68 |
|
|
69 |
bool File::IsDirectory() { |
bool File::IsDirectory() { |
70 |
if(!Exist()) return false; |
if(!Exist()) return false; |
71 |
|
|
|
#if WIN32 |
|
|
|
|
|
#else |
|
72 |
return S_ISDIR(Status.st_mode); |
return S_ISDIR(Status.st_mode); |
|
#endif |
|
73 |
} |
} |
74 |
|
|
75 |
unsigned long File::GetSize() { |
unsigned long File::GetSize() { |
76 |
if(!Exist()) return 0; |
if(!Exist()) return 0; |
77 |
|
|
78 |
#if WIN32 |
return Status.st_size; |
|
|
|
|
#else |
|
|
return Status.st_size; |
|
|
#endif |
|
79 |
} |
} |
80 |
|
|
81 |
FileListPtr File::GetFiles(std::string Dir) { |
FileListPtr File::GetFiles(std::string Dir) { |
|
#if WIN32 |
|
|
|
|
|
#else |
|
82 |
DIR* pDir = opendir(Dir.c_str()); |
DIR* pDir = opendir(Dir.c_str()); |
83 |
if (pDir == NULL) { |
if (pDir == NULL) { |
84 |
std::stringstream ss; |
std::stringstream ss; |
91 |
|
|
92 |
struct dirent* pEnt = readdir(pDir); |
struct dirent* pEnt = readdir(pDir); |
93 |
while (pEnt != NULL) { |
while (pEnt != NULL) { |
94 |
if (pEnt->d_type != DT_REG) { |
#ifdef _DIRENT_HAVE_D_TYPE |
95 |
pEnt = readdir(pDir); |
if (pEnt->d_type == DT_REG) { |
96 |
continue; |
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)); |
fileList->push_back(std::string(pEnt->d_name)); |
102 |
|
} |
103 |
|
#endif |
104 |
pEnt = readdir(pDir); |
pEnt = readdir(pDir); |
105 |
} |
} |
106 |
|
|
112 |
} |
} |
113 |
|
|
114 |
return fileList; |
return fileList; |
|
#endif |
|
115 |
} |
} |
116 |
|
|
117 |
void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) { |
void File::WalkDirectoryTree(std::string Dir, DirectoryWalker* pWalker) { |
119 |
File f = File(Dir); |
File f = File(Dir); |
120 |
if(!f.Exist()) throw Exception("Fail to stat `" + Dir + "`: " + f.GetErrorMsg()); |
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); |
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(); |
DirectoryWalkerMutex.Lock(); |
126 |
DirectoryWalkers.push_back(pWalker); |
DirectoryWalkers.push_back(pWalker); |
127 |
DWErrorMsg = "Failed to process directory tree: " + Dir; |
DWErrorMsg = "Failed to process directory tree: " + Dir; |
128 |
|
|
|
#if WIN32 |
|
|
|
|
|
#else |
|
129 |
if (ftw(Dir.c_str(), FtwCallback, 10)) { |
if (ftw(Dir.c_str(), FtwCallback, 10)) { |
130 |
DirectoryWalkers.pop_back(); |
DirectoryWalkers.pop_back(); |
131 |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
132 |
throw Exception(DWErrorMsg); |
throw Exception(DWErrorMsg); |
133 |
} |
} |
|
#endif |
|
134 |
DirectoryWalkers.pop_back(); |
DirectoryWalkers.pop_back(); |
135 |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
if (DirectoryWalkers.size() == 0) DirectoryWalkerMutex.Unlock(); |
136 |
|
#endif |
137 |
} |
} |
138 |
|
|
139 |
#if WIN32 |
#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 |
#else |
167 |
int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) { |
int File::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) { |
168 |
dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath)); |
dmsg(2,("File: FtwCallback(fpath=%s)\n", fpath)); |
176 |
} |
} |
177 |
|
|
178 |
return 0; |
return 0; |
179 |
}; |
} |
180 |
#endif |
#endif |
|
std::string File::basename(std::string path) { |
|
|
size_t begin = 0; |
|
|
size_t end = path.length(); |
|
|
|
|
|
for(size_t i=0; i<path.length(); ++i){ |
|
|
if(path[i] == File::DirSeparator) begin = i+1; |
|
|
} |
|
|
return path.substr(begin, end).c_str(); |
|
|
} |
|
|
|
|
|
std::string File::basename(std::string path, std::string sep) { |
|
|
std::string tmp = File::basename(path); |
|
|
if(sep.empty()) |
|
|
sep = "."; |
|
|
size_t begin = 0; |
|
|
size_t end = tmp.length(); |
|
|
size_t lastdot = tmp.find_last_of(sep)-1; |
|
|
if(lastdot > 0) |
|
|
end = lastdot; |
|
|
return tmp.substr(begin, end).c_str(); |
|
|
} |
|
181 |
} |
} |