aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_file_information.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-04-30 13:16:03 +0000
committerChristian Grothoff <christian@grothoff.org>2010-04-30 13:16:03 +0000
commitd59ea5663203392637e84dea69feb9671ca2a3de (patch)
tree2898909b01cc22b9b031bb76b99d6cd262ab3ee3 /src/fs/fs_file_information.c
parent4f2b474c1a9e51d71abe8d692fedddcb1d1e0ede (diff)
downloadgnunet-d59ea5663203392637e84dea69feb9671ca2a3de.tar.gz
gnunet-d59ea5663203392637e84dea69feb9671ca2a3de.zip
finishing initial coding of fs-publish serialization/deserialization
Diffstat (limited to 'src/fs/fs_file_information.c')
-rw-r--r--src/fs/fs_file_information.c106
1 files changed, 15 insertions, 91 deletions
diff --git a/src/fs/fs_file_information.c b/src/fs/fs_file_information.c
index 2d0194741..df5593f75 100644
--- a/src/fs/fs_file_information.c
+++ b/src/fs/fs_file_information.c
@@ -55,92 +55,6 @@ GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
55 55
56 56
57/** 57/**
58 * Closure for "data_reader_file".
59 */
60struct FileInfo
61{
62 /**
63 * Name of the file to read.
64 */
65 char *filename;
66
67 /**
68 * File descriptor, NULL if it has not yet been opened.
69 */
70 struct GNUNET_DISK_FileHandle *fd;
71};
72
73
74/**
75 * Function that provides data by reading from a file.
76 *
77 * @param cls closure (points to the file information)
78 * @param offset offset to read from; it is possible
79 * that the caller might need to go backwards
80 * a bit at times
81 * @param max maximum number of bytes that should be
82 * copied to buf; readers are not allowed
83 * to provide less data unless there is an error;
84 * a value of "0" will be used at the end to allow
85 * the reader to clean up its internal state
86 * @param buf where the reader should write the data
87 * @param emsg location for the reader to store an error message
88 * @return number of bytes written, usually "max", 0 on error
89 */
90static size_t
91data_reader_file(void *cls,
92 uint64_t offset,
93 size_t max,
94 void *buf,
95 char **emsg)
96{
97 struct FileInfo *fi = cls;
98 ssize_t ret;
99
100 if (max == 0)
101 {
102 if (fi->fd != NULL)
103 GNUNET_DISK_file_close (fi->fd);
104 GNUNET_free (fi->filename);
105 GNUNET_free (fi);
106 return 0;
107 }
108 if (fi->fd == NULL)
109 {
110 fi->fd = GNUNET_DISK_file_open (fi->filename,
111 GNUNET_DISK_OPEN_READ,
112 GNUNET_DISK_PERM_NONE);
113 if (fi->fd == NULL)
114 {
115 GNUNET_asprintf (emsg,
116 _("Could not open file `%s': %s"),
117 fi->filename,
118 STRERROR (errno));
119 return 0;
120 }
121 }
122 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
123 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
124 if (ret == -1)
125 {
126 GNUNET_asprintf (emsg,
127 _("Could not read file `%s': %s"),
128 fi->filename,
129 STRERROR (errno));
130 return 0;
131 }
132 if (ret != max)
133 {
134 GNUNET_asprintf (emsg,
135 _("Short read reading from file `%s'!"),
136 fi->filename);
137 return 0;
138 }
139 return max;
140}
141
142
143/**
144 * Create an entry for a file in a publish-structure. 58 * Create an entry for a file in a publish-structure.
145 * 59 *
146 * @param h handle to the file sharing subsystem 60 * @param h handle to the file sharing subsystem
@@ -182,17 +96,16 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
182 filename); 96 filename);
183 return NULL; 97 return NULL;
184 } 98 }
185 fi = GNUNET_malloc (sizeof(struct FileInfo)); 99 fi = GNUNET_FS_make_file_reader_context_ (filename);
186 fi->filename = GNUNET_STRINGS_filename_expand (filename); 100 if (fi == NULL)
187 if (fi->filename == NULL)
188 { 101 {
189 GNUNET_free (fi); 102 GNUNET_break (0);
190 return NULL; 103 return NULL;
191 } 104 }
192 ret = GNUNET_FS_file_information_create_from_reader (h, 105 ret = GNUNET_FS_file_information_create_from_reader (h,
193 client_info, 106 client_info,
194 sbuf.st_size, 107 sbuf.st_size,
195 &data_reader_file, 108 &GNUNET_FS_data_reader_file_,
196 fi, 109 fi,
197 keywords, 110 keywords,
198 meta, 111 meta,
@@ -284,6 +197,11 @@ GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
284 uint32_t priority, 197 uint32_t priority,
285 struct GNUNET_TIME_Absolute expirationTime) 198 struct GNUNET_TIME_Absolute expirationTime)
286{ 199{
200 if (GNUNET_YES == do_index)
201 {
202 GNUNET_break (0);
203 return NULL;
204 }
287 return GNUNET_FS_file_information_create_from_reader (h, 205 return GNUNET_FS_file_information_create_from_reader (h,
288 client_info, 206 client_info,
289 length, 207 length,
@@ -333,6 +251,12 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
333{ 251{
334 struct GNUNET_FS_FileInformation *ret; 252 struct GNUNET_FS_FileInformation *ret;
335 253
254 if ( (GNUNET_YES == do_index) &&
255 (reader != &GNUNET_FS_data_reader_file_) )
256 {
257 GNUNET_break (0);
258 return NULL;
259 }
336 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation)); 260 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
337 ret->h = h; 261 ret->h = h;
338 ret->client_info = client_info; 262 ret->client_info = client_info;