aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-auto-share.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-auto-share.c')
-rw-r--r--src/fs/gnunet-auto-share.c602
1 files changed, 300 insertions, 302 deletions
diff --git a/src/fs/gnunet-auto-share.c b/src/fs/gnunet-auto-share.c
index 8b04e474d..6fc71ebe5 100644
--- a/src/fs/gnunet-auto-share.c
+++ b/src/fs/gnunet-auto-share.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file fs/gnunet-auto-share.c 21 * @file fs/gnunet-auto-share.c
22 * @brief automatically publish files on GNUnet 22 * @brief automatically publish files on GNUnet
@@ -29,7 +29,7 @@
29#include "platform.h" 29#include "platform.h"
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31 31
32#define MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4) 32#define MAX_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 4)
33 33
34#define MIN_DELAY GNUNET_TIME_UNIT_MINUTES 34#define MIN_DELAY GNUNET_TIME_UNIT_MINUTES
35 35
@@ -38,9 +38,7 @@
38 * Item in our work queue (or in the set of files/directories 38 * Item in our work queue (or in the set of files/directories
39 * we have successfully published). 39 * we have successfully published).
40 */ 40 */
41struct WorkItem 41struct WorkItem {
42{
43
44 /** 42 /**
45 * PENDING Work is kept in a linked list. 43 * PENDING Work is kept in a linked list.
46 */ 44 */
@@ -162,16 +160,16 @@ static struct GNUNET_OS_Process *publish_proc;
162 * Compute the name of the state database file we will use. 160 * Compute the name of the state database file we will use.
163 */ 161 */
164static char * 162static char *
165get_state_file () 163get_state_file()
166{ 164{
167 char *ret; 165 char *ret;
168 166
169 GNUNET_asprintf (&ret, 167 GNUNET_asprintf(&ret,
170 "%s%s.auto-share", 168 "%s%s.auto-share",
171 dir_name, 169 dir_name,
172 (DIR_SEPARATOR == dir_name[strlen (dir_name) - 1]) 170 (DIR_SEPARATOR == dir_name[strlen(dir_name) - 1])
173 ? "" 171 ? ""
174 : DIR_SEPARATOR_STR); 172 : DIR_SEPARATOR_STR);
175 return ret; 173 return ret;
176} 174}
177 175
@@ -180,7 +178,7 @@ get_state_file ()
180 * Load the set of #work_finished items from disk. 178 * Load the set of #work_finished items from disk.
181 */ 179 */
182static void 180static void
183load_state () 181load_state()
184{ 182{
185 char *fn; 183 char *fn;
186 struct GNUNET_BIO_ReadHandle *rh; 184 struct GNUNET_BIO_ReadHandle *rh;
@@ -190,47 +188,47 @@ load_state ()
190 char *emsg; 188 char *emsg;
191 189
192 emsg = NULL; 190 emsg = NULL;
193 fn = get_state_file (); 191 fn = get_state_file();
194 rh = GNUNET_BIO_read_open (fn); 192 rh = GNUNET_BIO_read_open(fn);
195 GNUNET_free (fn); 193 GNUNET_free(fn);
196 if (NULL == rh) 194 if (NULL == rh)
197 return; 195 return;
198 fn = NULL; 196 fn = NULL;
199 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &n)) 197 if (GNUNET_OK != GNUNET_BIO_read_int32(rh, &n))
200 goto error; 198 goto error;
201 while (n-- > 0) 199 while (n-- > 0)
202 { 200 {
203 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "filename", &fn, 1024)) || 201 if ((GNUNET_OK != GNUNET_BIO_read_string(rh, "filename", &fn, 1024)) ||
204 (GNUNET_OK != 202 (GNUNET_OK !=
205 GNUNET_BIO_read (rh, "id", &id, sizeof (struct GNUNET_HashCode)))) 203 GNUNET_BIO_read(rh, "id", &id, sizeof(struct GNUNET_HashCode))))
206 goto error; 204 goto error;
207 wi = GNUNET_new (struct WorkItem); 205 wi = GNUNET_new(struct WorkItem);
208 wi->id = id; 206 wi->id = id;
209 wi->filename = fn; 207 wi->filename = fn;
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 208 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
211 "Loaded serialization ID for `%s' is `%s'\n", 209 "Loaded serialization ID for `%s' is `%s'\n",
212 wi->filename, 210 wi->filename,
213 GNUNET_h2s (&id)); 211 GNUNET_h2s(&id));
214 fn = NULL; 212 fn = NULL;
215 GNUNET_CRYPTO_hash (wi->filename, strlen (wi->filename), &id); 213 GNUNET_CRYPTO_hash(wi->filename, strlen(wi->filename), &id);
216 GNUNET_break (GNUNET_OK == 214 GNUNET_break(GNUNET_OK ==
217 GNUNET_CONTAINER_multihashmap_put ( 215 GNUNET_CONTAINER_multihashmap_put(
218 work_finished, 216 work_finished,
219 &id, 217 &id,
220 wi, 218 wi,
221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
222 } 220 }
223 if (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg)) 221 if (GNUNET_OK == GNUNET_BIO_read_close(rh, &emsg))
224 return; 222 return;
225 rh = NULL; 223 rh = NULL;
226error: 224error:
227 GNUNET_free_non_null (fn); 225 GNUNET_free_non_null(fn);
228 if (NULL != rh) 226 if (NULL != rh)
229 (void) GNUNET_BIO_read_close (rh, &emsg); 227 (void)GNUNET_BIO_read_close(rh, &emsg);
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 228 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
231 _ ("Failed to load state: %s\n"), 229 _("Failed to load state: %s\n"),
232 emsg); 230 emsg);
233 GNUNET_free_non_null (emsg); 231 GNUNET_free_non_null(emsg);
234} 232}
235 233
236 234
@@ -243,18 +241,18 @@ error:
243 * @return #GNUNET_OK to continue to iterate (if write worked) 241 * @return #GNUNET_OK to continue to iterate (if write worked)
244 */ 242 */
245static int 243static int
246write_item (void *cls, const struct GNUNET_HashCode *key, void *value) 244write_item(void *cls, const struct GNUNET_HashCode *key, void *value)
247{ 245{
248 struct GNUNET_BIO_WriteHandle *wh = cls; 246 struct GNUNET_BIO_WriteHandle *wh = cls;
249 struct WorkItem *wi = value; 247 struct WorkItem *wi = value;
250 248
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 249 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
252 "Saving serialization ID of file `%s' with value `%s'\n", 250 "Saving serialization ID of file `%s' with value `%s'\n",
253 wi->filename, 251 wi->filename,
254 GNUNET_h2s (&wi->id)); 252 GNUNET_h2s(&wi->id));
255 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, wi->filename)) || 253 if ((GNUNET_OK != GNUNET_BIO_write_string(wh, wi->filename)) ||
256 (GNUNET_OK != 254 (GNUNET_OK !=
257 GNUNET_BIO_write (wh, &wi->id, sizeof (struct GNUNET_HashCode)))) 255 GNUNET_BIO_write(wh, &wi->id, sizeof(struct GNUNET_HashCode))))
258 return GNUNET_SYSERR; /* write error, abort iteration */ 256 return GNUNET_SYSERR; /* write error, abort iteration */
259 return GNUNET_OK; 257 return GNUNET_OK;
260} 258}
@@ -264,38 +262,38 @@ write_item (void *cls, const struct GNUNET_HashCode *key, void *value)
264 * Save the set of #work_finished items on disk. 262 * Save the set of #work_finished items on disk.
265 */ 263 */
266static void 264static void
267save_state () 265save_state()
268{ 266{
269 uint32_t n; 267 uint32_t n;
270 struct GNUNET_BIO_WriteHandle *wh; 268 struct GNUNET_BIO_WriteHandle *wh;
271 char *fn; 269 char *fn;
272 270
273 n = GNUNET_CONTAINER_multihashmap_size (work_finished); 271 n = GNUNET_CONTAINER_multihashmap_size(work_finished);
274 fn = get_state_file (); 272 fn = get_state_file();
275 wh = GNUNET_BIO_write_open (fn); 273 wh = GNUNET_BIO_write_open(fn);
276 if (NULL == wh) 274 if (NULL == wh)
277 { 275 {
278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 276 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
279 _ ("Failed to save state to file %s\n"), 277 _("Failed to save state to file %s\n"),
280 fn); 278 fn);
281 GNUNET_free (fn); 279 GNUNET_free(fn);
282 return; 280 return;
283 } 281 }
284 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, n)) 282 if (GNUNET_OK != GNUNET_BIO_write_int32(wh, n))
285 { 283 {
286 (void) GNUNET_BIO_write_close (wh); 284 (void)GNUNET_BIO_write_close(wh);
287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 285 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
288 _ ("Failed to save state to file %s\n"), 286 _("Failed to save state to file %s\n"),
289 fn); 287 fn);
290 GNUNET_free (fn); 288 GNUNET_free(fn);
291 return; 289 return;
292 } 290 }
293 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished, &write_item, wh); 291 (void)GNUNET_CONTAINER_multihashmap_iterate(work_finished, &write_item, wh);
294 if (GNUNET_OK != GNUNET_BIO_write_close (wh)) 292 if (GNUNET_OK != GNUNET_BIO_write_close(wh))
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 293 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
296 _ ("Failed to save state to file %s\n"), 294 _("Failed to save state to file %s\n"),
297 fn); 295 fn);
298 GNUNET_free (fn); 296 GNUNET_free(fn);
299} 297}
300 298
301 299
@@ -305,19 +303,19 @@ save_state ()
305 * @param cls closure, unused 303 * @param cls closure, unused
306 */ 304 */
307static void 305static void
308do_stop_task (void *cls) 306do_stop_task(void *cls)
309{ 307{
310 do_shutdown = GNUNET_YES; 308 do_shutdown = GNUNET_YES;
311 if (NULL != publish_proc) 309 if (NULL != publish_proc)
312 { 310 {
313 GNUNET_OS_process_kill (publish_proc, SIGKILL); 311 GNUNET_OS_process_kill(publish_proc, SIGKILL);
314 return; 312 return;
315 } 313 }
316 if (NULL != run_task) 314 if (NULL != run_task)
317 { 315 {
318 GNUNET_SCHEDULER_cancel (run_task); 316 GNUNET_SCHEDULER_cancel(run_task);
319 run_task = NULL; 317 run_task = NULL;
320 } 318 }
321} 319}
322 320
323 321
@@ -325,7 +323,7 @@ do_stop_task (void *cls)
325 * Decide what the next task is (working or scanning) and schedule it. 323 * Decide what the next task is (working or scanning) and schedule it.
326 */ 324 */
327static void 325static void
328schedule_next_task (void); 326schedule_next_task(void);
329 327
330 328
331/** 329/**
@@ -335,7 +333,7 @@ schedule_next_task (void);
335 * @param cls the `struct WorkItem` we were working on 333 * @param cls the `struct WorkItem` we were working on
336 */ 334 */
337static void 335static void
338maint_child_death (void *cls) 336maint_child_death(void *cls)
339{ 337{
340 struct WorkItem *wi = cls; 338 struct WorkItem *wi = cls;
341 struct GNUNET_HashCode key; 339 struct GNUNET_HashCode key;
@@ -347,65 +345,65 @@ maint_child_death (void *cls)
347 const struct GNUNET_SCHEDULER_TaskContext *tc; 345 const struct GNUNET_SCHEDULER_TaskContext *tc;
348 346
349 run_task = NULL; 347 run_task = NULL;
350 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ); 348 pr = GNUNET_DISK_pipe_handle(sigpipe, GNUNET_DISK_PIPE_END_READ);
351 tc = GNUNET_SCHEDULER_get_task_context (); 349 tc = GNUNET_SCHEDULER_get_task_context();
352 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) 350 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
353 { 351 {
354 /* shutdown scheduled us, someone else will kill child, 352 /* shutdown scheduled us, someone else will kill child,
355 we should just try again */ 353 we should just try again */
356 run_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 354 run_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
357 pr, 355 pr,
358 &maint_child_death, 356 &maint_child_death,
359 wi); 357 wi);
360 return; 358 return;
361 } 359 }
362 /* consume the signal */ 360 /* consume the signal */
363 GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c))); 361 GNUNET_break(0 < GNUNET_DISK_file_read(pr, &c, sizeof(c)));
364 362
365 ret = GNUNET_OS_process_status (publish_proc, &type, &code); 363 ret = GNUNET_OS_process_status(publish_proc, &type, &code);
366 GNUNET_assert (GNUNET_SYSERR != ret); 364 GNUNET_assert(GNUNET_SYSERR != ret);
367 if (GNUNET_NO == ret) 365 if (GNUNET_NO == ret)
368 { 366 {
369 /* process still running? Then where did the SIGCHLD come from? 367 /* process still running? Then where did the SIGCHLD come from?
370 Well, let's declare it spurious (kernel bug?) and keep rolling. 368 Well, let's declare it spurious (kernel bug?) and keep rolling.
371 */ 369 */
372 GNUNET_break (0); 370 GNUNET_break(0);
373 run_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 371 run_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
374 pr, 372 pr,
375 &maint_child_death, 373 &maint_child_death,
376 wi); 374 wi);
377 return; 375 return;
378 } 376 }
379 GNUNET_assert (GNUNET_OK == ret); 377 GNUNET_assert(GNUNET_OK == ret);
380 378
381 GNUNET_OS_process_destroy (publish_proc); 379 GNUNET_OS_process_destroy(publish_proc);
382 publish_proc = NULL; 380 publish_proc = NULL;
383 381
384 if (GNUNET_YES == do_shutdown) 382 if (GNUNET_YES == do_shutdown)
385 { 383 {
386 GNUNET_free (wi->filename); 384 GNUNET_free(wi->filename);
387 GNUNET_free (wi); 385 GNUNET_free(wi);
388 return; 386 return;
389 } 387 }
390 if ((GNUNET_OS_PROCESS_EXITED == type) && (0 == code)) 388 if ((GNUNET_OS_PROCESS_EXITED == type) && (0 == code))
391 { 389 {
392 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 390 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
393 _ ("Publication of `%s' done\n"), 391 _("Publication of `%s' done\n"),
394 wi->filename); 392 wi->filename);
395 GNUNET_CRYPTO_hash (wi->filename, strlen (wi->filename), &key); 393 GNUNET_CRYPTO_hash(wi->filename, strlen(wi->filename), &key);
396 GNUNET_break (GNUNET_OK == 394 GNUNET_break(GNUNET_OK ==
397 GNUNET_CONTAINER_multihashmap_put ( 395 GNUNET_CONTAINER_multihashmap_put(
398 work_finished, 396 work_finished,
399 &key, 397 &key,
400 wi, 398 wi,
401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
402 } 400 }
403 else 401 else
404 { 402 {
405 GNUNET_CONTAINER_DLL_insert_tail (work_head, work_tail, wi); 403 GNUNET_CONTAINER_DLL_insert_tail(work_head, work_tail, wi);
406 } 404 }
407 save_state (); 405 save_state();
408 schedule_next_task (); 406 schedule_next_task();
409} 407}
410 408
411 409
@@ -414,17 +412,17 @@ maint_child_death (void *cls)
414 * respective handler by writing to the trigger pipe. 412 * respective handler by writing to the trigger pipe.
415 */ 413 */
416static void 414static void
417sighandler_child_death () 415sighandler_child_death()
418{ 416{
419 static char c; 417 static char c;
420 int old_errno = errno; /* back-up errno */ 418 int old_errno = errno; /* back-up errno */
421 419
422 GNUNET_break ( 420 GNUNET_break(
423 1 == 421 1 ==
424 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (sigpipe, 422 GNUNET_DISK_file_write(GNUNET_DISK_pipe_handle(sigpipe,
425 GNUNET_DISK_PIPE_END_WRITE), 423 GNUNET_DISK_PIPE_END_WRITE),
426 &c, 424 &c,
427 sizeof (c))); 425 sizeof(c)));
428 errno = old_errno; /* restore errno */ 426 errno = old_errno; /* restore errno */
429} 427}
430 428
@@ -435,7 +433,7 @@ sighandler_child_death ()
435 * @param cls closure, NULL 433 * @param cls closure, NULL
436 */ 434 */
437static void 435static void
438work (void *cls) 436work(void *cls)
439{ 437{
440 static char *argv[14]; 438 static char *argv[14];
441 static char anon_level[20]; 439 static char anon_level[20];
@@ -447,7 +445,7 @@ work (void *cls)
447 445
448 run_task = NULL; 446 run_task = NULL;
449 wi = work_head; 447 wi = work_head;
450 GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi); 448 GNUNET_CONTAINER_DLL_remove(work_head, work_tail, wi);
451 argc = 0; 449 argc = 0;
452 argv[argc++] = "gnunet-publish"; 450 argv[argc++] = "gnunet-publish";
453 if (verbose) 451 if (verbose)
@@ -458,41 +456,41 @@ work (void *cls)
458 argv[argc++] = "-d"; 456 argv[argc++] = "-d";
459 argv[argc++] = "-c"; 457 argv[argc++] = "-c";
460 argv[argc++] = cfg_filename; 458 argv[argc++] = cfg_filename;
461 GNUNET_snprintf (anon_level, sizeof (anon_level), "%u", anonymity_level); 459 GNUNET_snprintf(anon_level, sizeof(anon_level), "%u", anonymity_level);
462 argv[argc++] = "-a"; 460 argv[argc++] = "-a";
463 argv[argc++] = anon_level; 461 argv[argc++] = anon_level;
464 GNUNET_snprintf (content_prio, sizeof (content_prio), "%u", content_priority); 462 GNUNET_snprintf(content_prio, sizeof(content_prio), "%u", content_priority);
465 argv[argc++] = "-p"; 463 argv[argc++] = "-p";
466 argv[argc++] = content_prio; 464 argv[argc++] = content_prio;
467 GNUNET_snprintf (repl_level, sizeof (repl_level), "%u", replication_level); 465 GNUNET_snprintf(repl_level, sizeof(repl_level), "%u", replication_level);
468 argv[argc++] = "-r"; 466 argv[argc++] = "-r";
469 argv[argc++] = repl_level; 467 argv[argc++] = repl_level;
470 argv[argc++] = wi->filename; 468 argv[argc++] = wi->filename;
471 argv[argc] = NULL; 469 argv[argc] = NULL;
472 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Publishing `%s'\n"), wi->filename); 470 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Publishing `%s'\n"), wi->filename);
473 GNUNET_assert (NULL == publish_proc); 471 GNUNET_assert(NULL == publish_proc);
474 publish_proc = GNUNET_OS_start_process_vap (GNUNET_YES, 472 publish_proc = GNUNET_OS_start_process_vap(GNUNET_YES,
475 0, 473 0,
476 NULL, 474 NULL,
477 NULL, 475 NULL,
478 NULL, 476 NULL,
479 "gnunet-publish", 477 "gnunet-publish",
480 argv); 478 argv);
481 if (NULL == publish_proc) 479 if (NULL == publish_proc)
482 { 480 {
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 481 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
484 _ ("Failed to run `%s'\n"), 482 _("Failed to run `%s'\n"),
485 "gnunet-publish"); 483 "gnunet-publish");
486 GNUNET_CONTAINER_DLL_insert (work_head, work_tail, wi); 484 GNUNET_CONTAINER_DLL_insert(work_head, work_tail, wi);
487 run_task = 485 run_task =
488 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &work, NULL); 486 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES, &work, NULL);
489 return; 487 return;
490 } 488 }
491 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ); 489 pr = GNUNET_DISK_pipe_handle(sigpipe, GNUNET_DISK_PIPE_END_READ);
492 run_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 490 run_task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
493 pr, 491 pr,
494 &maint_child_death, 492 &maint_child_death,
495 wi); 493 wi);
496} 494}
497 495
498 496
@@ -505,39 +503,39 @@ work (void *cls)
505 * @return #GNUNET_OK (always) 503 * @return #GNUNET_OK (always)
506 */ 504 */
507static int 505static int
508determine_id (void *cls, const char *filename) 506determine_id(void *cls, const char *filename)
509{ 507{
510 struct GNUNET_HashCode *id = cls; 508 struct GNUNET_HashCode *id = cls;
511 struct stat sbuf; 509 struct stat sbuf;
512 struct GNUNET_HashCode fx[2]; 510 struct GNUNET_HashCode fx[2];
513 struct GNUNET_HashCode ft; 511 struct GNUNET_HashCode ft;
514 512
515 if (0 != stat (filename, &sbuf)) 513 if (0 != stat(filename, &sbuf))
516 { 514 {
517 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename); 515 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
518 return GNUNET_OK; 516 return GNUNET_OK;
519 } 517 }
520 GNUNET_CRYPTO_hash (filename, strlen (filename), &fx[0]); 518 GNUNET_CRYPTO_hash(filename, strlen(filename), &fx[0]);
521 if (! S_ISDIR (sbuf.st_mode)) 519 if (!S_ISDIR(sbuf.st_mode))
522 { 520 {
523 uint64_t fattr[2]; 521 uint64_t fattr[2];
524 522
525 fattr[0] = GNUNET_htonll (sbuf.st_size); 523 fattr[0] = GNUNET_htonll(sbuf.st_size);
526 fattr[0] = GNUNET_htonll (sbuf.st_mtime); 524 fattr[0] = GNUNET_htonll(sbuf.st_mtime);
527 525
528 GNUNET_CRYPTO_hash (fattr, sizeof (fattr), &fx[1]); 526 GNUNET_CRYPTO_hash(fattr, sizeof(fattr), &fx[1]);
529 } 527 }
530 else 528 else
531 { 529 {
532 memset (&fx[1], 1, sizeof (struct GNUNET_HashCode)); 530 memset(&fx[1], 1, sizeof(struct GNUNET_HashCode));
533 GNUNET_DISK_directory_scan (filename, &determine_id, &fx[1]); 531 GNUNET_DISK_directory_scan(filename, &determine_id, &fx[1]);
534 } 532 }
535 /* use hash here to make hierarchical structure distinct from 533 /* use hash here to make hierarchical structure distinct from
536 all files on the same level */ 534 all files on the same level */
537 GNUNET_CRYPTO_hash (fx, sizeof (fx), &ft); 535 GNUNET_CRYPTO_hash(fx, sizeof(fx), &ft);
538 /* use XOR here so that order of the files in the directory 536 /* use XOR here so that order of the files in the directory
539 does not matter! */ 537 does not matter! */
540 GNUNET_CRYPTO_hash_xor (&ft, id, id); 538 GNUNET_CRYPTO_hash_xor(&ft, id, id);
541 return GNUNET_OK; 539 return GNUNET_OK;
542} 540}
543 541
@@ -552,7 +550,7 @@ determine_id (void *cls, const char *filename)
552 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR during shutdown 550 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR during shutdown
553 */ 551 */
554static int 552static int
555add_file (void *cls, const char *filename) 553add_file(void *cls, const char *filename)
556{ 554{
557 struct WorkItem *wi; 555 struct WorkItem *wi;
558 struct GNUNET_HashCode key; 556 struct GNUNET_HashCode key;
@@ -560,29 +558,29 @@ add_file (void *cls, const char *filename)
560 558
561 if (GNUNET_YES == do_shutdown) 559 if (GNUNET_YES == do_shutdown)
562 return GNUNET_SYSERR; 560 return GNUNET_SYSERR;
563 if ((NULL != strstr (filename, "/.auto-share")) || 561 if ((NULL != strstr(filename, "/.auto-share")) ||
564 (NULL != strstr (filename, "\\.auto-share"))) 562 (NULL != strstr(filename, "\\.auto-share")))
565 return GNUNET_OK; /* skip internal file */ 563 return GNUNET_OK; /* skip internal file */
566 GNUNET_CRYPTO_hash (filename, strlen (filename), &key); 564 GNUNET_CRYPTO_hash(filename, strlen(filename), &key);
567 wi = GNUNET_CONTAINER_multihashmap_get (work_finished, &key); 565 wi = GNUNET_CONTAINER_multihashmap_get(work_finished, &key);
568 memset (&id, 0, sizeof (struct GNUNET_HashCode)); 566 memset(&id, 0, sizeof(struct GNUNET_HashCode));
569 determine_id (&id, filename); 567 determine_id(&id, filename);
570 if (NULL != wi) 568 if (NULL != wi)
571 { 569 {
572 if (0 == memcmp (&id, &wi->id, sizeof (struct GNUNET_HashCode))) 570 if (0 == memcmp(&id, &wi->id, sizeof(struct GNUNET_HashCode)))
573 return GNUNET_OK; /* skip: we did this one already */ 571 return GNUNET_OK; /* skip: we did this one already */
574 /* contents changed, need to re-do the directory... */ 572 /* contents changed, need to re-do the directory... */
575 GNUNET_assert ( 573 GNUNET_assert(
576 GNUNET_YES == 574 GNUNET_YES ==
577 GNUNET_CONTAINER_multihashmap_remove (work_finished, &key, wi)); 575 GNUNET_CONTAINER_multihashmap_remove(work_finished, &key, wi));
578 } 576 }
579 else 577 else
580 { 578 {
581 wi = GNUNET_new (struct WorkItem); 579 wi = GNUNET_new(struct WorkItem);
582 wi->filename = GNUNET_strdup (filename); 580 wi->filename = GNUNET_strdup(filename);
583 } 581 }
584 wi->id = id; 582 wi->id = id;
585 GNUNET_CONTAINER_DLL_insert (work_head, work_tail, wi); 583 GNUNET_CONTAINER_DLL_insert(work_head, work_tail, wi);
586 if (GNUNET_YES == do_shutdown) 584 if (GNUNET_YES == do_shutdown)
587 return GNUNET_SYSERR; 585 return GNUNET_SYSERR;
588 return GNUNET_OK; 586 return GNUNET_OK;
@@ -595,12 +593,12 @@ add_file (void *cls, const char *filename)
595 * @param cls NULL 593 * @param cls NULL
596 */ 594 */
597static void 595static void
598scan (void *cls) 596scan(void *cls)
599{ 597{
600 run_task = NULL; 598 run_task = NULL;
601 start_time = GNUNET_TIME_absolute_get (); 599 start_time = GNUNET_TIME_absolute_get();
602 (void) GNUNET_DISK_directory_scan (dir_name, &add_file, NULL); 600 (void)GNUNET_DISK_directory_scan(dir_name, &add_file, NULL);
603 schedule_next_task (); 601 schedule_next_task();
604} 602}
605 603
606 604
@@ -608,27 +606,27 @@ scan (void *cls)
608 * Decide what the next task is (working or scanning) and schedule it. 606 * Decide what the next task is (working or scanning) and schedule it.
609 */ 607 */
610static void 608static void
611schedule_next_task () 609schedule_next_task()
612{ 610{
613 struct GNUNET_TIME_Relative delay; 611 struct GNUNET_TIME_Relative delay;
614 612
615 if (GNUNET_YES == do_shutdown) 613 if (GNUNET_YES == do_shutdown)
616 return; 614 return;
617 GNUNET_assert (NULL == run_task); 615 GNUNET_assert(NULL == run_task);
618 if (NULL == work_head) 616 if (NULL == work_head)
619 { 617 {
620 /* delay by at most 4h, at least 1s, and otherwise in between depending 618 /* delay by at most 4h, at least 1s, and otherwise in between depending
621 on how long it took to scan */ 619 on how long it took to scan */
622 delay = GNUNET_TIME_absolute_get_duration (start_time); 620 delay = GNUNET_TIME_absolute_get_duration(start_time);
623 delay = GNUNET_TIME_relative_saturating_multiply (delay, 100); 621 delay = GNUNET_TIME_relative_saturating_multiply(delay, 100);
624 delay = GNUNET_TIME_relative_min (delay, MAX_DELAY); 622 delay = GNUNET_TIME_relative_min(delay, MAX_DELAY);
625 delay = GNUNET_TIME_relative_max (delay, MIN_DELAY); 623 delay = GNUNET_TIME_relative_max(delay, MIN_DELAY);
626 run_task = GNUNET_SCHEDULER_add_delayed (delay, &scan, NULL); 624 run_task = GNUNET_SCHEDULER_add_delayed(delay, &scan, NULL);
627 } 625 }
628 else 626 else
629 { 627 {
630 run_task = GNUNET_SCHEDULER_add_now (&work, NULL); 628 run_task = GNUNET_SCHEDULER_add_now(&work, NULL);
631 } 629 }
632} 630}
633 631
634 632
@@ -641,29 +639,29 @@ schedule_next_task ()
641 * @param c configuration 639 * @param c configuration
642 */ 640 */
643static void 641static void
644run (void *cls, 642run(void *cls,
645 char *const *args, 643 char *const *args,
646 const char *cfgfile, 644 const char *cfgfile,
647 const struct GNUNET_CONFIGURATION_Handle *c) 645 const struct GNUNET_CONFIGURATION_Handle *c)
648{ 646{
649 /* check arguments */ 647 /* check arguments */
650 if ((NULL == args[0]) || (NULL != args[1]) || 648 if ((NULL == args[0]) || (NULL != args[1]) ||
651 (GNUNET_YES != GNUNET_DISK_directory_test (args[0], GNUNET_YES))) 649 (GNUNET_YES != GNUNET_DISK_directory_test(args[0], GNUNET_YES)))
652 { 650 {
653 printf (_ ( 651 printf(_(
654 "You must specify one and only one directory name for automatic publication.\n")); 652 "You must specify one and only one directory name for automatic publication.\n"));
655 ret = -1; 653 ret = -1;
656 return; 654 return;
657 } 655 }
658 cfg_filename = GNUNET_strdup (cfgfile); 656 cfg_filename = GNUNET_strdup(cfgfile);
659 cfg = c; 657 cfg = c;
660 dir_name = args[0]; 658 dir_name = args[0];
661 work_finished = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO); 659 work_finished = GNUNET_CONTAINER_multihashmap_create(1024, GNUNET_NO);
662 load_state (); 660 load_state();
663 run_task = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, 661 run_task = GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_IDLE,
664 &scan, 662 &scan,
665 NULL); 663 NULL);
666 GNUNET_SCHEDULER_add_shutdown (&do_stop_task, NULL); 664 GNUNET_SCHEDULER_add_shutdown(&do_stop_task, NULL);
667} 665}
668 666
669 667
@@ -676,12 +674,12 @@ run (void *cls,
676 * @return #GNUNET_OK to continue to iterate 674 * @return #GNUNET_OK to continue to iterate
677 */ 675 */
678static int 676static int
679free_item (void *cls, const struct GNUNET_HashCode *key, void *value) 677free_item(void *cls, const struct GNUNET_HashCode *key, void *value)
680{ 678{
681 struct WorkItem *wi = value; 679 struct WorkItem *wi = value;
682 680
683 GNUNET_free (wi->filename); 681 GNUNET_free(wi->filename);
684 GNUNET_free (wi); 682 GNUNET_free(wi);
685 return GNUNET_OK; 683 return GNUNET_OK;
686} 684}
687 685
@@ -694,89 +692,89 @@ free_item (void *cls, const struct GNUNET_HashCode *key, void *value)
694 * @return 0 ok, 1 on error 692 * @return 0 ok, 1 on error
695 */ 693 */
696int 694int
697main (int argc, char *const *argv) 695main(int argc, char *const *argv)
698{ 696{
699 struct GNUNET_GETOPT_CommandLineOption options[] = { 697 struct GNUNET_GETOPT_CommandLineOption options[] = {
700 698 GNUNET_GETOPT_option_uint('a',
701 GNUNET_GETOPT_option_uint ('a', 699 "anonymity",
702 "anonymity", 700 "LEVEL",
703 "LEVEL", 701 gettext_noop(
704 gettext_noop ( 702 "set the desired LEVEL of sender-anonymity"),
705 "set the desired LEVEL of sender-anonymity"), 703 &anonymity_level),
706 &anonymity_level), 704
707 705 GNUNET_GETOPT_option_flag(
708 GNUNET_GETOPT_option_flag (
709 'd', 706 'd',
710 "disable-creation-time", 707 "disable-creation-time",
711 gettext_noop ( 708 gettext_noop(
712 "disable adding the creation time to the metadata of the uploaded file"), 709 "disable adding the creation time to the metadata of the uploaded file"),
713 &do_disable_creation_time), 710 &do_disable_creation_time),
714 711
715 GNUNET_GETOPT_option_flag ( 712 GNUNET_GETOPT_option_flag(
716 'D', 713 'D',
717 "disable-extractor", 714 "disable-extractor",
718 gettext_noop ("do not use libextractor to add keywords or metadata"), 715 gettext_noop("do not use libextractor to add keywords or metadata"),
719 &disable_extractor), 716 &disable_extractor),
720 717
721 GNUNET_GETOPT_option_uint ('p', 718 GNUNET_GETOPT_option_uint('p',
722 "priority", 719 "priority",
723 "PRIORITY", 720 "PRIORITY",
724 gettext_noop ( 721 gettext_noop(
725 "specify the priority of the content"), 722 "specify the priority of the content"),
726 &content_priority), 723 &content_priority),
727 724
728 GNUNET_GETOPT_option_uint ('r', 725 GNUNET_GETOPT_option_uint('r',
729 "replication", 726 "replication",
730 "LEVEL", 727 "LEVEL",
731 gettext_noop ( 728 gettext_noop(
732 "set the desired replication LEVEL"), 729 "set the desired replication LEVEL"),
733 &replication_level), 730 &replication_level),
734 731
735 GNUNET_GETOPT_option_verbose (&verbose), 732 GNUNET_GETOPT_option_verbose(&verbose),
736 733
737 GNUNET_GETOPT_OPTION_END}; 734 GNUNET_GETOPT_OPTION_END
735 };
738 struct WorkItem *wi; 736 struct WorkItem *wi;
739 int ok; 737 int ok;
740 struct GNUNET_SIGNAL_Context *shc_chld; 738 struct GNUNET_SIGNAL_Context *shc_chld;
741 739
742 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 740 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
743 return 2; 741 return 2;
744 sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO); 742 sigpipe = GNUNET_DISK_pipe(GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
745 GNUNET_assert (NULL != sigpipe); 743 GNUNET_assert(NULL != sigpipe);
746 shc_chld = 744 shc_chld =
747 GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death); 745 GNUNET_SIGNAL_handler_install(GNUNET_SIGCHLD, &sighandler_child_death);
748 ok = 746 ok =
749 (GNUNET_OK == 747 (GNUNET_OK ==
750 GNUNET_PROGRAM_run ( 748 GNUNET_PROGRAM_run(
751 argc, 749 argc,
752 argv, 750 argv,
753 "gnunet-auto-share [OPTIONS] FILENAME", 751 "gnunet-auto-share [OPTIONS] FILENAME",
754 gettext_noop ("Automatically publish files from a directory on GNUnet"), 752 gettext_noop("Automatically publish files from a directory on GNUnet"),
755 options, 753 options,
756 &run, 754 &run,
757 NULL)) 755 NULL))
758 ? ret 756 ? ret
759 : 1; 757 : 1;
760 if (NULL != work_finished) 758 if (NULL != work_finished)
761 { 759 {
762 (void) GNUNET_CONTAINER_multihashmap_iterate (work_finished, 760 (void)GNUNET_CONTAINER_multihashmap_iterate(work_finished,
763 &free_item, 761 &free_item,
764 NULL); 762 NULL);
765 GNUNET_CONTAINER_multihashmap_destroy (work_finished); 763 GNUNET_CONTAINER_multihashmap_destroy(work_finished);
766 } 764 }
767 while (NULL != (wi = work_head)) 765 while (NULL != (wi = work_head))
768 { 766 {
769 GNUNET_CONTAINER_DLL_remove (work_head, work_tail, wi); 767 GNUNET_CONTAINER_DLL_remove(work_head, work_tail, wi);
770 GNUNET_free (wi->filename); 768 GNUNET_free(wi->filename);
771 GNUNET_free (wi); 769 GNUNET_free(wi);
772 } 770 }
773 GNUNET_SIGNAL_handler_uninstall (shc_chld); 771 GNUNET_SIGNAL_handler_uninstall(shc_chld);
774 shc_chld = NULL; 772 shc_chld = NULL;
775 GNUNET_DISK_pipe_close (sigpipe); 773 GNUNET_DISK_pipe_close(sigpipe);
776 sigpipe = NULL; 774 sigpipe = NULL;
777 GNUNET_free_non_null (cfg_filename); 775 GNUNET_free_non_null(cfg_filename);
778 cfg_filename = NULL; 776 cfg_filename = NULL;
779 GNUNET_free ((void *) argv); 777 GNUNET_free((void *)argv);
780 return ok; 778 return ok;
781} 779}
782 780