tools.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <ccan/talloc/talloc.h>
  2. #include <ccan/grab_file/grab_file.h>
  3. #include <ccan/noerr/noerr.h>
  4. #include <ccan/read_write_all/read_write_all.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdarg.h>
  11. #include <errno.h>
  12. #include <err.h>
  13. #include "tools.h"
  14. static char *tmpdir = NULL;
  15. static unsigned int count;
  16. char *talloc_basename(const void *ctx, const char *dir)
  17. {
  18. char *p = strrchr(dir, '/');
  19. if (!p)
  20. return (char *)dir;
  21. return talloc_strdup(ctx, p+1);
  22. }
  23. char *talloc_dirname(const void *ctx, const char *dir)
  24. {
  25. char *p = strrchr(dir, '/');
  26. if (!p)
  27. return talloc_strdup(ctx, ".");
  28. return talloc_strndup(ctx, dir, p - dir);
  29. }
  30. char *talloc_getcwd(const void *ctx)
  31. {
  32. unsigned int len;
  33. char *cwd;
  34. /* *This* is why people hate C. */
  35. len = 32;
  36. cwd = talloc_array(ctx, char, len);
  37. while (!getcwd(cwd, len)) {
  38. if (errno != ERANGE) {
  39. talloc_free(cwd);
  40. return NULL;
  41. }
  42. cwd = talloc_realloc(ctx, cwd, char, len *= 2);
  43. }
  44. return cwd;
  45. }
  46. /* Returns output if command fails. */
  47. char *run_command(const void *ctx, const char *fmt, ...)
  48. {
  49. va_list ap;
  50. char *cmd, *contents;
  51. FILE *pipe;
  52. va_start(ap, fmt);
  53. cmd = talloc_vasprintf(ctx, fmt, ap);
  54. va_end(ap);
  55. /* Ensure stderr gets to us too. */
  56. cmd = talloc_asprintf_append(cmd, " 2>&1");
  57. pipe = popen(cmd, "r");
  58. if (!pipe)
  59. return talloc_asprintf(ctx, "Failed to run '%s'", cmd);
  60. contents = grab_fd(cmd, fileno(pipe), NULL);
  61. if (pclose(pipe) != 0)
  62. return talloc_asprintf(ctx, "Running '%s':\n%s",
  63. cmd, contents);
  64. talloc_free(cmd);
  65. return NULL;
  66. }
  67. static int unlink_all(char *dir)
  68. {
  69. char cmd[strlen(dir) + sizeof("rm -rf ")];
  70. sprintf(cmd, "rm -rf %s", dir);
  71. if (system(cmd) != 0)
  72. warn("Could not remove temporary work in %s", dir);
  73. return 0;
  74. }
  75. char *temp_file(const void *ctx, const char *extension)
  76. {
  77. /* For first call, create dir. */
  78. while (!tmpdir) {
  79. tmpdir = getenv("TMPDIR");
  80. if (!tmpdir)
  81. tmpdir = "/tmp";
  82. tmpdir = talloc_asprintf(talloc_autofree_context(),
  83. "%s/ccanlint-%u.%lu",
  84. tmpdir, getpid(), random());
  85. if (mkdir(tmpdir, 0700) != 0) {
  86. if (errno == EEXIST) {
  87. talloc_free(tmpdir);
  88. tmpdir = NULL;
  89. continue;
  90. }
  91. err(1, "mkdir %s failed", tmpdir);
  92. }
  93. talloc_set_destructor(tmpdir, unlink_all);
  94. }
  95. return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension);
  96. }
  97. bool move_file(const char *oldname, const char *newname)
  98. {
  99. char *contents;
  100. size_t size;
  101. int fd;
  102. bool ret;
  103. /* Simple case: rename works. */
  104. if (rename(oldname, newname) == 0)
  105. return true;
  106. /* Try copy and delete: not atomic! */
  107. contents = grab_file(NULL, oldname, &size);
  108. if (!contents)
  109. return false;
  110. fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  111. if (fd < 0) {
  112. ret = false;
  113. goto free;
  114. }
  115. ret = write_all(fd, contents, size);
  116. if (close(fd) != 0)
  117. ret = false;
  118. if (ret)
  119. unlink(oldname);
  120. else
  121. unlink(newname);
  122. free:
  123. talloc_free(contents);
  124. return ret;
  125. }