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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
54 * Obtain the filename from the file information structure.
56 * @param s structure to get the filename for
57 * @return "filename" field of the structure (can be NULL)
60 GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s)
67 * Set the filename in the file information structure.
68 * If filename was already set, frees it before setting the new one.
69 * Makes a copy of the argument.
71 * @param s structure to get the filename for
72 * @param filename filename to set
75 GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
78 GNUNET_free_non_null (s->filename);
80 s->filename = GNUNET_strdup (filename);
87 * Create an entry for a file in a publish-structure.
89 * @param h handle to the file sharing subsystem
90 * @param client_info initial value for the client-info value for this entry
91 * @param filename name of the file or directory to publish
92 * @param keywords under which keywords should this file be available
93 * directly; can be NULL
94 * @param meta metadata for the file
95 * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
96 * #GNUNET_SYSERR for simulation
97 * @param bo block options
98 * @return publish structure entry for the file
100 struct GNUNET_FS_FileInformation *
101 GNUNET_FS_file_information_create_from_file (
102 struct GNUNET_FS_Handle *h,
104 const char *filename,
105 const struct GNUNET_FS_Uri *keywords,
106 const struct GNUNET_CONTAINER_MetaData *meta,
108 const struct GNUNET_FS_BlockOptions *bo)
112 struct GNUNET_FS_FileInformation *ret;
116 /* FIXME: should include_symbolic_links be GNUNET_NO or GNUNET_YES here? */
118 GNUNET_DISK_file_size (filename, &fsize, GNUNET_NO, GNUNET_YES))
120 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
123 fi = GNUNET_FS_make_file_reader_context_ (filename);
130 GNUNET_FS_file_information_create_from_reader (h,
133 &GNUNET_FS_data_reader_file_,
142 ret->filename = GNUNET_strdup (filename);
144 while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
146 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
147 * this should be changed to EXTRACTOR_METAFORMAT_UTF8
149 GNUNET_CONTAINER_meta_data_insert (ret->meta,
151 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
152 EXTRACTOR_METAFORMAT_C_STRING,
161 * Create an entry for a file in a publish-structure.
163 * @param h handle to the file sharing subsystem
164 * @param client_info initial value for the client-info value for this entry
165 * @param length length of the file
166 * @param data data for the file (should not be used afterwards by
167 * the caller; callee will "free")
168 * @param keywords under which keywords should this file be available
169 * directly; can be NULL
170 * @param meta metadata for the file
171 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
172 * GNUNET_SYSERR for simulation
173 * @param bo block options
174 * @return publish structure entry for the file
176 struct GNUNET_FS_FileInformation *
177 GNUNET_FS_file_information_create_from_data (
178 struct GNUNET_FS_Handle *h,
182 const struct GNUNET_FS_Uri *keywords,
183 const struct GNUNET_CONTAINER_MetaData *meta,
185 const struct GNUNET_FS_BlockOptions *bo)
187 if (GNUNET_YES == do_index)
192 return GNUNET_FS_file_information_create_from_reader (h,
196 GNUNET_FS_data_reader_copy_,
206 * Create an entry for a file in a publish-structure.
208 * @param h handle to the file sharing subsystem
209 * @param client_info initial value for the client-info value for this entry
210 * @param length length of the file
211 * @param reader function that can be used to obtain the data for the file
212 * @param reader_cls closure for "reader"
213 * @param keywords under which keywords should this file be available
214 * directly; can be NULL
215 * @param meta metadata for the file
216 * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
217 * #GNUNET_SYSERR for simulation
218 * @param bo block options
219 * @return publish structure entry for the file
221 struct GNUNET_FS_FileInformation *
222 GNUNET_FS_file_information_create_from_reader (
223 struct GNUNET_FS_Handle *h,
226 GNUNET_FS_DataReader reader,
228 const struct GNUNET_FS_Uri *keywords,
229 const struct GNUNET_CONTAINER_MetaData *meta,
231 const struct GNUNET_FS_BlockOptions *bo)
233 struct GNUNET_FS_FileInformation *ret;
235 if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
240 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
242 ret->client_info = client_info;
243 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
244 if (ret->meta == NULL)
245 ret->meta = GNUNET_CONTAINER_meta_data_create ();
246 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
247 ret->data.file.reader = reader;
248 ret->data.file.reader_cls = reader_cls;
249 ret->data.file.do_index = do_index;
250 ret->data.file.file_size = length;
257 * Test if a given entry represents a directory.
259 * @param ent check if this FI represents a directory
260 * @return #GNUNET_YES if so, #GNUNET_NO if not
263 GNUNET_FS_file_information_is_directory (
264 const struct GNUNET_FS_FileInformation *ent)
266 return ent->is_directory;
271 * Create an entry for an empty directory in a publish-structure.
273 * @param h handle to the file sharing subsystem
274 * @param client_info initial value for the client-info value for this entry
275 * @param meta metadata for the directory
276 * @param keywords under which keywords should this directory be available
277 * directly; can be NULL
278 * @param bo block options
279 * @param filename name of the directory; can be NULL
280 * @return publish structure entry for the directory , NULL on error
282 struct GNUNET_FS_FileInformation *
283 GNUNET_FS_file_information_create_empty_directory (
284 struct GNUNET_FS_Handle *h,
286 const struct GNUNET_FS_Uri *keywords,
287 const struct GNUNET_CONTAINER_MetaData *meta,
288 const struct GNUNET_FS_BlockOptions *bo,
289 const char *filename)
291 struct GNUNET_FS_FileInformation *ret;
293 ret = GNUNET_new (struct GNUNET_FS_FileInformation);
295 ret->client_info = client_info;
296 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
297 ret->keywords = GNUNET_FS_uri_dup (keywords);
299 ret->is_directory = GNUNET_YES;
300 if (filename != NULL)
301 ret->filename = GNUNET_strdup (filename);
307 * Add an entry to a directory in a publish-structure. Clients
308 * should never modify publish structures that were passed to
309 * #GNUNET_FS_publish_start already.
311 * @param dir the directory
312 * @param ent the entry to add; the entry must not have been
313 * added to any other directory at this point and
314 * must not include @a dir in its structure
315 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
318 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
319 struct GNUNET_FS_FileInformation *ent)
321 if ((ent->dir != NULL) || (ent->next != NULL) ||
322 (dir->is_directory != GNUNET_YES))
325 return GNUNET_SYSERR;
328 ent->next = dir->data.dir.entries;
329 dir->data.dir.entries = ent;
330 dir->data.dir.dir_size = 0;
336 * Inspect a file or directory in a publish-structure. Clients
337 * should never modify publish structures that were passed to
338 * #GNUNET_FS_publish_start already. When called on a directory,
339 * this function will FIRST call @a proc with information about
340 * the directory itself and then for each of the files in the
341 * directory (but not for files in subdirectories). When called
342 * on a file, @a proc will be called exactly once (with information
343 * about the specific file).
345 * @param dir the directory
346 * @param proc function to call on each entry
347 * @param proc_cls closure for @a proc
350 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
351 GNUNET_FS_FileInformationProcessor proc,
354 struct GNUNET_FS_FileInformation *pos;
361 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size
362 : dir->data.file.file_size,
366 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
369 if (dir->is_directory != GNUNET_YES)
371 pos = dir->data.dir.entries;
378 (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size
379 : pos->data.file.file_size,
383 (pos->is_directory == GNUNET_YES) ? &no
384 : &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,
423 fi->data.dir.dir_size,
429 GNUNET_free_non_null (fi->data.dir.dir_data);
433 /* call clean-up function of the reader */
434 if (NULL != fi->data.file.reader)
436 (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
437 fi->data.file.reader = NULL;
439 /* clean up client-info */
441 cleaner (cleaner_cls,
443 fi->data.file.file_size,
447 &fi->data.file.do_index,
450 GNUNET_free_non_null (fi->filename);
451 GNUNET_free_non_null (fi->emsg);
452 if (NULL != fi->sks_uri)
453 GNUNET_FS_uri_destroy (fi->sks_uri);
454 if (NULL != fi->chk_uri)
455 GNUNET_FS_uri_destroy (fi->chk_uri);
456 /* clean up serialization */
457 if ((NULL != fi->serialization) && (0 != unlink (fi->serialization)))
458 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
461 if (NULL != fi->keywords)
462 GNUNET_FS_uri_destroy (fi->keywords);
463 if (NULL != fi->meta)
464 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
465 GNUNET_free_non_null (fi->serialization);
468 GNUNET_FS_tree_encoder_finish (fi->te, NULL);
475 /* end of fs_file_information.c */