60b639bf00ffa3c86f6195dd78365929f3005219
[rsync/rsync.git] / hlink.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "rsync.h"
21
22 extern int am_server;
23 extern int dry_run;
24
25 #if SUPPORT_HARD_LINKS
26 static int hlink_compare(struct file_struct *f1,struct file_struct *f2)
27 {
28   if (!S_ISREG(f1->mode) && !S_ISREG(f2->mode)) return 0;
29   if (!S_ISREG(f1->mode)) return -1;
30   if (!S_ISREG(f2->mode)) return 1;
31
32   if (f1->dev != f2->dev) 
33     return (f1->dev - f2->dev);
34
35   if (f1->inode != f2->inode) 
36     return (f1->inode - f2->inode);
37
38   return file_compare(f1,f2);
39 }
40
41
42 static struct file_struct *hlink_list = NULL;
43 static int hlink_count=0;
44 #endif
45
46 void init_hard_links(struct file_list *flist)
47 {
48 #if SUPPORT_HARD_LINKS
49   if (flist->count < 2) return;
50
51   if (hlink_list) free(hlink_list);
52     
53   if (!(hlink_list = 
54         (struct file_struct *)malloc(sizeof(hlink_list[0])*flist->count)))
55     out_of_memory("init_hard_links");
56
57   bcopy((char *)flist->files,
58         (char *)hlink_list,
59         sizeof(hlink_list[0])*flist->count);
60
61   qsort(hlink_list,flist->count,
62         sizeof(hlink_list[0]),
63         (int (*)())hlink_compare);
64
65   hlink_count=flist->count;
66 #endif
67 }
68
69 /* check if a file should be skipped because it is the same as an
70    earlier hard link */
71 int check_hard_link(struct file_struct *file)
72 {
73 #if SUPPORT_HARD_LINKS
74   int low=0,high=hlink_count-1;
75   int mid=0,ret=0;
76
77   if (!hlink_list || !S_ISREG(file->mode)) return 0;
78
79   while (low != high) {
80     mid = (low+high)/2;
81     ret = hlink_compare(&hlink_list[mid],file);
82     if (ret == 0) break;
83     if (ret > 0) 
84       high=mid;
85     else
86       low=mid+1;
87   }
88
89   if (hlink_compare(&hlink_list[mid],file) != 0) return 0;
90
91   if (mid > 0 &&
92       S_ISREG(hlink_list[mid-1].mode) &&
93       file->dev == hlink_list[mid-1].dev &&
94       file->inode == hlink_list[mid-1].inode)
95     return 1;
96 #endif
97
98   return 0;
99 }
100
101
102 /* create any hard links in the flist */
103 void do_hard_links(struct file_list *flist)
104 {
105 #if SUPPORT_HARD_LINKS
106   int i;
107   
108   if (!hlink_list) return;
109
110   for (i=1;i<hlink_count;i++) {
111     if (S_ISREG(hlink_list[i].mode) &&
112         S_ISREG(hlink_list[i-1].mode) &&
113         hlink_list[i].name && hlink_list[i-1].name &&
114         hlink_list[i].dev == hlink_list[i-1].dev &&
115         hlink_list[i].inode == hlink_list[i-1].inode) {
116       struct stat st1,st2;
117
118       if (lstat(hlink_list[i-1].name,&st1) != 0) continue;
119       if (lstat(hlink_list[i].name,&st2) != 0) {
120         if (!dry_run && link(hlink_list[i-1].name,hlink_list[i].name) != 0) {
121           fprintf(FINFO,"link %s => %s : %s\n",
122                   hlink_list[i].name,hlink_list[i-1].name,strerror(errno));
123           continue;
124         }
125       } else {
126         if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) continue;
127         
128         if (!dry_run && (unlink(hlink_list[i].name) != 0 ||
129                          link(hlink_list[i-1].name,hlink_list[i].name) != 0)) {
130           fprintf(FINFO,"link %s => %s : %s\n",
131                   hlink_list[i].name,hlink_list[i-1].name,strerror(errno));
132           continue;
133         }
134       }
135       fprintf(FINFO,"%s => %s\n",
136               hlink_list[i].name,hlink_list[i-1].name);
137     }   
138   }
139 #endif
140 }