]> git.ipfire.org Git - thirdparty/git.git/blobdiff - wrapper.c
Merge branch 'km/submodule-doc-use-sm-path' into maint
[thirdparty/git.git] / wrapper.c
index d20356a776bc63d4a94e4b421582433fb915087a..e1eaef2e1641457fabc8ab00b68d3b6b3f0e2354 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -4,12 +4,6 @@
 #include "cache.h"
 #include "config.h"
 
-static void do_nothing(size_t size)
-{
-}
-
-static void (*try_to_free_routine)(size_t size) = do_nothing;
-
 static int memory_limit_check(size_t size, int gentle)
 {
        static size_t limit = 0;
@@ -30,24 +24,11 @@ static int memory_limit_check(size_t size, int gentle)
        return 0;
 }
 
-try_to_free_t set_try_to_free_routine(try_to_free_t routine)
-{
-       try_to_free_t old = try_to_free_routine;
-       if (!routine)
-               routine = do_nothing;
-       try_to_free_routine = routine;
-       return old;
-}
-
 char *xstrdup(const char *str)
 {
        char *ret = strdup(str);
-       if (!ret) {
-               try_to_free_routine(strlen(str) + 1);
-               ret = strdup(str);
-               if (!ret)
-                       die("Out of memory, strdup failed");
-       }
+       if (!ret)
+               die("Out of memory, strdup failed");
        return ret;
 }
 
@@ -61,19 +42,13 @@ static void *do_xmalloc(size_t size, int gentle)
        if (!ret && !size)
                ret = malloc(1);
        if (!ret) {
-               try_to_free_routine(size);
-               ret = malloc(size);
-               if (!ret && !size)
-                       ret = malloc(1);
-               if (!ret) {
-                       if (!gentle)
-                               die("Out of memory, malloc failed (tried to allocate %lu bytes)",
-                                   (unsigned long)size);
-                       else {
-                               error("Out of memory, malloc failed (tried to allocate %lu bytes)",
-                                     (unsigned long)size);
-                               return NULL;
-                       }
+               if (!gentle)
+                       die("Out of memory, malloc failed (tried to allocate %lu bytes)",
+                           (unsigned long)size);
+               else {
+                       error("Out of memory, malloc failed (tried to allocate %lu bytes)",
+                             (unsigned long)size);
+                       return NULL;
                }
        }
 #ifdef XMALLOC_POISON
@@ -138,14 +113,8 @@ void *xrealloc(void *ptr, size_t size)
        ret = realloc(ptr, size);
        if (!ret && !size)
                ret = realloc(ptr, 1);
-       if (!ret) {
-               try_to_free_routine(size);
-               ret = realloc(ptr, size);
-               if (!ret && !size)
-                       ret = realloc(ptr, 1);
-               if (!ret)
-                       die("Out of memory, realloc failed");
-       }
+       if (!ret)
+               die("Out of memory, realloc failed");
        return ret;
 }
 
@@ -160,14 +129,8 @@ void *xcalloc(size_t nmemb, size_t size)
        ret = calloc(nmemb, size);
        if (!ret && (!nmemb || !size))
                ret = calloc(1, 1);
-       if (!ret) {
-               try_to_free_routine(nmemb * size);
-               ret = calloc(nmemb, size);
-               if (!ret && (!nmemb || !size))
-                       ret = calloc(1, 1);
-               if (!ret)
-                       die("Out of memory, calloc failed");
-       }
+       if (!ret)
+               die("Out of memory, calloc failed");
        return ret;
 }
 
@@ -445,21 +408,21 @@ FILE *fopen_or_warn(const char *path, const char *mode)
        return NULL;
 }
 
