1 |
#include "FileHandler.h"
|
2 |
#include <sys/stat.h>
|
3 |
#include <sys/types.h>
|
4 |
#include "../DateTime/DateTime.h"
|
5 |
|
6 |
//http://www.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html
|
7 |
|
8 |
#ifndef WIN32
|
9 |
#include <dirent.h>
|
10 |
#endif
|
11 |
|
12 |
#ifdef WIN32
|
13 |
#include <io.h>
|
14 |
#endif
|
15 |
|
16 |
FileHandler::FileHandler()
|
17 |
{
|
18 |
fileInfo=null;
|
19 |
|
20 |
}
|
21 |
FileHandler::~FileHandler()
|
22 |
{
|
23 |
if (fileInfo!=null)
|
24 |
{
|
25 |
delete fileInfo;
|
26 |
}
|
27 |
}
|
28 |
|
29 |
#ifndef WIN32
|
30 |
|
31 |
ObjectLinkedList<FileInfo> *FileHandler::listDirectory(String &fileDirectory)
|
32 |
{
|
33 |
return listDirectory(fileDirectory.buffer);
|
34 |
}
|
35 |
|
36 |
ObjectLinkedList<FileInfo> *FileHandler::listDirectory(char *fileDirectory)
|
37 |
{
|
38 |
ObjectLinkedList<FileInfo> *fileList = new ObjectLinkedList<FileInfo>();
|
39 |
|
40 |
DIR *dirp = opendir(fileDirectory);
|
41 |
|
42 |
String filePath=fileDirectory;
|
43 |
|
44 |
while (dirp)
|
45 |
{
|
46 |
int errno=0;
|
47 |
struct dirent * dp=null;
|
48 |
if ((dp = readdir(dirp)) != NULL)
|
49 |
{
|
50 |
if (dp->d_name!=null)
|
51 |
{
|
52 |
FileInfo *fileInfo = new FileInfo();
|
53 |
fileInfo->name->setString(dp->d_name);
|
54 |
|
55 |
String fullFilePath=filePath + dp->d_name;
|
56 |
|
57 |
struct stat *Stat=null;
|
58 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
59 |
|
60 |
int statStatus=stat(fullFilePath.buffer, Stat);
|
61 |
|
62 |
fileInfo->size=Stat->st_size;
|
63 |
String dateTime=DateTime(datetime_, Stat->st_mtime);
|
64 |
|
65 |
fileInfo->lastModified->setString(dateTime);
|
66 |
|
67 |
|
68 |
String dirPath=fullFilePath;
|
69 |
|
70 |
DIR *dirp=opendir(dirPath.buffer);
|
71 |
|
72 |
if(dirp!=null)
|
73 |
{
|
74 |
fileInfo->directory=true;
|
75 |
}
|
76 |
else
|
77 |
{
|
78 |
fileInfo->directory=false;
|
79 |
}
|
80 |
|
81 |
if (dirp!=null)
|
82 |
{
|
83 |
closedir(dirp);
|
84 |
}
|
85 |
|
86 |
/*
|
87 |
if ((Stat->st_mode & S_IFDIR) != S_IFDIR)
|
88 |
{
|
89 |
fileInfo->directory=false;
|
90 |
fileInfo->file=true;
|
91 |
}
|
92 |
else
|
93 |
{
|
94 |
fileInfo->directory=true;
|
95 |
fileInfo->file=false;
|
96 |
}
|
97 |
*/
|
98 |
if (Stat!=null)
|
99 |
{
|
100 |
free(Stat);
|
101 |
}
|
102 |
|
103 |
fileList->addLast(fileInfo);
|
104 |
}
|
105 |
else
|
106 |
{
|
107 |
closedir(dirp);
|
108 |
return fileList;
|
109 |
}
|
110 |
}
|
111 |
else
|
112 |
{
|
113 |
if (errno == 0)
|
114 |
{
|
115 |
closedir(dirp);
|
116 |
return fileList;
|
117 |
}
|
118 |
closedir(dirp);
|
119 |
return fileList;
|
120 |
}
|
121 |
}
|
122 |
|
123 |
return fileList;
|
124 |
}
|
125 |
|
126 |
#endif
|
127 |
|
128 |
#ifdef WIN32
|
129 |
|
130 |
ObjectLinkedList<FileInfo> *FileHandler::listDirectory(char *fileDirectory)
|
131 |
{
|
132 |
String directory=fileDirectory;
|
133 |
return listDirectory(directory);
|
134 |
}
|
135 |
|
136 |
ObjectLinkedList<FileInfo> *FileHandler::listDirectory(String &fileDirectory)
|
137 |
{
|
138 |
ObjectLinkedList<FileInfo> *fileList = new ObjectLinkedList<FileInfo>();
|
139 |
|
140 |
struct _finddata_t *findData;
|
141 |
|
142 |
findData=(_finddata_t*)malloc(sizeof(_finddata_t));
|
143 |
|
144 |
String searchPath=fileDirectory + "*";
|
145 |
|
146 |
long file=_findfirst(searchPath.buffer, findData);
|
147 |
|
148 |
if (file!=0)
|
149 |
{
|
150 |
fileList->addLast(createFileInfo(findData->name,fileDirectory.buffer));
|
151 |
|
152 |
int fileStatus=1;
|
153 |
|
154 |
while(fileStatus=_findnext(file, findData)!=-1)
|
155 |
{
|
156 |
FileInfo *fileInfo=createFileInfo(findData->name,fileDirectory.buffer);
|
157 |
fileList->addLast(fileInfo);
|
158 |
}
|
159 |
|
160 |
_findclose(file);
|
161 |
}
|
162 |
|
163 |
|
164 |
if (findData!=null)
|
165 |
{
|
166 |
free(findData);
|
167 |
}
|
168 |
|
169 |
return fileList;
|
170 |
}
|
171 |
|
172 |
FileInfo *FileHandler::createFileInfo(char *name, char *path)
|
173 |
{
|
174 |
FileInfo *fileInfo = new FileInfo();
|
175 |
fileInfo->name->setString(name);
|
176 |
|
177 |
String filePath=path;
|
178 |
filePath+=name;
|
179 |
|
180 |
struct stat *Stat=null;
|
181 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
182 |
|
183 |
int statStatus=stat(filePath.buffer, Stat);
|
184 |
|
185 |
fileInfo->size=Stat->st_size;
|
186 |
String dateTime=DateTime(datetime_, Stat->st_mtime);
|
187 |
|
188 |
fileInfo->lastModified->setString(dateTime);
|
189 |
|
190 |
if ((Stat->st_mode & S_IFDIR) != S_IFDIR)
|
191 |
{
|
192 |
fileInfo->directory=false;
|
193 |
fileInfo->file=true;
|
194 |
}
|
195 |
else
|
196 |
{
|
197 |
fileInfo->directory=true;
|
198 |
fileInfo->file=false;
|
199 |
}
|
200 |
|
201 |
if (Stat!=null)
|
202 |
{
|
203 |
free(Stat);
|
204 |
}
|
205 |
|
206 |
return fileInfo;
|
207 |
}
|
208 |
|
209 |
|
210 |
#endif
|
211 |
|
212 |
|
213 |
int FileHandler::getFileSize(const String &fileName)
|
214 |
{
|
215 |
return getFileSize(fileName.buffer);
|
216 |
}
|
217 |
|
218 |
int FileHandler::getFileSize(char *fileName)
|
219 |
{
|
220 |
struct stat *Stat=null;
|
221 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
222 |
|
223 |
int statStatus=stat(fileName,Stat);
|
224 |
int fileSize=Stat->st_size;
|
225 |
|
226 |
if (Stat!=null)
|
227 |
{
|
228 |
free(Stat);
|
229 |
}
|
230 |
|
231 |
return fileSize;
|
232 |
}
|
233 |
|
234 |
FileInfo *FileHandler::getFileInfo(const String &fileName)
|
235 |
{
|
236 |
return getFileInfo(fileName.buffer);
|
237 |
}
|
238 |
|
239 |
FileInfo *FileHandler::getFileInfo(char *fileName)
|
240 |
{
|
241 |
if (fileInfo!=null)
|
242 |
{
|
243 |
delete fileInfo;
|
244 |
fileInfo=null;
|
245 |
}
|
246 |
|
247 |
fileInfo = new FileInfo();
|
248 |
|
249 |
struct stat *Stat=null;
|
250 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
251 |
|
252 |
int statStatus=stat(fileName, Stat);
|
253 |
|
254 |
if (statStatus!=-1)
|
255 |
{
|
256 |
fileInfo->size=Stat->st_size;
|
257 |
|
258 |
#ifndef WIN32
|
259 |
|
260 |
String dateTime=DateTime(datetime_, Stat->st_mtime);
|
261 |
fileInfo->lastModified->setString(dateTime);
|
262 |
|
263 |
DIR *dirp=opendir(fileName);
|
264 |
|
265 |
if(dirp!=null)
|
266 |
{
|
267 |
fileInfo->directory=true;
|
268 |
}
|
269 |
else
|
270 |
{
|
271 |
fileInfo->directory=false;
|
272 |
}
|
273 |
|
274 |
|
275 |
if (dirp!=null)
|
276 |
{
|
277 |
closedir(dirp);
|
278 |
}
|
279 |
|
280 |
#endif
|
281 |
|
282 |
#ifdef WIN32
|
283 |
|
284 |
if ((Stat->st_mode & S_IFDIR) == S_IFDIR)
|
285 |
{
|
286 |
fileInfo->directory=true;
|
287 |
}
|
288 |
else
|
289 |
{
|
290 |
fileInfo->directory=false;
|
291 |
}
|
292 |
|
293 |
#endif
|
294 |
|
295 |
#ifdef WIN32
|
296 |
if ( (Stat->st_mode & _S_IREAD) != _S_IREAD )
|
297 |
#endif
|
298 |
#ifdef linux
|
299 |
if ( (Stat->st_mode & S_IRUSR) != S_IRUSR )
|
300 |
#endif
|
301 |
#ifdef sun
|
302 |
if ( (Stat->st_mode & S_IRUSR) != S_IRUSR )
|
303 |
#endif
|
304 |
{
|
305 |
fileInfo->readPermission=false;
|
306 |
}
|
307 |
else
|
308 |
{
|
309 |
fileInfo->readPermission=true;
|
310 |
}
|
311 |
}
|
312 |
|
313 |
if (Stat!=null)
|
314 |
{
|
315 |
free(Stat);
|
316 |
}
|
317 |
|
318 |
|
319 |
return fileInfo;
|
320 |
}
|
321 |
|