-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalFileSystem.cpp
167 lines (151 loc) · 4.39 KB
/
LocalFileSystem.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*************************************************************************
> File Name: LocalFileSystem.cpp
> Author: Weidong, ZHANG
> Mail: [email protected]
> Created Time: Thu 01 Jan 2015 06:20:31 PM PST
************************************************************************/
#include "LocalFileSystem.h"
namespace pdfs {
Metadata LocalFileSystem::readMetadata(const std::string filename)
{
Metadata metadata = {"", "", "", "", 0, 0, 0};
std::ifstream fin("metadata/" + filename,
std::ios_base::in | std::ios_base::binary);
if (!fin.good())
{
std::cerr << "Open metadata file failed.\n";
return metadata;
}
fin.read(reinterpret_cast<char*>(&metadata), sizeof(struct Metadata));
fin.close();
return metadata;
}
ssize_t LocalFileSystem::writeMetadata(
Metadata &metadata)
{
std::ofstream fout(METADATA_FOLDER + std::string(metadata.filename),
std::ios_base::out | std::ios_base::binary);
if (!fout.good())
{
std::cerr << "Open/Create metafile failed.\n";
return -1;
}
fout.write(reinterpret_cast<char*>(&metadata), sizeof(metadata));
fout.close();
return 0;
}
ssize_t LocalFileSystem::checkFileExists(std::string filename)
{
ssize_t exists = 0;
std::ifstream fin(filename, std::ifstream::in);
if (!fin.good())
exists = -1;
else
exists = 1;
fin.close();
return exists;
}
// Check directory exists, or create the directory
ssize_t LocalFileSystem::createDir(std::string path)
{
std::ifstream fin(path, std::ifstream::in);
if (!fin.good())
{
if(mkdir(path.c_str(), 0755) == -1)
{
std::cerr << "Create directory (" <<
path << ") failed.\n";
return -1;
}
}
fin.close();
return 0;
}
// Remove File
ssize_t LocalFileSystem::removeFile(std::string filename)
{
return remove(filename.c_str());
}
// Get File name from path
std::string LocalFileSystem::getBasename(
const std::string &pathname)
{
std::string basename = "";
size_t slashPos = pathname.find_last_of("/");
if ( slashPos != std::string::npos)
basename = pathname.substr(slashPos + 1,
pathname.size() - slashPos);
else
basename = pathname;
return basename;
}
// Get File size
unsigned int LocalFileSystem::getFileSize(const std::string &pathname)
{
std::ifstream fin(pathname);
if (!fin.is_open())
{
std::cerr << "Open file failed in getFileSize.\n";
return -1;
}
fin.seekg(0, std::ios::end);
std::streampos ps = fin.tellg();
fin.close();
return ps;
}
// Get File MD5
std::string LocalFileSystem::getFileMD5(const std::string &pathname)
{
MD5 md5;
return md5.calc(pathname.c_str());
}
std::string LocalFileSystem::getFileOwner(const std::string filename)
{
// IP:local_owner
return "someone";
}
size_t LocalFileSystem::writeFile(char *message, ssize_t size)
{
// Instance the messageheader from a char* pointer
struct messageheader header;
memcpy(&header, message, sizeof(struct messageheader));
// Concat the path of temp to filename
char filepath[128];
bzero(filepath, sizeof(filepath));
memcpy(filepath, UPLOAD_FOLDER.c_str(), UPLOAD_FOLDER.size());
memcpy(filepath + UPLOAD_FOLDER.size(), header.blockid,
sizeof(header.blockid));
// Open file to write
int fd = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
ftruncate(fd, header.blockdatasize);
int pwritebytes = 0;
if ((pwritebytes = pwrite(fd, message + sizeof(struct messageheader),
header.msgdatasize, header.msgdataoffset)) == -1)
{
perror("pwrite failed.\n");
}
close(fd);
return pwritebytes;
} // end of function writeFile
std::vector<std::string> LocalFileSystem::listDirFiles(const std::string dir)
{
std::vector<std::string> filenames;
DIR* dp;
struct dirent* dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
perror("opendir");
return std::move(filenames);
}
while((dirp = readdir(dp)) != NULL)
{
char fullname[255];
memset(fullname, 0, sizeof(fullname));
/* ignore hidden files */
if(dirp->d_name[0] == '.')
continue;
filenames.push_back(dirp->d_name);
}
return std::move(filenames);
} // end of function listDirFiles
} // end of namepsace pdfs