THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
14KB

  1. /*
  2. * Various trivial helper wrappers around standard functions
  3. */
  4. #include "cache.h"
  5. #include "config.h"
  6. static int memory_limit_check(size_t size, int gentle)
  7. {
  8. static size_t limit = 0;
  9. if (!limit) {
  10. limit = git_env_ulong("GIT_ALLOC_LIMIT", 0);
  11. if (!limit)
  12. limit = SIZE_MAX;
  13. }
  14. if (size > limit) {
  15. if (gentle) {
  16. error("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX,
  17. (uintmax_t)size, (uintmax_t)limit);
  18. return -1;
  19. } else
  20. die("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX,
  21. (uintmax_t)size, (uintmax_t)limit);
  22. }
  23. return 0;
  24. }
  25. char *xstrdup(const char *str)
  26. {
  27. char *ret = strdup(str);
  28. if (!ret)
  29. die("Out of memory, strdup failed");
  30. return ret;
  31. }
  32. static void *do_xmalloc(size_t size, int gentle)
  33. {
  34. void *ret;
  35. if (memory_limit_check(size, gentle))
  36. return NULL;
  37. ret = malloc(size);
  38. if (!ret && !size)
  39. ret = malloc(1);
  40. if (!ret) {
  41. if (!gentle)
  42. die("Out of memory, malloc failed (tried to allocate %lu bytes)",
  43. (unsigned long)size);
  44. else {
  45. error("Out of memory, malloc failed (tried to allocate %lu bytes)",
  46. (unsigned long)size);
  47. return NULL;
  48. }
  49. }
  50. #ifdef XMALLOC_POISON
  51. memset(ret, 0xA5, size);
  52. #endif
  53. return ret;
  54. }
  55. void *xmalloc(size_t size)
  56. {
  57. return do_xmalloc(size, 0);
  58. }
  59. static void *do_xmallocz(size_t size, int gentle)
  60. {
  61. void *ret;
  62. if (unsigned_add_overflows(size, 1)) {
  63. if (gentle) {
  64. error("Data too large to fit into virtual memory space.");
  65. return NULL;
  66. } else
  67. die("Data too large to fit into virtual memory space.");
  68. }
  69. ret = do_xmalloc(size + 1, gentle);
  70. if (ret)
  71. ((char*)ret)[size] = 0;
  72. return ret;
  73. }
  74. void *xmallocz(size_t size)
  75. {
  76. return do_xmallocz(size, 0);
  77. }
  78. void *xmallocz_gently(size_t size)
  79. {
  80. return do_xmallocz(size, 1);
  81. }
  82. /*
  83. * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
  84. * "data" to the allocated memory, zero terminates the allocated memory,
  85. * and returns a pointer to the allocated memory. If the allocation fails,
  86. * the program dies.
  87. */
  88. void *xmemdupz(const void *data, size_t len)
  89. {
  90. return memcpy(xmallocz(len), data, len);
  91. }
  92. char *xstrndup(const char *str, size_t len)
  93. {
  94. char *p = memchr(str, '\0', len);
  95. return xmemdupz(str, p ? p - str : len);
  96. }
  97. void *xrealloc(void *ptr, size_t size)
  98. {
  99. void *ret;
  100. memory_limit_check(size, 0);
  101. ret = realloc(ptr, size);
  102. if (!ret && !size)
  103. ret = realloc(ptr, 1);
  104. if (!ret)
  105. die("Out of memory, realloc failed");
  106. return ret;
  107. }
  108. void *xcalloc(size_t nmemb, size_t size)
  109. {
  110. void *ret;
  111. if (unsigned_mult_overflows(nmemb, size))
  112. die("data too large to fit into virtual memory space");
  113. memory_limit_check(size * nmemb, 0);
  114. ret = calloc(nmemb, size);
  115. if (!ret && (!nmemb || !size))
  116. ret = calloc(1, 1);
  117. if (!ret)
  118. die("Out of memory, calloc failed");
  119. return ret;
  120. }
  121. /*
  122. * Limit size of IO chunks, because huge chunks only cause pain. OS X
  123. * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
  124. * the absence of bugs, large chunks can result in bad latencies when
  125. * you decide to kill the process.
  126. *
  127. * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
  128. * that is smaller than that, clip it to SSIZE_MAX, as a call to
  129. * read(2) or write(2) larger than that is allowed to fail. As the last
  130. * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value"
  131. * to override this, if the definition of SSIZE_MAX given by the platform
  132. * is broken.
  133. */
  134. #ifndef MAX_IO_SIZE
  135. # define MAX_IO_SIZE_DEFAULT (8*1024*1024)
  136. # if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
  137. # define MAX_IO_SIZE SSIZE_MAX
  138. # else
  139. # define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
  140. # endif
  141. #endif
  142. /**
  143. * xopen() is the same as open(), but it die()s if the open() fails.
  144. */
  145. int xopen(const char *path, int oflag, ...)
  146. {
  147. mode_t mode = 0;
  148. va_list ap;
  149. /*
  150. * va_arg() will have undefined behavior if the specified type is not
  151. * compatible with the argument type. Since integers are promoted to
  152. * ints, we fetch the next argument as an int, and then cast it to a
  153. * mode_t to avoid undefined behavior.
  154. */
  155. va_start(ap, oflag);
  156. if (oflag & O_CREAT)
  157. mode = va_arg(ap, int);
  158. va_end(ap);
  159. for (;;) {
  160. int fd = open(path, oflag, mode);
  161. if (fd >= 0)
  162. return fd;
  163. if (errno == EINTR)
  164. continue;
  165. if ((oflag & O_RDWR) == O_RDWR)
  166. die_errno(_("could not open '%s' for reading and writing"), path);
  167. else if ((oflag & O_WRONLY) == O_WRONLY)
  168. die_errno(_("could not open '%s' for writing"), path);
  169. else
  170. die_errno(_("could not open '%s' for reading"), path);
  171. }
  172. }
  173. static int handle_nonblock(int fd, short poll_events, int err)
  174. {
  175. struct pollfd pfd;
  176. if (err != EAGAIN && err != EWOULDBLOCK)
  177. return 0;
  178. pfd.fd = fd;
  179. pfd.events = poll_events;
  180. /*
  181. * no need to check for errors, here;
  182. * a subsequent read/write will detect unrecoverable errors
  183. */
  184. poll(&pfd, 1, -1);
  185. return 1;
  186. }
  187. /*
  188. * xread() is the same a read(), but it automatically restarts read()
  189. * operations with a recoverable error (EAGAIN and EINTR). xread()
  190. * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
  191. */
  192. ssize_t xread(int fd, void *buf, size_t len)
  193. {
  194. ssize_t nr;
  195. if (len > MAX_IO_SIZE)
  196. len = MAX_IO_SIZE;
  197. while (1) {
  198. nr = read(fd, buf, len);
  199. if (nr < 0) {
  200. if (errno == EINTR)
  201. continue;
  202. if (handle_nonblock(fd, POLLIN, errno))
  203. continue;
  204. }
  205. return nr;
  206. }
  207. }
  208. /*
  209. * xwrite() is the same a write(), but it automatically restarts write()
  210. * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
  211. * GUARANTEE that "len" bytes is written even if the operation is successful.
  212. */
  213. ssize_t xwrite(int fd, const void *buf, size_t len)
  214. {
  215. ssize_t nr;
  216. if (len > MAX_IO_SIZE)
  217. len = MAX_IO_SIZE;
  218. while (1) {
  219. nr = write(fd, buf, len);
  220. if (nr < 0) {
  221. if (errno == EINTR)
  222. continue;
  223. if (handle_nonblock(fd, POLLOUT, errno))
  224. continue;
  225. }
  226. return nr;
  227. }
  228. }
  229. /*
  230. * xpread() is the same as pread(), but it automatically restarts pread()
  231. * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES
  232. * NOT GUARANTEE that "len" bytes is read even if the data is available.
  233. */
  234. ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
  235. {
  236. ssize_t nr;
  237. if (len > MAX_IO_SIZE)
  238. len = MAX_IO_SIZE;
  239. while (1) {
  240. nr = pread(fd, buf, len, offset);
  241. if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
  242. continue;
  243. return nr;
  244. }
  245. }
  246. ssize_t read_in_full(int fd, void *buf, size_t count)
  247. {
  248. char *p = buf;
  249. ssize_t total = 0;
  250. while (count > 0) {
  251. ssize_t loaded = xread(fd, p, count);
  252. if (loaded < 0)
  253. return -1;
  254. if (loaded == 0)
  255. return total;
  256. count -= loaded;
  257. p += loaded;
  258. total += loaded;
  259. }
  260. return total;
  261. }
  262. ssize_t write_in_full(int fd, const void *buf, size_t count)
  263. {
  264. const char *p = buf;
  265. ssize_t total = 0;
  266. while (count > 0) {
  267. ssize_t written = xwrite(fd, p, count);
  268. if (written < 0)
  269. return -1;
  270. if (!written) {
  271. errno = ENOSPC;
  272. return -1;
  273. }
  274. count -= written;
  275. p += written;
  276. total += written;
  277. }
  278. return total;
  279. }
  280. ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset)
  281. {
  282. char *p = buf;
  283. ssize_t total = 0;
  284. while (count > 0) {
  285. ssize_t loaded = xpread(fd, p, count, offset);
  286. if (loaded < 0)
  287. return -1;
  288. if (loaded == 0)
  289. return total;
  290. count -= loaded;
  291. p += loaded;
  292. total += loaded;
  293. offset += loaded;
  294. }
  295. return total;
  296. }
  297. int xdup(int fd)
  298. {
  299. int ret = dup(fd);
  300. if (ret < 0)
  301. die_errno("dup failed");
  302. return ret;
  303. }
  304. /**
  305. * xfopen() is the same as fopen(), but it die()s if the fopen() fails.
  306. */
  307. FILE *xfopen(const char *path, const char *mode)
  308. {
  309. for (;;) {
  310. FILE *fp = fopen(path, mode);
  311. if (fp)
  312. return fp;
  313. if (errno == EINTR)
  314. continue;
  315. if (*mode && mode[1] == '+')
  316. die_errno(_("could not open '%s' for reading and writing"), path);
  317. else if (*mode == 'w' || *mode == 'a')
  318. die_errno(_("could not open '%s' for writing"), path);
  319. else
  320. die_errno(_("could not open '%s' for reading"), path);
  321. }
  322. }
  323. FILE *xfdopen(int fd, const char *mode)
  324. {
  325. FILE *stream = fdopen(fd, mode);
  326. if (stream == NULL)
  327. die_errno("Out of memory? fdopen failed");
  328. return stream;
  329. }
  330. FILE *fopen_for_writing(const char *path)
  331. {
  332. FILE *ret = fopen(path, "w");
  333. if (!ret && errno == EPERM) {
  334. if (!unlink(path))
  335. ret = fopen(path, "w");
  336. else
  337. errno = EPERM;
  338. }
  339. return ret;
  340. }
  341. static void warn_on_inaccessible(const char *path)
  342. {
  343. warning_errno(_("unable to access '%s'"), path);
  344. }
  345. int warn_on_fopen_errors(const char *path)
  346. {
  347. if (errno != ENOENT && errno != ENOTDIR) {
  348. warn_on_inaccessible(path);
  349. return -1;
  350. }
  351. return 0;
  352. }
  353. FILE *fopen_or_warn(const char *path, const char *mode)
  354. {
  355. FILE *fp = fopen(path, mode);
  356. if (fp)
  357. return fp;
  358. warn_on_fopen_errors(path);
  359. return NULL;
  360. }
  361. int xmkstemp(char *filename_template)
  362. {
  363. int fd;
  364. char origtemplate[PATH_MAX];
  365. strlcpy(origtemplate, filename_template, sizeof(origtemplate));
  366. fd = mkstemp(filename_template);
  367. if (fd < 0) {
  368. int saved_errno = errno;
  369. const char *nonrelative_template;
  370. if (strlen(filename_template) != strlen(origtemplate))
  371. filename_template = origtemplate;
  372. nonrelative_template = absolute_path(filename_template);
  373. errno = saved_errno;
  374. die_errno("Unable to create temporary file '%s'",
  375. nonrelative_template);
  376. }
  377. return fd;
  378. }
  379. /* Adapted from libiberty's mkstemp.c. */
  380. #undef TMP_MAX
  381. #define TMP_MAX 16384
  382. int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
  383. {
  384. static const char letters[] =
  385. "abcdefghijklmnopqrstuvwxyz"
  386. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  387. "0123456789";
  388. static const int num_letters = ARRAY_SIZE(letters) - 1;
  389. static const char x_pattern[] = "XXXXXX";
  390. static const int num_x = ARRAY_SIZE(x_pattern) - 1;
  391. uint64_t value;
  392. struct timeval tv;
  393. char *filename_template;
  394. size_t len;
  395. int fd, count;
  396. len = strlen(pattern);
  397. if (len < num_x + suffix_len) {
  398. errno = EINVAL;
  399. return -1;
  400. }
  401. if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) {
  402. errno = EINVAL;
  403. return -1;
  404. }
  405. /*
  406. * Replace pattern's XXXXXX characters with randomness.
  407. * Try TMP_MAX different filenames.
  408. */
  409. gettimeofday(&tv, NULL);
  410. value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
  411. filename_template = &pattern[len - num_x - suffix_len];
  412. for (count = 0; count < TMP_MAX; ++count) {
  413. uint64_t v = value;
  414. int i;
  415. /* Fill in the random bits. */
  416. for (i = 0; i < num_x; i++) {
  417. filename_template[i] = letters[v % num_letters];
  418. v /= num_letters;
  419. }
  420. fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
  421. if (fd >= 0)
  422. return fd;
  423. /*
  424. * Fatal error (EPERM, ENOSPC etc).
  425. * It doesn't make sense to loop.
  426. */
  427. if (errno != EEXIST)
  428. break;
  429. /*
  430. * This is a random value. It is only necessary that
  431. * the next TMP_MAX values generated by adding 7777 to
  432. * VALUE are different with (module 2^32).
  433. */
  434. value += 7777;
  435. }
  436. /* We return the null string if we can't find a unique file name. */
  437. pattern[0] = '\0';
  438. return -1;
  439. }
  440. int git_mkstemp_mode(char *pattern, int mode)
  441. {
  442. /* mkstemp is just mkstemps with no suffix */
  443. return git_mkstemps_mode(pattern, 0, mode);
  444. }
  445. int xmkstemp_mode(char *filename_template, int mode)
  446. {
  447. int fd;
  448. char origtemplate[PATH_MAX];
  449. strlcpy(origtemplate, filename_template, sizeof(origtemplate));
  450. fd = git_mkstemp_mode(filename_template, mode);
  451. if (fd < 0) {
  452. int saved_errno = errno;
  453. const char *nonrelative_template;
  454. if (!filename_template[0])
  455. filename_template = origtemplate;
  456. nonrelative_template = absolute_path(filename_template);
  457. errno = saved_errno;
  458. die_errno("Unable to create temporary file '%s'",
  459. nonrelative_template);
  460. }
  461. return fd;
  462. }
  463. static int warn_if_unremovable(const char *op, const char *file, int rc)
  464. {
  465. int err;
  466. if (!rc || errno == ENOENT)
  467. return 0;
  468. err = errno;
  469. warning_errno("unable to %s '%s'", op, file);
  470. errno = err;
  471. return rc;
  472. }
  473. int unlink_or_msg(const char *file, struct strbuf *err)
  474. {
  475. int rc = unlink(file);
  476. assert(err);
  477. if (!rc || errno == ENOENT)
  478. return 0;
  479. strbuf_addf(err, "unable to unlink '%s': %s",
  480. file, strerror(errno));
  481. return -1;
  482. }
  483. int unlink_or_warn(const char *file)
  484. {
  485. return warn_if_unremovable("unlink", file, unlink(file));
  486. }
  487. int rmdir_or_warn(const char *file)
  488. {
  489. return warn_if_unremovable("rmdir", file, rmdir(file));
  490. }
  491. int remove_or_warn(unsigned int mode, const char *file)
  492. {
  493. return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
  494. }
  495. static int access_error_is_ok(int err, unsigned flag)
  496. {
  497. return (is_missing_file_error(err) ||
  498. ((flag & ACCESS_EACCES_OK) && err == EACCES));
  499. }
  500. int access_or_warn(const char *path, int mode, unsigned flag)
  501. {
  502. int ret = access(path, mode);
  503. if (ret && !access_error_is_ok(errno, flag))
  504. warn_on_inaccessible(path);
  505. return ret;
  506. }
  507. int access_or_die(const char *path, int mode, unsigned flag)
  508. {
  509. int ret = access(path, mode);
  510. if (ret && !access_error_is_ok(errno, flag))
  511. die_errno(_("unable to access '%s'"), path);
  512. return ret;
  513. }
  514. char *xgetcwd(void)
  515. {
  516. struct strbuf sb = STRBUF_INIT;
  517. if (strbuf_getcwd(&sb))
  518. die_errno(_("unable to get current working directory"));
  519. return strbuf_detach(&sb, NULL);
  520. }
  521. int xsnprintf(char *dst, size_t max, const char *fmt, ...)
  522. {
  523. va_list ap;
  524. int len;
  525. va_start(ap, fmt);
  526. len = vsnprintf(dst, max, fmt, ap);
  527. va_end(ap);
  528. if (len < 0)
  529. BUG("your snprintf is broken");
  530. if (len >= max)
  531. BUG("attempt to snprintf into too-small buffer");
  532. return len;
  533. }
  534. void write_file_buf(const char *path, const char *buf, size_t len)
  535. {
  536. int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  537. if (write_in_full(fd, buf, len) < 0)
  538. die_errno(_("could not write to '%s'"), path);
  539. if (close(fd))
  540. die_errno(_("could not close '%s'"), path);
  541. }
  542. void write_file(const char *path, const char *fmt, ...)
  543. {
  544. va_list params;
  545. struct strbuf sb = STRBUF_INIT;
  546. va_start(params, fmt);
  547. strbuf_vaddf(&sb, fmt, params);
  548. va_end(params);
  549. strbuf_complete_line(&sb);
  550. write_file_buf(path, sb.buf, sb.len);
  551. strbuf_release(&sb);
  552. }
  553. void sleep_millisec(int millisec)
  554. {
  555. poll(NULL, 0, millisec);
  556. }
  557. int xgethostname(char *buf, size_t len)
  558. {
  559. /*
  560. * If the full hostname doesn't fit in buf, POSIX does not
  561. * specify whether the buffer will be null-terminated, so to
  562. * be safe, do it ourselves.
  563. */
  564. int ret = gethostname(buf, len);
  565. if (!ret)
  566. buf[len - 1] = 0;
  567. return ret;
  568. }
  569. int is_empty_or_missing_file(const char *filename)
  570. {
  571. struct stat st;
  572. if (stat(filename, &st) < 0) {
  573. if (errno == ENOENT)
  574. return 1;
  575. die_errno(_("could not stat %s"), filename);
  576. }
  577. return !st.st_size;
  578. }