Line data Source code
1 : #include "../../burp.h"
2 : #include "../../alloc.h"
3 : #include "../../conf.h"
4 : #include "../../conffile.h"
5 : #include "../../handy.h"
6 : #include "../../fsops.h"
7 : #include "../../fzp.h"
8 : #include "../../lock.h"
9 : #include "../../log.h"
10 : #include "../../prepend.h"
11 : #include "../../strlist.h"
12 : #include "bedup.h"
13 :
14 : #include <uthash.h>
15 :
16 : #define LOCKFILE_NAME "lockfile"
17 : #define BEDUP_LOCKFILE_NAME "lockfile.bedup"
18 :
19 : #define DEF_MAX_LINKS 10000
20 :
21 : static int makelinks=0;
22 : static int deletedups=0;
23 :
24 : static uint64_t savedbytes=0;
25 : static uint64_t count=0;
26 : static int ccount=0;
27 :
28 : static struct lock *locklist=NULL;
29 :
30 : static int verbose=0;
31 :
32 : static unsigned int maxlinks=DEF_MAX_LINKS;
33 : static char ext[16]="";
34 :
35 : typedef struct file file_t;
36 :
37 : struct file
38 : {
39 : char *path;
40 : dev_t dev;
41 : ino_t ino;
42 : nlink_t nlink;
43 : uint64_t full_cksum;
44 : uint64_t part_cksum;
45 : file_t *next;
46 : };
47 :
48 : struct mystruct
49 : {
50 : off_t st_size;
51 : file_t *files;
52 : UT_hash_handle hh;
53 : };
54 :
55 : struct mystruct *myfiles=NULL;
56 :
57 4 : static struct mystruct *find_key(off_t st_size)
58 : {
59 : struct mystruct *s;
60 :
61 4 : HASH_FIND_INT(myfiles, &st_size, s);
62 4 : return s;
63 : }
64 :
65 2 : static int add_file(struct mystruct *s, struct file *f)
66 : {
67 : struct file *newfile;
68 2 : if(!(newfile=(struct file *)malloc_w(sizeof(struct file), __func__)))
69 : return -1;
70 : memcpy(newfile, f, sizeof(struct file));
71 2 : f->path=NULL;
72 2 : newfile->next=s->files;
73 2 : s->files=newfile;
74 : return 0;
75 : }
76 :
77 2 : static int add_key(off_t st_size, struct file *f)
78 : {
79 : struct mystruct *s;
80 :
81 2 : if(!(s=(struct mystruct *)malloc_w(sizeof(struct mystruct), __func__)))
82 : return -1;
83 2 : s->st_size=st_size;
84 2 : s->files=NULL;
85 2 : if(add_file(s, f)) return -1;
86 : //printf("HASH ADD %d\n", st_size);
87 6 : HASH_ADD_INT(myfiles, st_size, s);
88 : return 0;
89 : }
90 :
91 : static void file_free_content(struct file *file)
92 : {
93 2 : if(!file) return;
94 2 : free_w(&file->path);
95 : }
96 :
97 2 : static void file_free(struct file **file)
98 : {
99 4 : if(!file || !*file) return;
100 2 : file_free_content(*file);
101 2 : free_v((void **)file);
102 : }
103 :
104 2 : static void files_free(struct file **files)
105 : {
106 : struct file *f;
107 : struct file *fhead;
108 2 : if(!files || !*files) return;
109 : fhead=*files;
110 4 : while(fhead)
111 : {
112 2 : f=fhead;
113 2 : fhead=fhead->next;
114 2 : file_free(&f);
115 : }
116 : }
117 :
118 : static void mystruct_free_content(struct mystruct *mystruct)
119 : {
120 2 : if(!mystruct) return;
121 2 : files_free(&mystruct->files);
122 : }
123 :
124 2 : static void mystruct_free(struct mystruct **mystruct)
125 : {
126 4 : if(!mystruct || !*mystruct) return;
127 2 : mystruct_free_content(*mystruct);
128 2 : free_v((void **)mystruct);
129 : }
130 :
131 2 : static void mystruct_delete_all(void)
132 : {
133 : struct mystruct *tmp;
134 : struct mystruct *mystruct;
135 :
136 4 : HASH_ITER(hh, myfiles, mystruct, tmp)
137 : {
138 2 : HASH_DEL(myfiles, mystruct);
139 2 : mystruct_free(&mystruct);
140 : }
141 2 : myfiles=NULL;
142 2 : }
143 :
144 : #define FULL_CHUNK 4096
145 :
146 2 : static int full_match(struct file *o, struct file *n,
147 : struct fzp **ofp, struct fzp **nfp)
148 : {
149 : size_t ogot;
150 : size_t ngot;
151 2 : unsigned int i=0;
152 : static char obuf[FULL_CHUNK];
153 : static char nbuf[FULL_CHUNK];
154 :
155 2 : if(*ofp) fzp_seek(*ofp, 0, SEEK_SET);
156 0 : else if(!(*ofp=fzp_open(o->path, "rb")))
157 : {
158 : // Blank this entry so that it can be ignored from
159 : // now on.
160 0 : free_w(&o->path);
161 : return 0;
162 : }
163 :
164 2 : if(*nfp) fzp_seek(*nfp, 0, SEEK_SET);
165 0 : else if(!(*nfp=fzp_open(n->path, "rb"))) return 0;
166 :
167 : while(1)
168 : {
169 2 : ogot=fzp_read(*ofp, obuf, FULL_CHUNK);
170 2 : ngot=fzp_read(*nfp, nbuf, FULL_CHUNK);
171 2 : if(ogot!=ngot) return 0;
172 20 : for(i=0; i<ogot; i++)
173 20 : if(obuf[i]!=nbuf[i]) return 0;
174 2 : if(ogot<FULL_CHUNK) break;
175 : }
176 :
177 : return 1;
178 : }
179 :
180 : #define PART_CHUNK 1024
181 :
182 4 : static int get_part_cksum(struct file *f, struct fzp **fzp)
183 : {
184 : MD5_CTX md5;
185 4 : int got=0;
186 : static char buf[PART_CHUNK];
187 : unsigned char checksum[MD5_DIGEST_LENGTH+1];
188 :
189 4 : if(*fzp) fzp_seek(*fzp, 0, SEEK_SET);
190 4 : else if(!(*fzp=fzp_open(f->path, "rb")))
191 : {
192 0 : f->part_cksum=0;
193 0 : return 0;
194 : }
195 :
196 4 : if(!MD5_Init(&md5))
197 : {
198 0 : logp("MD5_Init() failed\n");
199 0 : return -1;
200 : }
201 :
202 4 : got=fzp_read(*fzp, buf, PART_CHUNK);
203 :
204 4 : if(!MD5_Update(&md5, buf, got))
205 : {
206 0 : logp("MD5_Update() failed\n");
207 0 : return -1;
208 : }
209 :
210 4 : if(!MD5_Final(checksum, &md5))
211 : {
212 0 : logp("MD5_Final() failed\n");
213 0 : return -1;
214 : }
215 :
216 4 : memcpy(&(f->part_cksum), checksum, sizeof(unsigned));
217 :
218 : // Try for a bit of efficiency - no need to calculate the full checksum
219 : // again if we already read the whole file.
220 4 : if(got<PART_CHUNK) f->full_cksum=f->part_cksum;
221 :
222 : return 0;
223 : }
224 :
225 0 : static int get_full_cksum(struct file *f, struct fzp **fzp)
226 : {
227 0 : size_t s=0;
228 : MD5_CTX md5;
229 : static char buf[FULL_CHUNK];
230 : unsigned char checksum[MD5_DIGEST_LENGTH+1];
231 :
232 0 : if(*fzp) fzp_seek(*fzp, 0, SEEK_SET);
233 0 : else if(!(*fzp=fzp_open(f->path, "rb")))
234 : {
235 0 : f->full_cksum=0;
236 0 : return 0;
237 : }
238 :
239 0 : if(!MD5_Init(&md5))
240 : {
241 0 : logp("MD5_Init() failed\n");
242 0 : return -1;
243 : }
244 :
245 0 : while((s=fzp_read(*fzp, buf, FULL_CHUNK))>0)
246 : {
247 0 : if(!MD5_Update(&md5, buf, s))
248 : {
249 0 : logp("MD5_Update() failed\n");
250 0 : return -1;
251 : }
252 0 : if(s<FULL_CHUNK) break;
253 : }
254 :
255 0 : if(!MD5_Final(checksum, &md5))
256 : {
257 0 : logp("MD5_Final() failed\n");
258 0 : return -1;
259 : }
260 :
261 0 : memcpy(&(f->full_cksum), checksum, sizeof(unsigned));
262 :
263 0 : return 0;
264 : }
265 :
266 : /* Make it atomic by linking to a temporary file, then moving it into place. */
267 1 : static int do_hardlink(struct file *o, struct file *n)
268 : {
269 1 : int ret=-1;
270 1 : char *tmppath=NULL;
271 1 : if(!(tmppath=prepend(o->path, ext)))
272 : {
273 0 : log_out_of_memory(__func__);
274 : goto end;
275 : }
276 1 : if(link(n->path, tmppath))
277 : {
278 0 : logp("Could not hardlink %s to %s: %s\n", tmppath, n->path,
279 0 : strerror(errno));
280 : goto end;
281 : }
282 1 : if((ret=do_rename(tmppath, o->path)))
283 : goto end;
284 1 : ret=0;
285 : end:
286 1 : free_w(&tmppath);
287 1 : return ret;
288 : }
289 :
290 : static void reset_old_file(struct file *oldfile, struct file *newfile,
291 : struct stat *info)
292 : {
293 : //printf("reset %s with %s %d\n", oldfile->path, newfile->path,
294 : // info->st_nlink);
295 0 : oldfile->nlink=info->st_nlink;
296 0 : free_w(&oldfile->path);
297 0 : oldfile->path=newfile->path;
298 0 : newfile->path=NULL;
299 : }
300 :
301 2 : static int check_files(struct mystruct *find, struct file *newfile,
302 0 : struct stat *info)
303 : {
304 2 : int found=0;
305 2 : struct fzp *nfp=NULL;
306 2 : struct fzp *ofp=NULL;
307 2 : struct file *f=NULL;
308 :
309 2 : for(f=find->files; f; f=f->next)
310 : {
311 : //printf(" against: '%s'\n", f->path);
312 2 : if(!f->path)
313 : {
314 : // If the full_match() function fails to open oldfile
315 : // (which could happen if burp deleted some old
316 : // directories), it will free path and set it to NULL.
317 : // Skip entries like this.
318 0 : continue;
319 : }
320 2 : if(newfile->dev!=f->dev)
321 : {
322 : // Different device.
323 0 : continue;
324 : }
325 2 : if(newfile->ino==f->ino)
326 : {
327 : // Same device, same inode, therefore these two files
328 : // are hardlinked to each other already.
329 : found++;
330 : break;
331 : }
332 2 : if((!newfile->part_cksum && get_part_cksum(newfile, &nfp))
333 2 : || (!f->part_cksum && get_part_cksum(f, &ofp)))
334 : {
335 : // Some error with md5sums Give up.
336 : return -1;
337 : }
338 2 : if(newfile->part_cksum!=f->part_cksum)
339 : {
340 0 : fzp_close(&ofp);
341 0 : continue;
342 : }
343 : //printf(" %s, %s\n", find->files->path, newfile->path);
344 : //printf(" part cksum matched\n");
345 :
346 2 : if((!newfile->full_cksum && get_full_cksum(newfile, &nfp))
347 2 : || (!f->full_cksum && get_full_cksum(f, &ofp)))
348 : {
349 : // Some error with md5sums Give up.
350 : return -1;
351 : }
352 2 : if(newfile->full_cksum!=f->full_cksum)
353 : {
354 0 : fzp_close(&ofp);
355 0 : continue;
356 : }
357 :
358 : //printf(" full cksum matched\n");
359 2 : if(!full_match(newfile, f, &nfp, &ofp))
360 : {
361 0 : fzp_close(&ofp);
362 0 : continue;
363 : }
364 : //printf(" full match\n");
365 : //printf("%s, %s\n", find->files->path, newfile->path);
366 :
367 : // If there are already enough links to this file, replace
368 : // our memory of it with the new file so that files later on
369 : // can link to the new one.
370 2 : if(f->nlink>=maxlinks)
371 : {
372 : // Just need to reset the path name and the number
373 : // of links, and pretend that it was found otherwise
374 : // NULL newfile will get added to the memory.
375 0 : reset_old_file(f, newfile, info);
376 0 : found++;
377 0 : break;
378 : }
379 :
380 2 : found++;
381 2 : count++;
382 :
383 2 : if(verbose) printf("%s\n", newfile->path);
384 :
385 : // Now hardlink it.
386 2 : if(makelinks)
387 : {
388 1 : switch(do_hardlink(newfile, f))
389 : {
390 : case 0:
391 1 : f->nlink++;
392 : // Only count bytes as saved if we
393 : // removed the last link.
394 1 : if(newfile->nlink==1)
395 1 : savedbytes+=info->st_size;
396 : break;
397 : case -1:
398 : // On error, replace the memory of the
399 : // old file with the one that we just
400 : // found. It might work better when
401 : // someone later tries to link to the
402 : // new one instead of the old one.
403 0 : reset_old_file(f, newfile, info);
404 0 : count--;
405 0 : break;
406 : default:
407 : // Abandon all hope.
408 : // This could happen if renaming the
409 : // hardlink failed in such a way that
410 : // the target file was unlinked without
411 : // being replaced - ie, if the max
412 : // number of hardlinks is being hit.
413 : return -1;
414 : }
415 : }
416 1 : else if(deletedups)
417 : {
418 0 : if(unlink(newfile->path))
419 : {
420 0 : logp("Could not delete %s: %s\n",
421 0 : newfile->path, strerror(errno));
422 : }
423 : else
424 : {
425 : // Only count bytes as saved if we removed the
426 : // last link.
427 0 : if(newfile->nlink==1)
428 0 : savedbytes+=info->st_size;
429 : }
430 : }
431 : else
432 : {
433 : // To be able to tell how many bytes
434 : // are saveable.
435 1 : savedbytes+=info->st_size;
436 : }
437 :
438 : break;
439 : }
440 2 : fzp_close(&nfp);
441 2 : fzp_close(&ofp);
442 :
443 2 : if(found)
444 : {
445 2 : free_w(&newfile->path);
446 2 : return 0;
447 : }
448 :
449 0 : if(add_file(find, newfile)) return -1;
450 :
451 0 : return 0;
452 : }
453 :
454 0 : static int looks_like_protocol1(const char *basedir)
455 : {
456 0 : int ret=-1;
457 0 : char *tmp=NULL;
458 0 : if(!(tmp=prepend_s(basedir, "current")))
459 : {
460 0 : log_out_of_memory(__func__);
461 0 : goto end;
462 : }
463 : // If there is a 'current' symlink here, we think it looks like a
464 : // protocol 1 backup.
465 0 : if(is_lnk_lstat(tmp)>0)
466 : {
467 : ret=1;
468 : goto end;
469 : }
470 0 : ret=0;
471 : end:
472 0 : free_w(&tmp);
473 0 : return ret;
474 : }
475 :
476 0 : static int get_link(const char *basedir, const char *lnk, char real[], size_t r)
477 : {
478 0 : readlink_w_in_dir(basedir, lnk, real, r);
479 : // Strip any trailing slash.
480 0 : if(real[strlen(real)-1]=='/')
481 0 : real[strlen(real)-1]='\0';
482 0 : return 0;
483 : }
484 :
485 0 : static int level_exclusion(int level, const char *fname,
486 : const char *working, const char *finishing)
487 : {
488 0 : if(level==0)
489 : {
490 : /* Be careful not to try to dedup the lockfiles.
491 : The lock actually gets lost if you open one to do a
492 : checksum
493 : and then close it. This caused me major headaches to
494 : figure out. */
495 0 : if(!strcmp(fname, LOCKFILE_NAME)
496 0 : || !strcmp(fname, BEDUP_LOCKFILE_NAME))
497 : return 1;
498 :
499 : /* Skip places where backups are going on. */
500 0 : if(!strcmp(fname, working)
501 0 : || !strcmp(fname, finishing))
502 : return 1;
503 :
504 0 : if(!strcmp(fname, "deleteme"))
505 : return 1;
506 : }
507 0 : else if(level==1)
508 : {
509 : // Do not dedup stuff that might be appended to later.
510 0 : if(!strncmp(fname, "log", strlen("log"))
511 0 : || !strncmp(fname, "verifylog", strlen("verifylog"))
512 0 : || !strncmp(fname, "restorelog", strlen("restorelog")))
513 : return 1;
514 : }
515 0 : return 0;
516 : }
517 :
518 : // Return 0 for directory processed, -1 for error, 1 for not processed.
519 2 : static int process_dir(const char *oldpath, const char *newpath,
520 : int burp_mode, int level)
521 : {
522 2 : int ret=-1;
523 2 : DIR *dirp=NULL;
524 2 : char *path=NULL;
525 : struct stat info;
526 2 : struct dirent *dirinfo=NULL;
527 : struct file newfile;
528 2 : struct mystruct *find=NULL;
529 : static char working[256]="";
530 : static char finishing[256]="";
531 :
532 2 : newfile.path=NULL;
533 :
534 2 : if(!(path=prepend_s(oldpath, newpath))) goto end;
535 :
536 2 : if(burp_mode && level==0)
537 : {
538 0 : if(get_link(path, "working", working, sizeof(working))
539 0 : || get_link(path, "finishing", finishing, sizeof(finishing)))
540 : goto end;
541 0 : if(!looks_like_protocol1(path))
542 : {
543 0 : logp("%s does not look like a protocol 1 storage directory - skipping\n", path);
544 0 : ret=1;
545 0 : goto end;
546 : }
547 : }
548 :
549 2 : if(!(dirp=opendir(path)))
550 : {
551 0 : logp("Could not opendir '%s': %s\n", path, strerror(errno));
552 0 : ret=1;
553 0 : goto end;
554 : }
555 10 : while((dirinfo=readdir(dirp)))
556 : {
557 8 : if(!strcmp(dirinfo->d_name, ".")
558 6 : || !strcmp(dirinfo->d_name, ".."))
559 4 : continue;
560 :
561 : //printf("try %s\n", dirinfo->d_name);
562 :
563 4 : if(burp_mode
564 0 : && level_exclusion(level, dirinfo->d_name,
565 : working, finishing))
566 0 : continue;
567 :
568 4 : free_w(&newfile.path);
569 4 : if(!(newfile.path=prepend_s(path, dirinfo->d_name)))
570 : goto end;
571 :
572 8 : if(lstat(newfile.path, &info))
573 0 : continue;
574 :
575 4 : if(S_ISDIR(info.st_mode))
576 : {
577 0 : if(process_dir(path, dirinfo->d_name,
578 : burp_mode, level+1))
579 : goto end;
580 0 : continue;
581 : }
582 4 : else if(!S_ISREG(info.st_mode)
583 4 : || !info.st_size) // ignore zero-length files
584 0 : continue;
585 :
586 4 : newfile.dev=info.st_dev;
587 4 : newfile.ino=info.st_ino;
588 4 : newfile.nlink=info.st_nlink;
589 4 : newfile.full_cksum=0;
590 4 : newfile.part_cksum=0;
591 4 : newfile.next=NULL;
592 :
593 4 : if((find=find_key(info.st_size)))
594 : {
595 : //printf("check %d: %s\n", info.st_size, newfile.path);
596 2 : if(check_files(find, &newfile, &info))
597 : goto end;
598 : }
599 : else
600 : {
601 : //printf("add: %s\n", newfile.path);
602 2 : if(add_key(info.st_size, &newfile))
603 : goto end;
604 : }
605 : }
606 : ret=0;
607 : end:
608 2 : if(dirp) closedir(dirp);
609 2 : free_w(&newfile.path);
610 2 : free_w(&path);
611 2 : return ret;
612 : }
613 :
614 0 : static void sighandler(__attribute__ ((unused)) int signum)
615 : {
616 0 : locks_release_and_free(&locklist);
617 0 : exit(1);
618 : }
619 :
620 0 : static int is_regular_file(const char *clientconfdir, const char *file)
621 : {
622 : struct stat statp;
623 0 : char *fullpath=NULL;
624 0 : if(!(fullpath=prepend_s(clientconfdir, file)))
625 : return 0;
626 0 : if(lstat(fullpath, &statp))
627 : {
628 0 : free_w(&fullpath);
629 0 : return 0;
630 : }
631 0 : free_w(&fullpath);
632 0 : return S_ISREG(statp.st_mode);
633 : }
634 :
635 0 : static int in_group(struct strlist *grouplist, const char *dedup_group)
636 : {
637 : struct strlist *g;
638 :
639 0 : for(g=grouplist; g; g=g->next)
640 0 : if(!strcmp(g->path, dedup_group)) return 1;
641 :
642 : return 0;
643 : }
644 :
645 0 : static int iterate_over_clients(struct conf **globalcs,
646 : struct strlist *grouplist)
647 : {
648 0 : int ret=0;
649 0 : DIR *dirp=NULL;
650 0 : struct conf **cconfs=NULL;
651 0 : struct dirent *dirinfo=NULL;
652 0 : const char *globalclientconfdir=get_string(globalcs[OPT_CLIENTCONFDIR]);
653 :
654 0 : if(!(cconfs=confs_alloc())) return -1;
655 0 : if(confs_init(cconfs)) return -1;
656 :
657 0 : if(!(dirp=opendir(globalclientconfdir)))
658 : {
659 0 : logp("Could not opendir '%s': %s\n",
660 0 : globalclientconfdir, strerror(errno));
661 0 : return 0;
662 : }
663 0 : while((dirinfo=readdir(dirp)))
664 : {
665 0 : char *lockfile=NULL;
666 0 : char *lockfilebase=NULL;
667 0 : char *client_lockdir=NULL;
668 0 : struct lock *lock=NULL;
669 :
670 0 : if(dirinfo->d_ino==0
671 : // looks_like...() also avoids '.' and '..'.
672 0 : || looks_like_tmp_or_hidden_file(dirinfo->d_name)
673 0 : || !is_regular_file(globalclientconfdir, dirinfo->d_name))
674 0 : continue;
675 :
676 0 : confs_free_content(cconfs);
677 0 : if(confs_init(cconfs)) return -1;
678 :
679 0 : if(set_string(cconfs[OPT_CNAME], dirinfo->d_name))
680 : return -1;
681 :
682 0 : if(conf_load_clientconfdir(globalcs, cconfs))
683 : {
684 0 : logp("could not load config for client %s\n",
685 : dirinfo->d_name);
686 0 : return 0;
687 : }
688 :
689 0 : if(grouplist)
690 : {
691 0 : const char *dedup_group=
692 0 : get_string(cconfs[OPT_DEDUP_GROUP]);
693 0 : if(!dedup_group
694 0 : || !in_group(grouplist, dedup_group))
695 0 : continue;
696 : }
697 :
698 0 : if(!(client_lockdir=get_string(cconfs[OPT_CLIENT_LOCKDIR])))
699 0 : client_lockdir=get_string(cconfs[OPT_DIRECTORY]);
700 :
701 0 : if(!(lockfilebase=prepend_s(client_lockdir, dirinfo->d_name))
702 0 : || !(lockfile=prepend_s(lockfilebase, BEDUP_LOCKFILE_NAME)))
703 : {
704 0 : free_w(&lockfilebase);
705 0 : free_w(&lockfile);
706 0 : ret=-1;
707 0 : break;
708 : }
709 0 : free_w(&lockfilebase);
710 :
711 0 : if(!(lock=lock_alloc_and_init(lockfile)))
712 : {
713 : ret=-1;
714 : break;
715 : }
716 0 : lock_get(lock);
717 0 : free_w(&lockfile);
718 :
719 0 : if(lock->status!=GET_LOCK_GOT)
720 : {
721 0 : logp("Could not get %s\n", lock->path);
722 0 : continue;
723 : }
724 0 : logp("Got %s\n", lock->path);
725 :
726 : // Remember that we got that lock.
727 0 : lock_add_to_list(&locklist, lock);
728 :
729 0 : switch(process_dir(get_string(cconfs[OPT_DIRECTORY]),
730 : dirinfo->d_name,
731 : 1 /* burp mode */, 0 /* level */))
732 : {
733 0 : case 0: ccount++;
734 0 : case 1: continue;
735 : default: ret=-1; break;
736 : }
737 : break;
738 : }
739 0 : closedir(dirp);
740 :
741 0 : locks_release_and_free(&locklist);
742 :
743 0 : confs_free(&cconfs);
744 :
745 0 : return ret;
746 : }
747 :
748 0 : static int process_from_conf(const char *configfile, char **groups)
749 : {
750 0 : int ret=-1;
751 0 : struct conf **globalcs=NULL;
752 0 : struct strlist *grouplist=NULL;
753 0 : struct lock *globallock=NULL;
754 :
755 0 : signal(SIGABRT, &sighandler);
756 0 : signal(SIGTERM, &sighandler);
757 0 : signal(SIGINT, &sighandler);
758 :
759 0 : if(*groups)
760 : {
761 0 : char *tok=NULL;
762 0 : if((tok=strtok(*groups, ",\n")))
763 : {
764 : do
765 : {
766 0 : if(strlist_add(&grouplist, tok, 1))
767 : {
768 0 : log_out_of_memory(__func__);
769 0 : goto end;
770 : }
771 0 : } while((tok=strtok(NULL, ",\n")));
772 : }
773 0 : if(!grouplist)
774 : {
775 0 : logp("unable to read list of groups\n");
776 0 : goto end;
777 : }
778 : }
779 :
780 : // Read directories from config files, and get locks.
781 0 : if(!(globalcs=confs_alloc())
782 0 : || confs_init(globalcs)
783 0 : || conf_load_global_only(configfile, globalcs))
784 : goto end;
785 :
786 0 : if(get_e_burp_mode(globalcs[OPT_BURP_MODE])!=BURP_MODE_SERVER)
787 : {
788 0 : logp("%s is not a server config file\n", configfile);
789 0 : goto end;
790 : }
791 0 : logp("Dedup clients from %s\n",
792 0 : get_string(globalcs[OPT_CLIENTCONFDIR]));
793 0 : maxlinks=get_int(globalcs[OPT_MAX_HARDLINKS]);
794 0 : if(grouplist)
795 : {
796 0 : struct strlist *g=NULL;
797 0 : logp("in dedup groups:\n");
798 0 : for(g=grouplist; g; g=g->next)
799 0 : logp("%s\n", g->path);
800 : }
801 : else
802 : {
803 0 : char *lockpath=NULL;
804 0 : const char *opt_lockfile=confs_get_lockfile(globalcs);
805 : // Only get the global lock when doing a global run.
806 : // If you are doing individual groups, you are likely
807 : // to want to do many different dedup jobs and a
808 : // global lock would get in the way.
809 0 : if(!(lockpath=prepend(opt_lockfile, ".bedup"))
810 0 : || !(globallock=lock_alloc_and_init(lockpath)))
811 : goto end;
812 0 : lock_get(globallock);
813 0 : if(globallock->status!=GET_LOCK_GOT)
814 : {
815 0 : logp("Could not get lock %s (%d)\n", lockpath,
816 : globallock->status);
817 0 : free_w(&lockpath);
818 0 : goto end;
819 : }
820 0 : logp("Got %s\n", lockpath);
821 : }
822 0 : ret=iterate_over_clients(globalcs, grouplist);
823 : end:
824 0 : confs_free(&globalcs);
825 0 : lock_release(globallock);
826 0 : lock_free(&globallock);
827 0 : strlists_free(&grouplist);
828 0 : return ret;
829 : }
830 :
831 2 : static int process_from_command_line(int argc, char *argv[])
832 : {
833 : int i;
834 4 : for(i=optind; i<argc; i++)
835 : {
836 : // Strip trailing slashes, for tidiness.
837 2 : if(argv[i][strlen(argv[i])-1]=='/')
838 0 : argv[i][strlen(argv[i])-1]='\0';
839 2 : if(process_dir("", argv[i],
840 : 0 /* not burp mode */, 0 /* level */))
841 : return 1;
842 : }
843 : return 0;
844 : }
845 :
846 2 : static int usage(void)
847 : {
848 2 : logfmt("\nUsage: %s [options]\n", prog);
849 2 : logfmt("\n");
850 2 : logfmt(" Options:\n");
851 2 : logfmt(" -c <path> Path to config file (default: %s).\n", config_default_path());
852 2 : logfmt(" -g <list of group names> Only run on the directories of clients that\n");
853 2 : logfmt(" are in one of the groups specified.\n");
854 2 : logfmt(" The list is comma-separated. To put a client in a\n");
855 2 : logfmt(" group, use the 'dedup_group' option in the client\n");
856 2 : logfmt(" configuration file on the server.\n");
857 2 : logfmt(" -h|-? Print this text and exit.\n");
858 2 : logfmt(" -d Delete any duplicate files found.\n");
859 2 : logfmt(" (non-burp mode only)\n");
860 2 : logfmt(" -l Hard link any duplicate files found.\n");
861 2 : logfmt(" -m <number> Maximum number of hard links to a single file.\n");
862 2 : logfmt(" (non-burp mode only - in burp mode, use the\n");
863 2 : logfmt(" max_hardlinks option in the configuration file)\n");
864 2 : logfmt(" The default is %d. On ext3, the maximum number\n", DEF_MAX_LINKS);
865 2 : logfmt(" of links possible is 32000, but space is needed\n");
866 2 : logfmt(" for the normal operation of burp.\n");
867 2 : logfmt(" -n <list of directories> Non-burp mode. Deduplicate any (set of) directories.\n");
868 2 : logfmt(" -v Print duplicate paths.\n");
869 2 : logfmt(" -V Print version and exit.\n");
870 2 : logfmt("\n");
871 2 : logfmt("By default, %s will read %s and deduplicate client storage\n", prog, config_default_path());
872 2 : logfmt("directories using special knowledge of the structure.\n");
873 2 : logfmt("\n");
874 2 : logfmt("With '-n', this knowledge is turned off and you have to specify the directories\n");
875 2 : logfmt("to deduplicate on the command line. Running with '-n' is therefore dangerous\n");
876 2 : logfmt("if you are deduplicating burp storage directories.\n\n");
877 2 : return 1;
878 : }
879 :
880 13 : int run_bedup(int argc, char *argv[])
881 : {
882 13 : int ret=0;
883 13 : int option=0;
884 13 : int nonburp=0;
885 13 : char *groups=NULL;
886 13 : int givenconfigfile=0;
887 13 : const char *configfile=NULL;
888 :
889 13 : configfile=config_default_path();
890 13 : snprintf(ext, sizeof(ext), ".bedup.%d", getpid());
891 :
892 28 : while((option=getopt(argc, argv, "c:dg:hlm:nvV?"))!=-1)
893 : {
894 18 : switch(option)
895 : {
896 : case 'c':
897 1 : configfile=optarg;
898 1 : givenconfigfile=1;
899 1 : break;
900 : case 'd':
901 2 : deletedups=1;
902 2 : break;
903 : case 'g':
904 1 : groups=optarg;
905 1 : break;
906 : case 'l':
907 2 : makelinks=1;
908 2 : break;
909 : case 'm':
910 4 : maxlinks=atoi(optarg);
911 2 : break;
912 : case 'n':
913 6 : nonburp=1;
914 6 : break;
915 : case 'V':
916 1 : logfmt("%s-%s\n", prog, VERSION);
917 1 : return 0;
918 : case 'v':
919 1 : verbose=1;
920 1 : break;
921 : case 'h':
922 : case '?':
923 2 : return usage();
924 : }
925 : }
926 :
927 10 : if(nonburp && givenconfigfile)
928 : {
929 1 : logp("-n and -c options are mutually exclusive\n");
930 1 : return 1;
931 : }
932 9 : if(nonburp && groups)
933 : {
934 1 : logp("-n and -g options are mutually exclusive\n");
935 1 : return 1;
936 : }
937 8 : if(!nonburp && maxlinks!=DEF_MAX_LINKS)
938 : {
939 1 : logp("-m option is specified via the configuration file in burp mode (max_hardlinks=)\n");
940 1 : return 1;
941 : }
942 7 : if(deletedups && makelinks)
943 : {
944 1 : logp("-d and -l options are mutually exclusive\n");
945 1 : return 1;
946 : }
947 6 : if(deletedups && !nonburp)
948 : {
949 1 : logp("-d option requires -n option\n");
950 1 : return 1;
951 : }
952 :
953 5 : if(optind>=argc)
954 : {
955 1 : if(nonburp)
956 : {
957 1 : logp("No directories found after options\n");
958 1 : return 1;
959 : }
960 : }
961 : else
962 : {
963 4 : if(!nonburp)
964 : {
965 1 : logp("Do not specify extra arguments.\n");
966 1 : return 1;
967 : }
968 : }
969 :
970 3 : if(maxlinks<2)
971 : {
972 1 : logp("The argument to -m needs to be greater than 1.\n");
973 1 : return 1;
974 : }
975 :
976 2 : if(nonburp)
977 : {
978 : // Read directories from command line.
979 2 : if(process_from_command_line(argc, argv))
980 0 : ret=1;
981 : }
982 : else
983 : {
984 0 : if(process_from_conf(configfile, &groups))
985 0 : ret=1;
986 : }
987 :
988 2 : if(!nonburp)
989 : {
990 0 : logp("%d client storages scanned\n", ccount);
991 : }
992 2 : logp("%" PRIu64 " duplicate %s found\n",
993 2 : count, count==1?"file":"files");
994 6 : logp("%" PRIu64 " bytes %s%s\n",
995 3 : savedbytes, (makelinks || deletedups)?"saved":"saveable",
996 : bytes_to_human(savedbytes));
997 2 : mystruct_delete_all();
998 2 : return ret;
999 : }
|