More tweaks for Actions.
[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 /**
29  * Sleep for a specified number of milliseconds.
30  *
31  * Always returns True.
32  **/
33 int msleep(int t)
34 {
35 #ifdef HAVE_NANOSLEEP
36         struct timespec ts;
37
38         ts.tv_sec = t / 1000;
39         ts.tv_nsec = (t % 1000) * 1000000L;
40
41         while (nanosleep(&ts, &ts) < 0 && errno == EINTR) {}
42
43 #elif defined HAVE_USLEEP
44         usleep(t*1000);
45
46 #else
47         int tdiff = 0;
48         struct timeval tval, t1, t2;
49
50         gettimeofday(&t1, NULL);
51
52         while (tdiff < t) {
53                 tval.tv_sec = (t-tdiff)/1000;
54                 tval.tv_usec = 1000*((t-tdiff)%1000);
55
56                 errno = 0;
57                 select(0,NULL,NULL, NULL, &tval);
58
59                 gettimeofday(&t2, NULL);
60                 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
61                         (t2.tv_usec - t1.tv_usec)/1000;
62                 if (tdiff < 0)
63                         t1 = t2; /* Time went backwards, so start over. */
64         }
65 #endif
66
67         return True;
68 }
69
70 #define MALLOC_MAX 0x40000000
71
72 void *_new_array(unsigned long num, unsigned int size, int use_calloc)
73 {
74         if (num >= MALLOC_MAX/size)
75                 return NULL;
76         return use_calloc ? calloc(num, size) : malloc(num * size);
77 }
78
79 void *_realloc_array(void *ptr, unsigned int size, size_t num)
80 {
81         if (num >= MALLOC_MAX/size)
82                 return NULL;
83         if (!ptr)
84                 return malloc(size * num);
85         return realloc(ptr, size * num);
86 }
87
88 const char *sum_as_hex(int csum_type, const char *sum, int flist_csum)
89 {
90         static char buf[MAX_DIGEST_LEN*2+1];
91         int i, x1, x2;
92         int canonical = canonical_checksum(csum_type);
93         int sum_len = csum_len_for_type(csum_type, flist_csum);
94         char *c;
95
96         if (!canonical)
97                 return NULL;
98
99         assert(sum_len*2 < (int)sizeof buf);
100
101         for (i = sum_len, c = buf; --i >= 0; ) {
102                 int ndx = canonical < 0 ? sum_len - i - 1 : i;
103                 x2 = CVAL(sum, ndx);
104                 x1 = x2 >> 4;
105                 x2 &= 0xF;
106                 *c++ = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
107                 *c++ = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
108         }
109
110         *c = '\0';
111
112         return buf;
113 }
114
115 NORETURN void out_of_memory(const char *str)
116 {
117         rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());
118         exit_cleanup(RERR_MALLOC);
119 }
120
121 NORETURN void overflow_exit(const char *str)
122 {
123         rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());
124         exit_cleanup(RERR_MALLOC);
125 }