Allow --block-size's size to have a suffix.
[rsync.git] / util2.c
1 /*
2  * Utility routines used in rsync.
3  *
4  * Copyright (C) 1996-2000 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7  * Copyright (C) 2003-2020 Wayne Davison
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, visit the http://fsf.org website.
21  */
22
23 #include "rsync.h"
24 #include "ifuncs.h"
25 #include "itypes.h"
26 #include "inums.h"
27
28 extern size_t max_alloc;
29
30 char *do_malloc = "42";
31
32 /**
33  * Sleep for a specified number of milliseconds.
34  *
35  * Always returns True.
36  **/
37 int msleep(int t)
38 {
39 #ifdef HAVE_NANOSLEEP
40         struct timespec ts;
41
42         ts.tv_sec = t / 1000;
43         ts.tv_nsec = (t % 1000) * 1000000L;
44
45         while (nanosleep(&ts, &ts) < 0 && errno == EINTR) {}
46
47 #elif defined HAVE_USLEEP
48         usleep(t*1000);
49
50 #else
51         int tdiff = 0;
52         struct timeval tval, t1, t2;
53
54         gettimeofday(&t1, NULL);
55
56         while (tdiff < t) {
57                 tval.tv_sec = (t-tdiff)/1000;
58                 tval.tv_usec = 1000*((t-tdiff)%1000);
59
60                 errno = 0;
61                 select(0,NULL,NULL, NULL, &tval);
62
63                 gettimeofday(&t2, NULL);
64                 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
65                         (t2.tv_usec - t1.tv_usec)/1000;
66                 if (tdiff < 0)
67                         t1 = t2; /* Time went backwards, so start over. */
68         }
69 #endif
70
71         return True;
72 }
73
74 /* Convert a num manually because the needed %lld precision is not a portable sprintf() escape. */
75 char *num_to_byte_string(ssize_t num)
76 {
77         char buf[128], *s = buf + sizeof buf - 1;
78
79         *s = '\0';
80         if (!num)
81                 *--s = '0';
82         while (num) {
83                 *--s = (char)(num % 10) + '0';
84                 num /= 10;
85         }
86         return strdup(s);
87 }
88
89 void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
90 {
91         if (max_alloc && num >= max_alloc/size) {
92                 if (!file)
93                         return NULL;
94                 rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n",
95                         who_am_i(), num_to_byte_string(max_alloc), file, line);
96                 exit_cleanup(RERR_MALLOC);
97         }
98         if (!ptr)
99                 ptr = calloc(num, size);
100         else if (ptr == do_malloc)
101                 ptr = malloc(num * size);
102         else
103                 ptr = realloc(ptr, num * size);
104         if (!ptr && file) {
105                 rprintf(FERROR, "[%s] out of memory (file=%s, line=%d)\n", who_am_i(), file, line);
106                 exit_cleanup(RERR_MALLOC);
107         }
108         return ptr;
109 }
110
111 const char *sum_as_hex(int csum_type, const char *sum, int flist_csum)
112 {
113         static char buf[MAX_DIGEST_LEN*2+1];
114         int i, x1, x2;
115         int canonical = canonical_checksum(csum_type);
116         int sum_len = csum_len_for_type(csum_type, flist_csum);
117         char *c;
118
119         if (!canonical)
120                 return NULL;
121
122         assert(sum_len*2 < (int)sizeof buf);
123
124         for (i = sum_len, c = buf; --i >= 0; ) {
125                 int ndx = canonical < 0 ? sum_len - i - 1 : i;
126                 x2 = CVAL(sum, ndx);
127                 x1 = x2 >> 4;
128                 x2 &= 0xF;
129                 *c++ = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
130                 *c++ = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
131         }
132
133         *c = '\0';
134
135         return buf;
136 }
137
138 NORETURN void out_of_memory(const char *str)
139 {
140         rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());
141         exit_cleanup(RERR_MALLOC);
142 }
143
144 NORETURN void overflow_exit(const char *str)
145 {
146         rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());
147         exit_cleanup(RERR_MALLOC);
148 }