All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Calvin Wan <calvinwan@google.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 2/6] wrapper.h: move declarations for wrapper.c functions from git-compat-util.h
Date: Tue, 16 May 2023 15:08:40 -0700	[thread overview]
Message-ID: <xmqq353wszdj.fsf@gitster.g> (raw)
In-Reply-To: <20230516170932.1358685-3-calvinwan@google.com> (Calvin Wan's message of "Tue, 16 May 2023 17:09:27 +0000")

Calvin Wan <calvinwan@google.com> writes:

> Since the functions in wrapper.c are widely used across the codebase,
> include it by default in git-compat-util.h. A future patch will remove
> now unnecessary inclusions of wrapper.h from other files.

The description taken alone implies the move in the other direction
(i.e. taking decls from wrapper.h and moving them to
git-compat-util.h), but that the patch actually does makes sense.
Slim compat-util.h by moving decls for wrapper functions that are
defined in wrapper.c to wrapper.h, but to avoid impacting the users
right away, include wrapper.h in the compat-util.h that these users
expect to find these decls in.

> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
>  git-compat-util.h | 113 +---------------------------------------------
>  wrapper.h         | 111 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 112 insertions(+), 112 deletions(-)
>
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 51af0a53aa..9898fe9374 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -626,8 +626,7 @@ static inline int git_has_dir_sep(const char *path)
>  #include "compat/bswap.h"
>  
>  #include "wildmatch.h"
> -
> -struct strbuf;
> +#include "wrapper.h"
>  
>  /* General helper functions */
>  NORETURN void usage(const char *err);
> @@ -1047,36 +1046,6 @@ static inline int cast_size_t_to_int(size_t a)
>  # define xalloca(size)      (xmalloc(size))
>  # define xalloca_free(p)    (free(p))
>  #endif
> -char *xstrdup(const char *str);
> -void *xmalloc(size_t size);
> -void *xmallocz(size_t size);
> -void *xmallocz_gently(size_t size);
> -void *xmemdupz(const void *data, size_t len);
> -char *xstrndup(const char *str, size_t len);
> -void *xrealloc(void *ptr, size_t size);
> -void *xcalloc(size_t nmemb, size_t size);
> -void xsetenv(const char *name, const char *value, int overwrite);
> -void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> -const char *mmap_os_err(void);
> -void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> -int xopen(const char *path, int flags, ...);
> -ssize_t xread(int fd, void *buf, size_t len);
> -ssize_t xwrite(int fd, const void *buf, size_t len);
> -ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
> -int xdup(int fd);
> -FILE *xfopen(const char *path, const char *mode);
> -FILE *xfdopen(int fd, const char *mode);
> -int xmkstemp(char *temp_filename);
> -int xmkstemp_mode(char *temp_filename, int mode);
> -char *xgetcwd(void);
> -FILE *fopen_for_writing(const char *path);
> -FILE *fopen_or_warn(const char *path, const char *mode);
> -
> -/*
> - * Like strncmp, but only return zero if s is NUL-terminated and exactly len
> - * characters long.  If it is not, consider it greater than t.
> - */
> -int xstrncmpz(const char *s, const char *t, size_t len);
>  
>  /*
>   * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
> @@ -1178,15 +1147,10 @@ static inline size_t xsize_t(off_t len)
>  	return (size_t) len;
>  }
>  
> -__attribute__((format (printf, 3, 4)))
> -int xsnprintf(char *dst, size_t max, const char *fmt, ...);
> -
>  #ifndef HOST_NAME_MAX
>  #define HOST_NAME_MAX 256
>  #endif
>  
> -int xgethostname(char *buf, size_t len);
> -
>  /* in ctype.c, for kwset users */
>  extern const unsigned char tolower_trans_tbl[256];
>  
> @@ -1427,72 +1391,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
>  #endif
>  #endif
>  
> -enum fsync_action {
> -	FSYNC_WRITEOUT_ONLY,
> -	FSYNC_HARDWARE_FLUSH
> -};
> -
> -/*
> - * Issues an fsync against the specified file according to the specified mode.
> - *
> - * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
> - * systems to flush the OS cache without issuing a flush command to the storage
> - * controller. If those interfaces are unavailable, the function fails with
> - * ENOSYS.
> - *
> - * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
> - * changes are durable. It is not expected to fail.
> - */
> -int git_fsync(int fd, enum fsync_action action);
> -
> -/*
> - * Writes out trace statistics for fsync using the trace2 API.
> - */
> -void trace_git_fsync_stats(void);
> -
> -/*
> - * Preserves errno, prints a message, but gives no warning for ENOENT.
> - * Returns 0 on success, which includes trying to unlink an object that does
> - * not exist.
> - */
> -int unlink_or_warn(const char *path);
> - /*
> -  * Tries to unlink file.  Returns 0 if unlink succeeded
> -  * or the file already didn't exist.  Returns -1 and
> -  * appends a message to err suitable for
> -  * 'error("%s", err->buf)' on error.
> -  */
> -int unlink_or_msg(const char *file, struct strbuf *err);
> -/*
> - * Preserves errno, prints a message, but gives no warning for ENOENT.
> - * Returns 0 on success, which includes trying to remove a directory that does
> - * not exist.
> - */
> -int rmdir_or_warn(const char *path);
> -/*
> - * Calls the correct function out of {unlink,rmdir}_or_warn based on
> - * the supplied file mode.
> - */
> -int remove_or_warn(unsigned int mode, const char *path);
> -
> -/*
> - * Call access(2), but warn for any error except "missing file"
> - * (ENOENT or ENOTDIR).
> - */
> -#define ACCESS_EACCES_OK (1U << 0)
> -int access_or_warn(const char *path, int mode, unsigned flag);
> -int access_or_die(const char *path, int mode, unsigned flag);
> -
> -/* Warn on an inaccessible file if errno indicates this is an error */
> -int warn_on_fopen_errors(const char *path);
> -
> -/*
> - * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
> - * may be racy. Do not use this as protection against an attacker who can
> - * simultaneously create paths.
> - */
> -int open_nofollow(const char *path, int flags);
> -
>  #ifndef SHELL_PATH
>  # define SHELL_PATH "/bin/sh"
>  #endif
> @@ -1632,13 +1530,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
>  	((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
>  #endif /* !__GNUC__ */
>  
> -void sleep_millisec(int millisec);
> -
> -/*
> - * Generate len bytes from the system cryptographically secure PRNG.
> - * Returns 0 on success and -1 on error, setting errno.  The inability to
> - * satisfy the full request is an error.
> - */
> -int csprng_bytes(void *buf, size_t len);
> -
>  #endif
> diff --git a/wrapper.h b/wrapper.h
> index f0c7d0616d..c85b1328d1 100644
> --- a/wrapper.h
> +++ b/wrapper.h
> @@ -1,6 +1,42 @@
>  #ifndef WRAPPER_H
>  #define WRAPPER_H
>  
> +char *xstrdup(const char *str);
> +void *xmalloc(size_t size);
> +void *xmallocz(size_t size);
> +void *xmallocz_gently(size_t size);
> +void *xmemdupz(const void *data, size_t len);
> +char *xstrndup(const char *str, size_t len);
> +void *xrealloc(void *ptr, size_t size);
> +void *xcalloc(size_t nmemb, size_t size);
> +void xsetenv(const char *name, const char *value, int overwrite);
> +void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> +const char *mmap_os_err(void);
> +void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> +int xopen(const char *path, int flags, ...);
> +ssize_t xread(int fd, void *buf, size_t len);
> +ssize_t xwrite(int fd, const void *buf, size_t len);
> +ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
> +int xdup(int fd);
> +FILE *xfopen(const char *path, const char *mode);
> +FILE *xfdopen(int fd, const char *mode);
> +int xmkstemp(char *temp_filename);
> +int xmkstemp_mode(char *temp_filename, int mode);
> +char *xgetcwd(void);
> +FILE *fopen_for_writing(const char *path);
> +FILE *fopen_or_warn(const char *path, const char *mode);
> +
> +/*
> + * Like strncmp, but only return zero if s is NUL-terminated and exactly len
> + * characters long.  If it is not, consider it greater than t.
> + */
> +int xstrncmpz(const char *s, const char *t, size_t len);
> +
> +__attribute__((format (printf, 3, 4)))
> +int xsnprintf(char *dst, size_t max, const char *fmt, ...);
> +
> +int xgethostname(char *buf, size_t len);
> +
>  /* set default permissions by passing mode arguments to open(2) */
>  int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
>  int git_mkstemp_mode(char *pattern, int mode);
> @@ -33,4 +69,79 @@ void write_file(const char *path, const char *fmt, ...);
>  /* Return 1 if the file is empty or does not exists, 0 otherwise. */
>  int is_empty_or_missing_file(const char *filename);
>  
> +enum fsync_action {
> +	FSYNC_WRITEOUT_ONLY,
> +	FSYNC_HARDWARE_FLUSH
> +};
> +
> +/*
> + * Issues an fsync against the specified file according to the specified mode.
> + *
> + * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
> + * systems to flush the OS cache without issuing a flush command to the storage
> + * controller. If those interfaces are unavailable, the function fails with
> + * ENOSYS.
> + *
> + * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
> + * changes are durable. It is not expected to fail.
> + */
> +int git_fsync(int fd, enum fsync_action action);
> +
> +/*
> + * Writes out trace statistics for fsync using the trace2 API.
> + */
> +void trace_git_fsync_stats(void);
> +
> +/*
> + * Preserves errno, prints a message, but gives no warning for ENOENT.
> + * Returns 0 on success, which includes trying to unlink an object that does
> + * not exist.
> + */
> +int unlink_or_warn(const char *path);
> + /*
> +  * Tries to unlink file.  Returns 0 if unlink succeeded
> +  * or the file already didn't exist.  Returns -1 and
> +  * appends a message to err suitable for
> +  * 'error("%s", err->buf)' on error.
> +  */
> +int unlink_or_msg(const char *file, struct strbuf *err);
> +/*
> + * Preserves errno, prints a message, but gives no warning for ENOENT.
> + * Returns 0 on success, which includes trying to remove a directory that does
> + * not exist.
> + */
> +int rmdir_or_warn(const char *path);
> +/*
> + * Calls the correct function out of {unlink,rmdir}_or_warn based on
> + * the supplied file mode.
> + */
> +int remove_or_warn(unsigned int mode, const char *path);
> +
> +/*
> + * Call access(2), but warn for any error except "missing file"
> + * (ENOENT or ENOTDIR).
> + */
> +#define ACCESS_EACCES_OK (1U << 0)
> +int access_or_warn(const char *path, int mode, unsigned flag);
> +int access_or_die(const char *path, int mode, unsigned flag);
> +
> +/* Warn on an inaccessible file if errno indicates this is an error */
> +int warn_on_fopen_errors(const char *path);
> +
> +/*
> + * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
> + * may be racy. Do not use this as protection against an attacker who can
> + * simultaneously create paths.
> + */
> +int open_nofollow(const char *path, int flags);
> +
> +void sleep_millisec(int millisec);
> +
> +/*
> + * Generate len bytes from the system cryptographically secure PRNG.
> + * Returns 0 on success and -1 on error, setting errno.  The inability to
> + * satisfy the full request is an error.
> + */
> +int csprng_bytes(void *buf, size_t len);
> +
>  #endif /* WRAPPER_H */

  reply	other threads:[~2023-05-16 22:08 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
2023-05-16 17:09 ` [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
2023-05-16 21:18   ` Junio C Hamano
2023-05-16 17:09 ` [PATCH 2/6] wrapper.h: move declarations for wrapper.c " Calvin Wan
2023-05-16 22:08   ` Junio C Hamano [this message]
2023-05-16 17:09 ` [PATCH 3/6] common.h: move non-compat specific macros and " Calvin Wan
2023-05-16 22:30   ` Junio C Hamano
2023-05-17 17:26     ` Calvin Wan
2023-05-16 17:09 ` [PATCH 4/6] usage.h: move declarations for usage.c " Calvin Wan
2023-05-16 17:09 ` [PATCH 5/6] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-05-16 17:09 ` [PATCH 6/6] common: move alloc macros to common.h Calvin Wan
2023-05-16 17:54 ` [PATCH 0/6] git-compat-util cleanups Junio C Hamano
2023-05-16 19:00   ` Calvin Wan
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
2023-05-23 19:29   ` [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
2023-05-26 21:06     ` Jonathan Tan
2023-05-23 19:29   ` [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c " Calvin Wan
2023-05-26 21:25     ` Jonathan Tan
2023-05-23 19:29   ` [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros " Calvin Wan
2023-05-26 21:19     ` Jonathan Tan
2023-05-28 12:04       ` Phillip Wood
2023-05-30 17:29         ` Jonathan Tan
2023-06-01  4:12       ` Junio C Hamano
2023-05-23 19:29   ` [PATCH v2 4/7] common.h: move non-compat specific macros and functions " Calvin Wan
2023-05-23 19:29   ` [PATCH v2 5/7] usage.h: move declarations for usage.c " Calvin Wan
2023-05-23 19:29   ` [PATCH v2 6/7] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-05-23 19:29   ` [PATCH v2 7/7] common: move alloc macros to common.h Calvin Wan
2023-05-26 21:27     ` Jonathan Tan
2023-05-26 21:03   ` [PATCH v2 0/7] git-compat-util cleanups Jonathan Tan
2023-06-06 17:07   ` [PATCH v3 0/8] " Calvin Wan
2023-06-06 17:09     ` [PATCH v3 1/8] git-compat-util: move strbuf.c funcs to its header Calvin Wan
2023-06-06 17:09     ` [PATCH v3 2/8] git-compat-util: move wrapper.c " Calvin Wan
2023-06-06 17:09     ` [PATCH v3 3/8] sane-ctype.h: create header for sane-ctype macros Calvin Wan
2023-06-06 17:09     ` [PATCH v3 4/8] kwset: move translation table from ctype Calvin Wan
2023-06-06 17:09     ` [PATCH v3 5/8] common.h: move non-compat specific macros and functions Calvin Wan
2023-06-06 22:45       ` Ramsay Jones
2023-06-07 17:02         ` Calvin Wan
2023-06-12 20:48           ` Junio C Hamano
2023-06-13 22:42             ` Calvin Wan
2023-06-13 23:10               ` Junio C Hamano
2023-06-14  1:58                 ` Calvin Wan
2023-06-06 17:10     ` [PATCH v3 6/8] git-compat-util: move usage.c funcs to its header Calvin Wan
2023-06-06 17:10     ` [PATCH v3 7/8] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-06-06 17:10     ` [PATCH v3 8/8] common: move alloc macros to common.h Calvin Wan
2023-06-30 20:22     ` [PATCH v4 0/6] git-compat-util cleanups Calvin Wan
2023-06-30 20:23       ` [PATCH v4 1/6] git-compat-util: move strbuf.c funcs to its header Calvin Wan
2023-06-30 20:23       ` [PATCH v4 2/6] git-compat-util: move wrapper.c " Calvin Wan
2023-06-30 20:23       ` [PATCH v4 3/6] sane-ctype.h: create header for sane-ctype macros Calvin Wan
2023-06-30 20:23       ` [PATCH v4 4/6] kwset: move translation table from ctype Calvin Wan
2023-06-30 20:23       ` [PATCH v4 5/6] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-06-30 20:23       ` [PATCH v4 6/6] common: move alloc macros to common.h Calvin Wan
2023-06-30 22:49         ` Junio C Hamano
2023-07-05 16:25           ` Calvin Wan
2023-07-05 17:33             ` Junio C Hamano
2023-07-05 17:58               ` Calvin Wan
2023-06-30 21:56       ` [PATCH v4 0/6] git-compat-util cleanups Junio C Hamano
2023-07-05 17:08       ` [PATCH v5 " Calvin Wan
2023-07-05 17:09         ` [PATCH v5 1/6] git-compat-util: move strbuf.c funcs to its header Calvin Wan
2023-07-05 17:09         ` [PATCH v5 2/6] git-compat-util: move wrapper.c " Calvin Wan
2023-07-05 17:09         ` [PATCH v5 3/6] sane-ctype.h: create header for sane-ctype macros Calvin Wan
2023-07-05 17:09         ` [PATCH v5 4/6] kwset: move translation table from ctype Calvin Wan
2023-07-05 17:09         ` [PATCH v5 5/6] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-07-05 17:09         ` [PATCH v5 6/6] git-compat-util: move alloc macros to git-compat-util.h Calvin Wan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq353wszdj.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=calvinwan@google.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.