-int xmkstemp(char *template)
+int xmkstemp(char *filename_template)
 {
        int fd;
        char origtemplate[PATH_MAX];
-       strlcpy(origtemplate, template, sizeof(origtemplate));
+       strlcpy(origtemplate, filename_template, sizeof(origtemplate));
 
-       fd = mkstemp(template);
+       fd = mkstemp(filename_template);
        if (fd < 0) {
                int saved_errno = errno;
                const char *nonrelative_template;
 
-               if (strlen(template) != strlen(origtemplate))
-                       template = origtemplate;
+               if (strlen(filename_template) != strlen(origtemplate))
+                       filename_template = origtemplate;
 
-               nonrelative_template = absolute_path(template);
+               nonrelative_template = absolute_path(filename_template);
                errno = saved_errno;
                die_errno("Unable to create temporary file '%s'",
                        nonrelative_template);
@@ -478,21 +441,23 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
                "abcdefghijklmnopqrstuvwxyz"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "0123456789";
-       static const int num_letters = 62;
+       static const int num_letters = ARRAY_SIZE(letters) - 1;
+       static const char x_pattern[] = "XXXXXX";
+       static const int num_x = ARRAY_SIZE(x_pattern) - 1;
        uint64_t value;
        struct timeval tv;
-       char *template;
+       char *filename_template;
        size_t len;
        int fd, count;
 
        len = strlen(pattern);
 
-       if (len < 6 + suffix_len) {
+       if (len < num_x + suffix_len) {
                errno = EINVAL;
                return -1;
        }
 
-       if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
+       if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) {
                errno = EINVAL;
                return -1;
        }
@@ -502,17 +467,16 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
         * Try TMP_MAX different filenames.
         */
        gettimeofday(&tv, NULL);
-       value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
-       template = &pattern[len - 6 - suffix_len];
+       value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
+       filename_template = &pattern[len - num_x - suffix_len];
        for (count = 0; count < TMP_MAX; ++count) {
                uint64_t v = value;
+               int i;
                /* Fill in the random bits. */
-               template[0] = letters[v % num_letters]; v /= num_letters;
-               template[1] = letters[v % num_letters]; v /= num_letters;
-               template[2] = letters[v % num_letters]; v /= num_letters;
-               template[3] = letters[v % num_letters]; v /= num_letters;
-               template[4] = letters[v % num_letters]; v /= num_letters;
-               template[5] = letters[v % num_letters]; v /= num_letters;
+               for (i = 0; i < num_x; i++) {
+                       filename_template[i] = letters[v % num_letters];
+                       v /= num_letters;
+               }
 
                fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
                if (fd >= 0)
@@ -541,21 +505,21 @@ int git_mkstemp_mode(char *pattern, int mode)
        return git_mkstemps_mode(pattern, 0, mode);
 }
 
-int xmkstemp_mode(char *template, int mode)
+int xmkstemp_mode(char *filename_template, int mode)
 {
        int fd;
        char origtemplate[PATH_MAX];
-       strlcpy(origtemplate, template, sizeof(origtemplate));
+       strlcpy(origtemplate, filename_template, sizeof(origtemplate));
 
-       fd = git_mkstemp_mode(template, mode);
+       fd = git_mkstemp_mode(filename_template, mode);
        if (fd < 0) {
                int saved_errno = errno;
                const char *nonrelative_template;
 
-               if (!template[0])
-                       template = origtemplate;
+               if (!filename_template[0])
+                       filename_template = origtemplate;
 
-               nonrelative_template = absolute_path(template);
+               nonrelative_template = absolute_path(filename_template);
                errno = saved_errno;
                die_errno("Unable to create temporary file '%s'",
                        nonrelative_template);
@@ -643,9 +607,9 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
        va_end(ap);
 
        if (len < 0)
-               die("BUG: your snprintf is broken");
+               BUG("your snprintf is broken");
        if (len >= max)
-               die("BUG: attempt to snprintf into too-small buffer");
+               BUG("attempt to snprintf into too-small buffer");
        return len;
 }
 
@@ -690,3 +654,16 @@ int xgethostname(char *buf, size_t len)
                buf[len - 1] = 0;
        return ret;
 }
+
+int is_empty_or_missing_file(const char *filename)
+{
+       struct stat st;
+
+       if (stat(filename, &st) < 0) {
+               if (errno == ENOENT)
+                       return 1;
+               die_errno(_("could not stat %s"), filename);
+       }
+
+       return !st.st_size;
+}