- Ensure in make_file() that we can't make a name that overflows
[rsync/rsync.git] / hlink.c
CommitLineData
6aae748e 1/*
dc5ddbcc
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
6e69cff1 4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
6aae748e 5
dc5ddbcc
AT
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.
6aae748e 10
dc5ddbcc
AT
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.
6aae748e 15
dc5ddbcc
AT
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.
19*/
20
21#include "rsync.h"
22
dc5ddbcc 23extern int dry_run;
9a52223b 24extern int verbose;
dc5ddbcc
AT
25
26#if SUPPORT_HARD_LINKS
11dc2740 27static int hlink_compare(struct file_struct **file1, struct file_struct **file2)
dc5ddbcc 28{
11dc2740
S
29 struct file_struct *f1 = *file1;
30 struct file_struct *f2 = *file2;
31
92cc9dd7
WD
32 if (f1->F_DEV != f2->F_DEV)
33 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
dc5ddbcc 34
92cc9dd7
WD
35 if (f1->F_INODE != f2->F_INODE)
36 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
dc5ddbcc 37
11dc2740
S
38 return file_compare(file1, file2);
39}
dc5ddbcc 40
11dc2740 41static struct file_struct **hlink_list;
3a6a366f 42static int hlink_count;
1d5cda22
WD
43
44#define LINKED(p1,p2) ((p1)->F_DEV == (p2)->F_DEV \
45 && (p1)->F_INODE == (p2)->F_INODE)
46
47/* Analyze the data in the hlink_list[], remove items that aren't multiply
48 * linked, and replace the dev+inode data with the head+next linked list. */
49static void link_idev_data(void)
50{
51 struct file_struct *head;
52 int from, to, start;
53
54 for (from = to = 0; from < hlink_count; from++) {
55 start = from;
56 head = hlink_list[start];
57 while (from < hlink_count-1
58 && LINKED(hlink_list[from], hlink_list[from+1])) {
59 hlink_list[from]->F_HEAD = head;
60 hlink_list[from]->F_NEXT = hlink_list[from+1];
61 from++;
62 }
63 if (from > start) {
64 hlink_list[from]->F_HEAD = head;
65 hlink_list[from]->F_NEXT = NULL;
66 hlink_list[to++] = head;
67 } else {
68 free((char*)head->link_u.idev);
69 head->link_u.idev = NULL;
70 }
71 }
72
73 if (!to) {
74 free(hlink_list);
75 hlink_list = NULL;
76 } else {
77 hlink_count = to;
78 if (!(hlink_list = realloc_array(hlink_list,
79 struct file_struct *, hlink_count)))
80 out_of_memory("init_hard_links");
81 }
82}
dc5ddbcc
AT
83#endif
84
85void init_hard_links(struct file_list *flist)
86{
87#if SUPPORT_HARD_LINKS
dd04a034 88 int i;
11dc2740 89
6e69cff1
MP
90 if (flist->count < 2)
91 return;
dc5ddbcc 92
6e69cff1
MP
93 if (hlink_list)
94 free(hlink_list);
95
11dc2740 96 if (!(hlink_list = new_array(struct file_struct *, flist->count)))
dd04a034 97 out_of_memory("init_hard_links");
6aae748e 98
11dc2740
S
99 hlink_count = 0;
100 for (i = 0; i < flist->count; i++) {
92cc9dd7 101 if (flist->files[i]->link_u.idev)
11dc2740
S
102 hlink_list[hlink_count++] = flist->files[i];
103 }
dc5ddbcc 104
11dc2740 105 qsort(hlink_list, hlink_count,
6e69cff1 106 sizeof(hlink_list[0]), (int (*)()) hlink_compare);
dc5ddbcc 107
6aae748e
WD
108 if (!hlink_count) {
109 free(hlink_list);
110 hlink_list = NULL;
1d5cda22
WD
111 } else
112 link_idev_data();
dc5ddbcc 113#endif
dc5ddbcc
AT
114}
115
a16bbc39 116#if SUPPORT_HARD_LINKS
1d5cda22 117static void hard_link_one(char *hlink1, char *hlink2)
a16bbc39 118{
1d5cda22
WD
119 if (do_link(hlink1, hlink2)) {
120 if (verbose > 0) {
121 rprintf(FINFO, "link %s => %s failed: %s\n",
122 hlink2, hlink1, strerror(errno));
a16bbc39
AT
123 }
124 }
1d5cda22 125 else if (verbose > 0)
310c9f30 126 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
a16bbc39
AT
127}
128#endif
129
fae5bb31
MP
130
131
132/**
133 * Create any hard links in the global hlink_list. They were put
134 * there by running init_hard_links on the filelist.
135 **/
136void do_hard_links(void)
dc5ddbcc
AT
137{
138#if SUPPORT_HARD_LINKS
1d5cda22
WD
139 struct file_struct *file;
140 char fbuf[MAXPATHLEN];
141 char *hlink1, *hlink2;
142 STRUCT_STAT st1, st2;
a16bbc39 143 int i;
a16bbc39 144
6e69cff1
MP
145 if (!hlink_list)
146 return;
147
1d5cda22
WD
148 for (i = 0; i < hlink_count; i++) {
149 file = hlink_list[i];
150 hlink1 = f_name_to(file, fbuf, sizeof fbuf);
151 if (link_stat(hlink1, &st1) != 0)
152 continue;
153 while ((file = file->F_NEXT) != NULL) {
154 hlink2 = f_name(file);
155 if (link_stat(hlink2, &st2) == 0) {
156 if (st2.st_dev == st1.st_dev
157 && st2.st_ino == st1.st_ino)
158 continue;
159 if (robust_unlink(hlink2)) {
160 if (verbose > 0) {
161 rprintf(FINFO,
162 "unlink %s failed: %s\n",
163 full_fname(hlink2),
164 strerror(errno));
165 }
166 continue;
167 }
168 }
169 hard_link_one(hlink1, hlink2);
6e69cff1 170 }
dc5ddbcc 171 }
dc5ddbcc
AT
172#endif
173}