File: limit.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 (181 lines) | stat: -rw-r--r-- 4,306 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
181
#include "limit.h"
#include "support/gutils.h"
#include "support.h"

extern int cache_limit;
// count of all opened files
int open_count = 0;
// count of all cached files
int cache_count = 0;
// open and cached file count synchronisation
pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER; 
// retrieved files still cached on the disk
struct cache *cache = NULL;
struct cache *last = NULL;

int __retrieve_limit(struct file_system_info *fsinfo, struct stats *, int);
int __release_limit(struct stats *, int);

struct cache {
	node_t *node;
	struct cache *next;
};

int cache_add(node_t *);
int cache_delete();

// public:

int retrieve_limit(struct file_system_info *fsinfo, struct stats *stats){

	int repo = 0;

	debug(3, "Retrieving file; currently %d open and %d cached files;\n", 
		   open_count, cache_count);
	if ((repo = repo_number(fsinfo, stats->path)) == -1)
		return -1;
	return __retrieve_limit(fsinfo, stats, repo);

};

int release_limit(struct file_system_info *fsinfo, struct stats *stats){

	int repo = 0;

	debug(3, "Releasing file; currently %d open and %d cached files;\n", 
		   open_count, cache_count);
	if ((repo == repo_number(fsinfo, stats->path)) == -1)
		return -1;
	return __release_limit(stats, repo);
		
};

// private:

int __retrieve_limit(struct file_system_info *fsinfo, struct stats *stats, int repo){

#define __retrieve_limit_finish(value){						\
			unlock(file_mutex[repo][stats->rev]);			\
			gstrdel(file);									\
			gstrdel(revision);								\
			return value;									\
		}

	char *file = NULL;
	char *revision = calloc(20, sizeof(char));
	struct stat temp;
    node_t *node;

	lock(file_mutex[repo][stats->rev]);
    if ((node = add_file(open_files, stats->path, stats->rev)) == NULL)
        __retrieve_limit_finish(-1);
	if (node->count > 0){
		node->count++;
		__retrieve_limit_finish(0);
	};

	// retrieving file
	if (create_tmp_file(stats, node) == -1)
		__retrieve_limit_finish(-1);
	if (gmstrcpy(&file, fsinfo->repos[repo], "/", stats->internal, 0) == -1)
		__retrieve_limit_finish(-1);
	sprintf(revision, "%dB", stats->rev);
	if (retrieve_rdiff(revision, file, node->tmp_path) != 0)
		__retrieve_limit_finish(-1);
	debug(3, "Retrieved to %s\n", node->tmp_path);
	if (stat(node->tmp_path, &temp) != 0)
		__retrieve_limit_finish(-1);
	node->count = 1;

	// cache control
	lock(cache_mutex);
	if (node->count == 1)
		open_count++;
	unlock(file_mutex[repo][stats->rev]);
	free(file);
	free(revision);
	while ((open_count + cache_count > cache_limit) && (open_count < cache_limit))
		cache_delete(fsinfo);
	unlock(cache_mutex);

	return 0;

};

int __release_limit(struct stats *stats, int repo){

#define __release_limit_finish(value) {					\
			unlock(file_mutex[repo][stats->rev]);		\
			return value;								\
		}

    node_t *node = get_open_file(stats->path);
	lock(file_mutex[repo][stats->rev]);
	if (node->count > 1){
		node->count--;
		__release_limit_finish(0);
	}
	lock(cache_mutex);
	if (open_count > cache_limit){
		// nothing is cached; if open_count > cache_limit, then cache_count = 0
		open_count--;
		unlock(cache_mutex);
		node->count = 0;
		unlink(node->tmp_path);
        delete_open_file(node);
	}
	else {
		open_count--;
		cache_add(node);
		unlock(cache_mutex);
	};
	__release_limit_finish(0);

};

int cache_add(node_t *node){

	struct cache *temp = single(struct cache);

	debug(3, "Expanding cache with %s file with %d open and %d cached files;\n", node->path, open_count, cache_count);
	cache_count++;
	temp->node = node;
	if (cache == NULL){
		cache = temp;
		last = temp;
	}
	else{
		last->next = temp;
		last = temp;
	};
	return 0;
	
};

int cache_delete(struct file_system_info *fsinfo){

	struct cache *temp = NULL;
	int repo = 0;
	if (cache == NULL)
		return -1;
     if ((repo = repo_number(fsinfo, cache->node->path)) == -1)
        return -1;
	debug(3, "Deleting from cache %s file with %d open and %d cached files;\n", cache->node->path, open_count, cache_count);
	lock(file_mutex[repo][cache->node->rev]);
	if (cache->node->count > 1){
		cache->node->count--;
		open_count++;
	}
	else{
		cache->node->count = 0;
		unlink(cache->node->tmp_path);
		gstrdel(cache->node->tmp_path);
	};
	unlock(file_mutex[repo][cache->node->rev]);
	cache_count--;
	temp = cache;
	cache = cache->next;
	free(temp);
	return 0;

};