-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSI_UNIX.cpp
More file actions
65 lines (60 loc) · 1.77 KB
/
OSI_UNIX.cpp
File metadata and controls
65 lines (60 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "osinterface.h"
#include "functions.h"
#include <dirent.h>
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <sys/stat.h>
std::string OSInterface::getCWD(){
char buf[255];
getcwd(buf, 255);
return std::string(buf);
}
bool OSInterface::isDir(std::string path){
DIR *dir;
if((dir = opendir(path.c_str())) == NULL){
return false;
}
return true;
}
void OSInterface::getDirInfo(std::string path, std::string pattern){
DIR *dir;
if(path == "/mnt") return;
if(path == "/proc") return;
if(path == "/run") return;
if(path == "/root") return;
if(path == "/sys") return;
if((dir = opendir(path.c_str())) == NULL){
throw new OSException(path, "failed to open dir.");
return;
}
struct dirent *entry = new struct dirent();
struct stat *finfo = new struct stat();
std::string abs_path(path);
std::map<std::string,struct dirent*> entries;
dirEntryT *de;
while((entry = readdir(dir))){
if(!matchExpression(std::string(entry->d_name), pattern)) continue;
de = new dirEntryT();
stat((abs_path + "/" + std::string(entry->d_name)).c_str(), finfo);
if(finfo == NULL)
throw new OSException(std::string(entry->d_name), std::string("failed to read file info."));
de->name = std::string(entry->d_name);
if(S_ISLNK(finfo->st_mode)){
continue;
}else if(S_ISREG(finfo->st_mode)){
de->type = de->FILE;
de->type_name = "FILE";
}else if(S_ISDIR(finfo->st_mode)){
de->type = de->DIR;
de->type_name = "DIR";
}else{
continue;
de->type = de->UNKNOWN;
de->type_name = "UNKNOWN";
}
de->byte_size = finfo->st_size;
dirs.insert(std::pair<std::string, dirEntryT*>(de->name, de));
}
closedir(dir);
}