-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_manager.cpp
172 lines (145 loc) · 3.84 KB
/
file_manager.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
168
169
170
171
172
/*
* File: file_manager.cpp
* Group: 14
* Description: Implementation of the FileManager class
*/
#include <fstream>
#include <cmath>
#include "file_manager.h"
#include "return_codes.h"
#include "constants.h"
#include "util.h"
#include "file_chunk.h"
#include <iostream> // remove
#include <sstream> // remove
FileManager::FileManager() {
pthread_mutex_init(&_mutex, NULL);
}
/*
* Destructor. Iterates through the files tables and delete all the dynamically
* allocated File objects.
*/
FileManager::~FileManager() {
Log::info("FileManager destructor()");
std::map<std::string, File*>::iterator it;
for (it = _filesTable.begin(); it != _filesTable.end(); ++it) {
delete it->second;
}
}
/*
* Returns a pointer to the given File object if it exists.
*
* filename: The requested File object.
*/
File* FileManager::getFile(std::string filename) {
Log::info("in FileManager::getFile()");
Log::info("getFile() filename = " + filename);
if (exists(filename))
return _filesTable[filename];
else
return NULL;
}
/*
* Returns true if the given file exists in the files table.
*
* filename: The file to check for
*/
bool FileManager::exists(std::string filename) {
return (_filesTable.count(filename) > 0);
}
/*
* Copy the given file to the location where all replicated files are stored
* and add it to the set of managed files.
*
* filepath: The path to the file to be added
*/
int FileManager::addLocalFile(std::string filepath) {
Log::info("in FileManager::addLocalFile()");
char* buffer;
std::ifstream inFile(filepath.c_str(),
std::ios::in | std::ios::binary | std::ios::ate);
if (!inFile.is_open())
return returnCodes::ERROR_FILE_NOT_FOUND;
// Copy file
// Read file into buffer
std::ifstream::pos_type size = inFile.tellg();
buffer = new char[size];
inFile.seekg(0, std::ios::beg);
inFile.read(buffer, size);
inFile.close();
// Write file from buffer
std::string filename = Util::extractFilename(filepath);
std::string outFilename = Util::generateUniqueFilename(
constants::FILES_DIR, filename);
std::ofstream outFile(outFilename.c_str(),
std::ios::out | std::ios::binary);
if (!outFile.is_open())
return returnCodes::ERROR_UNKNOWN;
outFile.write(buffer, size);
outFile.close();
delete[] buffer;
// Create File object and adds to files table
int numberOfChunks = ceil((double)size / (double)constants::CHUNK_SIZE);
File* file = new File(filename, numberOfChunks, true, size);
lock();
_filesTable[filename] = file;
unlock();
return returnCodes::OK;
}
/*
* Create a File object for the remote file and add it to the files table if it
* does not already exist.
*/
int FileManager::addRemoteFile(std::string filepath, int numberOfChunks, int fileSize) {
Log::info("in FileManager::addRemoteFile()");
lock();
if (_filesTable.count(filepath) == 0) {
File* file = new File(filepath, numberOfChunks, false, fileSize);
file->_numChunks = 0;
_filesTable[filepath] = file;
}
unlock();
return returnCodes::OK;
}
/*
* Write the given chunk to file.
*
* fc: A filled FileChunk object
*/
void FileManager::addChunkToFile(FileChunk& fc) {
Log::info("FileManager::addChunkToFile()");
lock();
if (_filesTable.count(fc.getFilename()) > 0) {
if (!_filesTable[fc.getFilename()]->isAvailable(fc.getChunkIndex()))
_filesTable[fc.getFilename()]->writeChunk(fc);
}
unlock();
}
/*
* Read a chunk from file.
*
* fc: A partly filled FileChunk object
*/
bool FileManager::getChunkFromFile(FileChunk& fc) {
Log::info("FileManager::getChunkFromFile()");
lock();
if (_filesTable.count(fc.getFilename()) == 0) {
unlock();
return false;
}
_filesTable[fc.getFilename()]->readChunk(fc);
unlock();
return true;
}
/*
* Lock the class to synchronize access across threads.
*/
void FileManager::lock() {
pthread_mutex_lock(&_mutex);
}
/*
* Unlock the class.
*/
void FileManager::unlock() {
pthread_mutex_unlock(&_mutex);
}