-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_handler.c
288 lines (242 loc) · 8.05 KB
/
files_handler.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
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
/**
* @file files_handler.c
* @brief Implementazione delle funzioni dichiarate in files_handler.h
* @author Emilio Panti 531844
* Si dichiara che il contenuto di questo file e' in ogni sua parte opera
* originale dell'autore
*/
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <error_handler.h>
#include <files_handler.h>
//mutex per operare nella directory dei file del server
pthread_mutex_t mtx_dirfile = PTHREAD_MUTEX_INITIALIZER;
/* ---------------------------- funzioni di utilita' -------------------------------- */
/**
* @function lock_dirfile
*
* @brief prende la lock della directory dei file
*
* @return 0 se successo, oppure un altro intero che rappresenta l'errore
*/
static int lock_dirfile(){
return pthread_mutex_lock(&mtx_dirfile);
}
/**
* @function unlock_dirfile
*
* @brief rilascia la lock della directory dei file
*
* @return 0 se successo, oppure un altro intero che rappresenta l'errore
*/
static int unlock_dirfile(){
return pthread_mutex_unlock(&mtx_dirfile);
}
/* -------------------------- interfaccia files_handler ------------------------------ */
/**
* @function get_filename
* @brief Restituisce solo il nome del file rispetto al suo path completo
*
* @param pathfile path del file
*
* @return puntatore al filename se successo, NULL ed errno settato se errore,
* NULL ed errno non settato se il file non è valido
*/
char* get_filename(char *pathfile){
//controllo gli argomenti
err_check_return(pathfile == NULL, EINVAL, "get_filename", NULL);
//rimuovo eventuali punti all'inizio del path
int stop = 0;
while (stop == 0){
if (*pathfile == '.') pathfile++;
else if (*pathfile == '\0') return NULL;
else stop = 1;
}
char *token = strtok(pathfile,"/");
while(token != NULL) {
pathfile = token;
token = strtok(NULL,"/");
}
//adesso pathfile contiene solo il file name
int len = strlen(pathfile) + 1;
char *file_name = malloc(sizeof(char)*len);
err_return_msg(file_name,NULL,NULL,"Errore: malloc\n");
strncpy(file_name, pathfile, len);
return file_name;
}
/**
* @function save_file
* @brief Salva il file passato da parametro nella directory indicata
*
* @param dir_file directory in cui salvare il file
* @param file_name nome del file da salvare
* @param buf puntatore al buffer dati da scrivere nel file
* @param len_buf lunghezza del buffer dati
*
* @return 0 in caso di successo, -1 in caso di errore
*/
int save_file(char *dir_file, char *file_name, char *buf, int len_buf){
//controllo gli argomenti
err_check_return(dir_file == NULL, EINVAL, "save_file", -1);
err_check_return(file_name == NULL, EINVAL, "save_file", -1);
err_check_return(buf == NULL, EINVAL, "save_file", -1);
err_check_return(len_buf < 0, EINVAL, "save_file", -1);
//creo il path per il nuovo file
int len = strlen(dir_file) + strlen(file_name) + 2;
char *new_file = malloc(sizeof(char)*len);
err_return_msg(new_file,NULL,-1,"Errore: malloc\n");
if (snprintf(new_file, len, "%s/%s", dir_file, file_name) < 0) {
fprintf(stderr, "Errore: snprintf\n");
free(new_file);
return -1;
}
int check = lock_dirfile();
if (check != 0 )free(new_file);
err_check_return(check != 0, check, "lock_dirfile", -1);
//creo (o sovrascrivo) il file
FILE *fp = fopen(new_file, "w");
if(fp == NULL) {
fprintf(stderr, "Errore: impossibile creare/aprire file %s\n", new_file);
free(new_file);
unlock_dirfile();
return -1;
}
else {
//scrivo nel file il buffer dati
if (fwrite(buf, len_buf, 1, fp) <= 0) {
fprintf(stderr, "Errore: impossibile scrivere sul file %s\n", new_file);
fclose(fp);
free(new_file);
unlock_dirfile();
return -1;
}
fflush(fp);
fclose(fp);
}
free(new_file);
check = unlock_dirfile();
err_check_return(check != 0, check, "unlock_dirfile", -1);
return 0;
}
/**
* @function get_mappedfile
* @brief Mappa il file in memoria nel puntatore passato da parametro
*
* @param mappedfile puntatore al file mappato
* @param dir_file directory in cui salvare il file
* @param file_name nome del file da mappare
*
* @return size del file in caso di successo (o se il file non esiste), -1 in caso di errore
*/
int get_mappedfile(char **mappedfile, char *dir_file, char *file_name){
//controllo gli argomenti
err_check_return(dir_file == NULL, EINVAL, "get_mappedfile", -1);
err_check_return(file_name == NULL, EINVAL, "get_mappedfile", -1);
err_check_return(mappedfile == NULL, EINVAL, "get_mappedfile", -1);
//creo il path del file da mappare
int len = strlen(dir_file) + strlen(file_name) + 2;
char *file_tomap = malloc(sizeof(char)*len);
err_return_msg(file_tomap,NULL,-1,"Errore: malloc\n");
if (snprintf(file_tomap, len, "%s/%s", dir_file, file_name) < 0) {
fprintf(stderr, "Errore: snprintf\n");
free(file_tomap);
return -1;
}
int check = lock_dirfile();
err_check_return(check != 0, check, "lock_dirfile", -1);
//controllo che esista il file
struct stat st;
if (stat(file_tomap, &st) == -1) {
check = unlock_dirfile();
if(check != 0) free(file_tomap);
err_check_return(check != 0, check, "unlock_dirfile", -1);
return 0;
}
int fd = open(file_tomap, O_RDONLY);
if (fd<0) {
fprintf(stderr, "ERRORE: aprendo il file %s\n", file_tomap);
close(fd);
free(file_tomap);
unlock_dirfile();
return -1;
}
//mappo il file in memoria
*mappedfile = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (*mappedfile == MAP_FAILED) {
fprintf(stderr, "ERRORE: mappando il file %s in memoria\n", file_tomap);
close(fd);
free(file_tomap);
unlock_dirfile();
return -1;
}
close(fd);
free(file_tomap);
check = unlock_dirfile();
err_check_return(check != 0, check, "unlock_dirfile", -1);
return st.st_size;
}
/**
* @function clean_dirfile
* @brief Rimuove tutti i file all'interno della directory passata da parametro
*
* @param path path della directory da ripulire
*
* @return 0 se successo, -1 in caso di fallimento (errno settato)
*/
int clean_dirfile(char *path) {
//controllo gli argomenti
err_check_return(path == NULL, EINVAL, "clean_dirfile", -1);
DIR *d = NULL;
int path_len = strlen(path);
struct dirent *file = NULL;
//apro la directory
d = opendir(path);
err_return_msg(d, NULL, -1, "Errore: opendir\n");
//scorro tutti i file nella directory
while ( (errno = 0, file = readdir(d)) != NULL) {
//non considero i file "." e ".."
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) {
//prendo il path del file
int len_path_file = path_len + strlen(file->d_name) + 2;
char *path_file = malloc(len_path_file);
if (path_file == NULL) {
closedir(d);
fprintf(stderr, "Errore: malloc\n");
return -1;
}
if (snprintf(path_file, len_path_file, "%s/%s", path, file->d_name) < 0) {
free(path_file);
closedir(d);
fprintf(stderr, "Errore: snprintf\n");
return -1;
}
//unlink del file
if (unlink(path_file) != 0) {
free(path_file);
closedir(d);
fprintf(stderr, "Errore: unlink file\n");
return -1;
}
free(path_file);
}
}
//se c'è stato un errore
if (errno != 0) {
closedir(d);
fprintf(stderr, "Errore: eliminazione file\n");
return -1;
}
//chiudo la directory
if (closedir(d) == -1) {
fprintf(stderr, "Errore: closedir\n");
return -1;
}
return 0;
}