blob: ece040b799f6ead3614d6061d60dbe3c17c2cac4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnar07800602009-04-20 15:00:56 +02002#ifndef GIT_COMPAT_UTIL_H
3#define GIT_COMPAT_UTIL_H
4
Ingo Molnar07800602009-04-20 15:00:56 +02005#define _BSD_SOURCE 1
Chanho Park512fe362014-09-12 11:10:17 +09006/* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
7#define _DEFAULT_SOURCE 1
Ingo Molnar07800602009-04-20 15:00:56 +02008
Jiri Olsaed93d0a2018-09-13 14:54:11 +02009#include <fcntl.h>
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -030010#include <stdbool.h>
Ingo Molnar07800602009-04-20 15:00:56 +020011#include <stddef.h>
12#include <stdlib.h>
13#include <stdarg.h>
Arnaldo Carvalho de Melo6c346642017-06-16 11:39:15 -030014#include <linux/compiler.h>
Arnaldo Carvalho de Meload0902e2018-04-06 14:53:56 -030015#include <sys/types.h>
Frederic Weisbecker1fe2c102009-08-12 10:19:53 +020016
Ingo Molnar07800602009-04-20 15:00:56 +020017/* General helper functions */
Arnaldo Carvalho de Melo6c346642017-06-16 11:39:15 -030018void usage(const char *err) __noreturn;
Arnaldo Carvalho de Meloafaed6d2017-06-16 11:57:54 -030019void die(const char *err, ...) __noreturn __printf(1, 2);
Ingo Molnar07800602009-04-20 15:00:56 +020020
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020021static inline void *zalloc(size_t size)
22{
23 return calloc(1, size);
24}
25
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -030026#define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
27
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030028struct dirent;
Arnaldo Carvalho de Meload0902e2018-04-06 14:53:56 -030029struct nsinfo;
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030030struct strlist;
31
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020032int mkdir_p(char *path, mode_t mode);
Joe Stringer9a9c7332017-01-26 13:19:59 -080033int rm_rf(const char *path);
Masami Hiramatsue1ce7262016-04-26 18:02:42 +090034struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
35bool lsdir_no_dot_filter(const char *name, struct dirent *d);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020036int copyfile(const char *from, const char *to);
Adrian Hunter9a17d722013-10-14 13:43:41 +030037int copyfile_mode(const char *from, const char *to, mode_t mode);
Krister Johansenf045b8c2017-07-05 18:48:11 -070038int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi);
Jiri Olsaed93d0a2018-09-13 14:54:11 +020039int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020040
Jiri Olsa727ebd52013-11-28 11:30:14 +010041ssize_t readn(int fd, void *buf, size_t n);
David Carrillo-Cisneros7c724402017-07-17 21:25:37 -070042ssize_t writen(int fd, const void *buf, size_t n);
Arnaldo Carvalho de Meloe206d552010-04-03 10:19:26 -030043
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030044size_t hex_width(u64 v);
Jiri Olsab2aff5f2012-10-27 23:18:30 +020045int hex2u64(const char *ptr, u64 *val);
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030046
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030047extern unsigned int page_size;
Arnaldo Carvalho de Melo9ac94e32018-05-17 15:03:05 -030048int __pure cacheline_size(void);
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030049
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -030050int sysctl__max_stack(void);
51
Wang Nan07bc5c62015-11-06 13:55:35 +000052int fetch_kernel_version(unsigned int *puint,
53 char *str, size_t str_sz);
Wang Nand3e0ce32015-11-06 13:58:09 +000054#define KVER_VERSION(x) (((x) >> 16) & 0xff)
55#define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
56#define KVER_SUBLEVEL(x) ((x) & 0xff)
57#define KVER_FMT "%d.%d.%d"
58#define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
Wang Nan07bc5c62015-11-06 13:55:35 +000059
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090060const char *perf_tip(const char *dirpath);
61
Arnaldo Carvalho de Melo8feb8ef2018-11-19 16:56:22 -030062#ifndef HAVE_GET_CURRENT_DIR_NAME
63char *get_current_dir_name(void);
64#endif
65
Arnaldo Carvalho de Melo120010c2017-03-02 12:55:49 -030066#ifndef HAVE_SCHED_GETCPU_SUPPORT
67int sched_getcpu(void);
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -030068#endif
69
Arnaldo Carvalho de Melo86bcdb52017-07-18 17:15:29 -030070#ifndef HAVE_SETNS_SUPPORT
71int setns(int fd, int nstype);
72#endif
73
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -030074extern bool perf_singlethreaded;
75
76void perf_set_singlethreaded(void);
77void perf_set_multithreaded(void);
78
Arnaldo Carvalho de Melo5c61d702018-01-22 17:11:30 -030079#ifndef O_CLOEXEC
80#ifdef __sparc__
81#define O_CLOEXEC 0x400000
82#elif defined(__alpha__) || defined(__hppa__)
83#define O_CLOEXEC 010000000
84#else
85#define O_CLOEXEC 02000000
86#endif
87#endif
88
Borislav Petkov13559152013-02-20 16:32:31 +010089#endif /* GIT_COMPAT_UTIL_H */