-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.h
57 lines (49 loc) · 1.39 KB
/
file.h
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
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<ctime>
#include<sstream>
#include<pthread.h>
using namespace std;
typedef struct FileMetaData FileMetaData;
struct FileMetaData{
// int id;
string filename; // filename can also be id??
string path;
bool hasWriteLock = false;
string owner; // username of file owner
string whoHasWriteLock; // who has write lock ?
time_t lastModified;
int currentReaders = 0;
pthread_mutex_t fileMutex;
pthread_cond_t fileWait;
// add methods here
FileMetaData() {
fileMutex = PTHREAD_MUTEX_INITIALIZER;
fileWait = PTHREAD_COND_INITIALIZER;
whoHasWriteLock = "";
// hasWriteLock = false;
// currentReaders = 0;
}
~FileMetaData() {
pthread_mutex_destroy(&fileMutex);
pthread_cond_destroy(&fileWait);
}
FileMetaData(string filename) {
FileMetaData();
this->filename = filename;
}
string toString() {
stringstream oss;
oss << filename << " " << path << " "
<< " " << hasWriteLock << " " << owner
<< " " << lastModified << " " << currentReaders;
return oss.str();
}
void fromString(string str) {
stringstream iss(str);
iss >> filename >> path
>> hasWriteLock >> owner >> lastModified
>> currentReaders;
}
};