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 include_symbolic_links 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.
277 * @param h handle to the file sharing subsystem
278 * @param client_info initial value for the client-info value for this entry
279 * @param meta metadata for the directory
280 * @param keywords under which keywords should this directory be available
281 * directly; can be NULL
282 * @param bo block options
283 * @param filename name of the directory; can be NULL
284 * @return publish structure entry for the directory , NULL on error
286 struct GNUNET_FS_FileInformation *
287 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
289 const struct GNUNET_FS_Uri
292 GNUNET_CONTAINER_MetaData
295 GNUNET_FS_BlockOptions *bo,
296 const char *filename)
298 struct GNUNET_FS_FileInformation *ret;
300 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
302 ret->client_info = client_info;
303 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
304 ret->keywords = GNUNET_FS_uri_dup (keywords);
306 ret->is_directory = GNUNET_YES;
307 if (filename != NULL)
308 ret->filename = GNUNET_strdup (filename);
314 * Add an entry to a directory in a publish-structure. Clients
315 * should never modify publish structures that were passed to
316 * #GNUNET_FS_publish_start already.
318 * @param dir the directory
319 * @param ent the entry to add; the entry must not have been
320 * added to any other directory at this point and
321 * must not include @a dir in its structure
322 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
325 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
326 struct GNUNET_FS_FileInformation *ent)
328 if ((ent->dir != NULL) || (ent->next != NULL) || (dir->is_directory != GNUNET_YES))
331 return GNUNET_SYSERR;
334 ent->next = dir->data.dir.entries;
335 dir->data.dir.entries = ent;
336 dir->data.dir.dir_size = 0;
342 * Inspect a file or directory in a publish-structure. Clients
343 * should never modify publish structures that were passed to
344 * #GNUNET_FS_publish_start already. When called on a directory,
345 * this function will FIRST call @a proc with information about
346 * the directory itself and then for each of the files in the
347 * directory (but not for files in subdirectories). When called
348 * on a file, @a proc will be called exactly once (with information
349 * about the specific file).
351 * @param dir the directory
352 * @param proc function to call on each entry
353 * @param proc_cls closure for @a proc
356 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
357 GNUNET_FS_FileInformationProcessor proc,
360 struct GNUNET_FS_FileInformation *pos;
366 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size : dir->data.
368 dir->meta, &dir->keywords, &dir->bo,
369 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
372 if (dir->is_directory != GNUNET_YES)
374 pos = dir->data.dir.entries;
380 (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size : pos->data.
381 file.file_size, pos->meta, &pos->keywords, &pos->bo,
382 (pos->is_directory == GNUNET_YES) ? &no : &pos->data.file.do_index,
391 * Destroy publish-structure. Clients should never destroy publish
392 * structures that were passed to #GNUNET_FS_publish_start already.
394 * @param fi structure to destroy
395 * @param cleaner function to call on each entry in the structure
396 * (useful to clean up client_info); can be NULL; return
398 * @param cleaner_cls closure for @a cleaner
401 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
402 GNUNET_FS_FileInformationProcessor cleaner,
405 struct GNUNET_FS_FileInformation *pos;
409 if (GNUNET_YES == fi->is_directory)
411 /* clean up directory */
412 while (NULL != (pos = fi->data.dir.entries))
414 fi->data.dir.entries = pos->next;
415 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
417 /* clean up client-info */
419 cleaner (cleaner_cls, fi, fi->data.dir.dir_size, fi->meta, &fi->keywords,
420 &fi->bo, &no, &fi->client_info);
421 GNUNET_free_non_null (fi->data.dir.dir_data);
425 /* call clean-up function of the reader */
426 if (NULL != fi->data.file.reader)
428 (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
429 fi->data.file.reader = NULL;
431 /* clean up client-info */
433 cleaner (cleaner_cls, fi, fi->data.file.file_size, fi->meta,
434 &fi->keywords, &fi->bo, &fi->data.file.do_index,
437 GNUNET_free_non_null (fi->filename);
438 GNUNET_free_non_null (fi->emsg);
439 GNUNET_free_non_null (fi->chk_uri);
440 /* clean up serialization */
441 if ((NULL != fi->serialization) && (0 != UNLINK (fi->serialization)))
442 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
444 if (NULL != fi->keywords)
445 GNUNET_FS_uri_destroy (fi->keywords);
446 if (NULL != fi->meta)
447 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
448 GNUNET_free_non_null (fi->serialization);
451 GNUNET_FS_tree_encoder_finish (fi->te, NULL);
458 /* end of fs_file_information.c */