fs_file_information.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/fs_file_information.c
  19. * @brief Manage information for publishing directory hierarchies
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - metadata filename clean up code
  24. * - metadata/ksk generation for directories from contained files
  25. */
  26. #include "platform.h"
  27. #include <extractor.h>
  28. #include "gnunet_fs_service.h"
  29. #include "fs.h"
  30. #include "fs_tree.h"
  31. /**
  32. * Add meta data that libextractor finds to our meta data
  33. * container.
  34. *
  35. * @param cls closure, our meta data container
  36. * @param plugin_name name of the plugin that produced this value;
  37. * special values can be used (i.e. '&lt;zlib&gt;' for zlib being
  38. * used in the main libextractor library and yielding
  39. * meta data).
  40. * @param type libextractor-type describing the meta data
  41. * @param format basic format information about data
  42. * @param data_mime_type mime-type of data (not of the original file);
  43. * can be NULL (if mime-type is not known)
  44. * @param data actual meta-data found
  45. * @param data_len number of bytes in data
  46. * @return always 0 to continue extracting
  47. */
  48. static int
  49. add_to_md(void *cls,
  50. const char *plugin_name,
  51. enum EXTRACTOR_MetaType type,
  52. enum EXTRACTOR_MetaFormat format,
  53. const char *data_mime_type,
  54. const char *data,
  55. size_t data_len)
  56. {
  57. struct GNUNET_CONTAINER_MetaData *md = cls;
  58. (void) GNUNET_CONTAINER_meta_data_insert (md,
  59. plugin_name,
  60. type,
  61. format,
  62. data_mime_type,
  63. data,
  64. data_len);
  65. return 0;
  66. }
  67. /**
  68. * Extract meta-data from a file.
  69. *
  70. * @return GNUNET_SYSERR on error, otherwise the number
  71. * of meta-data items obtained
  72. */
  73. int
  74. GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
  75. *md, const char *filename,
  76. struct EXTRACTOR_PluginList *
  77. extractors)
  78. {
  79. int old;
  80. if (filename == NULL)
  81. return GNUNET_SYSERR;
  82. if (extractors == NULL)
  83. return 0;
  84. old = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
  85. GNUNET_assert (old >= 0);
  86. EXTRACTOR_extract (extractors,
  87. filename,
  88. NULL, 0,
  89. &add_to_md,
  90. md);
  91. return (GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL) - old);
  92. }
  93. /**
  94. * Obtain the name under which this file information
  95. * structure is stored on disk. Only works for top-level
  96. * file information structures.
  97. *
  98. * @param s structure to get the filename for
  99. * @return NULL on error, otherwise filename that
  100. * can be passed to "GNUNET_FS_file_information_recover"
  101. * to read this fi-struct from disk.
  102. */
  103. const char *
  104. GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
  105. {
  106. if (NULL != s->dir)
  107. return NULL;
  108. return s->serialization;
  109. }
  110. /**
  111. * Create an entry for a file in a publish-structure.
  112. *
  113. * @param h handle to the file sharing subsystem
  114. * @param client_info initial value for the client-info value for this entry
  115. * @param filename name of the file or directory to publish
  116. * @param keywords under which keywords should this file be available
  117. * directly; can be NULL
  118. * @param meta metadata for the file
  119. * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  120. * GNUNET_SYSERR for simulation
  121. * @param bo block options
  122. * @return publish structure entry for the file
  123. */
  124. struct GNUNET_FS_FileInformation *
  125. GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
  126. void *client_info,
  127. const char *filename,
  128. const struct GNUNET_FS_Uri *keywords,
  129. const struct GNUNET_CONTAINER_MetaData *meta,
  130. int do_index,
  131. const struct GNUNET_FS_BlockOptions *bo)
  132. {
  133. struct FileInfo *fi;
  134. struct stat sbuf;
  135. struct GNUNET_FS_FileInformation *ret;
  136. const char *fn;
  137. const char *ss;
  138. if (0 != STAT (filename, &sbuf))
  139. {
  140. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  141. "stat",
  142. filename);
  143. return NULL;
  144. }
  145. fi = GNUNET_FS_make_file_reader_context_ (filename);
  146. if (fi == NULL)
  147. {
  148. GNUNET_break (0);
  149. return NULL;
  150. }
  151. ret = GNUNET_FS_file_information_create_from_reader (h,
  152. client_info,
  153. sbuf.st_size,
  154. &GNUNET_FS_data_reader_file_,
  155. fi,
  156. keywords,
  157. meta,
  158. do_index,
  159. bo);
  160. if (ret == NULL)
  161. return NULL;
  162. ret->h = h;
  163. ret->filename = GNUNET_strdup (filename);
  164. fn = filename;
  165. while (NULL != (ss = strstr (fn,
  166. DIR_SEPARATOR_STR)))
  167. fn = ss + 1;
  168. GNUNET_CONTAINER_meta_data_insert (ret->meta,
  169. "<gnunet>",
  170. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
  171. EXTRACTOR_METAFORMAT_C_STRING,
  172. "text/plain",
  173. fn,
  174. strlen (fn) + 1);
  175. return ret;
  176. }
  177. /**
  178. * Create an entry for a file in a publish-structure.
  179. *
  180. * @param h handle to the file sharing subsystem
  181. * @param client_info initial value for the client-info value for this entry
  182. * @param length length of the file
  183. * @param data data for the file (should not be used afterwards by
  184. * the caller; callee will "free")
  185. * @param keywords under which keywords should this file be available
  186. * directly; can be NULL
  187. * @param meta metadata for the file
  188. * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  189. * GNUNET_SYSERR for simulation
  190. * @param bo block options
  191. * @return publish structure entry for the file
  192. */
  193. struct GNUNET_FS_FileInformation *
  194. GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
  195. void *client_info,
  196. uint64_t length,
  197. void *data,
  198. const struct GNUNET_FS_Uri *keywords,
  199. const struct GNUNET_CONTAINER_MetaData *meta,
  200. int do_index,
  201. const struct GNUNET_FS_BlockOptions *bo)
  202. {
  203. if (GNUNET_YES == do_index)
  204. {
  205. GNUNET_break (0);
  206. return NULL;
  207. }
  208. return GNUNET_FS_file_information_create_from_reader (h,
  209. client_info,
  210. length,
  211. &GNUNET_FS_data_reader_copy_,
  212. data,
  213. keywords,
  214. meta,
  215. do_index,
  216. bo);
  217. }
  218. /**
  219. * Create an entry for a file in a publish-structure.
  220. *
  221. * @param h handle to the file sharing subsystem
  222. * @param client_info initial value for the client-info value for this entry
  223. * @param length length of the file
  224. * @param reader function that can be used to obtain the data for the file
  225. * @param reader_cls closure for "reader"
  226. * @param keywords under which keywords should this file be available
  227. * directly; can be NULL
  228. * @param meta metadata for the file
  229. * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  230. * GNUNET_SYSERR for simulation
  231. * @param bo block options
  232. * @return publish structure entry for the file
  233. */
  234. struct GNUNET_FS_FileInformation *
  235. GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
  236. void *client_info,
  237. uint64_t length,
  238. GNUNET_FS_DataReader reader,
  239. void *reader_cls,
  240. const struct GNUNET_FS_Uri *keywords,
  241. const struct GNUNET_CONTAINER_MetaData *meta,
  242. int do_index,
  243. const struct GNUNET_FS_BlockOptions *bo)
  244. {
  245. struct GNUNET_FS_FileInformation *ret;
  246. if ( (GNUNET_YES == do_index) &&
  247. (reader != &GNUNET_FS_data_reader_file_) )
  248. {
  249. GNUNET_break (0);
  250. return NULL;
  251. }
  252. ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
  253. ret->h = h;
  254. ret->client_info = client_info;
  255. ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
  256. if (ret->meta == NULL)
  257. ret->meta = GNUNET_CONTAINER_meta_data_create ();
  258. ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
  259. ret->data.file.reader = reader;
  260. ret->data.file.reader_cls = reader_cls;
  261. ret->data.file.do_index = do_index;
  262. ret->data.file.file_size = length;
  263. ret->bo = *bo;
  264. return ret;
  265. }
  266. /**
  267. * Closure for "dir_scan_cb".
  268. */
  269. struct DirScanCls
  270. {
  271. /**
  272. * Metadata extractors to use.
  273. */
  274. struct EXTRACTOR_PluginList *extractors;
  275. /**
  276. * Master context.
  277. */
  278. struct GNUNET_FS_Handle *h;
  279. /**
  280. * Function to call on each directory entry.
  281. */
  282. GNUNET_FS_FileProcessor proc;
  283. /**
  284. * Closure for proc.
  285. */
  286. void *proc_cls;
  287. /**
  288. * Scanner to use for subdirectories.
  289. */
  290. GNUNET_FS_DirectoryScanner scanner;
  291. /**
  292. * Closure for scanner.
  293. */
  294. void *scanner_cls;
  295. /**
  296. * Set to an error message (if any).
  297. */
  298. char *emsg;
  299. /**
  300. * Block options.
  301. */
  302. const struct GNUNET_FS_BlockOptions *bo;
  303. /**
  304. * Should files be indexed?
  305. */
  306. int do_index;
  307. };
  308. /**
  309. * Function called on each entry in a file to
  310. * cause default-publishing.
  311. * @param cls closure (struct DirScanCls)
  312. * @param filename name of the file to be published
  313. * @return GNUNET_OK on success, GNUNET_SYSERR to abort
  314. */
  315. static int
  316. dir_scan_cb (void *cls,
  317. const char *filename)
  318. {
  319. struct DirScanCls *dsc = cls;
  320. struct stat sbuf;
  321. struct GNUNET_FS_FileInformation *fi;
  322. struct GNUNET_FS_Uri *ksk_uri;
  323. struct GNUNET_FS_Uri *keywords;
  324. struct GNUNET_CONTAINER_MetaData *meta;
  325. if (0 != STAT (filename, &sbuf))
  326. {
  327. GNUNET_asprintf (&dsc->emsg,
  328. _("`%s' failed on file `%s': %s"),
  329. "stat",
  330. filename,
  331. STRERROR (errno));
  332. return GNUNET_SYSERR;
  333. }
  334. if (S_ISDIR (sbuf.st_mode))
  335. {
  336. fi = GNUNET_FS_file_information_create_from_directory (dsc->h,
  337. NULL,
  338. filename,
  339. dsc->scanner,
  340. dsc->scanner_cls,
  341. dsc->do_index,
  342. dsc->bo,
  343. &dsc->emsg);
  344. if (NULL == fi)
  345. {
  346. GNUNET_assert (NULL != dsc->emsg);
  347. return GNUNET_SYSERR;
  348. }
  349. }
  350. else
  351. {
  352. meta = GNUNET_CONTAINER_meta_data_create ();
  353. GNUNET_FS_meta_data_extract_from_file (meta,
  354. filename,
  355. dsc->extractors);
  356. keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
  357. ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
  358. fi = GNUNET_FS_file_information_create_from_file (dsc->h,
  359. NULL,
  360. filename,
  361. ksk_uri,
  362. meta,
  363. dsc->do_index,
  364. dsc->bo);
  365. GNUNET_CONTAINER_meta_data_destroy (meta);
  366. GNUNET_FS_uri_destroy (keywords);
  367. GNUNET_FS_uri_destroy (ksk_uri);
  368. }
  369. dsc->proc (dsc->proc_cls,
  370. filename,
  371. fi);
  372. return GNUNET_OK;
  373. }
  374. /**
  375. * Simple, useful default implementation of a directory scanner
  376. * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
  377. * UNIX filename, will publish all files in the directory except hidden
  378. * files (those starting with a "."). Metadata will be extracted
  379. * using GNU libextractor; the specific list of plugins should be
  380. * specified in "cls", passing NULL will disable (!) metadata
  381. * extraction. Keywords will be derived from the metadata and be
  382. * subject to default canonicalization. This is strictly a
  383. * convenience function.
  384. *
  385. * @param cls must be of type "struct EXTRACTOR_Extractor*"
  386. * @param h handle to the file sharing subsystem
  387. * @param dirname name of the directory to scan
  388. * @param do_index should files be indexed or inserted
  389. * @param bo block options
  390. * @param proc function called on each entry
  391. * @param proc_cls closure for proc
  392. * @param emsg where to store an error message (on errors)
  393. * @return GNUNET_OK on success
  394. */
  395. int
  396. GNUNET_FS_directory_scanner_default (void *cls,
  397. struct GNUNET_FS_Handle *h,
  398. const char *dirname,
  399. int do_index,
  400. const struct GNUNET_FS_BlockOptions *bo,
  401. GNUNET_FS_FileProcessor proc,
  402. void *proc_cls,
  403. char **emsg)
  404. {
  405. struct EXTRACTOR_PluginList *ex = cls;
  406. struct DirScanCls dsc;
  407. dsc.h = h;
  408. dsc.extractors = ex;
  409. dsc.proc = proc;
  410. dsc.proc_cls = proc_cls;
  411. dsc.scanner = &GNUNET_FS_directory_scanner_default;
  412. dsc.scanner_cls = cls;
  413. dsc.do_index = do_index;
  414. dsc.bo = bo;
  415. if (-1 == GNUNET_DISK_directory_scan (dirname,
  416. &dir_scan_cb,
  417. &dsc))
  418. {
  419. GNUNET_assert (NULL != dsc.emsg);
  420. *emsg = dsc.emsg;
  421. return GNUNET_SYSERR;
  422. }
  423. return GNUNET_OK;
  424. }
  425. /**
  426. * Closure for dirproc function.
  427. */
  428. struct EntryProcCls
  429. {
  430. /**
  431. * Linked list of directory entries that is being
  432. * created.
  433. */
  434. struct GNUNET_FS_FileInformation *entries;
  435. };
  436. /**
  437. * Function that processes a directory entry that
  438. * was obtained from the scanner.
  439. * @param cls our closure
  440. * @param filename name of the file (unused, why there???)
  441. * @param fi information for publishing the file
  442. */
  443. static void
  444. dirproc (void *cls,
  445. const char *filename,
  446. struct GNUNET_FS_FileInformation *fi)
  447. {
  448. struct EntryProcCls *dc = cls;
  449. GNUNET_assert (fi->next == NULL);
  450. GNUNET_assert (fi->dir == NULL);
  451. fi->next = dc->entries;
  452. dc->entries = fi;
  453. }
  454. /**
  455. * Create a publish-structure from an existing file hierarchy, inferring
  456. * and organizing keywords and metadata as much as possible. This
  457. * function primarily performs the recursive build and re-organizes
  458. * keywords and metadata; for automatically getting metadata
  459. * extraction, scanning of directories and creation of the respective
  460. * GNUNET_FS_FileInformation entries the default scanner should be
  461. * passed (GNUNET_FS_directory_scanner_default). This is strictly a
  462. * convenience function.
  463. *
  464. * @param h handle to the file sharing subsystem
  465. * @param client_info initial value for the client-info value for this entry
  466. * @param filename name of the top-level file or directory
  467. * @param scanner function used to get a list of files in a directory
  468. * @param scanner_cls closure for scanner
  469. * @param do_index should files in the hierarchy be indexed?
  470. * @param bo block options
  471. * @param emsg where to store an error message
  472. * @return publish structure entry for the directory, NULL on error
  473. */
  474. struct GNUNET_FS_FileInformation *
  475. GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
  476. void *client_info,
  477. const char *filename,
  478. GNUNET_FS_DirectoryScanner scanner,
  479. void *scanner_cls,
  480. int do_index,
  481. const struct GNUNET_FS_BlockOptions *bo,
  482. char **emsg)
  483. {
  484. struct GNUNET_FS_FileInformation *ret;
  485. struct EntryProcCls dc;
  486. struct GNUNET_FS_Uri *ksk;
  487. struct GNUNET_CONTAINER_MetaData *meta;
  488. const char *fn;
  489. const char *ss;
  490. char *dn;
  491. dc.entries = NULL;
  492. meta = GNUNET_CONTAINER_meta_data_create ();
  493. GNUNET_FS_meta_data_make_directory (meta);
  494. scanner (scanner_cls,
  495. h,
  496. filename,
  497. do_index,
  498. bo,
  499. &dirproc,
  500. &dc,
  501. emsg);
  502. ksk = NULL; // FIXME...
  503. // FIXME: create meta!
  504. ret = GNUNET_FS_file_information_create_empty_directory (h,
  505. client_info,
  506. ksk,
  507. meta,
  508. bo);
  509. GNUNET_CONTAINER_meta_data_destroy (meta);
  510. ret->data.dir.entries = dc.entries;
  511. while (dc.entries != NULL)
  512. {
  513. dc.entries->dir = ret;
  514. dc.entries = dc.entries->next;
  515. }
  516. fn = filename;
  517. while ( (NULL != (ss = strstr (fn,
  518. DIR_SEPARATOR_STR))) &&
  519. (strlen (ss) > 1) )
  520. fn = ss + 1;
  521. GNUNET_asprintf (&dn,
  522. "%s/",
  523. fn);
  524. GNUNET_CONTAINER_meta_data_insert (ret->meta,
  525. "<gnunet>",
  526. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
  527. EXTRACTOR_METAFORMAT_C_STRING,
  528. "text/plain",
  529. dn,
  530. strlen (dn) + 1);
  531. GNUNET_free (dn);
  532. ret->filename = GNUNET_strdup (filename);
  533. return ret;
  534. }
  535. /**
  536. * Test if a given entry represents a directory.
  537. *
  538. * @param ent check if this FI represents a directory
  539. * @return GNUNET_YES if so, GNUNET_NO if not
  540. */
  541. int
  542. GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation *ent)
  543. {
  544. return ent->is_directory;
  545. }
  546. /**
  547. * Create an entry for an empty directory in a publish-structure.
  548. * This function should be used by applications for which the
  549. * use of "GNUNET_FS_file_information_create_from_directory"
  550. * is not appropriate.
  551. *
  552. * @param h handle to the file sharing subsystem
  553. * @param client_info initial value for the client-info value for this entry
  554. * @param meta metadata for the directory
  555. * @param keywords under which keywords should this directory be available
  556. * directly; can be NULL
  557. * @param bo block options
  558. * @return publish structure entry for the directory , NULL on error
  559. */
  560. struct GNUNET_FS_FileInformation *
  561. GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
  562. void *client_info,
  563. const struct GNUNET_FS_Uri *keywords,
  564. const struct GNUNET_CONTAINER_MetaData *meta,
  565. const struct GNUNET_FS_BlockOptions *bo)
  566. {
  567. struct GNUNET_FS_FileInformation *ret;
  568. ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
  569. ret->h = h;
  570. ret->client_info = client_info;
  571. ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
  572. ret->keywords = GNUNET_FS_uri_dup (keywords);
  573. ret->bo = *bo;
  574. ret->is_directory = GNUNET_YES;
  575. return ret;
  576. }
  577. /**
  578. * Add an entry to a directory in a publish-structure. Clients
  579. * should never modify publish structures that were passed to
  580. * "GNUNET_FS_publish_start" already.
  581. *
  582. * @param dir the directory
  583. * @param ent the entry to add; the entry must not have been
  584. * added to any other directory at this point and
  585. * must not include "dir" in its structure
  586. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  587. */
  588. int
  589. GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
  590. struct GNUNET_FS_FileInformation *ent)
  591. {
  592. if ( (ent->dir != NULL) ||
  593. (ent->next != NULL) ||
  594. (! dir->is_directory) )
  595. {
  596. GNUNET_break (0);
  597. return GNUNET_SYSERR;
  598. }
  599. ent->dir = dir;
  600. ent->next = dir->data.dir.entries;
  601. dir->data.dir.entries = ent;
  602. dir->data.dir.dir_size = 0;
  603. return GNUNET_OK;
  604. }
  605. /**
  606. * Inspect a file or directory in a publish-structure. Clients
  607. * should never modify publish structures that were passed to
  608. * "GNUNET_FS_publish_start" already. When called on a directory,
  609. * this function will FIRST call "proc" with information about
  610. * the directory itself and then for each of the files in the
  611. * directory (but not for files in subdirectories). When called
  612. * on a file, "proc" will be called exactly once (with information
  613. * about the specific file).
  614. *
  615. * @param dir the directory
  616. * @param proc function to call on each entry
  617. * @param proc_cls closure for proc
  618. */
  619. void
  620. GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
  621. GNUNET_FS_FileInformationProcessor proc,
  622. void *proc_cls)
  623. {
  624. struct GNUNET_FS_FileInformation *pos;
  625. int no;
  626. no = GNUNET_NO;
  627. if (GNUNET_OK !=
  628. proc (proc_cls,
  629. dir,
  630. (dir->is_directory) ? dir->data.dir.dir_size : dir->data.file.file_size,
  631. dir->meta,
  632. &dir->keywords,
  633. &dir->bo,
  634. (dir->is_directory) ? &no : &dir->data.file.do_index,
  635. &dir->client_info))
  636. return;
  637. if (! dir->is_directory)
  638. return;
  639. pos = dir->data.dir.entries;
  640. while (pos != NULL)
  641. {
  642. no = GNUNET_NO;
  643. if (GNUNET_OK !=
  644. proc (proc_cls,
  645. pos,
  646. (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
  647. pos->meta,
  648. &pos->keywords,
  649. &pos->bo,
  650. (dir->is_directory) ? &no : &dir->data.file.do_index,
  651. &pos->client_info))
  652. break;
  653. pos = pos->next;
  654. }
  655. }
  656. /**
  657. * Destroy publish-structure. Clients should never destroy publish
  658. * structures that were passed to "GNUNET_FS_publish_start" already.
  659. *
  660. * @param fi structure to destroy
  661. * @param cleaner function to call on each entry in the structure
  662. * (useful to clean up client_info); can be NULL; return
  663. * values are ignored
  664. * @param cleaner_cls closure for cleaner
  665. */
  666. void
  667. GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
  668. GNUNET_FS_FileInformationProcessor cleaner,
  669. void *cleaner_cls)
  670. {
  671. struct GNUNET_FS_FileInformation *pos;
  672. int no;
  673. no = GNUNET_NO;
  674. if (fi->is_directory)
  675. {
  676. /* clean up directory */
  677. while (NULL != (pos = fi->data.dir.entries))
  678. {
  679. fi->data.dir.entries = pos->next;
  680. GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
  681. }
  682. /* clean up client-info */
  683. if (NULL != cleaner)
  684. cleaner (cleaner_cls,
  685. fi,
  686. fi->data.dir.dir_size,
  687. fi->meta,
  688. &fi->keywords,
  689. &fi->bo,
  690. &no,
  691. &fi->client_info);
  692. GNUNET_free_non_null (fi->data.dir.dir_data);
  693. }
  694. else
  695. {
  696. /* call clean-up function of the reader */
  697. if (fi->data.file.reader != NULL)
  698. fi->data.file.reader (fi->data.file.reader_cls, 0, 0,
  699. NULL, NULL);
  700. /* clean up client-info */
  701. if (NULL != cleaner)
  702. cleaner (cleaner_cls,
  703. fi,
  704. fi->data.file.file_size,
  705. fi->meta,
  706. &fi->keywords,
  707. &fi->bo,
  708. &fi->data.file.do_index,
  709. &fi->client_info);
  710. }
  711. GNUNET_free_non_null (fi->filename);
  712. GNUNET_free_non_null (fi->emsg);
  713. GNUNET_free_non_null (fi->chk_uri);
  714. /* clean up serialization */
  715. if ( (NULL != fi->serialization) &&
  716. (0 != UNLINK (fi->serialization)) )
  717. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  718. "unlink",
  719. fi->serialization);
  720. if (NULL != fi->keywords)
  721. GNUNET_FS_uri_destroy (fi->keywords);
  722. if (NULL != fi->meta)
  723. GNUNET_CONTAINER_meta_data_destroy (fi->meta);
  724. GNUNET_free_non_null (fi->serialization);
  725. if (fi->te != NULL)
  726. {
  727. GNUNET_FS_tree_encoder_finish (fi->te,
  728. NULL, NULL);
  729. fi->te = NULL;
  730. }
  731. GNUNET_free (fi);
  732. }
  733. /* end of fs_file_information.c */