posix_util.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. static pthread_mutex_t gTempdirLock = PTHREAD_MUTEX_INITIALIZER;
  29. static int gTempdirNonce = 0;
  30. int recursiveDeleteContents(const char *path)
  31. {
  32. int ret;
  33. DIR *dp;
  34. struct dirent *de;
  35. char tmp[PATH_MAX];
  36. dp = opendir(path);
  37. if (!dp) {
  38. ret = -errno;
  39. fprintf(stderr, "recursiveDelete(%s) failed with error %d\n", path, ret);
  40. return ret;
  41. }
  42. while (1) {
  43. de = readdir(dp);
  44. if (!de) {
  45. ret = 0;
  46. break;
  47. }
  48. if ((de->d_name[0] == '.') && (de->d_name[1] == '\0'))
  49. continue;
  50. if ((de->d_name[0] == '.') && (de->d_name[1] == '.') &&
  51. (de->d_name[2] == '\0'))
  52. continue;
  53. snprintf(tmp, sizeof(tmp), "%s/%s", path, de->d_name);
  54. ret = recursiveDelete(tmp);
  55. if (ret)
  56. break;
  57. }
  58. if (closedir(dp)) {
  59. ret = -errno;
  60. fprintf(stderr, "recursiveDelete(%s): closedir failed with "
  61. "error %d\n", path, ret);
  62. return ret;
  63. }
  64. return ret;
  65. }
  66. /*
  67. * Simple recursive delete implementation.
  68. * It could be optimized, but there is no need at the moment.
  69. * TODO: use fstat, etc.
  70. */
  71. int recursiveDelete(const char *path)
  72. {
  73. int ret;
  74. struct stat stBuf;
  75. ret = stat(path, &stBuf);
  76. if (ret != 0) {
  77. ret = -errno;
  78. fprintf(stderr, "recursiveDelete(%s): stat failed with "
  79. "error %d\n", path, ret);
  80. return ret;
  81. }
  82. if (S_ISDIR(stBuf.st_mode)) {
  83. ret = recursiveDeleteContents(path);
  84. if (ret)
  85. return ret;
  86. ret = rmdir(path);
  87. if (ret) {
  88. ret = errno;
  89. fprintf(stderr, "recursiveDelete(%s): rmdir failed with error %d\n",
  90. path, ret);
  91. return ret;
  92. }
  93. } else {
  94. ret = unlink(path);
  95. if (ret) {
  96. ret = -errno;
  97. fprintf(stderr, "recursiveDelete(%s): unlink failed with "
  98. "error %d\n", path, ret);
  99. return ret;
  100. }
  101. }
  102. return 0;
  103. }
  104. int createTempDir(char *tempDir, int nameMax, int mode)
  105. {
  106. char tmp[PATH_MAX];
  107. int pid, nonce;
  108. const char *base = getenv("TMPDIR");
  109. if (!base)
  110. base = "/tmp";
  111. if (base[0] != '/') {
  112. // canonicalize non-absolute TMPDIR
  113. if (realpath(base, tmp) == NULL) {
  114. return -errno;
  115. }
  116. base = tmp;
  117. }
  118. pid = getpid();
  119. pthread_mutex_lock(&gTempdirLock);
  120. nonce = gTempdirNonce++;
  121. pthread_mutex_unlock(&gTempdirLock);
  122. snprintf(tempDir, nameMax, "%s/temp.%08d.%08d", base, pid, nonce);
  123. if (mkdir(tempDir, mode) == -1) {
  124. int ret = errno;
  125. return -ret;
  126. }
  127. return 0;
  128. }
  129. void sleepNoSig(int sec)
  130. {
  131. int ret;
  132. struct timespec req, rem;
  133. rem.tv_sec = sec;
  134. rem.tv_nsec = 0;
  135. do {
  136. req = rem;
  137. ret = nanosleep(&req, &rem);
  138. } while ((ret == -1) && (errno == EINTR));
  139. if (ret == -1) {
  140. ret = errno;
  141. fprintf(stderr, "nanosleep error %d (%s)\n", ret, strerror(ret));
  142. }
  143. }