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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file topology/friends.c
23 * @brief library to read and write the FRIENDS file
24 * @author Christian Grothoff
27 #include "gnunet_friends_lib.h"
31 * Parse the FRIENDS file.
33 * @param cfg our configuration
34 * @param cb function to call on each friend found
35 * @param cb_cls closure for @a cb
36 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
39 GNUNET_FRIENDS_parse(const struct GNUNET_CONFIGURATION_Handle *cfg,
40 GNUNET_FRIENDS_Callback cb,
47 struct GNUNET_PeerIdentity pid;
52 GNUNET_CONFIGURATION_get_value_filename(cfg,
57 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
63 GNUNET_DISK_directory_create_for_file(fn))
65 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
72 GNUNET_DISK_file_test(fn)) &&
74 GNUNET_DISK_fn_write(fn,
77 GNUNET_DISK_PERM_USER_READ |
78 GNUNET_DISK_PERM_USER_WRITE |
79 GNUNET_DISK_OPEN_CREATE)))
80 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
84 GNUNET_DISK_file_size(fn,
93 data = GNUNET_malloc_large(fsize);
96 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc");
100 ssize = GNUNET_DISK_fn_read(fn,
104 (fsize != (uint64_t)ssize))
106 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR,
111 return GNUNET_SYSERR;
117 while ((pos < fsize) &&
118 (!isspace((unsigned char)data[pos])))
121 GNUNET_CRYPTO_eddsa_public_key_from_string(&data[start],
125 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
126 _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
127 (unsigned long long)pos,
145 * Handle for writing a friends file.
147 struct GNUNET_FRIENDS_Writer {
149 * Handle to the file.
151 struct GNUNET_DISK_FileHandle *fh;
156 * Start writing a fresh FRIENDS file. Will make a backup of the
159 * @param cfg configuration to use.
160 * @return NULL on error
162 struct GNUNET_FRIENDS_Writer *
163 GNUNET_FRIENDS_write_start(const struct GNUNET_CONFIGURATION_Handle *cfg)
165 struct GNUNET_FRIENDS_Writer *w;
169 GNUNET_CONFIGURATION_get_value_filename(cfg, "TOPOLOGY", "FRIENDS", &fn))
171 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
172 "topology", "FRIENDS");
176 GNUNET_DISK_directory_create_for_file(fn))
178 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
179 _("Directory for file `%s' does not seem to be writable.\n"),
184 if (GNUNET_OK == GNUNET_DISK_file_test(fn))
185 GNUNET_DISK_file_backup(fn);
186 w = GNUNET_new(struct GNUNET_FRIENDS_Writer);
187 w->fh = GNUNET_DISK_file_open(fn,
188 GNUNET_DISK_OPEN_CREATE |
189 GNUNET_DISK_OPEN_WRITE |
190 GNUNET_DISK_OPEN_FAILIFEXISTS,
191 GNUNET_DISK_PERM_USER_READ);
203 * Finish writing out the friends file.
205 * @param w write handle
206 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
209 GNUNET_FRIENDS_write_stop(struct GNUNET_FRIENDS_Writer *w)
213 ret = GNUNET_DISK_file_close(w->fh);
220 * Add a friend to the friends file.
222 * @param w write handle
223 * @param friend_id friend to add
224 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
227 GNUNET_FRIENDS_write(struct GNUNET_FRIENDS_Writer *w,
228 const struct GNUNET_PeerIdentity *friend_id)
234 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&friend_id->public_key);
235 GNUNET_asprintf(&buf,
241 GNUNET_DISK_file_write(w->fh,
246 return GNUNET_SYSERR;
253 /* end of friends.c */