From 3636ea628d051cf2ba7a9038c50528c561d0aeaa Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 19 Dec 2020 18:43:38 +0100 Subject: change GNUNET_DISK_fn_write() to always do atomic writes and to NOT overwrite existing files; also change the return value to not return the size of the written file but GNUNET_OK on success, and integrate creating the directory if needed; breaks API, hence bumping libgnunetutil version --- src/include/gnunet_common.h | 2 +- src/include/gnunet_configuration_lib.h | 59 +++++++++++++++++----------------- src/include/gnunet_crypto_lib.h | 30 ++++++++--------- src/include/gnunet_disk_lib.h | 46 ++++++++++++++------------ 4 files changed, 71 insertions(+), 66 deletions(-) (limited to 'src/include') diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index bc2055709..b5af1146d 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h @@ -345,7 +345,7 @@ struct GNUNET_AsyncScopeSave * #GNUNET_NO to stop iteration with no error, * #GNUNET_SYSERR to abort iteration with error! */ -typedef int +typedef enum GNUNET_GenericReturnValue (*GNUNET_FileNameCallback) (void *cls, const char *filename); diff --git a/src/include/gnunet_configuration_lib.h b/src/include/gnunet_configuration_lib.h index b4e7f7d8f..ad55c1b9a 100644 --- a/src/include/gnunet_configuration_lib.h +++ b/src/include/gnunet_configuration_lib.h @@ -82,7 +82,7 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg); * @param filename name of the configuration file, NULL to load defaults * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename); @@ -95,7 +95,7 @@ GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, * @param defaults_d directory with the defaults * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, const char *defaults_d); @@ -120,7 +120,7 @@ GNUNET_CONFIGURATION_default (void); * @param filename name of the configuration file * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename); @@ -149,7 +149,7 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, * when we come across them * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, @@ -163,19 +163,20 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg, * @param filename where to write the configuration * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename); /** * Write only configuration entries that have been changed to configuration file + * * @param cfg_default default configuration * @param cfg_new new configuration * @param filename where to write the configuration diff between default and new * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct @@ -204,7 +205,7 @@ GNUNET_CONFIGURATION_get_diff (const struct * @param cfg configuration to inspect * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed) */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg); @@ -215,7 +216,7 @@ GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg); * @param cfg the configuration * @return status code */ -typedef int +typedef enum GNUNET_GenericReturnValue (*GNUNET_CONFIGURATION_Callback)(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg); @@ -231,7 +232,7 @@ typedef int * @return #GNUNET_SYSERR if parsing the configuration failed, * otherwise return value from @a cb. */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse_and_run (const char *filename, GNUNET_CONFIGURATION_Callback cb, void *cb_cls); @@ -311,7 +312,7 @@ GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, * @param number where to store the numeric value of the option * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -328,7 +329,7 @@ GNUNET_CONFIGURATION_get_value_number (const struct * @param number where to store the floating value of the option * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -345,7 +346,7 @@ GNUNET_CONFIGURATION_get_value_float (const struct * @param time set to the time value stored in the configuration * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -362,7 +363,7 @@ GNUNET_CONFIGURATION_get_value_time (const struct * @param size set to the size in bytes as stored in the configuration * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -378,7 +379,7 @@ GNUNET_CONFIGURATION_get_value_size (const struct * @param option option of interest * @return #GNUNET_YES if so, #GNUNET_NO if not. */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option); @@ -394,12 +395,12 @@ GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, * value, or NULL if option is not specified * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int -GNUNET_CONFIGURATION_get_value_string (const struct - GNUNET_CONFIGURATION_Handle *cfg, - const char *section, - const char *option, - char **value); +enum GNUNET_GenericReturnValue +GNUNET_CONFIGURATION_get_value_string ( + const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *section, + const char *option, + char **value); /** @@ -413,7 +414,7 @@ GNUNET_CONFIGURATION_get_value_string (const struct * value, or NULL if option is not specified * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -466,7 +467,7 @@ GNUNET_CONFIGURATION_iterate_section_values (const struct * or NULL if option is not specified and no default given * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -483,7 +484,7 @@ GNUNET_CONFIGURATION_get_value_choice (const struct * @param option option of interest * @return #GNUNET_YES, #GNUNET_NO or if option has no valid value, #GNUNET_SYSERR */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -502,7 +503,7 @@ GNUNET_CONFIGURATION_get_value_yesno (const struct * #GNUNET_NO is the value does not exist * #GNUNET_SYSERR on decoding error */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, @@ -527,9 +528,9 @@ GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg, * @return $-expanded string */ char * -GNUNET_CONFIGURATION_expand_dollar (const struct - GNUNET_CONFIGURATION_Handle *cfg, - char *orig); +GNUNET_CONFIGURATION_expand_dollar ( + const struct GNUNET_CONFIGURATION_Handle *cfg, + char *orig); /** @@ -573,7 +574,7 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, * @return #GNUNET_OK on success, * #GNUNET_SYSERR if the filename is not in the list */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, @@ -592,7 +593,7 @@ GNUNET_CONFIGURATION_remove_value_filename (struct * @return #GNUNET_OK on success, * #GNUNET_SYSERR if the filename already in the list */ -int +enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 6dc56f0e4..5521dad3c 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -1023,7 +1023,7 @@ GNUNET_CRYPTO_hmac_derive_key ( * @param ... pair of void * & size_t for context chunks, terminated by NULL * @return #GNUNET_YES on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, @@ -1049,7 +1049,7 @@ GNUNET_CRYPTO_hkdf (void *result, * @param argp va_list of void * & size_t pairs for context chunks * @return #GNUNET_YES on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, @@ -1072,7 +1072,7 @@ GNUNET_CRYPTO_hkdf_v (void *result, * @param argp va_list of void * & size_t pairs for context chunks * @return #GNUNET_YES on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf_v (void *result, size_t out_len, const void *xts, @@ -1116,7 +1116,7 @@ GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r, * @param ... void * & size_t pairs for context chunks * @return #GNUNET_YES on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf (void *result, size_t out_len, const void *xts, @@ -1215,7 +1215,7 @@ GNUNET_CRYPTO_eddsa_public_key_to_string ( * @param pub where to store the public key * @return #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_public_key_from_string ( const char *enc, size_t enclen, @@ -1245,7 +1245,7 @@ GNUNET_CRYPTO_eddsa_private_key_from_string ( * @param pub where to store the public key * @return #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_public_key_from_string ( const char *enc, size_t enclen, @@ -1267,7 +1267,7 @@ GNUNET_CRYPTO_eddsa_public_key_from_string ( * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but * we found an existing file, #GNUNET_SYSERR on failure */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_key_from_file (const char *filename, int do_create, struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey); @@ -1606,7 +1606,7 @@ GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc); * @param key_material where to write the key material (xyG) * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material); @@ -1622,7 +1622,7 @@ GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, * @param key_material where to write the key material H(h(x)yG) * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material); @@ -1637,7 +1637,7 @@ GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, * @param key_material where to write the key material H(h(x)yG) * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material); @@ -1653,7 +1653,7 @@ GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, * @param key_material where to write the key material H(yX)=H(h(x)yG) * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material); @@ -1668,7 +1668,7 @@ GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, * @param key_material where to write the key material H(yX)=H(h(x)yG) * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, struct GNUNET_HashCode *key_material); @@ -1689,7 +1689,7 @@ GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, * @param[out] sig where to write the signature * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_sign_ ( const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, @@ -1736,7 +1736,7 @@ GNUNET_CRYPTO_eddsa_sign_ ( * @param[out] sig where to write the signature * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdsa_sign_ ( const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, @@ -2333,7 +2333,7 @@ GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig, * @param public_key public key of the signer * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature */ -int +enum GNUNET_GenericReturnValue GNUNET_CRYPTO_rsa_verify (const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_RsaSignature *sig, const struct GNUNET_CRYPTO_RsaPublicKey *public_key); diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h index 3f29dc2e4..b794088c4 100644 --- a/src/include/gnunet_disk_lib.h +++ b/src/include/gnunet_disk_lib.h @@ -251,7 +251,7 @@ enum GNUNET_DISK_PipeEnd * @param h handle to check * @return #GNUNET_YES if invalid, #GNUNET_NO if valid */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h); @@ -263,7 +263,7 @@ GNUNET_DISK_handle_invalid (const struct GNUNET_DISK_FileHandle *h); * @return #GNUNET_YES if yes, #GNUNET_NO if not a file, #GNUNET_SYSERR if something * else (will print an error message in that case, too). */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_test (const char *fil); @@ -304,7 +304,7 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, * and return #GNUNET_SYSERR for directories. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_size (const char *filename, uint64_t *size, int include_symbolic_links, @@ -326,7 +326,7 @@ GNUNET_DISK_file_size (const char *filename, * @param ino set to the inode ID * @return #GNUNET_OK on success */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_get_identifiers (const char *filename, uint64_t *dev, uint64_t *ino); @@ -385,7 +385,7 @@ GNUNET_DISK_file_open (const char *fn, * @param size where to write size of the file * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size (struct GNUNET_DISK_FileHandle *fh, off_t *size); @@ -447,7 +447,7 @@ GNUNET_DISK_pipe_from_fd (enum GNUNET_DISK_PipeFlags pf, * @param p pipe * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p); @@ -458,7 +458,7 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p); * @param end which end of the pipe to close * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end); @@ -485,7 +485,7 @@ GNUNET_DISK_pipe_detach_end (struct GNUNET_DISK_PipeHandle *p, * @param h file handle * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h); @@ -609,16 +609,19 @@ GNUNET_DISK_file_write_blocking (const struct GNUNET_DISK_FileHandle *h, /** - * Write a buffer to a file. If the file is longer than - * the given buffer size, it will be truncated. + * Write a buffer to a file atomically. The directory is created if + * necessary. Fail if @a filename already exists or if not exactly @a buf + * with @a buf_size bytes could be written to @a filename. * * @param fn file name * @param buffer the data to write * @param n number of bytes to write * @param mode file permissions - * @return number of bytes written on success, #GNUNET_SYSERR on error + * @return #GNUNET_OK on success, + * #GNUNET_NO if a file existed under @a filename + * #GNUNET_SYSERR on failure */ -ssize_t +enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write (const char *fn, const void *buffer, size_t n, @@ -632,7 +635,7 @@ GNUNET_DISK_fn_write (const char *fn, * @param dst destination file name * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_copy (const char *src, const char *dst); @@ -659,7 +662,7 @@ GNUNET_DISK_directory_scan (const char *dir_name, * @returns #GNUNET_OK on success, #GNUNET_SYSERR on failure, * #GNUNET_NO if directory exists but is not writeable */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file (const char *filename); @@ -675,8 +678,9 @@ GNUNET_DISK_directory_create_for_file (const char *filename); * @return #GNUNET_YES if yes, #GNUNET_NO if not; #GNUNET_SYSERR if it * does not exist or `stat`ed */ -int -GNUNET_DISK_directory_test (const char *fil, int is_readable); +enum GNUNET_GenericReturnValue +GNUNET_DISK_directory_test (const char *fil, + int is_readable); /** @@ -685,7 +689,7 @@ GNUNET_DISK_directory_test (const char *fil, int is_readable); * @param filename the file to remove * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove (const char *filename); @@ -707,7 +711,7 @@ GNUNET_DISK_purge_cfg_dir (const char *cfg_filename, * @param dir the directory to create * @returns #GNUNET_SYSERR on failure, #GNUNET_OK otherwise */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create (const char *dir); @@ -725,7 +729,7 @@ GNUNET_DISK_filename_canonicalize (char *fn); * @param user new owner of the file * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_change_owner (const char *filename, const char *user); @@ -758,7 +762,7 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h, * @param h mapping handle * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h); @@ -768,7 +772,7 @@ GNUNET_DISK_file_unmap (struct GNUNET_DISK_MapHandle *h); * @param h handle to an open file * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ -int +enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h); -- cgit v1.2.3