summaryrefslogtreecommitdiff
path: root/src/hard_link_stack.c
blob: abef55ef5b6fc51870d062db84fe70a44bdff380 (plain)
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
/***************************************************************************
 *   Copyright (C) 2010 by Roberto Maar                                    *
 *   robi6@users.sf.net                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

//construct for global collect of hardlinks

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "hard_link_stack.h"

static struct link_stack_head head;

void init_link_stack(){
	head.count = 0;
	head.begin = NULL;
	head.pointer = NULL;
}


void add_link_stack(ext2_ino_t inode_nr, __u32 link_count, char* name, __u32 generation){
	struct link_entry* this;
	
	this = malloc (sizeof(struct link_entry));
	if (!this) 
		goto errout;
	this->inode_nr = inode_nr;
	this->link_count = link_count -1 ;
	this->name = malloc(strlen(name) +1);
	this->generation = generation;
	if (!this->name) 
		goto errout;
	strcpy(this->name,name);
	this->next = head.begin;
	head.begin = this;
	head.count++;	
return;

errout:
	if(this->name)
		free(this->name);
	if(this)
		free(this);
}


// subfunction for check_find_dir() use in stage 2 of magical recover 
int rename_hardlink_path(char *old, char *neu){
	char *newname;
	char *endname;
	int size = strlen(old);
	
	head.pointer = head.begin;
	while (head.pointer) {
		if (! strcmp(old,head.pointer->name)){
			newname=malloc(strlen(neu) + strlen(head.pointer->name) - size +1);
			if (! newname)
				return 1;				 
			strcpy(newname,neu);
			endname = head.pointer->name + size;
			strcat(newname,endname);
			free(head.pointer->name);
			head.pointer->name = newname;
//#ifdef DEBUG
			fprintf(stderr,"HL-DB change %s -> %s\n",old,neu);
//#endif
		}
		head.pointer = head.pointer->next;
	}
	return 0;
}




		
char* check_link_stack(ext2_ino_t inode_nr, __u32 generation){

	head.pointer = head.begin;
	while (head.pointer) {
		 if((head.pointer->inode_nr == inode_nr) && (head.pointer->generation == generation))
			break;
		head.pointer = head.pointer->next;
	}

#ifdef DEBUG
	if (head.pointer)
		printf("HARD_LINK found -> %s\n",head.pointer->name);
#endif
	return (head.pointer) ? head.pointer->name : NULL ;
}


// not used ; gcc warning okay
static void del_link_stack(struct link_entry* entry){
	if(entry->name)	
		free(entry->name);
	head.pointer = head.begin;
	if (head.begin->next){
		while ((head.pointer->next)  && (head.pointer != entry) && (head.pointer->next != entry))
			head.pointer = head.pointer->next;
		if(head.begin == entry)
			head.begin = entry->next;
		else
			head.pointer->next = entry->next;
	}
	else 
		head.begin = NULL;
	free(entry);
	head.count--;		
}



int  match_link_stack(ext2_ino_t inode_nr, __u32 generation){
	int retval = 1;
	if ((head.pointer->inode_nr == inode_nr) && (head.pointer->generation == generation)){
//		if (! --(head.pointer->link_count))
//			del_link_stack(head.pointer);
		(head.pointer->link_count)-- ;
		retval = 0;
	}
return retval;
}



void clear_link_stack(){
	int d_count = 0 ;

	fflush(stdout);
	if (head.count){
		fprintf(stderr,"Hardlink Database : %lu entries\n", (long unsigned int)head.count);
	
		head.pointer = head.begin;
		while (head.pointer){
			if(head.pointer->link_count){
				fprintf(stderr,"%10d\t%s\n",head.pointer->link_count,head.pointer->name);
				d_count++ ;
			}
			if(head.pointer->name)	
				free(head.pointer->name);
			head.begin = head.pointer->next;
			free(head.pointer);
			head.pointer = head.begin;
		}
		if (! d_count) 
			fprintf(stderr,"all Hardlinks be resolved\n");
	}
}