/ libpkg / private / utils.h
utils.h
  1  /*-
  2   * Copyright (c) 2011-2025 Baptiste Daroussin <bapt@FreeBSD.org>
  3   * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
  4   * Copyright (c) 2023 Serenity Cyber Security, LLC
  5   *                    Author: Gleb Popov <arrowd@FreeBSD.org>
  6   * All rights reserved.
  7   *
  8   * SPDX-License-Identifier: BSD-2-Clause
  9   */
 10  
 11  #ifndef _PKG_UTIL_H
 12  #define _PKG_UTIL_H
 13  
 14  #include <sys/types.h>
 15  #include <sys/stat.h>
 16  #include <sys/param.h>
 17  #include <ucl.h>
 18  #include <pkg.h>
 19  #include <xstring.h>
 20  
 21  #define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)
 22  #define RELATIVE_PATH(p) (&p[strspn(p, "/")])
 23  
 24  #define ERROR_SQLITE(db, query) do { \
 25  	pkg_emit_error("sqlite error while executing %s in file %s:%d: %s", query, \
 26  	__FILE__, __LINE__, sqlite3_errmsg(db)); \
 27  } while(0)
 28  
 29  #define ERROR_STMT_SQLITE(db, statement) do { \
 30  	pkg_emit_error("sqlite error while executing %s in file %s:%d: %s", sqlite3_expanded_sql(statement), \
 31  	__FILE__, __LINE__, sqlite3_errmsg(db)); \
 32  } while (0)
 33  
 34  struct hardlink {
 35  	ino_t ino;
 36  	dev_t dev;
 37  	const char *path;
 38  };
 39  typedef vec_t(struct hardlink *) hardlinks_t;
 40  
 41  struct tempdir {
 42  	char name[PATH_MAX];
 43  	char temp[PATH_MAX];
 44  	size_t len;
 45  	int fd;
 46  };
 47  typedef vec_t(struct tempdir *) tempdirs_t;
 48  
 49  struct dns_srvinfo {
 50  	unsigned int type;
 51  	unsigned int class;
 52  	unsigned int ttl;
 53  	unsigned int priority;
 54  	unsigned int weight;
 55  	unsigned int port;
 56  	unsigned int finalweight;
 57  	char host[MAXHOSTNAMELEN];
 58  	struct dns_srvinfo *next;
 59  };
 60  
 61  int file_to_buffer(const char *, char **, off_t *);
 62  int file_to_bufferat(int, const char *, char **, off_t *);
 63  int format_exec_cmd(char **, const char *, const char *, const char *, const char *,
 64      int argc, char **argv, bool lua);
 65  int is_dir(const char *);
 66  int is_link(const char *);
 67  
 68  bool check_for_hardlink(hardlinks_t *hl, struct stat *st);
 69  bool is_valid_abi(const char *arch, bool emit_error);
 70  bool is_valid_os_version(struct pkg *pkg);
 71  
 72  struct dns_srvinfo *
 73  	dns_getsrvinfo(const char *zone);
 74  
 75  int set_nameserver(const char *nsname);
 76  void set_blocking(int fd);
 77  void set_nonblocking(int fd);
 78  
 79  int pkg_symlink_cksum(const char *path, const char *root, char *cksum);
 80  int pkg_symlink_cksumat(int fd, const char *path, const char *root,
 81      char *cksum);
 82  
 83  pid_t process_spawn_pipe(FILE *inout[2], const char *command);
 84  
 85  void *parse_mode(const char *str);
 86  int *text_diff(char *a, char *b);
 87  int merge_3way(char *pivot, char *v1, char *v2, xstring *out);
 88  bool mkdirat_p(int fd, const char *path);
 89  int get_socketpair(int *);
 90  int checkflags(const char *mode, int *optr);
 91  bool match_ucl_lists(const char *buffer, const ucl_object_t *globs, const ucl_object_t *regexes);
 92  bool pkg_match_paths_list(const ucl_object_t *paths, const char *file);
 93  char *get_dirname(char *dir);
 94  char *rtrimspace(char *buf);
 95  void hidden_tempfile(char *buf, int buflen, const char *path);
 96  void append_random_suffix(char *buf, int buflen, int suffixlen);
 97  char *json_escape(const char *str);
 98  const char *get_http_auth(void);
 99  bool c_charv_contains(c_charv_t *, const char *, bool);
100  bool charv_contains(charv_t *, const char *, bool);
101  bool str_ends_with(const char *str, const char *end);
102  int char_cmp(const void *a, const void *b);
103  const char *charv_search(charv_t *, const char *);
104  DEFINE_VEC_INSERT_SORTED_PROTO(charv_t, charv, char *);
105  
106  uid_t get_uid_from_uname(const char *);
107  gid_t get_gid_from_gname(const char *);
108  
109  #endif