2 This file is part of GNUnet.
3 Copyright (C) 2009, 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/fs_file_information.c
23 * @brief Manage information for publishing directory hierarchies
24 * @author Christian Grothoff
28 #include <extractor.h>
30 #include "gnunet_fs_service.h"
36 * Obtain the name under which this file information
37 * structure is stored on disk. Only works for top-level
38 * file information structures.
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.
45 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
49 return s->serialization;
53 * Obtain the filename from the file information structure.
55 * @param s structure to get the filename for
56 * @return "filename" field of the structure (can be NULL)
59 GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s)
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.
70 * @param s structure to get the filename for
71 * @param filename filename to set
74 GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
77 GNUNET_free_non_null (s->filename);
79 s->filename = GNUNET_strdup (filename);
86 * Create an entry for a file in a publish-structure.
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
99 struct GNUNET_FS_FileInformation *
100 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
102 const char *filename,
103 const struct GNUNET_FS_Uri
106 GNUNET_CONTAINER_MetaData *meta,
108 const struct GNUNET_FS_BlockOptions
113 struct GNUNET_FS_FileInformation *ret;
118 char fn_conv[MAX_PATH];
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))
124 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
127 fi = GNUNET_FS_make_file_reader_context_ (filename);
134 GNUNET_FS_file_information_create_from_reader (h, client_info,
136 &GNUNET_FS_data_reader_file_,
142 ret->filename = GNUNET_strdup (filename);
146 plibc_conv_to_win_path (filename, fn_conv);
149 while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
151 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
152 * this should be changed to EXTRACTOR_METAFORMAT_UTF8
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);
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);
170 * Create an entry for a file in a publish-structure.
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
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,
189 const struct GNUNET_FS_Uri
192 GNUNET_CONTAINER_MetaData *meta,
194 const struct GNUNET_FS_BlockOptions
197 if (GNUNET_YES == do_index)
202 return GNUNET_FS_file_information_create_from_reader (h, client_info, length,
203 &GNUNET_FS_data_reader_copy_,
204 data, keywords, meta,
210 * Create an entry for a file in a publish-structure.
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
225 struct GNUNET_FS_FileInformation *
226 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
229 GNUNET_FS_DataReader reader,
231 const struct GNUNET_FS_Uri
234 GNUNET_CONTAINER_MetaData *meta,
237 GNUNET_FS_BlockOptions *bo)
239 struct GNUNET_FS_FileInformation *ret;
241 if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
246 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
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;
263 * Test if a given entry represents a directory.
265 * @param ent check if this FI represents a directory
266 * @return #GNUNET_YES if so, #GNUNET_NO if not
269 GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
272 return ent->is_directory;
277 * Create an entry for an empty directory in a publish-structure.
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
288 struct GNUNET_FS_FileInformation *
289 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
291 const struct GNUNET_FS_Uri
294 GNUNET_CONTAINER_MetaData
297 GNUNET_FS_BlockOptions *bo,
298 const char *filename)
300 struct GNUNET_FS_FileInformation *ret;
302 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
304 ret->client_info = client_info;
305 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
306 ret->keywords = GNUNET_FS_uri_dup (keywords);
308 ret->is_directory = GNUNET_YES;
309 if (filename != NULL)
310 ret->filename = GNUNET_strdup (filename);
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.
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
327 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
328 struct GNUNET_FS_FileInformation *ent)
330 if ((ent->dir != NULL) || (ent->next != NULL) || (dir->is_directory != GNUNET_YES))
333 return GNUNET_SYSERR;
336 ent->next = dir->data.dir.entries;
337 dir->data.dir.entries = ent;
338 dir->data.dir.dir_size = 0;
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).
353 * @param dir the directory
354 * @param proc function to call on each entry
355 * @param proc_cls closure for @a proc
358 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
359 GNUNET_FS_FileInformationProcessor proc,
362 struct GNUNET_FS_FileInformation *pos;
368 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size : dir->data.
370 dir->meta, &dir->keywords, &dir->bo,
371 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
374 if (dir->is_directory != GNUNET_YES)
376 pos = dir->data.dir.entries;
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,
393 * Destroy publish-structure. Clients should never destroy publish
394 * structures that were passed to #GNUNET_FS_publish_start already.
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
400 * @param cleaner_cls closure for @a cleaner
403 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
404 GNUNET_FS_FileInformationProcessor cleaner,
407 struct GNUNET_FS_FileInformation *pos;
411 if (GNUNET_YES == fi->is_directory)
413 /* clean up directory */
414 while (NULL != (pos = fi->data.dir.entries))
416 fi->data.dir.entries = pos->next;
417 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
419 /* clean up client-info */
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);
427 /* call clean-up function of the reader */
428 if (NULL != fi->data.file.reader)
430 (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
431 fi->data.file.reader = NULL;
433 /* clean up client-info */
435 cleaner (cleaner_cls, fi, fi->data.file.file_size, fi->meta,
436 &fi->keywords, &fi->bo, &fi->data.file.do_index,
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",
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);
456 GNUNET_FS_tree_encoder_finish (fi->te, NULL);
463 /* end of fs_file_information.c */