t9117: test specifying full url to git svn init -T
[gitweb.git] / wrapper.c
index 6fcaa4dc62b504078ea5202cfd7a096e79a83c5e..9afc1a021c224d4ff2230fffeb0e4f0882d4a99e 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -152,6 +152,9 @@ void *xcalloc(size_t nmemb, size_t size)
 {
        void *ret;
 
+       if (unsigned_mult_overflows(nmemb, size))
+               die("data too large to fit into virtual memory space");
+
        memory_limit_check(size * nmemb, 0);
        ret = calloc(nmemb, size);
        if (!ret && (!nmemb || !size))
@@ -236,8 +239,24 @@ ssize_t xread(int fd, void *buf, size_t len)
            len = MAX_IO_SIZE;
        while (1) {
                nr = read(fd, buf, len);
-               if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
-                       continue;
+               if (nr < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       if (errno == EAGAIN || errno == EWOULDBLOCK) {
+                               struct pollfd pfd;
+                               pfd.events = POLLIN;
+                               pfd.fd = fd;
+                               /*
+                                * it is OK if this poll() failed; we
+                                * want to leave this infinite loop
+                                * only when read() returns with
+                                * success, or an expected failure,
+                                * which would be checked by the next
+                                * call to read(2).
+                                */
+                               poll(&pfd, 1, -1);
+                       }
+               }
                return nr;
        }
 }
@@ -375,6 +394,19 @@ FILE *xfdopen(int fd, const char *mode)
        return stream;
 }
 
+FILE *fopen_for_writing(const char *path)
+{
+       FILE *ret = fopen(path, "w");
+
+       if (!ret && errno == EPERM) {
+               if (!unlink(path))
+                       ret = fopen(path, "w");
+               else
+                       errno = EPERM;
+       }
+       return ret;
+}
+
 int xmkstemp(char *template)
 {
        int fd;
@@ -601,18 +633,6 @@ int access_or_die(const char *path, int mode, unsigned flag)
        return ret;
 }
 
-struct passwd *xgetpwuid_self(void)
-{
-       struct passwd *pw;
-
-       errno = 0;
-       pw = getpwuid(getuid());
-       if (!pw)
-               die(_("unable to look up current user in the passwd file: %s"),
-                   errno ? strerror(errno) : _("no such user"));
-       return pw;
-}
-
 char *xgetcwd(void)
 {
        struct strbuf sb = STRBUF_INIT;