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.
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/>.
20 * @file topology/friends.c
21 * @brief library to read and write the FRIENDS file
22 * @author Christian Grothoff
25 #include "gnunet_friends_lib.h"
29 * Parse the FRIENDS file.
31 * @param cfg our configuration
32 * @param cb function to call on each friend found
33 * @param cb_cls closure for @a cb
34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
37 GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
38 GNUNET_FRIENDS_Callback cb,
45 struct GNUNET_PeerIdentity pid;
50 GNUNET_CONFIGURATION_get_value_filename (cfg,
55 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
61 GNUNET_DISK_directory_create_for_file (fn))
63 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
70 GNUNET_DISK_file_test (fn)) &&
72 GNUNET_DISK_fn_write (fn,
75 GNUNET_DISK_PERM_USER_READ |
76 GNUNET_DISK_PERM_USER_WRITE |
77 GNUNET_DISK_OPEN_CREATE)) )
78 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
82 GNUNET_DISK_file_size (fn,
91 data = GNUNET_malloc_large (fsize);
94 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
98 ssize = GNUNET_DISK_fn_read (fn,
102 (fsize != (uint64_t) ssize) )
104 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
109 return GNUNET_SYSERR;
115 while ( (pos < fsize) &&
116 (! isspace ((unsigned char) data[pos])) )
119 GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
124 _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
125 (unsigned long long) pos,
143 * Handle for writing a friends file.
145 struct GNUNET_FRIENDS_Writer
148 * Handle to the file.
150 struct GNUNET_DISK_FileHandle *fh;
155 * Start writing a fresh FRIENDS file. Will make a backup of the
158 * @param cfg configuration to use.
159 * @return NULL on error
161 struct GNUNET_FRIENDS_Writer *
162 GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
164 struct GNUNET_FRIENDS_Writer *w;
168 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
170 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
171 "topology", "FRIENDS");
175 GNUNET_DISK_directory_create_for_file (fn))
177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
178 _("Directory for file `%s' does not seem to be writable.\n"),
183 if (GNUNET_OK == GNUNET_DISK_file_test (fn))
184 GNUNET_DISK_file_backup (fn);
185 w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
186 w->fh = GNUNET_DISK_file_open (fn,
187 GNUNET_DISK_OPEN_CREATE |
188 GNUNET_DISK_OPEN_WRITE |
189 GNUNET_DISK_OPEN_FAILIFEXISTS,
190 GNUNET_DISK_PERM_USER_READ);
202 * Finish writing out the friends file.
204 * @param w write handle
205 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
208 GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
212 ret = GNUNET_DISK_file_close (w->fh);
219 * Add a friend to the friends file.
221 * @param w write handle
222 * @param friend_id friend to add
223 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
226 GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
227 const struct GNUNET_PeerIdentity *friend_id)
233 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key);
234 GNUNET_asprintf (&buf,
240 GNUNET_DISK_file_write (w->fh,
245 return GNUNET_SYSERR;
252 /* end of friends.c */