-
Notifications
You must be signed in to change notification settings - Fork 15
/
hardlink.c
171 lines (136 loc) · 3.45 KB
/
hardlink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#include <sys/param.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "applefile.h"
#include "transcript.h"
struct devlist {
struct devlist *d_next;
dev_t d_dev;
struct inolist *d_ilist;
};
struct inolist {
struct inolist *i_next;
ino_t i_ino;
char *i_name;
int i_flag;
};
static struct devlist *dev_head = NULL;
static char *i_insert( struct devlist *dev_head,
struct pathinfo *pinfo );
static struct devlist *d_insert( struct devlist **dev_head,
struct pathinfo *pinfo );
void hardlink_free( void );
char *
hardlink( struct pathinfo *pinfo )
{
struct devlist *device;
device = d_insert( &dev_head, pinfo );
return( i_insert( device, pinfo ));
}
static struct devlist *
d_insert( struct devlist **dev_head, struct pathinfo *pinfo )
{
struct devlist *new, **cur;
for ( cur = dev_head; *cur != NULL; cur = &(*cur)->d_next ) {
if ( pinfo->pi_stat.st_dev <= (*cur)->d_dev ) {
break;
}
}
if (( (*cur) != NULL ) && ( pinfo->pi_stat.st_dev == (*cur)->d_dev )) {
return( *cur );
}
if (( new = ( struct devlist * ) malloc( sizeof( struct devlist )))
== NULL ) {
perror( "d_insert malloc" );
exit( 2 );
}
new->d_dev = pinfo->pi_stat.st_dev;
new->d_ilist = NULL;
new->d_next = *cur;
*cur = new;
return( *cur );
}
static char *
i_insert( struct devlist *dev_head, struct pathinfo *pinfo )
{
struct inolist *new, **cur;
for ( cur = &dev_head->d_ilist; *cur != NULL; cur = &(*cur)->i_next ) {
if ( pinfo->pi_stat.st_ino <= (*cur)->i_ino ) {
break;
}
}
if (( (*cur) != NULL ) && ( pinfo->pi_stat.st_ino == (*cur)->i_ino )) {
return( (*cur)->i_name );
}
if (( new = ( struct inolist * ) malloc( sizeof( struct inolist )))
== NULL ) {
perror( "i_insert malloc" );
exit( 2 );
}
if (( new->i_name = ( char * ) malloc( strlen( pinfo->pi_name ) + 1 ))
== NULL ) {
perror( "i_insert malloc" );
exit( 2 );
}
strcpy( new->i_name, pinfo->pi_name );
new->i_ino = pinfo->pi_stat.st_ino;
new->i_flag = 0;
new->i_next = *cur;
*cur = new;
return( NULL );
}
void
hardlink_free( )
{
struct devlist *dev_next;
struct inolist *ino_head, *ino_next;
while ( dev_head != NULL ) {
dev_next = dev_head->d_next;
ino_head = dev_head->d_ilist;
while ( ino_head != NULL ) {
ino_next = ino_head->i_next;
free( ino_head->i_name);
free( ino_head );
ino_head = ino_next;
}
free( dev_head );
dev_head = dev_next;
}
}
int
hardlink_changed( struct pathinfo *pinfo, int set )
{
struct devlist *dcur;
struct inolist *icur;
for ( dcur = dev_head; dcur != NULL; dcur = dcur->d_next ) {
if ( pinfo->pi_stat.st_dev <= dcur->d_dev ) {
break;
}
}
if (( dcur == NULL ) || ( pinfo->pi_stat.st_dev != dcur->d_dev )) {
fprintf( stderr, "hardlink_changed: %s: dev not found\n",
pinfo->pi_name );
exit( 2 );
}
for ( icur = dcur->d_ilist; icur != NULL; icur = icur->i_next ) {
if ( pinfo->pi_stat.st_ino <= icur->i_ino ) {
break;
}
}
if (( icur == NULL ) || ( pinfo->pi_stat.st_ino != icur->i_ino )) {
fprintf( stderr, "hardlink_changed: %s: ino not found\n",
pinfo->pi_name );
exit( 2 );
}
if ( set ) {
icur->i_flag = 1;
}
return( icur->i_flag );
}