2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff
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.
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.
17 * @file topology/friends.c
18 * @brief library to read and write the FRIENDS file
19 * @author Christian Grothoff
22 #include "gnunet_friends_lib.h"
26 * Parse the FRIENDS file.
28 * @param cfg our configuration
29 * @param cb function to call on each friend found
30 * @param cb_cls closure for @a cb
31 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
34 GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
35 GNUNET_FRIENDS_Callback cb,
42 struct GNUNET_PeerIdentity pid;
47 GNUNET_CONFIGURATION_get_value_filename (cfg,
52 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
58 GNUNET_DISK_directory_create_for_file (fn))
60 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
67 GNUNET_DISK_file_test (fn)) &&
69 GNUNET_DISK_fn_write (fn,
72 GNUNET_DISK_PERM_USER_READ |
73 GNUNET_DISK_PERM_USER_WRITE |
74 GNUNET_DISK_OPEN_CREATE)) )
75 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
79 GNUNET_DISK_file_size (fn,
88 data = GNUNET_malloc_large (fsize);
91 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
95 ssize = GNUNET_DISK_fn_read (fn,
99 (fsize != (uint64_t) ssize) )
101 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
106 return GNUNET_SYSERR;
112 while ( (pos < fsize) &&
113 (! isspace ((unsigned char) data[pos])) )
116 GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
121 _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
122 (unsigned long long) pos,
140 * Handle for writing a friends file.
142 struct GNUNET_FRIENDS_Writer
145 * Handle to the file.
147 struct GNUNET_DISK_FileHandle *fh;
152 * Start writing a fresh FRIENDS file. Will make a backup of the
155 * @param cfg configuration to use.
156 * @return NULL on error
158 struct GNUNET_FRIENDS_Writer *
159 GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
161 struct GNUNET_FRIENDS_Writer *w;
165 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
167 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
168 "topology", "FRIENDS");
172 GNUNET_DISK_directory_create_for_file (fn))
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
175 _("Directory for file `%s' does not seem to be writable.\n"),
180 if (GNUNET_OK == GNUNET_DISK_file_test (fn))
181 GNUNET_DISK_file_backup (fn);
182 w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
183 w->fh = GNUNET_DISK_file_open (fn,
184 GNUNET_DISK_OPEN_CREATE |
185 GNUNET_DISK_OPEN_WRITE |
186 GNUNET_DISK_OPEN_FAILIFEXISTS,
187 GNUNET_DISK_PERM_USER_READ);
199 * Finish writing out the friends file.
201 * @param w write handle
202 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
205 GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
209 ret = GNUNET_DISK_file_close (w->fh);
216 * Add a friend to the friends file.
218 * @param w write handle
219 * @param friend_id friend to add
220 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
223 GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
224 const struct GNUNET_PeerIdentity *friend_id)
230 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key);
231 GNUNET_asprintf (&buf,
237 GNUNET_DISK_file_write (w->fh,
242 return GNUNET_SYSERR;
249 /* end of friends.c */