GNUnet 0.22.0
fs_file_information.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2011 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
26#include "platform.h"
27#if HAVE_EXTRACTOR_H
28#include <extractor.h>
29#endif
30#include "gnunet_fs_service.h"
31#include "fs_api.h"
32#include "fs_tree.h"
33
34
44const char *
46{
47 if (NULL != s->dir)
48 return NULL;
49 return s->serialization;
50}
51
52
59const char *
62{
63 return s->filename;
64}
65
66
75void
77 const char *filename)
78{
80 if (filename)
82 else
83 s->filename = NULL;
84}
85
86
89 struct GNUNET_FS_Handle *h,
90 void *client_info,
91 const char *filename,
92 const struct GNUNET_FS_Uri *keywords,
93 const struct GNUNET_FS_MetaData *meta,
94 int do_index,
95 const struct GNUNET_FS_BlockOptions *bo)
96{
97 struct FileInfo *fi;
98 uint64_t fsize;
100 const char *fn;
101 const char *ss;
102
103 /* FIXME: should include_symbolic_links be GNUNET_NO or GNUNET_YES here? */
104 if (GNUNET_OK !=
106 {
108 return NULL;
109 }
111 if (NULL == fi)
112 {
113 GNUNET_break (0);
114 return NULL;
115 }
116 ret =
119 fsize,
121 fi,
122 keywords,
123 meta,
124 do_index,
125 bo);
126 if (ret == NULL)
127 return NULL;
128 ret->h = h;
129 ret->filename = GNUNET_strdup (filename);
130 fn = filename;
131 while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
132 fn = ss + 1;
133/* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
134 * this should be changed to EXTRACTOR_METAFORMAT_UTF8
135 */
137 "<gnunet>",
139 EXTRACTOR_METAFORMAT_C_STRING,
140 "text/plain",
141 fn,
142 strlen (fn) + 1);
143 return ret;
144}
145
146
149 struct GNUNET_FS_Handle *h,
150 void *client_info,
151 uint64_t length,
152 void *data,
153 const struct GNUNET_FS_Uri *keywords,
154 const struct GNUNET_FS_MetaData *meta,
155 int do_index,
156 const struct GNUNET_FS_BlockOptions *bo)
157{
158 if (GNUNET_YES == do_index)
159 {
160 GNUNET_break (0);
161 return NULL;
162 }
165 length,
166 &
168 data,
169 keywords,
170 meta,
171 do_index,
172 bo);
173}
174
175
178 struct GNUNET_FS_Handle *h,
179 void *client_info,
180 uint64_t length,
182 void *reader_cls,
183 const struct GNUNET_FS_Uri *keywords,
184 const struct GNUNET_FS_MetaData *meta,
185 int do_index,
186 const struct GNUNET_FS_BlockOptions *bo)
187{
189
191 {
192 GNUNET_break (0);
193 return NULL;
194 }
196 ret->h = h;
197 ret->client_info = client_info;
199 if (ret->meta == NULL)
201 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
202 ret->data.file.reader = reader;
203 ret->data.file.reader_cls = reader_cls;
204 ret->data.file.do_index = do_index;
205 ret->data.file.file_size = length;
206 ret->bo = *bo;
207 return ret;
208}
209
210
217int
219 const struct GNUNET_FS_FileInformation *ent)
220{
221 return ent->is_directory;
222}
223
224
227 struct GNUNET_FS_Handle *h,
228 void *client_info,
229 const struct GNUNET_FS_Uri *keywords,
230 const struct GNUNET_FS_MetaData *meta,
231 const struct GNUNET_FS_BlockOptions *bo,
232 const char *filename)
233{
235
237 ret->h = h;
238 ret->client_info = client_info;
240 ret->keywords = GNUNET_FS_uri_dup (keywords);
241 ret->bo = *bo;
242 ret->is_directory = GNUNET_YES;
243 if (filename != NULL)
244 ret->filename = GNUNET_strdup (filename);
245 return ret;
246}
247
248
249int
251 struct GNUNET_FS_FileInformation *ent)
252{
253 if ((ent->dir != NULL) || (ent->next != NULL) ||
254 (dir->is_directory != GNUNET_YES))
255 {
256 GNUNET_break (0);
257 return GNUNET_SYSERR;
258 }
259 ent->dir = dir;
260 ent->next = dir->data.dir.entries;
261 dir->data.dir.entries = ent;
262 dir->data.dir.dir_size = 0;
263 return GNUNET_OK;
264}
265
266
281void
284 void *proc_cls)
285{
286 struct GNUNET_FS_FileInformation *pos;
287 int no;
288
289 no = GNUNET_NO;
290 if (GNUNET_OK !=
291 proc (proc_cls,
292 dir,
293 (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size
294 : dir->data.file.file_size,
295 dir->meta,
296 &dir->keywords,
297 &dir->bo,
298 (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
299 &dir->client_info))
300 return;
301 if (dir->is_directory != GNUNET_YES)
302 return;
303 pos = dir->data.dir.entries;
304 while (pos != NULL)
305 {
306 no = GNUNET_NO;
307 if (GNUNET_OK !=
308 proc (proc_cls,
309 pos,
310 (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size
311 : pos->data.file.file_size,
312 pos->meta,
313 &pos->keywords,
314 &pos->bo,
315 (pos->is_directory == GNUNET_YES) ? &no
316 : &pos->data.file.do_index,
317 &pos->client_info))
318 break;
319 pos = pos->next;
320 }
321}
322
323
334void
337 void *cleaner_cls)
338{
339 struct GNUNET_FS_FileInformation *pos;
340 int no;
341
342 no = GNUNET_NO;
343 if (GNUNET_YES == fi->is_directory)
344 {
345 /* clean up directory */
346 while (NULL != (pos = fi->data.dir.entries))
347 {
348 fi->data.dir.entries = pos->next;
349 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
350 }
351 /* clean up client-info */
352 if (NULL != cleaner)
353 cleaner (cleaner_cls,
354 fi,
355 fi->data.dir.dir_size,
356 fi->meta,
357 &fi->keywords,
358 &fi->bo,
359 &no,
360 &fi->client_info);
362 }
363 else
364 {
365 /* call clean-up function of the reader */
366 if (NULL != fi->data.file.reader)
367 {
368 (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
369 fi->data.file.reader = NULL;
370 }
371 /* clean up client-info */
372 if (NULL != cleaner)
373 cleaner (cleaner_cls,
374 fi,
375 fi->data.file.file_size,
376 fi->meta,
377 &fi->keywords,
378 &fi->bo,
379 &fi->data.file.do_index,
380 &fi->client_info);
381 }
382 GNUNET_free (fi->filename);
383 GNUNET_free (fi->emsg);
384 if (NULL != fi->sks_uri)
386 if (NULL != fi->chk_uri)
388 /* clean up serialization */
389 if ((NULL != fi->serialization) && (0 != unlink (fi->serialization)))
391 "unlink",
392 fi->serialization);
393 if (NULL != fi->keywords)
395 if (NULL != fi->meta)
398 if (NULL != fi->te)
399 {
401 fi->te = NULL;
402 }
403 GNUNET_free (fi);
404}
405
406
407/* end of fs_file_information.c */
void * GNUNET_FS_make_file_reader_context_(const char *filename)
Create the closure for the GNUNET_FS_data_reader_file_() callback.
Definition: fs_api.c:509
size_t GNUNET_FS_data_reader_copy_(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data by copying from a buffer.
Definition: fs_api.c:545
size_t GNUNET_FS_data_reader_file_(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data by reading from a file.
Definition: fs_api.c:447
shared definitions for the FS library
void GNUNET_FS_tree_encoder_finish(struct GNUNET_FS_TreeEncoder *te, char **emsg)
Clean up a tree encoder and return information about possible errors.
Definition: fs_tree.c:432
Merkle-tree-ish-CHK file encoding for GNUnet.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:98
static int ret
Final status code.
Definition: gnunet-arm.c:93
static char * dir
Set to the directory where runtime files are stored.
Definition: gnunet-arm.c:88
static char * data
The data to insert into the dht.
static char * filename
static struct GNUNET_FS_MetaData * meta
Meta-data provided via command-line option.
static struct GNUNET_FS_BlockOptions bo
Options we set for published blocks.
API for file sharing via GNUnet.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:222
struct GNUNET_FS_Uri * GNUNET_FS_uri_dup(const struct GNUNET_FS_Uri *uri)
Duplicate URI.
Definition: fs_uri.c:987
#define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
const char * GNUNET_FS_file_information_get_filename(const struct GNUNET_FS_FileInformation *s)
Obtain the filename from the file information structure.
int(* GNUNET_FS_FileInformationProcessor)(void *cls, struct GNUNET_FS_FileInformation *fi, uint64_t length, struct GNUNET_FS_MetaData *meta, struct GNUNET_FS_Uri **uri, struct GNUNET_FS_BlockOptions *bo, int *do_index, void **client_info)
Function called on entries in a struct GNUNET_FS_FileInformation iteration.
size_t(* GNUNET_FS_DataReader)(void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
Function that provides data.
int GNUNET_FS_file_information_add(struct GNUNET_FS_FileInformation *dir, struct GNUNET_FS_FileInformation *ent)
Add an entry to a directory in a publish-structure.
void GNUNET_FS_file_information_inspect(struct GNUNET_FS_FileInformation *dir, GNUNET_FS_FileInformationProcessor proc, void *proc_cls)
Inspect a file or directory in a publish-structure.
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_empty_directory(struct GNUNET_FS_Handle *h, void *client_info, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_FS_MetaData *meta, const struct GNUNET_FS_BlockOptions *bo, const char *filename)
Create an entry for an empty directory in a publish-structure.
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition: fs_uri.c:677
void GNUNET_FS_file_information_set_filename(struct GNUNET_FS_FileInformation *s, const char *filename)
Set the filename in the file information structure.
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_from_data(struct GNUNET_FS_Handle *h, void *client_info, uint64_t length, void *data, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_FS_MetaData *meta, int do_index, const struct GNUNET_FS_BlockOptions *bo)
Create an entry for a file in a publish-structure.
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_from_file(struct GNUNET_FS_Handle *h, void *client_info, const char *filename, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_FS_MetaData *meta, int do_index, const struct GNUNET_FS_BlockOptions *bo)
Create an entry for a file in a publish-structure.
int GNUNET_FS_file_information_is_directory(const struct GNUNET_FS_FileInformation *ent)
Test if a given entry represents a directory.
struct GNUNET_FS_FileInformation * GNUNET_FS_file_information_create_from_reader(struct GNUNET_FS_Handle *h, void *client_info, uint64_t length, GNUNET_FS_DataReader reader, void *reader_cls, const struct GNUNET_FS_Uri *keywords, const struct GNUNET_FS_MetaData *meta, int do_index, const struct GNUNET_FS_BlockOptions *bo)
Create an entry for a file in a publish-structure.
void GNUNET_FS_file_information_destroy(struct GNUNET_FS_FileInformation *fi, GNUNET_FS_FileInformationProcessor cleaner, void *cleaner_cls)
Destroy publish-structure.
const char * GNUNET_FS_file_information_get_id(struct GNUNET_FS_FileInformation *s)
Obtain the name under which this file information structure is stored on disk.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_FS_MetaData * GNUNET_FS_meta_data_create(void)
Create a fresh meta data container.
Definition: meta_data.c:127
struct GNUNET_FS_MetaData * GNUNET_FS_meta_data_duplicate(const struct GNUNET_FS_MetaData *md)
Duplicate a MetaData token.
Definition: meta_data.c:527
int GNUNET_FS_meta_data_insert(struct GNUNET_FS_MetaData *md, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Extend metadata.
Definition: meta_data.c:254
void GNUNET_FS_meta_data_destroy(struct GNUNET_FS_MetaData *md)
Free meta data.
Definition: meta_data.c:166
#define DIR_SEPARATOR_STR
Definition: platform.h:166
Closure for GNUNET_FS_data_reader_file_().
Definition: fs_api.c:413
Settings for publishing a block (which may of course also apply to an entire directory or file).
Information for a file or directory that is about to be published.
Definition: fs_api.h:228
char * serialization
Under what filename is this struct serialized (for operational persistence).
Definition: fs_api.h:287
struct GNUNET_FS_Uri * keywords
Keywords to use for KBlocks.
Definition: fs_api.h:258
char * filename
Name of the file or directory (must be an absolute path).
Definition: fs_api.h:302
size_t dir_size
Size of the directory itself (in bytes); 0 if the size has not yet been calculated.
Definition: fs_api.h:368
struct GNUNET_FS_TreeEncoder * te
Encoder being used to publish this file.
Definition: fs_api.h:292
struct GNUNET_FS_FileInformation::@52::@53 file
Data for a file.
GNUNET_FS_DataReader reader
Function that can be used to read the data for the file.
Definition: fs_api.h:317
uint64_t file_size
Size of the file (in bytes).
Definition: fs_api.h:334
char * emsg
Error message (non-NULL if this operation failed).
Definition: fs_api.h:297
void * client_info
Pointer kept for the client.
Definition: fs_api.h:248
struct GNUNET_FS_FileInformation * next
Files in a directory are kept as a linked list.
Definition: fs_api.h:232
struct GNUNET_FS_BlockOptions bo
Block options for the file.
Definition: fs_api.h:275
struct GNUNET_FS_MetaData * meta
Metadata to use for the file.
Definition: fs_api.h:253
union GNUNET_FS_FileInformation::@52 data
Data describing either the file or the directory.
struct GNUNET_FS_Uri * sks_uri
SKS URI for this file or directory.
Definition: fs_api.h:270
void * dir_data
Pointer to the data for the directory (or NULL if not available).
Definition: fs_api.h:374
int do_index
Should the file be indexed or inserted?
Definition: fs_api.h:339
void * reader_cls
Closure for reader.
Definition: fs_api.h:322
struct GNUNET_FS_FileInformation * entries
Linked list of entries in the directory.
Definition: fs_api.h:362
struct GNUNET_FS_FileInformation * dir
If this is a file in a directory, "dir" refers to the directory; otherwise NULL.
Definition: fs_api.h:238
struct GNUNET_FS_Uri * chk_uri
CHK for this file or directory.
Definition: fs_api.h:264
int is_directory
Is this struct for a file or directory?
Definition: fs_api.h:391
Master context for most FS operations.
Definition: fs_api.h:1070
Meta data to associate with a file, directory or namespace.
Definition: meta_data.c:92
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:167