00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "rsync.h"
00029
00030 extern int verbose;
00031 extern int dry_run;
00032 extern int module_id;
00033 extern int modify_window;
00034 extern int relative_paths;
00035 extern int human_readable;
00036 extern mode_t orig_umask;
00037 extern char *partial_dir;
00038 extern struct filter_list_struct server_filter_list;
00039
00040 int sanitize_paths = 0;
00041
00042
00043
00044
00045
00046
00047 void set_nonblocking(int fd)
00048 {
00049 int val;
00050
00051 if ((val = fcntl(fd, F_GETFL, 0)) == -1)
00052 return;
00053 if (!(val & NONBLOCK_FLAG)) {
00054 val |= NONBLOCK_FLAG;
00055 fcntl(fd, F_SETFL, val);
00056 }
00057 }
00058
00059
00060
00061
00062 void set_blocking(int fd)
00063 {
00064 int val;
00065
00066 if ((val = fcntl(fd, F_GETFL, 0)) == -1)
00067 return;
00068 if (val & NONBLOCK_FLAG) {
00069 val &= ~NONBLOCK_FLAG;
00070 fcntl(fd, F_SETFL, val);
00071 }
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 int fd_pair(int fd[2])
00081 {
00082 int ret;
00083
00084 #ifdef HAVE_SOCKETPAIR
00085 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
00086 #else
00087 ret = pipe(fd);
00088 #endif
00089
00090 if (ret == 0) {
00091 set_nonblocking(fd[0]);
00092 set_nonblocking(fd[1]);
00093 }
00094
00095 return ret;
00096 }
00097
00098 void print_child_argv(char **cmd)
00099 {
00100 rprintf(FINFO, "opening connection using ");
00101 for (; *cmd; cmd++) {
00102
00103
00104
00105 if (strspn(*cmd, "abcdefghijklmnopqrstuvwxyz"
00106 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00107 "0123456789"
00108 ",.-_=+@/") != strlen(*cmd)) {
00109 rprintf(FINFO, "\"%s\" ", *cmd);
00110 } else {
00111 rprintf(FINFO, "%s ", *cmd);
00112 }
00113 }
00114 rprintf(FINFO, "\n");
00115 }
00116
00117 void out_of_memory(char *str)
00118 {
00119 rprintf(FERROR, "ERROR: out of memory in %s\n", str);
00120 exit_cleanup(RERR_MALLOC);
00121 }
00122
00123 void overflow_exit(char *str)
00124 {
00125 rprintf(FERROR, "ERROR: buffer overflow in %s\n", str);
00126 exit_cleanup(RERR_MALLOC);
00127 }
00128
00129 int set_modtime(char *fname, time_t modtime, mode_t mode)
00130 {
00131 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
00132 if (S_ISLNK(mode))
00133 return 1;
00134 #endif
00135
00136 if (verbose > 2) {
00137 rprintf(FINFO, "set modtime of %s to (%ld) %s",
00138 fname, (long)modtime,
00139 asctime(localtime(&modtime)));
00140 }
00141
00142 if (dry_run)
00143 return 0;
00144
00145 {
00146 #ifdef HAVE_UTIMES
00147 struct timeval t[2];
00148 t[0].tv_sec = time(NULL);
00149 t[0].tv_usec = 0;
00150 t[1].tv_sec = modtime;
00151 t[1].tv_usec = 0;
00152 # ifdef HAVE_LUTIMES
00153 if (S_ISLNK(mode))
00154 return lutimes(fname, t);
00155 # endif
00156 return utimes(fname, t);
00157 #elif defined HAVE_UTIMBUF
00158 struct utimbuf tbuf;
00159 tbuf.actime = time(NULL);
00160 tbuf.modtime = modtime;
00161 return utime(fname,&tbuf);
00162 #elif defined HAVE_UTIME
00163 time_t t[2];
00164 t[0] = time(NULL);
00165 t[1] = modtime;
00166 return utime(fname,t);
00167 #else
00168 #error No file-time-modification routine found!
00169 #endif
00170 }
00171 }
00172
00173
00174
00175
00176 int mkdir_defmode(char *fname)
00177 {
00178 int ret;
00179
00180 umask(orig_umask);
00181 ret = do_mkdir(fname, ACCESSPERMS);
00182 umask(0);
00183
00184 return ret;
00185 }
00186
00187
00188
00189 int create_directory_path(char *fname)
00190 {
00191 char *p;
00192 int ret = 0;
00193
00194 while (*fname == '/')
00195 fname++;
00196 while (strncmp(fname, "./", 2) == 0)
00197 fname += 2;
00198
00199 umask(orig_umask);
00200 p = fname;
00201 while ((p = strchr(p,'/')) != NULL) {
00202 *p = '\0';
00203 if (do_mkdir(fname, ACCESSPERMS) < 0 && errno != EEXIST)
00204 ret = -1;
00205 *p++ = '/';
00206 }
00207 umask(0);
00208
00209 return ret;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 int full_write(int desc, char *ptr, size_t len)
00223 {
00224 int total_written;
00225
00226 total_written = 0;
00227 while (len > 0) {
00228 int written = write(desc, ptr, len);
00229 if (written < 0) {
00230 if (errno == EINTR)
00231 continue;
00232 return written;
00233 }
00234 total_written += written;
00235 ptr += written;
00236 len -= written;
00237 }
00238 return total_written;
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 static int safe_read(int desc, char *ptr, size_t len)
00253 {
00254 int n_chars;
00255
00256 if (len == 0)
00257 return len;
00258
00259 do {
00260 n_chars = read(desc, ptr, len);
00261 } while (n_chars < 0 && errno == EINTR);
00262
00263 return n_chars;
00264 }
00265
00266
00267
00268
00269
00270 int copy_file(const char *source, const char *dest, mode_t mode)
00271 {
00272 int ifd;
00273 int ofd;
00274 char buf[1024 * 8];
00275 int len;
00276
00277 ifd = do_open(source, O_RDONLY, 0);
00278 if (ifd == -1) {
00279 rsyserr(FERROR, errno, "open %s", full_fname(source));
00280 return -1;
00281 }
00282
00283 if (robust_unlink(dest) && errno != ENOENT) {
00284 rsyserr(FERROR, errno, "unlink %s", full_fname(dest));
00285 return -1;
00286 }
00287
00288 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
00289 if (ofd == -1) {
00290 rsyserr(FERROR, errno, "open %s", full_fname(dest));
00291 close(ifd);
00292 return -1;
00293 }
00294
00295 while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {
00296 if (full_write(ofd, buf, len) < 0) {
00297 rsyserr(FERROR, errno, "write %s", full_fname(dest));
00298 close(ifd);
00299 close(ofd);
00300 return -1;
00301 }
00302 }
00303
00304 if (len < 0) {
00305 rsyserr(FERROR, errno, "read %s", full_fname(source));
00306 close(ifd);
00307 close(ofd);
00308 return -1;
00309 }
00310
00311 if (close(ifd) < 0) {
00312 rsyserr(FINFO, errno, "close failed on %s",
00313 full_fname(source));
00314 }
00315
00316 if (close(ofd) < 0) {
00317 rsyserr(FERROR, errno, "close failed on %s",
00318 full_fname(dest));
00319 return -1;
00320 }
00321
00322 return 0;
00323 }
00324
00325
00326 #define MAX_RENAMES_DIGITS 3
00327 #define MAX_RENAMES 1000
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 int robust_unlink(const char *fname)
00340 {
00341 #ifndef ETXTBSY
00342 return do_unlink(fname);
00343 #else
00344 static int counter = 1;
00345 int rc, pos, start;
00346 char path[MAXPATHLEN];
00347
00348 rc = do_unlink(fname);
00349 if (rc == 0 || errno != ETXTBSY)
00350 return rc;
00351
00352 if ((pos = strlcpy(path, fname, MAXPATHLEN)) >= MAXPATHLEN)
00353 pos = MAXPATHLEN - 1;
00354
00355 while (pos > 0 && path[pos-1] != '/')
00356 pos--;
00357 pos += strlcpy(path+pos, ".rsync", MAXPATHLEN-pos);
00358
00359 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
00360 errno = ETXTBSY;
00361 return -1;
00362 }
00363
00364
00365 start = counter;
00366 do {
00367 sprintf(&path[pos], "%03d", counter);
00368 if (++counter >= MAX_RENAMES)
00369 counter = 1;
00370 } while ((rc = access(path, 0)) == 0 && counter != start);
00371
00372 if (verbose > 0) {
00373 rprintf(FINFO,"renaming %s to %s because of text busy\n",
00374 fname, path);
00375 }
00376
00377
00378 if (do_rename(fname, path) != 0) {
00379 errno = ETXTBSY;
00380 return -1;
00381 }
00382 return 0;
00383 #endif
00384 }
00385
00386
00387
00388
00389
00390 int robust_rename(char *from, char *to, char *partialptr,
00391 int mode)
00392 {
00393 int tries = 4;
00394
00395 while (tries--) {
00396 if (do_rename(from, to) == 0)
00397 return 0;
00398
00399 switch (errno) {
00400 #ifdef ETXTBSY
00401 case ETXTBSY:
00402 if (robust_unlink(to) != 0)
00403 return -1;
00404 break;
00405 #endif
00406 case EXDEV:
00407 if (partialptr) {
00408 if (!handle_partial_dir(partialptr,PDIR_CREATE))
00409 return -1;
00410 to = partialptr;
00411 }
00412 if (copy_file(from, to, mode) != 0)
00413 return -2;
00414 do_unlink(from);
00415 return 1;
00416 default:
00417 return -1;
00418 }
00419 }
00420 return -1;
00421 }
00422
00423 static pid_t all_pids[10];
00424 static int num_pids;
00425
00426
00427 pid_t do_fork(void)
00428 {
00429 pid_t newpid = fork();
00430
00431 if (newpid != 0 && newpid != -1) {
00432 all_pids[num_pids++] = newpid;
00433 }
00434 return newpid;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 void kill_all(int sig)
00448 {
00449 int i;
00450
00451 for (i = 0; i < num_pids; i++) {
00452
00453
00454
00455 pid_t p = all_pids[i];
00456
00457 if (p == getpid())
00458 continue;
00459 if (p <= 0)
00460 continue;
00461
00462 kill(p, sig);
00463 }
00464 }
00465
00466
00467 int name_to_uid(char *name, uid_t *uid)
00468 {
00469 struct passwd *pass;
00470 if (!name || !*name)
00471 return 0;
00472 pass = getpwnam(name);
00473 if (pass) {
00474 *uid = pass->pw_uid;
00475 return 1;
00476 }
00477 return 0;
00478 }
00479
00480
00481 int name_to_gid(char *name, gid_t *gid)
00482 {
00483 struct group *grp;
00484 if (!name || !*name)
00485 return 0;
00486 grp = getgrnam(name);
00487 if (grp) {
00488 *gid = grp->gr_gid;
00489 return 1;
00490 }
00491 return 0;
00492 }
00493
00494
00495 int lock_range(int fd, int offset, int len)
00496 {
00497 struct flock lock;
00498
00499 lock.l_type = F_WRLCK;
00500 lock.l_whence = SEEK_SET;
00501 lock.l_start = offset;
00502 lock.l_len = len;
00503 lock.l_pid = 0;
00504
00505 return fcntl(fd,F_SETLK,&lock) == 0;
00506 }
00507
00508 static int filter_server_path(char *arg)
00509 {
00510 char *s;
00511
00512 if (server_filter_list.head) {
00513 for (s = arg; (s = strchr(s, '/')) != NULL; ) {
00514 *s = '\0';
00515 if (check_filter(&server_filter_list, arg, 1) < 0) {
00516
00517 return 1;
00518 }
00519 *s++ = '/';
00520 }
00521 }
00522 return 0;
00523 }
00524
00525 static void glob_expand_one(char *s, char ***argv_ptr, int *argc_ptr,
00526 int *maxargs_ptr)
00527 {
00528 char **argv = *argv_ptr;
00529 int argc = *argc_ptr;
00530 int maxargs = *maxargs_ptr;
00531 #if !defined HAVE_GLOB || !defined HAVE_GLOB_H
00532 if (argc == maxargs) {
00533 maxargs += MAX_ARGS;
00534 if (!(argv = realloc_array(argv, char *, maxargs)))
00535 out_of_memory("glob_expand_one");
00536 *argv_ptr = argv;
00537 *maxargs_ptr = maxargs;
00538 }
00539 if (!*s)
00540 s = ".";
00541 s = argv[argc++] = strdup(s);
00542 filter_server_path(s);
00543 #else
00544 glob_t globbuf;
00545
00546 if (maxargs <= argc)
00547 return;
00548 if (!*s)
00549 s = ".";
00550
00551 if (sanitize_paths)
00552 s = sanitize_path(NULL, s, "", 0);
00553 else
00554 s = strdup(s);
00555
00556 memset(&globbuf, 0, sizeof globbuf);
00557 if (!filter_server_path(s))
00558 glob(s, 0, NULL, &globbuf);
00559 if (MAX((int)globbuf.gl_pathc, 1) > maxargs - argc) {
00560 maxargs += globbuf.gl_pathc + MAX_ARGS;
00561 if (!(argv = realloc_array(argv, char *, maxargs)))
00562 out_of_memory("glob_expand_one");
00563 *argv_ptr = argv;
00564 *maxargs_ptr = maxargs;
00565 }
00566 if (globbuf.gl_pathc == 0)
00567 argv[argc++] = s;
00568 else {
00569 int i;
00570 free(s);
00571 for (i = 0; i < (int)globbuf.gl_pathc; i++) {
00572 if (!(argv[argc++] = strdup(globbuf.gl_pathv[i])))
00573 out_of_memory("glob_expand_one");
00574 }
00575 }
00576 globfree(&globbuf);
00577 #endif
00578 *argc_ptr = argc;
00579 }
00580
00581
00582 void glob_expand(char *base1, char ***argv_ptr, int *argc_ptr, int *maxargs_ptr)
00583 {
00584 char *s = (*argv_ptr)[*argc_ptr];
00585 char *p, *q;
00586 char *base = base1;
00587 int base_len = strlen(base);
00588
00589 if (!s || !*s)
00590 return;
00591
00592 if (strncmp(s, base, base_len) == 0)
00593 s += base_len;
00594
00595 if (!(s = strdup(s)))
00596 out_of_memory("glob_expand");
00597
00598 if (asprintf(&base," %s/", base1) <= 0)
00599 out_of_memory("glob_expand");
00600 base_len++;
00601
00602 for (q = s; *q; q = p + base_len) {
00603 if ((p = strstr(q, base)) != NULL)
00604 *p = '\0';
00605 glob_expand_one(q, argv_ptr, argc_ptr, maxargs_ptr);
00606 if (!p)
00607 break;
00608 }
00609
00610 free(s);
00611 free(base);
00612 }
00613
00614
00615
00616
00617 void strlower(char *s)
00618 {
00619 while (*s) {
00620 if (isupper(*(unsigned char *)s))
00621 *s = tolower(*(unsigned char *)s);
00622 s++;
00623 }
00624 }
00625
00626
00627
00628
00629
00630 size_t pathjoin(char *dest, size_t destsize, const char *p1, const char *p2)
00631 {
00632 size_t len = strlcpy(dest, p1, destsize);
00633 if (len < destsize - 1) {
00634 if (!len || dest[len-1] != '/')
00635 dest[len++] = '/';
00636 if (len < destsize - 1)
00637 len += strlcpy(dest + len, p2, destsize - len);
00638 else {
00639 dest[len] = '\0';
00640 len += strlen(p2);
00641 }
00642 }
00643 else
00644 len += strlen(p2) + 1;
00645 return len;
00646 }
00647
00648
00649
00650
00651
00652 size_t stringjoin(char *dest, size_t destsize, ...)
00653 {
00654 va_list ap;
00655 size_t len, ret = 0;
00656 const char *src;
00657
00658 va_start(ap, destsize);
00659 while (1) {
00660 if (!(src = va_arg(ap, const char *)))
00661 break;
00662 len = strlen(src);
00663 ret += len;
00664 if (destsize > 1) {
00665 if (len >= destsize)
00666 len = destsize - 1;
00667 memcpy(dest, src, len);
00668 destsize -= len;
00669 dest += len;
00670 }
00671 }
00672 *dest = '\0';
00673 va_end(ap);
00674
00675 return ret;
00676 }
00677
00678 int count_dir_elements(const char *p)
00679 {
00680 int cnt = 0, new_component = 1;
00681 while (*p) {
00682 if (*p++ == '/')
00683 new_component = 1;
00684 else if (new_component) {
00685 new_component = 0;
00686 cnt++;
00687 }
00688 }
00689 return cnt;
00690 }
00691
00692
00693
00694
00695
00696 unsigned int clean_fname(char *name, BOOL collapse_dot_dot)
00697 {
00698 char *limit = name - 1, *t = name, *f = name;
00699 int anchored;
00700
00701 if (!name)
00702 return 0;
00703
00704 if ((anchored = *f == '/') != 0)
00705 *t++ = *f++;
00706 while (*f) {
00707
00708 if (*f == '/') {
00709 f++;
00710 continue;
00711 }
00712 if (*f == '.') {
00713
00714 if (f[1] == '/') {
00715 f += 2;
00716 continue;
00717 }
00718
00719 if (collapse_dot_dot
00720 && f[1] == '.' && (f[2] == '/' || !f[2])) {
00721 char *s = t - 1;
00722 if (s == name && anchored) {
00723 f += 2;
00724 continue;
00725 }
00726 while (s > limit && *--s != '/') {}
00727 if (s != t - 1 && (s < name || *s == '/')) {
00728 t = s + 1;
00729 f += 2;
00730 continue;
00731 }
00732 limit = t + 2;
00733 }
00734 }
00735 while (*f && (*t++ = *f++) != '/') {}
00736 }
00737
00738 if (t > name+anchored && t[-1] == '/')
00739 t--;
00740 if (t == name)
00741 *t++ = '.';
00742 *t = '\0';
00743
00744 return t - name;
00745 }
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 char *sanitize_path(char *dest, const char *p, const char *rootdir, int depth)
00770 {
00771 char *start, *sanp;
00772 int rlen = 0, leave_one_dotdir = relative_paths;
00773
00774 if (dest != p) {
00775 int plen = strlen(p);
00776 if (*p == '/') {
00777 if (!rootdir)
00778 rootdir = lp_path(module_id);
00779 rlen = strlen(rootdir);
00780 depth = 0;
00781 p++;
00782 }
00783 if (dest) {
00784 if (rlen + plen + 1 >= MAXPATHLEN)
00785 return NULL;
00786 } else if (!(dest = new_array(char, rlen + plen + 1)))
00787 out_of_memory("sanitize_path");
00788 if (rlen) {
00789 memcpy(dest, rootdir, rlen);
00790 if (rlen > 1)
00791 dest[rlen++] = '/';
00792 }
00793 }
00794
00795 start = sanp = dest + rlen;
00796 while (*p != '\0') {
00797
00798 if (*p == '/') {
00799 p++;
00800 continue;
00801 }
00802
00803
00804
00805
00806 if (*p == '.' && (p[1] == '/' || p[1] == '\0')) {
00807 if (leave_one_dotdir && p[1])
00808 leave_one_dotdir = 0;
00809 else {
00810
00811 p++;
00812 continue;
00813 }
00814 }
00815 if (*p == '.' && p[1] == '.' && (p[2] == '/' || p[2] == '\0')) {
00816
00817 if (depth <= 0 || sanp != start) {
00818 p += 2;
00819 if (sanp != start) {
00820
00821 --sanp;
00822 while (sanp > start && sanp[-1] != '/') {
00823
00824 sanp--;
00825 }
00826 }
00827 continue;
00828 }
00829
00830 depth--;
00831
00832 start = sanp + 3;
00833 }
00834
00835 while (*p && (*sanp++ = *p++) != '/') {}
00836 }
00837 if (sanp == dest) {
00838
00839 *sanp++ = '.';
00840 }
00841 *sanp = '\0';
00842
00843 return dest;
00844 }
00845
00846 char curr_dir[MAXPATHLEN];
00847 unsigned int curr_dir_len;
00848
00849
00850
00851
00852
00853
00854 int push_dir(char *dir)
00855 {
00856 static int initialised;
00857 unsigned int len;
00858
00859 if (!initialised) {
00860 initialised = 1;
00861 getcwd(curr_dir, sizeof curr_dir - 1);
00862 curr_dir_len = strlen(curr_dir);
00863 }
00864
00865 if (!dir)
00866 return 0;
00867
00868 len = strlen(dir);
00869 if (len == 1 && *dir == '.')
00870 return 1;
00871
00872 if ((*dir == '/' ? len : curr_dir_len + 1 + len) >= sizeof curr_dir)
00873 return 0;
00874
00875 if (chdir(dir))
00876 return 0;
00877
00878 if (*dir == '/') {
00879 memcpy(curr_dir, dir, len + 1);
00880 curr_dir_len = len;
00881 } else {
00882 curr_dir[curr_dir_len++] = '/';
00883 memcpy(curr_dir + curr_dir_len, dir, len + 1);
00884 curr_dir_len += len;
00885 }
00886
00887 curr_dir_len = clean_fname(curr_dir, 1);
00888
00889 return 1;
00890 }
00891
00892
00893
00894
00895
00896 int pop_dir(char *dir)
00897 {
00898 if (chdir(dir))
00899 return 0;
00900
00901 curr_dir_len = strlcpy(curr_dir, dir, sizeof curr_dir);
00902 if (curr_dir_len >= sizeof curr_dir)
00903 curr_dir_len = sizeof curr_dir - 1;
00904
00905 return 1;
00906 }
00907
00908
00909
00910
00911
00912
00913 char *full_fname(const char *fn)
00914 {
00915 static char *result = NULL;
00916 char *m1, *m2, *m3;
00917 char *p1, *p2;
00918
00919 if (result)
00920 free(result);
00921
00922 if (*fn == '/')
00923 p1 = p2 = "";
00924 else {
00925 p1 = curr_dir;
00926 for (p2 = p1; *p2 == '/'; p2++) {}
00927 if (*p2)
00928 p2 = "/";
00929 }
00930 if (module_id >= 0) {
00931 m1 = " (in ";
00932 m2 = lp_name(module_id);
00933 m3 = ")";
00934 if (p1 == curr_dir) {
00935 if (!lp_use_chroot(module_id)) {
00936 char *p = lp_path(module_id);
00937 if (*p != '/' || p[1])
00938 p1 += strlen(p);
00939 }
00940 }
00941 } else
00942 m1 = m2 = m3 = "";
00943
00944 asprintf(&result, "\"%s%s%s\"%s%s%s", p1, p2, fn, m1, m2, m3);
00945
00946 return result;
00947 }
00948
00949 static char partial_fname[MAXPATHLEN];
00950
00951 char *partial_dir_fname(const char *fname)
00952 {
00953 char *t = partial_fname;
00954 int sz = sizeof partial_fname;
00955 const char *fn;
00956
00957 if ((fn = strrchr(fname, '/')) != NULL) {
00958 fn++;
00959 if (*partial_dir != '/') {
00960 int len = fn - fname;
00961 strncpy(t, fname, len);
00962 t += len;
00963 sz -= len;
00964 }
00965 } else
00966 fn = fname;
00967 if ((int)pathjoin(t, sz, partial_dir, fn) >= sz)
00968 return NULL;
00969 if (server_filter_list.head) {
00970 static int len;
00971 if (!len)
00972 len = strlen(partial_dir);
00973 t[len] = '\0';
00974 if (check_filter(&server_filter_list, partial_fname, 1) < 0)
00975 return NULL;
00976 t[len] = '/';
00977 if (check_filter(&server_filter_list, partial_fname, 0) < 0)
00978 return NULL;
00979 }
00980
00981 return partial_fname;
00982 }
00983
00984
00985
00986 int handle_partial_dir(const char *fname, int create)
00987 {
00988 char *fn, *dir;
00989
00990 if (fname != partial_fname)
00991 return 1;
00992 if (!create && *partial_dir == '/')
00993 return 1;
00994 if (!(fn = strrchr(partial_fname, '/')))
00995 return 1;
00996
00997 *fn = '\0';
00998 dir = partial_fname;
00999 if (create) {
01000 STRUCT_STAT st;
01001 int statret = do_lstat(dir, &st);
01002 if (statret == 0 && !S_ISDIR(st.st_mode)) {
01003 if (do_unlink(dir) < 0)
01004 return 0;
01005 statret = -1;
01006 }
01007 if (statret < 0 && do_mkdir(dir, 0700) < 0)
01008 return 0;
01009 } else
01010 do_rmdir(dir);
01011 *fn = '/';
01012
01013 return 1;
01014 }
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040 int unsafe_symlink(const char *dest, const char *src)
01041 {
01042 const char *name, *slash;
01043 int depth = 0;
01044
01045
01046 if (!dest || !*dest || *dest == '/')
01047 return 1;
01048
01049
01050 for (name = src; (slash = strchr(name, '/')) != 0; name = slash+1) {
01051 if (strncmp(name, "../", 3) == 0) {
01052 depth = 0;
01053 } else if (strncmp(name, "./", 2) == 0) {
01054
01055 } else {
01056 depth++;
01057 }
01058 }
01059 if (strcmp(name, "..") == 0)
01060 depth = 0;
01061
01062 for (name = dest; (slash = strchr(name, '/')) != 0; name = slash+1) {
01063 if (strncmp(name, "../", 3) == 0) {
01064
01065
01066 if (--depth < 0)
01067 return 1;
01068 } else if (strncmp(name, "./", 2) == 0) {
01069
01070 } else {
01071 depth++;
01072 }
01073 }
01074 if (strcmp(name, "..") == 0)
01075 depth--;
01076
01077 return (depth < 0);
01078 }
01079
01080
01081
01082
01083 char *human_num(int64 num)
01084 {
01085 static char bufs[4][128];
01086 static unsigned int n;
01087 char *s;
01088
01089 n = (n + 1) % (sizeof bufs / sizeof bufs[0]);
01090
01091 if (human_readable) {
01092 char units = '\0';
01093 int mult = human_readable == 1 ? 1000 : 1024;
01094 double dnum = 0;
01095 if (num > mult*mult*mult) {
01096 dnum = (double)num / (mult*mult*mult);
01097 units = 'G';
01098 } else if (num > mult*mult) {
01099 dnum = (double)num / (mult*mult);
01100 units = 'M';
01101 } else if (num > mult) {
01102 dnum = (double)num / mult;
01103 units = 'K';
01104 }
01105 if (units) {
01106 sprintf(bufs[n], "%.2f%c", dnum, units);
01107 return bufs[n];
01108 }
01109 }
01110
01111 s = bufs[n] + sizeof bufs[0] - 1;
01112 *s = '\0';
01113
01114 if (!num)
01115 *--s = '0';
01116 while (num) {
01117 *--s = (num % 10) + '0';
01118 num /= 10;
01119 }
01120 return s;
01121 }
01122
01123
01124
01125
01126 char *human_dnum(double dnum, int decimal_digits)
01127 {
01128 char *buf = human_num(dnum);
01129 int len = strlen(buf);
01130 if (isdigit(*(uchar*)(buf+len-1))) {
01131
01132 buf -= decimal_digits + 1;
01133 snprintf(buf, len + decimal_digits + 2, "%.*f", decimal_digits, dnum);
01134 }
01135 return buf;
01136 }
01137
01138
01139
01140
01141 char *timestring(time_t t)
01142 {
01143 static char TimeBuf[200];
01144 struct tm *tm = localtime(&t);
01145 char *p;
01146
01147 #ifdef HAVE_STRFTIME
01148 strftime(TimeBuf, sizeof TimeBuf - 1, "%Y/%m/%d %H:%M:%S", tm);
01149 #else
01150 strlcpy(TimeBuf, asctime(tm), sizeof TimeBuf);
01151 #endif
01152
01153 if ((p = strchr(TimeBuf, '\n')) != NULL)
01154 *p = '\0';
01155
01156 return TimeBuf;
01157 }
01158
01159
01160
01161
01162
01163
01164
01165 int msleep(int t)
01166 {
01167 int tdiff = 0;
01168 struct timeval tval, t1, t2;
01169
01170 gettimeofday(&t1, NULL);
01171
01172 while (tdiff < t) {
01173 tval.tv_sec = (t-tdiff)/1000;
01174 tval.tv_usec = 1000*((t-tdiff)%1000);
01175
01176 errno = 0;
01177 select(0,NULL,NULL, NULL, &tval);
01178
01179 gettimeofday(&t2, NULL);
01180 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
01181 (t2.tv_usec - t1.tv_usec)/1000;
01182 }
01183
01184 return True;
01185 }
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196 int cmp_time(time_t file1, time_t file2)
01197 {
01198 if (file2 > file1) {
01199 if (file2 - file1 <= modify_window)
01200 return 0;
01201 return -1;
01202 }
01203 if (file1 - file2 <= modify_window)
01204 return 0;
01205 return 1;
01206 }
01207
01208
01209 #ifdef __INSURE__XX
01210 #include <dlfcn.h>
01211
01212
01213
01214
01215
01216
01217 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
01218 {
01219 static int (*fn)();
01220 int ret;
01221 char *cmd;
01222
01223 asprintf(&cmd, "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'",
01224 getpid(), getpid(), getpid());
01225
01226 if (!fn) {
01227 static void *h;
01228 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
01229 fn = dlsym(h, "_Insure_trap_error");
01230 }
01231
01232 ret = fn(a1, a2, a3, a4, a5, a6);
01233
01234 system(cmd);
01235
01236 free(cmd);
01237
01238 return ret;
01239 }
01240 #endif
01241
01242 #define MALLOC_MAX 0x40000000
01243
01244 void *_new_array(unsigned int size, unsigned long num)
01245 {
01246 if (num >= MALLOC_MAX/size)
01247 return NULL;
01248 return malloc(size * num);
01249 }
01250
01251 void *_realloc_array(void *ptr, unsigned int size, unsigned long num)
01252 {
01253 if (num >= MALLOC_MAX/size)
01254 return NULL;
01255
01256 if (!ptr)
01257 return malloc(size * num);
01258 return realloc(ptr, size * num);
01259 }
01260
01261
01262
01263
01264 const char *find_filename_suffix(const char *fn, int fn_len, int *len_ptr)
01265 {
01266 const char *suf, *s;
01267 BOOL had_tilde;
01268 int s_len;
01269
01270
01271 while (fn_len && *fn == '.') fn++, fn_len--;
01272
01273
01274 if (fn_len > 1 && fn[fn_len-1] == '~')
01275 fn_len--, had_tilde = True;
01276 else
01277 had_tilde = False;
01278
01279
01280 suf = "";
01281 *len_ptr = 0;
01282
01283
01284 for (s = fn + fn_len; fn_len > 1; ) {
01285 while (*--s != '.' && s != fn) {}
01286 if (s == fn)
01287 break;
01288 s_len = fn_len - (s - fn);
01289 fn_len = s - fn;
01290 if (s_len == 4) {
01291 if (strcmp(s+1, "bak") == 0
01292 || strcmp(s+1, "old") == 0)
01293 continue;
01294 } else if (s_len == 5) {
01295 if (strcmp(s+1, "orig") == 0)
01296 continue;
01297 } else if (s_len > 2 && had_tilde
01298 && s[1] == '~' && isdigit(*(uchar*)(s+2)))
01299 continue;
01300 *len_ptr = s_len;
01301 suf = s;
01302 if (s_len == 1)
01303 break;
01304
01305 for (s++, s_len--; s_len > 0; s++, s_len--) {
01306 if (!isdigit(*(uchar*)s))
01307 return suf;
01308 }
01309
01310 s = suf;
01311 }
01312
01313 return suf;
01314 }
01315
01316
01317
01318
01319
01320
01321
01322
01323 #define UNIT (1 << 16)
01324
01325 uint32 fuzzy_distance(const char *s1, int len1, const char *s2, int len2)
01326 {
01327 uint32 a[MAXPATHLEN], diag, above, left, diag_inc, above_inc, left_inc;
01328 int32 cost;
01329 int i1, i2;
01330
01331 if (!len1 || !len2) {
01332 if (!len1) {
01333 s1 = s2;
01334 len1 = len2;
01335 }
01336 for (i1 = 0, cost = 0; i1 < len1; i1++)
01337 cost += s1[i1];
01338 return (int32)len1 * UNIT + cost;
01339 }
01340
01341 for (i2 = 0; i2 < len2; i2++)
01342 a[i2] = (i2+1) * UNIT;
01343
01344 for (i1 = 0; i1 < len1; i1++) {
01345 diag = i1 * UNIT;
01346 above = (i1+1) * UNIT;
01347 for (i2 = 0; i2 < len2; i2++) {
01348 left = a[i2];
01349 if ((cost = *((uchar*)s1+i1) - *((uchar*)s2+i2)) != 0) {
01350 if (cost < 0)
01351 cost = UNIT - cost;
01352 else
01353 cost = UNIT + cost;
01354 }
01355 diag_inc = diag + cost;
01356 left_inc = left + UNIT + *((uchar*)s1+i1);
01357 above_inc = above + UNIT + *((uchar*)s2+i2);
01358 a[i2] = above = left < above
01359 ? (left_inc < diag_inc ? left_inc : diag_inc)
01360 : (above_inc < diag_inc ? above_inc : diag_inc);
01361 diag = left;
01362 }
01363 }
01364
01365 return a[len2-1];
01366 }
01367
01368 #define BB_SLOT_SIZE (16*1024)
01369 #define BB_PER_SLOT_BITS (BB_SLOT_SIZE * 8)
01370 #define BB_PER_SLOT_INTS (BB_SLOT_SIZE / 4)
01371
01372 struct bitbag {
01373 uint32 **bits;
01374 int slot_cnt;
01375 };
01376
01377 struct bitbag *bitbag_create(int max_ndx)
01378 {
01379 struct bitbag *bb = new(struct bitbag);
01380 bb->slot_cnt = (max_ndx + BB_PER_SLOT_BITS - 1) / BB_PER_SLOT_BITS;
01381
01382 if (!(bb->bits = (uint32**)calloc(bb->slot_cnt, sizeof (uint32*))))
01383 out_of_memory("bitbag_create");
01384
01385 return bb;
01386 }
01387
01388 void bitbag_set_bit(struct bitbag *bb, int ndx)
01389 {
01390 int slot = ndx / BB_PER_SLOT_BITS;
01391 ndx %= BB_PER_SLOT_BITS;
01392
01393 if (!bb->bits[slot]) {
01394 if (!(bb->bits[slot] = (uint32*)calloc(BB_PER_SLOT_INTS, 4)))
01395 out_of_memory("bitbag_set_bit");
01396 }
01397
01398 bb->bits[slot][ndx/32] |= 1u << (ndx % 32);
01399 }
01400
01401 #if 0
01402 void bitbag_clear_bit(struct bitbag *bb, int ndx)
01403 {
01404 int slot = ndx / BB_PER_SLOT_BITS;
01405 ndx %= BB_PER_SLOT_BITS;
01406
01407 if (!bb->bits[slot])
01408 return;
01409
01410 bb->bits[slot][ndx/32] &= ~(1u << (ndx % 32));
01411 }
01412
01413 int bitbag_check_bit(struct bitbag *bb, int ndx)
01414 {
01415 int slot = ndx / BB_PER_SLOT_BITS;
01416 ndx %= BB_PER_SLOT_BITS;
01417
01418 if (!bb->bits[slot])
01419 return 0;
01420
01421 return bb->bits[slot][ndx/32] & (1u << (ndx % 32)) ? 1 : 0;
01422 }
01423 #endif
01424
01425
01426 int bitbag_next_bit(struct bitbag *bb, int after)
01427 {
01428 uint32 bits, mask;
01429 int i, ndx = after + 1;
01430 int slot = ndx / BB_PER_SLOT_BITS;
01431 ndx %= BB_PER_SLOT_BITS;
01432
01433 mask = (1u << (ndx % 32)) - 1;
01434 for (i = ndx / 32; slot < bb->slot_cnt; slot++, i = mask = 0) {
01435 if (!bb->bits[slot])
01436 continue;
01437 for ( ; i < BB_PER_SLOT_INTS; i++, mask = 0) {
01438 if (!(bits = bb->bits[slot][i] & ~mask))
01439 continue;
01440
01441
01442 switch (bits ^ (bits & (bits-1))) {
01443 #define LOG2(n) case 1u << n: return slot*BB_PER_SLOT_BITS + i*32 + n
01444 LOG2(0); LOG2(1); LOG2(2); LOG2(3);
01445 LOG2(4); LOG2(5); LOG2(6); LOG2(7);
01446 LOG2(8); LOG2(9); LOG2(10); LOG2(11);
01447 LOG2(12); LOG2(13); LOG2(14); LOG2(15);
01448 LOG2(16); LOG2(17); LOG2(18); LOG2(19);
01449 LOG2(20); LOG2(21); LOG2(22); LOG2(23);
01450 LOG2(24); LOG2(25); LOG2(26); LOG2(27);
01451 LOG2(28); LOG2(29); LOG2(30); LOG2(31);
01452 }
01453 return -1;
01454 }
01455 }
01456
01457 return -1;
01458 }