util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "../perf.h"
  2. #include "util.h"
  3. #include <sys/mman.h>
  4. /*
  5. * XXX We need to find a better place for these things...
  6. */
  7. bool perf_host = true;
  8. bool perf_guest = true;
  9. void event_attr_init(struct perf_event_attr *attr)
  10. {
  11. if (!perf_host)
  12. attr->exclude_host = 1;
  13. if (!perf_guest)
  14. attr->exclude_guest = 1;
  15. }
  16. int mkdir_p(char *path, mode_t mode)
  17. {
  18. struct stat st;
  19. int err;
  20. char *d = path;
  21. if (*d != '/')
  22. return -1;
  23. if (stat(path, &st) == 0)
  24. return 0;
  25. while (*++d == '/');
  26. while ((d = strchr(d, '/'))) {
  27. *d = '\0';
  28. err = stat(path, &st) && mkdir(path, mode);
  29. *d++ = '/';
  30. if (err)
  31. return -1;
  32. while (*d == '/')
  33. ++d;
  34. }
  35. return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
  36. }
  37. static int slow_copyfile(const char *from, const char *to)
  38. {
  39. int err = 0;
  40. char *line = NULL;
  41. size_t n;
  42. FILE *from_fp = fopen(from, "r"), *to_fp;
  43. if (from_fp == NULL)
  44. goto out;
  45. to_fp = fopen(to, "w");
  46. if (to_fp == NULL)
  47. goto out_fclose_from;
  48. while (getline(&line, &n, from_fp) > 0)
  49. if (fputs(line, to_fp) == EOF)
  50. goto out_fclose_to;
  51. err = 0;
  52. out_fclose_to:
  53. fclose(to_fp);
  54. free(line);
  55. out_fclose_from:
  56. fclose(from_fp);
  57. out:
  58. return err;
  59. }
  60. int copyfile(const char *from, const char *to)
  61. {
  62. int fromfd, tofd;
  63. struct stat st;
  64. void *addr;
  65. int err = -1;
  66. if (stat(from, &st))
  67. goto out;
  68. if (st.st_size == 0) /* /proc? do it slowly... */
  69. return slow_copyfile(from, to);
  70. fromfd = open(from, O_RDONLY);
  71. if (fromfd < 0)
  72. goto out;
  73. tofd = creat(to, 0755);
  74. if (tofd < 0)
  75. goto out_close_from;
  76. addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
  77. if (addr == MAP_FAILED)
  78. goto out_close_to;
  79. if (write(tofd, addr, st.st_size) == st.st_size)
  80. err = 0;
  81. munmap(addr, st.st_size);
  82. out_close_to:
  83. close(tofd);
  84. if (err)
  85. unlink(to);
  86. out_close_from:
  87. close(fromfd);
  88. out:
  89. return err;
  90. }
  91. unsigned long convert_unit(unsigned long value, char *unit)
  92. {
  93. *unit = ' ';
  94. if (value > 1000) {
  95. value /= 1000;
  96. *unit = 'K';
  97. }
  98. if (value > 1000) {
  99. value /= 1000;
  100. *unit = 'M';
  101. }
  102. if (value > 1000) {
  103. value /= 1000;
  104. *unit = 'G';
  105. }
  106. return value;
  107. }
  108. int readn(int fd, void *buf, size_t n)
  109. {
  110. void *buf_start = buf;
  111. while (n) {
  112. int ret = read(fd, buf, n);
  113. if (ret <= 0)
  114. return ret;
  115. n -= ret;
  116. buf += ret;
  117. }
  118. return buf - buf_start;
  119. }