-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefines.c
More file actions
312 lines (265 loc) · 7.76 KB
/
Copy pathdefines.c
File metadata and controls
312 lines (265 loc) · 7.76 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/** @file defines.c
* @brief Contiene l'implementazione delle funzioni
* specifiche del progetto.
*/
#include "defines.h"
#include "err_exit.h"
/**
* Controlla se string2 inizia con gli stessi caratteri di string1
* @param string1 - stringa per il controllo
* @param string2 - stringa da controllare
* @return 1 - se stroing2 non inizia con string1
* @return 0 - se string2 inizia con string1
* @return -1 - in caso di stringhe non valide
**/
int check_string(const char *string1, char *string2)
{
if(strcmp(string1, string2) == 0){
return 0;
}
if (string1 == NULL || string2 == NULL ||
strlen(string1) > strlen(string2)) {
return -1;
}
size_t i;
for (i = 0; string1[i] == string2[i]; i++);
return (i == strlen(string1)) ? 0 : 1;
}
/**
* Controlla se il file <path> è di dimensione <= a 4096 Byte
* @param path - percorso dove si trova il file nel filesystem
* @return 1 - se #path è > 4096 Byte
* @return 0 - se #path è <= 4096 Byte
* @return -1 - in caso di path non valido
**/
int check_size(const char *path)
{
if (path == NULL) {
return -1;
}
struct stat buffer;
errno = 0;
if ((stat(path, &buffer)) == -1 && errno != 0) {
char msg[PATH_MAX];
snprintf(msg, PATH_MAX, "stat failed: %s ", path);
perror(msg);
return -1;
}
else {
if (buffer.st_size <= MAX_FILE_SIZE) {
return 0;
} else {
return 1;
}
}
}
/**
* Conta il numero di caratteri del file passato come parametro
* @param fd - file descriptor del file
* @return Br - numero di caratteri del file
* @return -1 - in caso di errore delle system call
**/
ssize_t count_char(int fd)
{
ssize_t Br;
char buffer[MAX_FILE_SIZE + 1];
if ((Br = read(fd, buffer, MAX_FILE_SIZE)) == -1){
return -2;
}
// the fd is now reusable
if (lseek(fd, 0, SEEK_SET) == -1){
return -1;
}
return Br;
}
/**
* Controlla se <path> è una directory
* @param path - path da controllare
* @return 0 - in caso non sia una directory
* @return R \ {0} - se è una directory
**/
int is_dir(const char *path)
{
struct stat tmp;
if ((stat(path, &tmp)) == -1) {
errExit("stat failed: ");
}
return S_ISDIR(tmp.st_mode);
}
/**
* Cambia la working directory con <path>, aggiorna la variabile d'ambiente "PWD" con il nuovo valore
* Assume che <path> sia regolare.
* @param path - nuova working directory
**/
int Chdir(const char *path)
{
int ret = 0;
if ((chdir(path)) != 0) {
perror("chdir failed: ");
ret = 1;
}
if ((setenv("PWD", path, 1)) == -1) {
perror("setenv failed: ");
ret = -1;
}
return ret;
}
/**
* Divide il file fd in PARTS parti
* @param parts - array di stringhe contenenti le PARTS parti finali
* @param fd - file descriptor del file da leggere
* @param tot_char - numero totale di caratteri del file fd
* @return 0 - in caso di successo
* @return R \ {0} - altrimenti
*/
int split_file(char parts[][1025], int fd, size_t tot_char)
{
size_t chunk = tot_char / PARTS;
ssize_t Br;
size_t reminder = tot_char % PARTS;
if (reminder != 0)
chunk++;
else
reminder = -1;
for (size_t i = 0; i < PARTS; i++) {
if(reminder > 0) {
reminder--;
}
else if(reminder == 0) {
chunk--;
//in modo tale che poi non venga più modificato il valore di chunk
reminder = -1;
}
if (chunk != 0) {
if ((Br = read(fd, parts[i], chunk)) == -1) {
return -1;
}
parts[i][Br] = '\0';
}
}
// the fd is now reusable
lseek(fd, 0, SEEK_SET);
return 0;
}
/**
* Controlla se all'interno della stringa è presente "_out"
* @param str - stringa in input
* @return 0 - in caso affermativo
* @return 1- altrimenti
*/
int ends_with(const char *str){
if (!str) {
return -1;
}
for (size_t i = 0; i < strlen(str); i++)
{
if(i < strlen(str) - 3 && str[i] == '_'){
if(str[i + 1] == 'o' && str[i + 2] == 'u' && str[i + 3] == 't'){
return 0;
}
}
}
return 1;
}
/**
* Ricerca ricorsivamente i file che iniziano con sendme_ e hanno # inferiore a 4K
* @param dirlist - struttura contenente i percorsi dei file
* @param start_path - percorso di partenza della ricerca
* @return 0 - in caso di successo
* @return 1 - in caso di errore di allocazione della memoria
* @return 2 - in caso la system call opendir fallisca
**/
int init_dirlist(dirlist_t *dirlist, const char *start_path)
{
char new_path[PATH_MAX];
struct dirent *de;
DIR *dp = opendir(start_path);
if (!dp) {
fprintf(stderr, "opendir: unable to open directory %s\n", start_path);
return 2;
}
while ((de = readdir(dp)) != NULL) {
if (strcmp(de->d_name, ".") == 0
|| strcmp(de->d_name, "..") == 0)
{ continue; }
if (de->d_type == DT_DIR) {
strcpy(new_path, start_path);
strcat(new_path, "/");
strcat(new_path, de->d_name);
strcat(new_path, "\0");
init_dirlist(dirlist, new_path);
}
else if (de->d_type == DT_REG) {
if (check_string("sendme_", de->d_name) == 0 &&
ends_with(de->d_name) == 1 &&
check_size(start_path) == 0 &&
dirlist->index < MAX_FILE) {
size_t to_alloc = strlen(start_path) + strlen(de->d_name) + 1 + 1 + 1;
dirlist->list[dirlist->index] = (char *) calloc(to_alloc , sizeof(char));
MCHECK(dirlist->list[dirlist->index]);
snprintf(dirlist->list[dirlist->index], to_alloc, "%s/%s", start_path, de->d_name);
dirlist->index++;
}
}
}
closedir(dp);
return 0;
}
/**
* Controlla se <child> ha finito di inviare le <PARTS> parti al server
* @param matrice - matrice N x PARTS che tiene traccia dei file inviati dal client
* @param child - identidicativo del processo figlio
* @return 1 - in caso <child> non abbia terminato l'invio delle parti del file
* @return 0 - in caso <child> abbia terminato l'invio delle parti del file
*/
int has_child_finished(int **matrice, size_t child)
{
for (size_t i = 0; i < PARTS; ++i) {
if (matrice[child][i] != 1) {
return 1;
}
}
return 0;
}
/**
* Appende la striga "_out" alla stringa passata come parametro
* @param string - stringa dove verrà appeso "_out"
* @return la stringa modificata
**/
char *append_out(const char *string)
{
//contiene il risultato
char result[PATH_MAX] = {0};
//array di supporto
char tmp[PATH_MAX] = {0};
//conta quanti caratteri ha letto per l'estensione
int occ = 0;
//copio la stringa passata in result
strncpy(result, string, strlen(string));
int i = strlen(result) - 1;
//caso in cui la stringa non contenga l'estensione
if (strrchr(result, '.') == NULL || string[strlen(string) - 1] == '.') {
return strdup(strcat(result, "_out"));
}
//altrimenti la stringa ha l'estensione
else {
//scorro finche non trovo il primo punto e salvo l'estensione in tmp
while (result[i] != '.') {
tmp[i] = result[i];
i--;
occ++;
}
int j = i;
//copio il punto e copio in tmp alla posizione giusta "_out"
tmp[i] = result[i];
result[i++] = '_';
result[i++] = 'o';
result[i++] = 'u';
result[i++] = 't';
// copio in result l'estensione dopo "_out"
for (int k = 0; k <= occ; k++) {
result[i++] = tmp[j++];
}
return strdup(result);
}
}