posix_util.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "util/posix_util.h"
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <pthread.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <limits.h>
  29. static pthread_mutex_t gTempdirLock = PTHREAD_MUTEX_INITIALIZER;
  30. static int gTempdirNonce = 0;
  31. int recursiveDeleteContents(const char *path)
  32. {
  33. int ret;
  34. DIR *dp;
  35. struct dirent *de;
  36. char tmp[PATH_MAX];
  37. dp = opendir(path);
  38. if (!dp) {
  39. ret = -errno;
  40. fprintf(stderr, "recursiveDelete(%s) failed with error %d\n", path, ret);
  41. return ret;
  42. }
  43. while (1) {
  44. de = readdir(dp);
  45. if (!de) {
  46. ret = 0;
  47. break;
  48. }
  49. if ((de->d_name[0] == '.') && (de->d_name[1] == '\0'))
  50. continue;
  51. if ((de->d_name[0] == '.') && (de->d_name[1] == '.') &&
  52. (de->d_name[2] == '\0'))
  53. continue;
  54. snprintf(tmp, sizeof(tmp), "%s/%s", path, de->d_name);
  55. ret = recursiveDelete(tmp);
  56. if (ret)
  57. break;
  58. }
  59. if (closedir(dp)) {
  60. ret = -errno;
  61. fprintf(stderr, "recursiveDelete(%s): closedir failed with "
  62. "error %d\n", path, ret);
  63. return ret;
  64. }
  65. return ret;
  66. }
  67. /*
  68. * Simple recursive delete implementation.
  69. * It could be optimized, but there is no need at the moment.
  70. * TODO: use fstat, etc.
  71. */
  72. int recursiveDelete(const char *path)
  73. {
  74. int ret;
  75. struct stat stBuf;
  76. ret = stat(path, &stBuf);
  77. if (ret != 0) {
  78. ret = -errno;
  79. fprintf(stderr, "recursiveDelete(%s): stat failed with "
  80. "error %d\n", path, ret);
  81. return ret;
  82. }
  83. if (S_ISDIR(stBuf.st_mode)) {
  84. ret = recursiveDeleteContents(path);
  85. if (ret)
  86. return ret;
  87. ret = rmdir(path);
  88. if (ret) {
  89. ret = errno;
  90. fprintf(stderr, "recursiveDelete(%s): rmdir failed with error %d\n",
  91. path, ret);
  92. return ret;
  93. }
  94. } else {
  95. ret = unlink(path);
  96. if (ret) {
  97. ret = -errno;
  98. fprintf(stderr, "recursiveDelete(%s): unlink failed with "
  99. "error %d\n", path, ret);
  100. return ret;
  101. }
  102. }
  103. return 0;
  104. }
  105. int createTempDir(char *tempDir, int nameMax, int mode)
  106. {
  107. char tmp[PATH_MAX];
  108. int pid, nonce;
  109. const char *base = getenv("TMPDIR");
  110. if (!base)
  111. base = "/tmp";
  112. if (base[0] != '/') {
  113. // canonicalize non-absolute TMPDIR
  114. if (realpath(base, tmp) == NULL) {
  115. return -errno;
  116. }
  117. base = tmp;
  118. }
  119. pid = getpid();
  120. pthread_mutex_lock(&gTempdirLock);
  121. nonce = gTempdirNonce++;
  122. pthread_mutex_unlock(&gTempdirLock);
  123. snprintf(tempDir, nameMax, "%s/temp.%08d.%08d", base, pid, nonce);
  124. if (mkdir(tempDir, mode) == -1) {
  125. int ret = errno;
  126. return -ret;
  127. }
  128. return 0;
  129. }
  130. void sleepNoSig(int sec)
  131. {
  132. int ret;
  133. struct timespec req, rem;
  134. rem.tv_sec = sec;
  135. rem.tv_nsec = 0;
  136. do {
  137. req = rem;
  138. ret = nanosleep(&req, &rem);
  139. } while ((ret == -1) && (errno == EINTR));
  140. if (ret == -1) {
  141. ret = errno;
  142. fprintf(stderr, "nanosleep error %d (%s)\n", ret, strerror(ret));
  143. }
  144. }