add validation for inner consensus element
[oweals/gnunet.git] / src / fs / fs_file_information.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/fs_file_information.c
23  * @brief  Manage information for publishing directory hierarchies
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #if HAVE_EXTRACTOR_H
28 #include <extractor.h>
29 #endif
30 #include "gnunet_fs_service.h"
31 #include "fs_api.h"
32 #include "fs_tree.h"
33
34
35 /**
36  * Obtain the name under which this file information
37  * structure is stored on disk.  Only works for top-level
38  * file information structures.
39  *
40  * @param s structure to get the filename for
41  * @return NULL on error, otherwise filename that
42  *         can be used to read this fi-struct from disk.
43  */
44 const char *
45 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
46 {
47   if (NULL != s->dir)
48     return NULL;
49   return s->serialization;
50 }
51
52 /**
53  * Obtain the filename from the file information structure.
54  *
55  * @param s structure to get the filename for
56  * @return "filename" field of the structure (can be NULL)
57  */
58 const char *
59 GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s)
60 {
61   return s->filename;
62 }
63
64
65 /**
66  * Set the filename in the file information structure.
67  * If filename was already set, frees it before setting the new one.
68  * Makes a copy of the argument.
69  *
70  * @param s structure to get the filename for
71  * @param filename filename to set
72  */
73 void
74 GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
75                                          const char *filename)
76 {
77   GNUNET_free_non_null (s->filename);
78   if (filename)
79     s->filename = GNUNET_strdup (filename);
80   else
81     s->filename = NULL;
82 }
83
84
85 /**
86  * Create an entry for a file in a publish-structure.
87  *
88  * @param h handle to the file sharing subsystem
89  * @param client_info initial value for the client-info value for this entry
90  * @param filename name of the file or directory to publish
91  * @param keywords under which keywords should this file be available
92  *         directly; can be NULL
93  * @param meta metadata for the file
94  * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
95  *                #GNUNET_SYSERR for simulation
96  * @param bo block options
97  * @return publish structure entry for the file
98  */
99 struct GNUNET_FS_FileInformation *
100 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
101                                              void *client_info,
102                                              const char *filename,
103                                              const struct GNUNET_FS_Uri
104                                              *keywords,
105                                              const struct
106                                              GNUNET_CONTAINER_MetaData *meta,
107                                              int do_index,
108                                              const struct GNUNET_FS_BlockOptions
109                                              *bo)
110 {
111   struct FileInfo *fi;
112   uint64_t fsize;
113   struct GNUNET_FS_FileInformation *ret;
114   const char *fn;
115   const char *ss;
116
117 #if WINDOWS
118   char fn_conv[MAX_PATH];
119 #endif
120
121   /* FIXME: should include_symbolic_links be GNUNET_NO or GNUNET_YES here? */
122   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fsize, GNUNET_NO, GNUNET_YES))
123   {
124     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
125     return NULL;
126   }
127   fi = GNUNET_FS_make_file_reader_context_ (filename);
128   if (NULL == fi)
129   {
130     GNUNET_break (0);
131     return NULL;
132   }
133   ret =
134       GNUNET_FS_file_information_create_from_reader (h, client_info,
135                                                      fsize,
136                                                      &GNUNET_FS_data_reader_file_,
137                                                      fi, keywords, meta,
138                                                      do_index, bo);
139   if (ret == NULL)
140     return NULL;
141   ret->h = h;
142   ret->filename = GNUNET_strdup (filename);
143 #if !WINDOWS
144   fn = filename;
145 #else
146   plibc_conv_to_win_path (filename, fn_conv);
147   fn = fn_conv;
148 #endif
149   while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
150     fn = ss + 1;
151 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
152  * this should be changed to EXTRACTOR_METAFORMAT_UTF8
153  */
154 #if !WINDOWS
155   GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
156                                      EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
157                                      EXTRACTOR_METAFORMAT_C_STRING,
158                                      "text/plain", fn, strlen (fn) + 1);
159 #else
160   GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
161                                      EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
162                                      EXTRACTOR_METAFORMAT_UTF8,
163                                      "text/plain", fn, strlen (fn) + 1);
164 #endif
165   return ret;
166 }
167
168
169 /**
170  * Create an entry for a file in a publish-structure.
171  *
172  * @param h handle to the file sharing subsystem
173  * @param client_info initial value for the client-info value for this entry
174  * @param length length of the file
175  * @param data data for the file (should not be used afterwards by
176  *        the caller; callee will "free")
177  * @param keywords under which keywords should this file be available
178  *         directly; can be NULL
179  * @param meta metadata for the file
180  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
181  *                GNUNET_SYSERR for simulation
182  * @param bo block options
183  * @return publish structure entry for the file
184  */
185 struct GNUNET_FS_FileInformation *
186 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
187                                              void *client_info, uint64_t length,
188                                              void *data,
189                                              const struct GNUNET_FS_Uri
190                                              *keywords,
191                                              const struct
192                                              GNUNET_CONTAINER_MetaData *meta,
193                                              int do_index,
194                                              const struct GNUNET_FS_BlockOptions
195                                              *bo)
196 {
197   if (GNUNET_YES == do_index)
198   {
199     GNUNET_break (0);
200     return NULL;
201   }
202   return GNUNET_FS_file_information_create_from_reader (h, client_info, length,
203                                                         &GNUNET_FS_data_reader_copy_,
204                                                         data, keywords, meta,
205                                                         do_index, bo);
206 }
207
208
209 /**
210  * Create an entry for a file in a publish-structure.
211  *
212  * @param h handle to the file sharing subsystem
213  * @param client_info initial value for the client-info value for this entry
214  * @param length length of the file
215  * @param reader function that can be used to obtain the data for the file
216  * @param reader_cls closure for "reader"
217  * @param keywords under which keywords should this file be available
218  *         directly; can be NULL
219  * @param meta metadata for the file
220  * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
221  *                #GNUNET_SYSERR for simulation
222  * @param bo block options
223  * @return publish structure entry for the file
224  */
225 struct GNUNET_FS_FileInformation *
226 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
227                                                void *client_info,
228                                                uint64_t length,
229                                                GNUNET_FS_DataReader reader,
230                                                void *reader_cls,
231                                                const struct GNUNET_FS_Uri
232                                                *keywords,
233                                                const struct
234                                                GNUNET_CONTAINER_MetaData *meta,
235                                                int do_index,
236                                                const struct
237                                                GNUNET_FS_BlockOptions *bo)
238 {
239   struct GNUNET_FS_FileInformation *ret;
240
241   if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
242   {
243     GNUNET_break (0);
244     return NULL;
245   }
246   ret = GNUNET_new (struct GNUNET_FS_FileInformation);
247   ret->h = h;
248   ret->client_info = client_info;
249   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
250   if (ret->meta == NULL)
251     ret->meta = GNUNET_CONTAINER_meta_data_create ();
252   ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
253   ret->data.file.reader = reader;
254   ret->data.file.reader_cls = reader_cls;
255   ret->data.file.do_index = do_index;
256   ret->data.file.file_size = length;
257   ret->bo = *bo;
258   return ret;
259 }
260
261
262 /**
263  * Test if a given entry represents a directory.
264  *
265  * @param ent check if this FI represents a directory
266  * @return #GNUNET_YES if so, #GNUNET_NO if not
267  */
268 int
269 GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
270                                          *ent)
271 {
272   return ent->is_directory;
273 }
274
275
276 /**
277  * Create an entry for an empty directory in a publish-structure.
278  *
279  * @param h handle to the file sharing subsystem
280  * @param client_info initial value for the client-info value for this entry
281  * @param meta metadata for the directory
282  * @param keywords under which keywords should this directory be available
283  *         directly; can be NULL
284  * @param bo block options
285  * @param filename name of the directory; can be NULL
286  * @return publish structure entry for the directory , NULL on error
287  */
288 struct GNUNET_FS_FileInformation *
289 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
290                                                    void *client_info,
291                                                    const struct GNUNET_FS_Uri
292                                                    *keywords,
293                                                    const struct
294                                                    GNUNET_CONTAINER_MetaData
295                                                    *meta,
296                                                    const struct
297                                                    GNUNET_FS_BlockOptions *bo,
298                                                    const char *filename)
299 {
300   struct GNUNET_FS_FileInformation *ret;
301
302   ret = GNUNET_new (struct GNUNET_FS_FileInformation);
303   ret->h = h;
304   ret->client_info = client_info;
305   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
306   ret->keywords = GNUNET_FS_uri_dup (keywords);
307   ret->bo = *bo;
308   ret->is_directory = GNUNET_YES;
309   if (filename != NULL)
310     ret->filename = GNUNET_strdup (filename);
311   return ret;
312 }
313
314
315 /**
316  * Add an entry to a directory in a publish-structure.  Clients
317  * should never modify publish structures that were passed to
318  * #GNUNET_FS_publish_start already.
319  *
320  * @param dir the directory
321  * @param ent the entry to add; the entry must not have been
322  *            added to any other directory at this point and
323  *            must not include @a dir in its structure
324  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
325  */
326 int
327 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
328                                 struct GNUNET_FS_FileInformation *ent)
329 {
330   if ((ent->dir != NULL) || (ent->next != NULL) || (dir->is_directory != GNUNET_YES))
331   {
332     GNUNET_break (0);
333     return GNUNET_SYSERR;
334   }
335   ent->dir = dir;
336   ent->next = dir->data.dir.entries;
337   dir->data.dir.entries = ent;
338   dir->data.dir.dir_size = 0;
339   return GNUNET_OK;
340 }
341
342
343 /**
344  * Inspect a file or directory in a publish-structure.  Clients
345  * should never modify publish structures that were passed to
346  * #GNUNET_FS_publish_start already.  When called on a directory,
347  * this function will FIRST call @a proc with information about
348  * the directory itself and then for each of the files in the
349  * directory (but not for files in subdirectories).  When called
350  * on a file, @a proc will be called exactly once (with information
351  * about the specific file).
352  *
353  * @param dir the directory
354  * @param proc function to call on each entry
355  * @param proc_cls closure for @a proc
356  */
357 void
358 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
359                                     GNUNET_FS_FileInformationProcessor proc,
360                                     void *proc_cls)
361 {
362   struct GNUNET_FS_FileInformation *pos;
363   int no;
364
365   no = GNUNET_NO;
366   if (GNUNET_OK !=
367       proc (proc_cls, dir,
368             (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size : dir->data.
369             file.file_size,
370             dir->meta, &dir->keywords, &dir->bo,
371             (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
372             &dir->client_info))
373     return;
374   if (dir->is_directory != GNUNET_YES)
375     return;
376   pos = dir->data.dir.entries;
377   while (pos != NULL)
378   {
379     no = GNUNET_NO;
380     if (GNUNET_OK !=
381         proc (proc_cls, pos,
382               (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size : pos->data.
383               file.file_size, pos->meta, &pos->keywords, &pos->bo,
384               (pos->is_directory == GNUNET_YES) ? &no : &pos->data.file.do_index,
385               &pos->client_info))
386       break;
387     pos = pos->next;
388   }
389 }
390
391
392 /**
393  * Destroy publish-structure.  Clients should never destroy publish
394  * structures that were passed to #GNUNET_FS_publish_start already.
395  *
396  * @param fi structure to destroy
397  * @param cleaner function to call on each entry in the structure
398  *        (useful to clean up client_info); can be NULL; return
399  *        values are ignored
400  * @param cleaner_cls closure for @a cleaner
401  */
402 void
403 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
404                                     GNUNET_FS_FileInformationProcessor cleaner,
405                                     void *cleaner_cls)
406 {
407   struct GNUNET_FS_FileInformation *pos;
408   int no;
409
410   no = GNUNET_NO;
411   if (GNUNET_YES == fi->is_directory)
412   {
413     /* clean up directory */
414     while (NULL != (pos = fi->data.dir.entries))
415     {
416       fi->data.dir.entries = pos->next;
417       GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
418     }
419     /* clean up client-info */
420     if (NULL != cleaner)
421       cleaner (cleaner_cls, fi, fi->data.dir.dir_size, fi->meta, &fi->keywords,
422                &fi->bo, &no, &fi->client_info);
423     GNUNET_free_non_null (fi->data.dir.dir_data);
424   }
425   else
426   {
427     /* call clean-up function of the reader */
428     if (NULL != fi->data.file.reader)
429     {
430       (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
431       fi->data.file.reader = NULL;
432     }
433     /* clean up client-info */
434     if (NULL != cleaner)
435       cleaner (cleaner_cls, fi, fi->data.file.file_size, fi->meta,
436                &fi->keywords, &fi->bo, &fi->data.file.do_index,
437                &fi->client_info);
438   }
439   GNUNET_free_non_null (fi->filename);
440   GNUNET_free_non_null (fi->emsg);
441   if (NULL != fi->sks_uri)
442       GNUNET_FS_uri_destroy (fi->sks_uri);
443   if (NULL != fi->chk_uri)
444       GNUNET_FS_uri_destroy (fi->chk_uri);
445   /* clean up serialization */
446   if ((NULL != fi->serialization) && (0 != UNLINK (fi->serialization)))
447     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
448                               fi->serialization);
449   if (NULL != fi->keywords)
450     GNUNET_FS_uri_destroy (fi->keywords);
451   if (NULL != fi->meta)
452     GNUNET_CONTAINER_meta_data_destroy (fi->meta);
453   GNUNET_free_non_null (fi->serialization);
454   if (NULL != fi->te)
455   {
456     GNUNET_FS_tree_encoder_finish (fi->te, NULL);
457     fi->te = NULL;
458   }
459   GNUNET_free (fi);
460 }
461
462
463 /* end of fs_file_information.c */