File: full.c

package info (click to toggle)
rdiff-backup-fs 1.0.0-8
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 952 kB
  • sloc: sh: 3,800; ansic: 2,944; makefile: 23
file content (180 lines) | stat: -rw-r--r-- 4,997 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
#include "full.h"

// prototypes:

int add_revs_dir(char *, char *);

void read_revision_all(char *, char *, int);

int read_stats_all(struct stats *stats, char *prefix, int rev, FILE *file);

#define CURRENT_SNAPSHOT "mirror_metadata.current.snapshot"

// variables:

tree_t structure_tree = NULL;

// public:

int full_build(struct file_system_info *fsinfo){

    int i = 0;
	
    gtreenew(&structure_tree);
    if ((fsinfo->rev_count[0] = gather_revisions(fsinfo, fsinfo->repos[0], data_dir)) == -1)
        return -1;
    for (i = fsinfo->rev_count[0] - 1; i >= 0; i--){
        add_snapshot(fsinfo->revs[i], CURRENT_SNAPSHOT, data_dir);
		read_revision_all(NULL, fsinfo->revs[i], fsinfo->rev_count[0] - i - 1);
	};
	return 0;

};

int full_build_multi(struct file_system_info *fsinfo){

#define full_build_multi_free_revs											\
            if (fsinfo->revs != NULL){										\
            	char *snapshot = NULL;                                      \
				gmstrcpy(&snapshot, data_dir, "/", CURRENT_SNAPSHOT, 0);	\
				unlink(snapshot);											\
				gstrdel(snapshot);											\
			}

#define full_build_multi_finish(value) {								    \
			full_build_multi_free_revs;                                     \
			return value;													\
		}

    int i = 0, j = 0;

    debug(2, "Received %d repos;\n", fsinfo->repo_count);
	gtreenew(&structure_tree);
    for (i = 0; i < fsinfo->repo_count; i++){
        if ((fsinfo->rev_count[i] = gather_revisions(fsinfo, fsinfo->repos[i], data_dir)) == -1)
            continue;
		if (add_repo_dir(fsinfo->repo_names[i], structure_tree) == -1){
			full_build_multi_free_revs;
			continue;
		};
		for (j = fsinfo->rev_count[i] - 1; j >= 0; j--){
            add_snapshot(fsinfo->revs[j], CURRENT_SNAPSHOT, data_dir);
		    read_revision_all(fsinfo->repo_names[i], fsinfo->revs[j], fsinfo->rev_count[i] - j - 1);
		};
		full_build_multi_free_revs;
    };
    return 0;
};

int full_get_file(struct file_system_info *fsinfo, char *repo, char *revision, char *internal, stats_t **stats){
	
    (void) fsinfo;
    
	char *path = NULL;
	int result = 0;

	set_path(path, repo, revision, internal);
	result = gtreeget(structure_tree, path, stats);
	gstrdel(path);
	return result;
	
};

char** full_get_children(struct file_system_info *fsinfo, char *repo, char *revision, char *internal){

    (void) fsinfo;

	char *path = NULL;
	char **result = NULL;

	set_path(path, repo, revision, internal);
	result = gtreecld(structure_tree, path);
	gstrdel(path);
	return result;

};

// private:

void read_revision_all(char *repo, char *rev, int rev_index){

#define read_revision_error {						\
	    gstrdel(file);									\
	    gstrdel(rev_dir);								\
	    if (file != NULL)								\
	    	fclose(file);								\
	    return;										\
	}
	
	// printf("[Function: read_repo_revision] Received repo %s and revision %s;\n", repo, rev);
    char *rev_dir = NULL;
    char *file_path = NULL;
    FILE *file = NULL;
    struct stats *stats = calloc(1, sizeof(struct stats));

	if (repo == NULL){
		if (gmstrcpy(&rev_dir, "/", get_revs_dir(rev), 0) != 0)
			read_revision_error;
	}
	else{ // repo != NULL
    	if (gmstrcpy(&rev_dir, "/", repo, "/", get_revs_dir(rev), 0) != 0)
			read_revision_error;
	};
    if (gmstrcpy(&file_path, data_dir, "/", CURRENT_SNAPSHOT, 0) != 0)
		read_revision_error;
    if ((file = fopen(file_path, "r")) == NULL)
		read_revision_error;
    add_revs_dir(rev, repo);

    while (read_stats_all(stats, rev_dir, rev_index, file) == 0)
        update_tree(structure_tree, stats, stats->path);

    fclose(file);
    gmstrcpy(&file_path, data_dir, "/", rev, 0);
    unlink(file_path);
    gstrdel(file_path);
    gstrdel(rev_dir);
    free(stats);
    
};

int read_stats_all(struct stats *stats, char *prefix, int rev, FILE *file){

	if (read_stats(stats, file) != 0)
		return -1;
	if (gmstrcpy(&stats->path, prefix, "/", stats->internal, 0) == -1)
		return -1;
	gstrdel(stats->internal);
	if (gpthcldptr(&stats->name, stats->path) == -1)
		return -1;
	if (stats->type == S_IFDIR)
		stats->size = DIR_SIZE;
    stats->internal = stats->path + strlen(prefix) + strlen("/");
    stats->rev = rev;
	debug(2, "Read data of file %s\n", stats->path);
    return 0;
   
};

int add_revs_dir(char *revision, char *repository){
    
    struct stats stats;
    
    memset(&stats, 0, sizeof(stats));
    if (revision == NULL)
		return -1;
    if (repository == NULL){
		gmstrcpy(&stats.path, "/", get_revs_dir(revision), NULL);
		// printf("[Function: add_revs_dir] Adding revision %s to the data structure;\n", stats->path);
		// stats->name = stats->path + strlen("/")'
		stats.name = stats.path + 1;
    }
    else{
		gmstrcpy(&stats.path, "/", repository, "/", get_revs_dir(revision), NULL);
		// stats->name = stats->path + strlen("/") + strlen(repository) + strlen("/");
		stats.name = stats.path + 1 + strlen(repository) + 1;
    };
	set_directory_stats(&stats);
    return gtreeadd(structure_tree, &stats, stats.path);
    
};