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 passed to "GNUNET_FS_file_information_recover"
41 * to read this fi-struct from disk.
44 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
48 return s->serialization;
52 * Obtain the filename from the file information structure.
54 * @param s structure to get the filename for
55 * @return "filename" field of the structure (can be NULL)
58 GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s)
65 * Set the filename in the file information structure.
66 * If filename was already set, frees it before setting the new one.
67 * Makes a copy of the argument.
69 * @param s structure to get the filename for
70 * @param filename filename to set
73 GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
76 GNUNET_free_non_null (s->filename);
78 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 if (0 != STAT (filename, &sbuf))
121 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
124 fi = GNUNET_FS_make_file_reader_context_ (filename);
131 GNUNET_FS_file_information_create_from_reader (h, client_info,
133 &GNUNET_FS_data_reader_file_,
139 ret->filename = GNUNET_strdup (filename);
143 plibc_conv_to_win_path (filename, fn_conv);
146 while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
149 GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
150 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
151 EXTRACTOR_METAFORMAT_C_STRING,
152 "text/plain", fn, strlen (fn) + 1);
154 GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
155 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
156 EXTRACTOR_METAFORMAT_UTF8,
157 "text/plain", fn, strlen (fn) + 1);
164 * Create an entry for a file in a publish-structure.
166 * @param h handle to the file sharing subsystem
167 * @param client_info initial value for the client-info value for this entry
168 * @param length length of the file
169 * @param data data for the file (should not be used afterwards by
170 * the caller; callee will "free")
171 * @param keywords under which keywords should this file be available
172 * directly; can be NULL
173 * @param meta metadata for the file
174 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
175 * GNUNET_SYSERR for simulation
176 * @param bo block options
177 * @return publish structure entry for the file
179 struct GNUNET_FS_FileInformation *
180 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
181 void *client_info, uint64_t length,
183 const struct GNUNET_FS_Uri
186 GNUNET_CONTAINER_MetaData *meta,
188 const struct GNUNET_FS_BlockOptions
191 if (GNUNET_YES == do_index)
196 return GNUNET_FS_file_information_create_from_reader (h, client_info, length,
197 &GNUNET_FS_data_reader_copy_,
198 data, keywords, meta,
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 (struct GNUNET_FS_Handle *h,
223 GNUNET_FS_DataReader reader,
225 const struct GNUNET_FS_Uri
228 GNUNET_CONTAINER_MetaData *meta,
231 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_malloc (sizeof (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 (const struct GNUNET_FS_FileInformation
266 return ent->is_directory;
271 * Create an entry for an empty directory in a publish-structure.
272 * This function should be used by applications for which the
273 * use of "GNUNET_FS_file_information_create_from_directory"
274 * is not appropriate.
276 * @param h handle to the file sharing subsystem
277 * @param client_info initial value for the client-info value for this entry
278 * @param meta metadata for the directory
279 * @param keywords under which keywords should this directory be available
280 * directly; can be NULL
281 * @param bo block options
282 * @param filename name of the directory; can be NULL
283 * @return publish structure entry for the directory , NULL on error
285 struct GNUNET_FS_FileInformation *
286 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
288 const struct GNUNET_FS_Uri
291 GNUNET_CONTAINER_MetaData
294 GNUNET_FS_BlockOptions *bo,
295 const char *filename)
297 struct GNUNET_FS_FileInformation *ret;
299 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
301 ret->client_info = client_info;
302 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
303 ret->keywords = GNUNET_FS_uri_dup (keywords);
305 ret->is_directory = GNUNET_YES;
306 if (filename != NULL)
307 ret->filename = GNUNET_strdup (filename);
313 * Add an entry to a directory in a publish-structure. Clients
314 * should never modify publish structures that were passed to
315 * "GNUNET_FS_publish_start" already.
317 * @param dir the directory
318 * @param ent the entry to add; the entry must not have been
319 * added to any other directory at this point and
320 * must not include "dir" in its structure
321 * @return GNUNET_OK on success, GNUNET_SYSERR on error
324 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
325 struct GNUNET_FS_FileInformation *ent)
327 if ((ent->dir != NULL) || (ent->next != NULL) || (dir->is_directory != GNUNET_YES))
330 return GNUNET_SYSERR;
333 ent->next = dir->data.dir.entries;
334 dir->data.dir.entries = ent;
335 dir->data.dir.dir_size = 0;
341 * Inspect a file or directory in a publish-structure. Clients
342 * should never modify publish structures that were passed to
343 * "GNUNET_FS_publish_start" already. When called on a directory,
344 * this function will FIRST call "proc" with information about
345 * the directory itself and then for each of the files in the
346 * directory (but not for files in subdirectories). When called
347 * on a file, "proc" will be called exactly once (with information
348 * about the specific file).
350 * @param dir the directory
351 * @param proc function to call on each entry
352 * @param proc_cls closure for proc
355 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
356 GNUNET_FS_FileInformationProcessor proc,
359 struct GNUNET_FS_FileInformation *pos;
365 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size : dir->data.
366 file.file_size, dir->meta, &dir->keywords, &dir->bo,
367 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
370 if (dir->is_directory != GNUNET_YES)
372 pos = dir->data.dir.entries;
378 (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size : pos->data.
379 file.file_size, pos->meta, &pos->keywords, &pos->bo,
380 (pos->is_directory == GNUNET_YES) ? &no : &pos->data.file.do_index,
389 * Destroy publish-structure. Clients should never destroy publish
390 * structures that were passed to "GNUNET_FS_publish_start" already.
392 * @param fi structure to destroy
393 * @param cleaner function to call on each entry in the structure
394 * (useful to clean up client_info); can be NULL; return
396 * @param cleaner_cls closure for cleaner
399 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
400 GNUNET_FS_FileInformationProcessor cleaner,
403 struct GNUNET_FS_FileInformation *pos;
407 if (fi->is_directory == GNUNET_YES)
409 /* clean up directory */
410 while (NULL != (pos = fi->data.dir.entries))
412 fi->data.dir.entries = pos->next;
413 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
415 /* clean up client-info */
417 cleaner (cleaner_cls, fi, fi->data.dir.dir_size, fi->meta, &fi->keywords,
418 &fi->bo, &no, &fi->client_info);
419 GNUNET_free_non_null (fi->data.dir.dir_data);
423 /* call clean-up function of the reader */
424 if (fi->data.file.reader != NULL)
425 fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
426 /* clean up client-info */
428 cleaner (cleaner_cls, fi, fi->data.file.file_size, fi->meta,
429 &fi->keywords, &fi->bo, &fi->data.file.do_index,
432 GNUNET_free_non_null (fi->filename);
433 GNUNET_free_non_null (fi->emsg);
434 GNUNET_free_non_null (fi->chk_uri);
435 /* clean up serialization */
436 if ((NULL != fi->serialization) && (0 != UNLINK (fi->serialization)))
437 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
439 if (NULL != fi->keywords)
440 GNUNET_FS_uri_destroy (fi->keywords);
441 if (NULL != fi->meta)
442 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
443 GNUNET_free_non_null (fi->serialization);
446 GNUNET_FS_tree_encoder_finish (fi->te, NULL, NULL);
453 /* end of fs_file_information.c */