- fixed spelling errors in man page
[rsync/rsync.git] / hlink.c
CommitLineData
dc5ddbcc
AT
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
22extern int am_server;
23extern int dry_run;
9a52223b 24extern int verbose;
dc5ddbcc
AT
25
26#if SUPPORT_HARD_LINKS
27static int hlink_compare(struct file_struct *f1,struct file_struct *f2)
28{
29 if (!S_ISREG(f1->mode) && !S_ISREG(f2->mode)) return 0;
30 if (!S_ISREG(f1->mode)) return -1;
31 if (!S_ISREG(f2->mode)) return 1;
32
33 if (f1->dev != f2->dev)
34 return (f1->dev - f2->dev);
35
36 if (f1->inode != f2->inode)
37 return (f1->inode - f2->inode);
38
39 return file_compare(f1,f2);
40}
41
42
43static struct file_struct *hlink_list = NULL;
44static int hlink_count=0;
45#endif
46
47void init_hard_links(struct file_list *flist)
48{
49#if SUPPORT_HARD_LINKS
50 if (flist->count < 2) return;
51
52 if (hlink_list) free(hlink_list);
53
54 if (!(hlink_list =
55 (struct file_struct *)malloc(sizeof(hlink_list[0])*flist->count)))
56 out_of_memory("init_hard_links");
57
7b8356d0
AT
58 bcopy((char *)flist->files,
59 (char *)hlink_list,
60 sizeof(hlink_list[0])*flist->count);
dc5ddbcc
AT
61
62 qsort(hlink_list,flist->count,
63 sizeof(hlink_list[0]),
64 (int (*)())hlink_compare);
65
66 hlink_count=flist->count;
67#endif
68}
69
70/* check if a file should be skipped because it is the same as an
71 earlier hard link */
72int check_hard_link(struct file_struct *file)
73{
74#if SUPPORT_HARD_LINKS
527cea66 75 int low=0,high=hlink_count-1;
e3cd198f 76 int ret=0;
dc5ddbcc
AT
77
78 if (!hlink_list || !S_ISREG(file->mode)) return 0;
79
80 while (low != high) {
e3cd198f 81 int mid = (low+high)/2;
dc5ddbcc
AT
82 ret = hlink_compare(&hlink_list[mid],file);
83 if (ret == 0) break;
84 if (ret > 0)
85 high=mid;
86 else
87 low=mid+1;
88 }
89
e3cd198f 90 if (hlink_compare(&hlink_list[low],file) != 0) return 0;
dc5ddbcc 91
e3cd198f
AT
92 if (low > 0 &&
93 S_ISREG(hlink_list[low-1].mode) &&
94 file->dev == hlink_list[low-1].dev &&
95 file->inode == hlink_list[low-1].inode)
dc5ddbcc
AT
96 return 1;
97#endif
98
99 return 0;
100}
101
102
103/* create any hard links in the flist */
104void do_hard_links(struct file_list *flist)
105{
106#if SUPPORT_HARD_LINKS
107 int i;
108
109 if (!hlink_list) return;
110
111 for (i=1;i<hlink_count;i++) {
112 if (S_ISREG(hlink_list[i].mode) &&
113 S_ISREG(hlink_list[i-1].mode) &&
114 hlink_list[i].name && hlink_list[i-1].name &&
115 hlink_list[i].dev == hlink_list[i-1].dev &&
116 hlink_list[i].inode == hlink_list[i-1].inode) {
117 struct stat st1,st2;
118
82306bf6
AT
119 if (link_stat(hlink_list[i-1].name,&st1) != 0) continue;
120 if (link_stat(hlink_list[i].name,&st2) != 0) {
dc5ddbcc 121 if (!dry_run && link(hlink_list[i-1].name,hlink_list[i].name) != 0) {
9a52223b
AT
122 if (verbose > 0)
123 fprintf(FINFO,"link %s => %s : %s\n",
124 hlink_list[i].name,
125 hlink_list[i-1].name,strerror(errno));
dc5ddbcc
AT
126 continue;
127 }
128 } else {
129 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) continue;
130
131 if (!dry_run && (unlink(hlink_list[i].name) != 0 ||
132 link(hlink_list[i-1].name,hlink_list[i].name) != 0)) {
9a52223b
AT
133 if (verbose > 0)
134 fprintf(FINFO,"link %s => %s : %s\n",
135 hlink_list[i].name,
136 hlink_list[i-1].name,strerror(errno));
dc5ddbcc
AT
137 continue;
138 }
139 }
9a52223b
AT
140 if (verbose > 0)
141 fprintf(FINFO,"%s => %s\n",
142 hlink_list[i].name,hlink_list[i-1].name);
dc5ddbcc
AT
143 }
144 }
145#endif
146}