util.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef GIT_COMPAT_UTIL_H
  3. #define GIT_COMPAT_UTIL_H
  4. #define _BSD_SOURCE 1
  5. /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
  6. #define _DEFAULT_SOURCE 1
  7. #include <fcntl.h>
  8. #include <stdbool.h>
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include <linux/compiler.h>
  13. #include <sys/types.h>
  14. /* General helper functions */
  15. void usage(const char *err) __noreturn;
  16. void die(const char *err, ...) __noreturn __printf(1, 2);
  17. static inline void *zalloc(size_t size)
  18. {
  19. return calloc(1, size);
  20. }
  21. #define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  22. struct dirent;
  23. struct nsinfo;
  24. struct strlist;
  25. int mkdir_p(char *path, mode_t mode);
  26. int rm_rf(const char *path);
  27. struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *));
  28. bool lsdir_no_dot_filter(const char *name, struct dirent *d);
  29. int copyfile(const char *from, const char *to);
  30. int copyfile_mode(const char *from, const char *to, mode_t mode);
  31. int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi);
  32. int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size);
  33. ssize_t readn(int fd, void *buf, size_t n);
  34. ssize_t writen(int fd, const void *buf, size_t n);
  35. size_t hex_width(u64 v);
  36. int hex2u64(const char *ptr, u64 *val);
  37. extern unsigned int page_size;
  38. int __pure cacheline_size(void);
  39. int sysctl__max_stack(void);
  40. int fetch_kernel_version(unsigned int *puint,
  41. char *str, size_t str_sz);
  42. #define KVER_VERSION(x) (((x) >> 16) & 0xff)
  43. #define KVER_PATCHLEVEL(x) (((x) >> 8) & 0xff)
  44. #define KVER_SUBLEVEL(x) ((x) & 0xff)
  45. #define KVER_FMT "%d.%d.%d"
  46. #define KVER_PARAM(x) KVER_VERSION(x), KVER_PATCHLEVEL(x), KVER_SUBLEVEL(x)
  47. const char *perf_tip(const char *dirpath);
  48. #ifndef HAVE_GET_CURRENT_DIR_NAME
  49. char *get_current_dir_name(void);
  50. #endif
  51. #ifndef HAVE_SCHED_GETCPU_SUPPORT
  52. int sched_getcpu(void);
  53. #endif
  54. #ifndef HAVE_SETNS_SUPPORT
  55. int setns(int fd, int nstype);
  56. #endif
  57. extern bool perf_singlethreaded;
  58. void perf_set_singlethreaded(void);
  59. void perf_set_multithreaded(void);
  60. #ifndef O_CLOEXEC
  61. #ifdef __sparc__
  62. #define O_CLOEXEC 0x400000
  63. #elif defined(__alpha__) || defined(__hppa__)
  64. #define O_CLOEXEC 010000000
  65. #else
  66. #define O_CLOEXEC 02000000
  67. #endif
  68. #endif
  69. #endif /* GIT_COMPAT_UTIL_H */