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;
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(
101 struct GNUNET_FS_Handle *h,
103 const char *filename,
104 const struct GNUNET_FS_Uri *keywords,
105 const struct GNUNET_CONTAINER_MetaData *meta,
107 const struct GNUNET_FS_BlockOptions *bo)
111 struct GNUNET_FS_FileInformation *ret;
115 /* FIXME: should include_symbolic_links be GNUNET_NO or GNUNET_YES here? */
117 GNUNET_DISK_file_size(filename, &fsize, GNUNET_NO, GNUNET_YES))
119 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
122 fi = GNUNET_FS_make_file_reader_context_(filename);
129 GNUNET_FS_file_information_create_from_reader(h,
132 &GNUNET_FS_data_reader_file_,
141 ret->filename = GNUNET_strdup(filename);
143 while (NULL != (ss = strstr(fn, DIR_SEPARATOR_STR)))
145 /* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
146 * this should be changed to EXTRACTOR_METAFORMAT_UTF8
148 GNUNET_CONTAINER_meta_data_insert(ret->meta,
150 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
151 EXTRACTOR_METAFORMAT_C_STRING,
160 * Create an entry for a file in a publish-structure.
162 * @param h handle to the file sharing subsystem
163 * @param client_info initial value for the client-info value for this entry
164 * @param length length of the file
165 * @param data data for the file (should not be used afterwards by
166 * the caller; callee will "free")
167 * @param keywords under which keywords should this file be available
168 * directly; can be NULL
169 * @param meta metadata for the file
170 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
171 * GNUNET_SYSERR for simulation
172 * @param bo block options
173 * @return publish structure entry for the file
175 struct GNUNET_FS_FileInformation *
176 GNUNET_FS_file_information_create_from_data(
177 struct GNUNET_FS_Handle *h,
181 const struct GNUNET_FS_Uri *keywords,
182 const struct GNUNET_CONTAINER_MetaData *meta,
184 const struct GNUNET_FS_BlockOptions *bo)
186 if (GNUNET_YES == do_index)
191 return GNUNET_FS_file_information_create_from_reader(h,
194 &GNUNET_FS_data_reader_copy_,
204 * Create an entry for a file in a publish-structure.
206 * @param h handle to the file sharing subsystem
207 * @param client_info initial value for the client-info value for this entry
208 * @param length length of the file
209 * @param reader function that can be used to obtain the data for the file
210 * @param reader_cls closure for "reader"
211 * @param keywords under which keywords should this file be available
212 * directly; can be NULL
213 * @param meta metadata for the file
214 * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
215 * #GNUNET_SYSERR for simulation
216 * @param bo block options
217 * @return publish structure entry for the file
219 struct GNUNET_FS_FileInformation *
220 GNUNET_FS_file_information_create_from_reader(
221 struct GNUNET_FS_Handle *h,
224 GNUNET_FS_DataReader reader,
226 const struct GNUNET_FS_Uri *keywords,
227 const struct GNUNET_CONTAINER_MetaData *meta,
229 const struct GNUNET_FS_BlockOptions *bo)
231 struct GNUNET_FS_FileInformation *ret;
233 if ((GNUNET_YES == do_index) && (reader != &GNUNET_FS_data_reader_file_))
238 ret = GNUNET_new(struct GNUNET_FS_FileInformation);
240 ret->client_info = client_info;
241 ret->meta = GNUNET_CONTAINER_meta_data_duplicate(meta);
242 if (ret->meta == NULL)
243 ret->meta = GNUNET_CONTAINER_meta_data_create();
244 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup(keywords);
245 ret->data.file.reader = reader;
246 ret->data.file.reader_cls = reader_cls;
247 ret->data.file.do_index = do_index;
248 ret->data.file.file_size = length;
255 * Test if a given entry represents a directory.
257 * @param ent check if this FI represents a directory
258 * @return #GNUNET_YES if so, #GNUNET_NO if not
261 GNUNET_FS_file_information_is_directory(
262 const struct GNUNET_FS_FileInformation *ent)
264 return ent->is_directory;
269 * Create an entry for an empty directory in a publish-structure.
271 * @param h handle to the file sharing subsystem
272 * @param client_info initial value for the client-info value for this entry
273 * @param meta metadata for the directory
274 * @param keywords under which keywords should this directory be available
275 * directly; can be NULL
276 * @param bo block options
277 * @param filename name of the directory; can be NULL
278 * @return publish structure entry for the directory , NULL on error
280 struct GNUNET_FS_FileInformation *
281 GNUNET_FS_file_information_create_empty_directory(
282 struct GNUNET_FS_Handle *h,
284 const struct GNUNET_FS_Uri *keywords,
285 const struct GNUNET_CONTAINER_MetaData *meta,
286 const struct GNUNET_FS_BlockOptions *bo,
287 const char *filename)
289 struct GNUNET_FS_FileInformation *ret;
291 ret = GNUNET_new(struct GNUNET_FS_FileInformation);
293 ret->client_info = client_info;
294 ret->meta = GNUNET_CONTAINER_meta_data_duplicate(meta);
295 ret->keywords = GNUNET_FS_uri_dup(keywords);
297 ret->is_directory = GNUNET_YES;
298 if (filename != NULL)
299 ret->filename = GNUNET_strdup(filename);
305 * Add an entry to a directory in a publish-structure. Clients
306 * should never modify publish structures that were passed to
307 * #GNUNET_FS_publish_start already.
309 * @param dir the directory
310 * @param ent the entry to add; the entry must not have been
311 * added to any other directory at this point and
312 * must not include @a dir in its structure
313 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
316 GNUNET_FS_file_information_add(struct GNUNET_FS_FileInformation *dir,
317 struct GNUNET_FS_FileInformation *ent)
319 if ((ent->dir != NULL) || (ent->next != NULL) ||
320 (dir->is_directory != GNUNET_YES))
323 return GNUNET_SYSERR;
326 ent->next = dir->data.dir.entries;
327 dir->data.dir.entries = ent;
328 dir->data.dir.dir_size = 0;
334 * Inspect a file or directory in a publish-structure. Clients
335 * should never modify publish structures that were passed to
336 * #GNUNET_FS_publish_start already. When called on a directory,
337 * this function will FIRST call @a proc with information about
338 * the directory itself and then for each of the files in the
339 * directory (but not for files in subdirectories). When called
340 * on a file, @a proc will be called exactly once (with information
341 * about the specific file).
343 * @param dir the directory
344 * @param proc function to call on each entry
345 * @param proc_cls closure for @a proc
348 GNUNET_FS_file_information_inspect(struct GNUNET_FS_FileInformation *dir,
349 GNUNET_FS_FileInformationProcessor proc,
352 struct GNUNET_FS_FileInformation *pos;
359 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size
360 : dir->data.file.file_size,
364 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
367 if (dir->is_directory != GNUNET_YES)
369 pos = dir->data.dir.entries;
376 (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size
377 : pos->data.file.file_size,
381 (pos->is_directory == GNUNET_YES) ? &no
382 : &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 */
421 fi->data.dir.dir_size,
427 GNUNET_free_non_null(fi->data.dir.dir_data);
431 /* call clean-up function of the reader */
432 if (NULL != fi->data.file.reader)
434 (void)fi->data.file.reader(fi->data.file.reader_cls, 0, 0, NULL, NULL);
435 fi->data.file.reader = NULL;
437 /* clean up client-info */
441 fi->data.file.file_size,
445 &fi->data.file.do_index,
448 GNUNET_free_non_null(fi->filename);
449 GNUNET_free_non_null(fi->emsg);
450 if (NULL != fi->sks_uri)
451 GNUNET_FS_uri_destroy(fi->sks_uri);
452 if (NULL != fi->chk_uri)
453 GNUNET_FS_uri_destroy(fi->chk_uri);
454 /* clean up serialization */
455 if ((NULL != fi->serialization) && (0 != unlink(fi->serialization)))
456 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
459 if (NULL != fi->keywords)
460 GNUNET_FS_uri_destroy(fi->keywords);
461 if (NULL != fi->meta)
462 GNUNET_CONTAINER_meta_data_destroy(fi->meta);
463 GNUNET_free_non_null(fi->serialization);
466 GNUNET_FS_tree_encoder_finish(fi->te, NULL);
473 /* end of fs_file_information.c */