10b7e4b6adb9b0a31e3733d0d0f92d39e7be229f
[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 /* We convert a num manually because need %lld precision, and that's 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         while (num) {
81                 *--s = (char)(num % 10) + '0';
82                 num /= 10;
83         }
84         return strdup(s);
85 }
86
87 void *_my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
88 {
89         if (num >= max_alloc/size) {
90                 if (!file)
91                         return NULL;
92                 rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n",
93                         who_am_i(), num_to_byte_string(max_alloc), file, line);
94                 exit_cleanup(RERR_MALLOC);
95         }
96         if (!ptr)
97                 ptr = calloc(num, size);
98         else if (ptr == do_malloc)
99                 ptr = malloc(num * size);
100         else
101                 ptr = realloc(ptr, num * size);
102         if (!ptr && file) {
103                 rprintf(FERROR, "[%s] out of memory (file=%s, line=%d)\n", who_am_i(), file, line);
104                 exit_cleanup(RERR_MALLOC);
105         }
106         return ptr;
107 }
108
109 const char *sum_as_hex(int csum_type, const char *sum, int flist_csum)
110 {
111         static char buf[MAX_DIGEST_LEN*2+1];
112         int i, x1, x2;
113         int canonical = canonical_checksum(csum_type);
114         int sum_len = csum_len_for_type(csum_type, flist_csum);
115         char *c;
116
117         if (!canonical)
118                 return NULL;
119
120         assert(sum_len*2 < (int)sizeof buf);
121
122         for (i = sum_len, c = buf; --i >= 0; ) {
123                 int ndx = canonical < 0 ? sum_len - i - 1 : i;
124                 x2 = CVAL(sum, ndx);
125                 x1 = x2 >> 4;
126                 x2 &= 0xF;
127                 *c++ = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
128                 *c++ = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
129         }
130
131         *c = '\0';
132
133         return buf;
134 }
135
136 NORETURN void out_of_memory(const char *str)
137 {
138         rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());
139         exit_cleanup(RERR_MALLOC);
140 }
141
142 NORETURN void overflow_exit(const char *str)
143 {
144         rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());
145         exit_cleanup(RERR_MALLOC);
146 }