2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int make_backups
;
26 extern struct file_list
*the_file_list
;
28 #ifdef SUPPORT_HARD_LINKS
30 #define FPTR(i) (the_file_list->files[i])
31 #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
32 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
34 static int hlink_compare(int *int1
, int *int2
)
36 struct file_struct
*f1
= FPTR(*int1
);
37 struct file_struct
*f2
= FPTR(*int2
);
39 if (f1
->F_DEV
!= f2
->F_DEV
)
40 return (int) (f1
->F_DEV
> f2
->F_DEV
? 1 : -1);
42 if (f1
->F_INODE
!= f2
->F_INODE
)
43 return (int) (f1
->F_INODE
> f2
->F_INODE
? 1 : -1);
45 return f_name_cmp(f1
, f2
);
48 static int *hlink_list
;
49 static int hlink_count
;
51 /* Analyze the data in the hlink_list[], remove items that aren't multiply
52 * linked, and replace the dev+inode data with the hlindex+next linked list. */
53 static void link_idev_data(void)
55 int head
, from
, to
, start
;
57 alloc_pool_t hlink_pool
;
58 alloc_pool_t idev_pool
= the_file_list
->hlink_pool
;
60 hlink_pool
= pool_create(128 * 1024, sizeof (struct hlink
),
61 out_of_memory
, POOL_INTERN
);
63 for (from
= to
= 0; from
< hlink_count
; from
++) {
65 head
= hlink_list
[start
];
66 while (from
< hlink_count
-1
67 && LINKED(hlink_list
[from
], hlink_list
[from
+1])) {
68 pool_free(idev_pool
, 0, FPTR(hlink_list
[from
])->link_u
.idev
);
69 FPTR(hlink_list
[from
])->link_u
.links
= pool_talloc(hlink_pool
,
70 struct hlink
, 1, "hlink_list");
72 FPTR(hlink_list
[from
])->F_HLINDEX
= to
;
73 FPTR(hlink_list
[from
])->F_NEXT
= hlink_list
[from
+1];
77 pool_free(idev_pool
, 0, FPTR(hlink_list
[from
])->link_u
.idev
);
78 FPTR(hlink_list
[from
])->link_u
.links
= pool_talloc(hlink_pool
,
79 struct hlink
, 1, "hlink_list");
81 FPTR(head
)->flags
|= FLAG_HLINK_TOL
;
82 FPTR(hlink_list
[from
])->F_HLINDEX
= to
;
83 FPTR(hlink_list
[from
])->F_NEXT
= head
;
84 FPTR(hlink_list
[from
])->flags
|= FLAG_HLINK_EOL
;
85 hlink_list
[to
++] = head
;
87 pool_free(idev_pool
, 0, FPTR(head
)->link_u
.idev
);
88 FPTR(head
)->link_u
.idev
= NULL
;
95 pool_destroy(hlink_pool
);
99 hlink_list
= realloc_array(hlink_list
, int, hlink_count
);
101 out_of_memory("init_hard_links");
103 the_file_list
->hlink_pool
= hlink_pool
;
104 pool_destroy(idev_pool
);
108 void init_hard_links(void)
110 #ifdef SUPPORT_HARD_LINKS
113 if (the_file_list
->count
< 2)
119 if (!(hlink_list
= new_array(int, the_file_list
->count
)))
120 out_of_memory("init_hard_links");
123 for (i
= 0; i
< the_file_list
->count
; i
++) {
124 if (FPTR(i
)->link_u
.idev
)
125 hlink_list
[hlink_count
++] = i
;
128 qsort(hlink_list
, hlink_count
,
129 sizeof hlink_list
[0], (int (*)()) hlink_compare
);
139 int hard_link_check(struct file_struct
*file
, int ndx
, int skip
)
141 #ifdef SUPPORT_HARD_LINKS
142 if (!hlink_list
|| !file
->link_u
.links
)
144 if (skip
&& !(file
->flags
& FLAG_HLINK_EOL
))
145 hlink_list
[file
->F_HLINDEX
] = file
->F_NEXT
;
146 if (hlink_list
[file
->F_HLINDEX
] != ndx
) {
148 rprintf(FINFO
, "\"%s\" is a hard link\n",
149 safe_fname(f_name(file
)));
157 #ifdef SUPPORT_HARD_LINKS
158 int hard_link_one(struct file_struct
*file
, int ndx
, char *fname
,
159 int statret
, STRUCT_STAT
*st
, char *toname
, int terse
,
160 int itemizing
, enum logcode code
)
162 if (do_link(toname
, fname
)) {
164 rsyserr(FERROR
, errno
, "link %s => %s failed",
165 full_fname(fname
), safe_fname(toname
));
171 itemize(file
, ndx
, statret
, st
,
172 ITEM_LOCAL_CHANGE
| ITEM_XNAME_FOLLOWS
, 0,
173 terse
? "" : toname
);
175 if (code
&& verbose
&& !terse
) {
176 rprintf(code
, "%s => %s\n",
177 safe_fname(fname
), safe_fname(toname
));
184 void hard_link_cluster(struct file_struct
*file
, int master
, int itemizing
,
187 #ifdef SUPPORT_HARD_LINKS
188 char hlink1
[MAXPATHLEN
];
190 STRUCT_STAT st1
, st2
;
191 int statret
, ndx
= master
;
193 if (link_stat(f_name_to(file
, hlink1
), &st1
, 0) < 0)
195 if (!(file
->flags
& FLAG_HLINK_TOL
)) {
196 while (!(file
->flags
& FLAG_HLINK_EOL
)) {
206 hlink2
= f_name(file
);
207 if ((statret
= link_stat(hlink2
, &st2
, 0)) == 0) {
208 if (st2
.st_dev
== st1
.st_dev
209 && st2
.st_ino
== st1
.st_ino
) {
211 itemize(file
, ndx
, statret
, &st2
,
212 ITEM_LOCAL_CHANGE
| ITEM_XNAME_FOLLOWS
,
218 if (!make_backup(hlink2
))
220 } else if (robust_unlink(hlink2
)) {
222 rsyserr(FINFO
, errno
,
229 hard_link_one(file
, ndx
, hlink2
, statret
,
230 &st2
, hlink1
, 0, itemizing
, code
);
231 } while (!(file
->flags
& FLAG_HLINK_EOL
));