2 This file is part of GNUnet.
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_file_information.c
23 * @brief Manage information for publishing directory hierarchies
24 * @author Christian Grothoff
27 #include <extractor.h>
28 #include "gnunet_fs_service.h"
34 * Obtain the name under which this file information
35 * structure is stored on disk. Only works for top-level
36 * file information structures.
38 * @param s structure to get the filename for
39 * @return NULL on error, otherwise filename that
40 * can be used to read this fi-struct from disk.
43 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
47 return s->serialization;
51 * Obtain the filename from the file information structure.
53 * @param s structure to get the filename for
54 * @return "filename" field of the structure (can be NULL)
57 GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s)
64 * Set the filename in the file information structure.
65 * If filename was already set, frees it before setting the new one.
66 * Makes a copy of the argument.
68 * @param s structure to get the filename for
69 * @param filename filename to set
72 GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
75 GNUNET_free_non_null (s->filename);
77 s->filename = GNUNET_strdup (filename);
84 * Create an entry for a file in a publish-structure.
86 * @param h handle to the file sharing subsystem
87 * @param client_info initial value for the client-info value for this entry
88 * @param filename name of the file or directory to publish
89 * @param keywords under which keywords should this file be available
90 * directly; can be NULL
91 * @param meta metadata for the file
92 * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
93 * #GNUNET_SYSERR for simulation
94 * @param bo block options
95 * @return publish structure entry for the file
97 struct GNUNET_FS_FileInformation *
98 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
100 const char *filename,
101 const struct GNUNET_FS_Uri
104 GNUNET_CONTAINER_MetaData *meta,
106 const struct GNUNET_FS_BlockOptions
111 struct GNUNET_FS_FileInformation *ret;
116 char fn_conv[MAX_PATH];
119 /* FIXME: should includeSymLinks be GNUNET_NO or GNUNET_YES here? */
120 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fsize, GNUNET_NO, GNUNET_YES))
122 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
125 fi = GNUNET_FS_make_file_reader_context_ (filename);
132 GNUNET_FS_file_information_create_from_reader (h, client_info,
134 &GNUNET_FS_data_reader_file_,
140 ret->filename = GNUNET_strdup (filename);
144 plibc_conv_to_win_path (filename, fn_conv);
147 while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
149 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
150 * this should be changed to EXTRACTOR_METAFORMAT_UTF8
153 GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
154 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
155 EXTRACTOR_METAFORMAT_C_STRING,
156 "text/plain", fn, strlen (fn) + 1);
158 GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
159 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
160 EXTRACTOR_METAFORMAT_UTF8,
161 "text/plain", fn, strlen (fn) + 1);
168 * Create an entry for a file in a publish-structure.
170 * @param h handle to the file sharing subsystem
171 * @param client_info initial value for the client-info value for this entry
172 * @param length length of the file
173 * @param data data for the file (should not be used afterwards by
174 * the caller; callee will "free")
175 * @param keywords under which keywords should this file be available
176 * directly; can be NULL
177 * @param meta metadata for the file
178 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
179 * GNUNET_SYSERR for simulation
180 * @param bo block options
181 * @return publish structure entry for the file
183 struct GNUNET_FS_FileInformation *
184 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
185 void *client_info, uint64_t length,
187 const struct GNUNET_FS_Uri
190 GNUNET_CONTAINER_MetaData *meta,
192 const struct GNUNET_FS_BlockOptions
195 if (GNUNET_YES == do_index)
200 return GNUNET_FS_file_information_create_from_reader (h, client_info, length,
201 &GNUNET_FS_data_reader_copy_,
202 data, keywords, meta,
208 * Create an entry for a file in a publish-structure.
210 * @param h handle to the file sharing subsystem
211 * @param client_info initial value for the client-info value for this entry
212 * @param length length of the file
213 * @param reader function that can be used to obtain the data for the file
214 * @param reader_cls closure for "reader"
215 * @param keywords under which keywords should this file be available
216 * directly; can be NULL
217 * @param meta metadata for the file
218 * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
219 * #GNUNET_SYSERR for simulation
220 * @param bo block options
221 * @return publish structure entry for the file
223 struct GNUNET_FS_FileInformation *
224 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
227 GNUNET_FS_DataReader reader,
229 const struct GNUNET_FS_Uri
232 GNUNET_CONTAINER_MetaData *meta,
235 GNUNET_FS_BlockOptions *bo)
237 struct GNUNET_FS_FileInformation *ret;
239 if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
244 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
246 ret->client_info = client_info;
247 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
248 if (ret->meta == NULL)
249 ret->meta = GNUNET_CONTAINER_meta_data_create ();
250 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
251 ret->data.file.reader = reader;
252 ret->data.file.reader_cls = reader_cls;
253 ret->data.file.do_index = do_index;
254 ret->data.file.file_size = length;
261 * Test if a given entry represents a directory.
263 * @param ent check if this FI represents a directory
264 * @return #GNUNET_YES if so, #GNUNET_NO if not
267 GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
270 return ent->is_directory;
275 * Create an entry for an empty directory in a publish-structure.
276 * This function should be used by applications for which the
277 * use of "GNUNET_FS_file_information_create_from_directory"
278 * is not appropriate.
280 * @param h handle to the file sharing subsystem
281 * @param client_info initial value for the client-info value for this entry
282 * @param meta metadata for the directory
283 * @param keywords under which keywords should this directory be available
284 * directly; can be NULL
285 * @param bo block options
286 * @param filename name of the directory; can be NULL
287 * @return publish structure entry for the directory , NULL on error
289 struct GNUNET_FS_FileInformation *
290 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
292 const struct GNUNET_FS_Uri
295 GNUNET_CONTAINER_MetaData
298 GNUNET_FS_BlockOptions *bo,
299 const char *filename)
301 struct GNUNET_FS_FileInformation *ret;
303 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
305 ret->client_info = client_info;
306 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
307 ret->keywords = GNUNET_FS_uri_dup (keywords);
309 ret->is_directory = GNUNET_YES;
310 if (filename != NULL)
311 ret->filename = GNUNET_strdup (filename);
317 * Add an entry to a directory in a publish-structure. Clients
318 * should never modify publish structures that were passed to
319 * #GNUNET_FS_publish_start already.
321 * @param dir the directory
322 * @param ent the entry to add; the entry must not have been
323 * added to any other directory at this point and
324 * must not include @a dir in its structure
325 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
328 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
329 struct GNUNET_FS_FileInformation *ent)
331 if ((ent->dir != NULL) || (ent->next != NULL) || (dir->is_directory != GNUNET_YES))
334 return GNUNET_SYSERR;
337 ent->next = dir->data.dir.entries;
338 dir->data.dir.entries = ent;
339 dir->data.dir.dir_size = 0;
345 * Inspect a file or directory in a publish-structure. Clients
346 * should never modify publish structures that were passed to
347 * #GNUNET_FS_publish_start already. When called on a directory,
348 * this function will FIRST call @a proc with information about
349 * the directory itself and then for each of the files in the
350 * directory (but not for files in subdirectories). When called
351 * on a file, @a proc will be called exactly once (with information
352 * about the specific file).
354 * @param dir the directory
355 * @param proc function to call on each entry
356 * @param proc_cls closure for @a proc
359 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
360 GNUNET_FS_FileInformationProcessor proc,
363 struct GNUNET_FS_FileInformation *pos;
369 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size : dir->data.
371 dir->meta, &dir->keywords, &dir->bo,
372 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
375 if (dir->is_directory != GNUNET_YES)
377 pos = dir->data.dir.entries;
383 (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size : pos->data.
384 file.file_size, pos->meta, &pos->keywords, &pos->bo,
385 (pos->is_directory == GNUNET_YES) ? &no : &pos->data.file.do_index,
394 * Destroy publish-structure. Clients should never destroy publish
395 * structures that were passed to #GNUNET_FS_publish_start already.
397 * @param fi structure to destroy
398 * @param cleaner function to call on each entry in the structure
399 * (useful to clean up client_info); can be NULL; return
401 * @param cleaner_cls closure for @a cleaner
404 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
405 GNUNET_FS_FileInformationProcessor cleaner,
408 struct GNUNET_FS_FileInformation *pos;
412 if (GNUNET_YES == fi->is_directory)
414 /* clean up directory */
415 while (NULL != (pos = fi->data.dir.entries))
417 fi->data.dir.entries = pos->next;
418 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
420 /* clean up client-info */
422 cleaner (cleaner_cls, fi, fi->data.dir.dir_size, fi->meta, &fi->keywords,
423 &fi->bo, &no, &fi->client_info);
424 GNUNET_free_non_null (fi->data.dir.dir_data);
428 /* call clean-up function of the reader */
429 if (NULL != fi->data.file.reader)
431 (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
432 fi->data.file.reader = NULL;
434 /* clean up client-info */
436 cleaner (cleaner_cls, fi, fi->data.file.file_size, fi->meta,
437 &fi->keywords, &fi->bo, &fi->data.file.do_index,
440 GNUNET_free_non_null (fi->filename);
441 GNUNET_free_non_null (fi->emsg);
442 GNUNET_free_non_null (fi->chk_uri);
443 /* clean up serialization */
444 if ((NULL != fi->serialization) && (0 != UNLINK (fi->serialization)))
445 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
447 if (NULL != fi->keywords)
448 GNUNET_FS_uri_destroy (fi->keywords);
449 if (NULL != fi->meta)
450 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
451 GNUNET_free_non_null (fi->serialization);
454 GNUNET_FS_tree_encoder_finish (fi->te, NULL);
461 /* end of fs_file_information.c */