X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/e42c9458c2f1e3a78d6d45e99741d6edb38fc0cc..8d9dc9f99db13d42272b7c150f2e6849331ccdc1:/util.c diff --git a/util.c b/util.c index cc39cdb4..5b16fc4b 100644 --- a/util.c +++ b/util.c @@ -60,44 +60,49 @@ struct map_struct *map_file(int fd,OFF_T len) char *map_ptr(struct map_struct *map,OFF_T offset,int len) { - int nread = -2; + int nread; - if (map->map) - return map->map+offset; + if (map->map) + return map->map+offset; - if (len == 0) - return NULL; + if (len == 0) + return NULL; - if (len > (map->size-offset)) - len = map->size-offset; + if (len > (map->size-offset)) + len = map->size-offset; - if (offset >= map->p_offset && - offset+len <= map->p_offset+map->p_len) { - return (map->p + (offset - map->p_offset)); - } + if (offset >= map->p_offset && + offset+len <= map->p_offset+map->p_len) { + return (map->p + (offset - map->p_offset)); + } - len = MAX(len,CHUNK_SIZE); - if (len > (map->size-offset)) - len = map->size-offset; + len = MAX(len,CHUNK_SIZE); + if (len > (map->size-offset)) + len = map->size-offset; - if (len > map->p_size) { - if (map->p) free(map->p); - map->p = (char *)malloc(len); - if (!map->p) out_of_memory("map_ptr"); - map->p_size = len; - } + if (len > map->p_size) { + if (map->p) free(map->p); + map->p = (char *)malloc(len); + if (!map->p) out_of_memory("map_ptr"); + map->p_size = len; + } - if (do_lseek(map->fd,offset,SEEK_SET) != offset || - (nread=read(map->fd,map->p,len)) != len) { - rprintf(FERROR,"EOF in map_ptr! (offset=%d len=%d nread=%d errno=%d)\n", - (int)offset, len, nread, errno); - exit_cleanup(1); - } + map->p_offset = offset; + map->p_len = len; - map->p_offset = offset; - map->p_len = len; + if (do_lseek(map->fd,offset,SEEK_SET) != offset) { + rprintf(FERROR,"lseek failed in map_ptr\n"); + exit_cleanup(1); + } - return map->p; + if ((nread=read(map->fd,map->p,len)) != len) { + if (nread < 0) nread = 0; + /* the best we can do is zero the buffer - the file + has changed mid transfer! */ + memset(map->p+nread, 0, len - nread); + } + + return map->p; } @@ -628,26 +633,19 @@ int vslprintf(char *str, int n, const char *format, va_list ap) buf = malloc(len); if (!buf) { /* can't call debug or we would recurse */ - exit(1); + exit_cleanup(1); } } - ret = vsprintf(buf, format, ap); - - if (ret < 0) { - str[0] = 0; - return -1; - } - - if (ret < n) { - n = ret; - } else if (ret > n) { - ret = -1; + vsprintf(buf, format, ap); + ret = strlen(buf); + if (ret > n) { + /* yikes! */ + exit_cleanup(1); } - - buf[n] = 0; + buf[ret] = 0; - memcpy(str, buf, n+1); + memcpy(str, buf, ret+1); return ret; #endif @@ -665,3 +663,4 @@ int slprintf(char *str, int n, char *format, ...) va_end(ap); return ret; } +