GNUnet debian-0.24.3-24-gfea921bd2
gnunet-service-messenger_list_messages.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2025 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
27
28#include "gnunet_util_lib.h"
29
30void
32{
33 GNUNET_assert (messages);
34
35 messages->head = NULL;
36 messages->tail = NULL;
37}
38
39
40void
42{
43 GNUNET_assert (messages);
44
45 while (messages->head)
46 {
47 struct GNUNET_MESSENGER_ListMessage *element = messages->head;
48
49 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
50 GNUNET_free (element);
51 }
52
53 messages->head = NULL;
54 messages->tail = NULL;
55}
56
57
58void
60 const struct GNUNET_HashCode *hash)
61{
62 struct GNUNET_MESSENGER_ListMessage *element;
63
64 GNUNET_assert ((messages) && (hash));
65
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Add new message to list: %s\n",
68
70
71 GNUNET_memcpy (&(element->hash), hash, sizeof(struct GNUNET_HashCode));
72
73 GNUNET_CONTAINER_DLL_insert_tail (messages->head, messages->tail, element);
74}
75
76
77void
80{
81 struct GNUNET_MESSENGER_ListMessage *element;
82
83 GNUNET_assert ((messages) && (origin));
84
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copy messages from list!\n");
86
87 for (element = origin->head; element; element = element->next)
88 add_to_list_messages (messages, &(element->hash));
89}
90
91
92void
94 const struct GNUNET_HashCode *hash)
95{
96 struct GNUNET_MESSENGER_ListMessage *element;
97
98 GNUNET_assert ((messages) && (hash));
99
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Remove message from list: %s\n",
101 GNUNET_h2s (hash));
102
103 for (element = messages->head; element; element = element->next)
104 if (0 == GNUNET_CRYPTO_hash_cmp (&(element->hash), hash))
105 {
106 GNUNET_CONTAINER_DLL_remove (messages->head, messages->tail, element);
107 GNUNET_free (element);
108 break;
109 }
110}
111
112
113void
115 const char *path)
116{
118 struct GNUNET_HashCode hash;
119 ssize_t len;
120
121 GNUNET_assert ((messages) && (path));
122
123 if (GNUNET_YES != GNUNET_DISK_file_test (path))
124 return;
125
126 {
127 enum GNUNET_DISK_AccessPermissions permission;
128
130
132 }
133
134 if (! handle)
135 return;
136
138
139 do {
140 len = GNUNET_DISK_file_read (handle, &hash, sizeof(hash));
141
142 if (len != sizeof(hash))
143 break;
144
145 add_to_list_messages (messages, &hash);
146 } while (len == sizeof(hash));
147
149}
150
151
152void
154 const char *path)
155{
157
158 GNUNET_assert ((messages) && (path));
159
160 {
161 enum GNUNET_DISK_AccessPermissions permission;
162
164
167 }
168
169 if (! handle)
170 return;
171
173
174 {
175 struct GNUNET_MESSENGER_ListMessage *element;
176
177 for (element = messages->head; element; element = element->next)
178 GNUNET_DISK_file_write (handle, &(element->hash), sizeof(element->hash));
179 }
180
183}
static char origin[GNUNET_DNSPARSER_MAX_NAME_LENGTH]
Current origin.
void remove_from_list_messages(struct GNUNET_MESSENGER_ListMessages *messages, const struct GNUNET_HashCode *hash)
Removes the first entry with a matching hash from the list.
void clear_list_messages(struct GNUNET_MESSENGER_ListMessages *messages)
Clears the list of message hashes.
void load_list_messages(struct GNUNET_MESSENGER_ListMessages *messages, const char *path)
Loads the list of message hashes from a file under a given path.
void init_list_messages(struct GNUNET_MESSENGER_ListMessages *messages)
Initializes list of message hashes as empty list.
void copy_list_messages(struct GNUNET_MESSENGER_ListMessages *messages, const struct GNUNET_MESSENGER_ListMessages *origin)
Copies all message hashes from an origin to another list.
void save_list_messages(const struct GNUNET_MESSENGER_ListMessages *messages, const char *path)
Saves the list of message hashes to a file under a given path.
void add_to_list_messages(struct GNUNET_MESSENGER_ListMessages *messages, const struct GNUNET_HashCode *hash)
Adds a specific hash from a message to the end of the list.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition: gnunet-vpn.c:35
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1258
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:533
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:710
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:219
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
Definition: disk.c:1451
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1332
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:673
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:218
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
Handle used to access files (and pipes).
A 512-bit hashcode.
struct GNUNET_MESSENGER_ListMessage * next
struct GNUNET_MESSENGER_ListMessage * head
struct GNUNET_MESSENGER_ListMessage * tail