-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_file.c
178 lines (166 loc) · 4.17 KB
/
abstract_file.c
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
173
174
175
176
177
178
#include <glib.h>
#include <errno.h>
#include "log.h"
#include "abstract_file.h"
void abstract_file_init (AbstractFile *f, const char *name)
{
f->id = 0;
g_strlcpy(f->name, name, MAX_FILE_NAME_LENGTH);
sem_init(&f->file_lock, 0, 1);
}
void abstract_file_destroy (AbstractFile *f)
{
if (sem_destroy(&f->file_lock) != 0)
{
error("abstract_file_destroy: sem_destroy error (%d)", errno);
}
}
char *abstract_file_to_string (AbstractFile *f, char buffer[MAX_FILE_NAME_LENGTH])
{
if (f)
{
if (!lock_timed_out(abstract_file_lock(f)))
{
g_snprintf(buffer, MAX_FILE_NAME_LENGTH, "%ld"FIS"%s", f->id, f->name);
abstract_file_unlock(f);
}
else
{
warn("Timeout waiting for lock in abstract_file_to_string");
buffer[0] = 0;
}
}
else
{
buffer[0] = 0;
}
return buffer;
}
const char *abstract_file_get_name (AbstractFile *f)
{
return f->name;
}
void _set_name (AbstractFile *f, const char *new_name)
{
if (!lock_timed_out(abstract_file_lock(f)))
{
g_strlcpy(f->name, new_name, MAX_FILE_NAME_LENGTH);
abstract_file_unlock(f);
}
}
int file_id_cmp (AbstractFile *f1, AbstractFile *f2)
{
int res = 0;
if (f1!=f2) // required since we could try to double lock ourselves :(
{
if (!lock_timed_out(abstract_file_lock(f1)))
{
if (!lock_timed_out(abstract_file_lock(f2)))
{
if (!f1) { res = 1; }
else if (!f2) { res = -1; }
else {res = f1->id - f2->id; }
abstract_file_unlock(f2);
}
else
{
warn("Lock timed out in file_id_cmp");
}
abstract_file_unlock(f1);
}
else
{
warn("Lock timed out in file_id_cmp");
}
}
return res;
}
int file_name_cmp (AbstractFile *f1, AbstractFile *f2)
{
int res = 0;
if (f1!=f2) // required since we could try to double lock ourselves :(
{
if (!lock_timed_out(abstract_file_lock(f1)))
{
if (!lock_timed_out(abstract_file_lock(f2)))
{
if (!f1) { res = 1; }
else if (!f2) { res = -1; }
else {res = g_strcmp0(f1->name, f2->name); }
abstract_file_unlock(f2);
}
else
{
warn("Lock timed out in file_name_cmp");
}
abstract_file_unlock(f1);
}
else
{
warn("Lock timed out in file_name_cmp");
}
}
return res;
}
int file_name_id_cmp (AbstractFile *f1, AbstractFile *f2)
{
int res = 0;
if (f1!=f2) // required since we could try to double lock ourselves :(
{
if (!lock_timed_out(abstract_file_lock(f1)))
{
if (!lock_timed_out(abstract_file_lock(f2)))
{
if (!f1){ res = 1; }
else if (!f2) { res = -1; }
else {
int name_cmp = g_strcmp0(f1->name, f2->name);
if (name_cmp == 0)
{
res = f1->id - f2->id;
}
else
{
res = name_cmp;
}
}
abstract_file_unlock(f2);
}
else
{
warn("Lock timed out in file_name_id_cmp");
}
abstract_file_unlock(f1);
}
else
{
warn("Lock timed out in file_name_id_cmp");
}
}
return res;
}
int file_name_str_cmp (AbstractFile *f, char *name)
{
int res = 0;
if (!lock_timed_out(abstract_file_lock(f)))
{
if (!f) { res = 1; }
else if (!name) { res = -1; }
else { res = g_strcmp0(f->name, name); }
abstract_file_unlock(f);
}
else
{
warn("Couldn't acquire lock for file_name_str_cmp");
return 1;
}
return res;
}
file_id_t get_file_id (AbstractFile *f)
{
return f->id;
}
void set_file_id (AbstractFile *f, file_id_t id)
{
f->id = id;
}