aboutsummaryrefslogtreecommitdiff
path: root/fusewrapper.c
blob: 604f4a27215944b1f07f75ca735a03e7d09829d5 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* $Date: 2010-09-30 15:40:39 -0300 (Thu, 30 Sep 2010) $ */
/* $Revision: 1364 $ */

#define _GNU_SOURCE
#define FUSE_USE_VERSION 26

#include <assert.h>

#include <stdlib.h>
#include <fuse.h>
#include <errno.h>
#include <malloc.h>
#include <time.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "medianin.h"
#include "log.h"

/* Variable global que contiene el SimpleFS */
medianin *fs = NULL;

/*
 * Operaciones Básicas
 */

static int fs_getattr(const char *path, struct stat *stbuf)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("getattr:%s", path);
	rta = medianin_getattr(fs, path, stbuf);
	debug2(" -> mode:%o size:%li", stbuf->st_mode, (long)stbuf->st_size);
	debug3(" returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_readlink(const char *path, char *buf, size_t size)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("readlink:%s size:%li", path, (long)size);
	rta = medianin_readlink(fs, path, buf, size);
	debug2(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_mknod(const char *path, mode_t mode, dev_t rdev)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("mknod:%s mode:%o", path, mode);
	rta = medianin_mknod(fs, path, mode);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_unlink(const char *path)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("unlink_file:%s", path);
	rta = medianin_unlink(fs, path);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_symlink(const char *from, const char *to)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("symlink from:%s to:%s", from, to);
	rta = medianin_symlink(fs, from, to);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_rename(const char *from, const char *to)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("rename from:%s to:%s", from, to);
	rta = medianin_rename(fs, from, to);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_link(const char *from, const char *to)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("link from:%s to:%s", from, to);
	rta = medianin_link(fs, from, to);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_chmod(const char *path, mode_t mode)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("chmod:%s modo:%o", path, mode);
	rta = medianin_chmod(fs, path, mode);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_chown(const char *path, uid_t uid, gid_t gid)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("chown:%s uid:%i gid:%i", path, uid, gid);
	rta = medianin_chown(fs, path, uid, gid);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_truncate(const char *path, off_t size)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("truncate:%s size:%li", path, (long)size);
	rta = medianin_truncate(fs, path, size);
	debug1("\n");
	return rta;
}

static int fs_utime(const char *path, struct utimbuf *utb)
{
	int rta = 0;
	struct timespec tv[2];
	tv[0].tv_sec = utb->actime;
	tv[0].tv_nsec = 0;
	tv[1].tv_sec = utb->modtime;
	tv[1].tv_nsec = 0;
	debug3("[%li] ", time(NULL));
	debug1("utime:%s atime:%li mtime:%li",
	       path, (long)utb->actime, (long)utb->modtime);
	rta = medianin_utimens(fs, path, tv);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_open(const char *path, struct fuse_file_info *fi)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("*open:%s flags:%i", path, fi->flags);
	debug1("\n");
	return rta;
}

static int fs_read(const char *path, char *buf, size_t size, off_t offset,
		   struct fuse_file_info *fi)
{
	int rta = 0;
	(void) fi;
	debug3("[%li] ", time(NULL));
	debug1("read:%s size:%li offset:%li", path, (long)size, (long)offset);
	rta = medianin_read(fs, path, buf, size, offset);
	debug2(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_write(const char *path, const char *buf, size_t size,off_t offset,
		    struct fuse_file_info *fi)
{
	int rta = 0;
	(void) fi;
	debug3("[%li] ", time(NULL));
	debug1("write:%s size:%li offset:%li", path, (long)size, (long)offset);
	rta = medianin_write(fs, path, buf, size, offset);
	debug2(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_statfs(const char *path, struct statvfs *stbuf)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("statfs:%s", path);
	rta = medianin_statfs(fs, path, stbuf);
	debug2(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}


static int fs_release(const char *path, struct fuse_file_info *fi)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("*release:%s flags:%i", path, fi->flags);
	debug1("\n");
	return rta;
}

static int fs_utimens(const char *path, const struct timespec tv[2])
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("utimens:%s atime:%li mtime:%li",
	       path, (long)tv[0].tv_sec, (long)tv[1].tv_sec);
	rta = medianin_utimens(fs, path, tv);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

/*
 * Operaciones sobre Directorios
 */

static int fs_mkdir(const char *path, mode_t mode)
{
	int rta =  0;
	debug3("[%li] ", time(NULL));
	debug1("mkdir:%s mode:%o", path, mode);
	rta = medianin_mkdir(fs, path, mode);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_rmdir(const char *path)
{
	int rta = 0;
	debug3("[%li] ", time(NULL));
	debug1("rmdir:%s", path);
	rta = medianin_rmdir(fs, path);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}

static int fs_readdir(const char *path, void *buf,
		      fuse_fill_dir_t filler, off_t offset,
		      struct fuse_file_info *fi)
{
	int rta = 0;
	(void) fi; /* Para que no diga que la variable está sin usar */
	debug3("[%li] ", time(NULL));
	debug1("readdir:%s", path);
	rta = medianin_readdir(fs, path, buf, filler);
	debug3(" -> returns:%i", rta);
	debug1("\n");
	return rta;
}


static struct fuse_operations fs_oper =
{
	/* Operaciones sobre archivos, NI = no implementada */
	.getattr  = fs_getattr, /* Done */
	.readlink = fs_readlink,
	.mknod	  = fs_mknod,   /* Done */
	.unlink	  = fs_unlink,
	.symlink  = fs_symlink,
	.rename	  = fs_rename,
	.link	  = fs_link,
	.chmod	  = fs_chmod,   /* Almost done */
	.chown	  = fs_chown,   /* Almost done */
	.truncate = fs_truncate,
	.utime    = fs_utime,   /* Almost done */
	.open	  = fs_open,	/* NI */
	.read	  = fs_read,
	.write	  = fs_write,
	.statfs	  = fs_statfs,  /* Done */
	.release  = fs_release,	/* NI */
	.utimens  = fs_utimens, /* Almost done */
	/* Operaciones sobre directorios */
	.mkdir	  = fs_mkdir,
	.rmdir	  = fs_rmdir,
	.readdir  = fs_readdir,
};

/* !! end Rafa !! */

int main(int argc, char *argv[])
{
	/* Cheat es una variable al estilo de argv pero solo con argumentos
	 * parseados como validos. La uso para trabajar menos
	 */
	char *cheat[10]; /* 10 es un indice que nunca se alcanza */
	int i = 1;
	int argi = 1;
	char *logfilename = NULL;
	char *mountpoint = NULL;
	char *tailptr = NULL; /* Variable temporal usada pora strtol */
	int loglevel = 3; /* default loglevel */
	fs_oper.utimens = 0;
	cheat[0] = argv[0];

	/* i==1 && argi == 1   i se usa con cheat y argi con argv */
	/* cuando consumo un argumento aumento argi */
	/* cuando agrego uno para fusemain aumento i */
	while (argi < argc)
	{
		if (strcmp(argv[argi], "-h") == 0)
		{
			printf("uso: %s [opciones] mountpoint\n"
				"opciones:\n"
				"\t-h\t\tImprime esta ayuda.\n\n"
				"\t-logfile file\n"
				"\t-l file\t\tGuarda los logs en el archivo"
					" especificado.\n\n"
				"\t-loglevel num\n"
				"\t-n num\t\tEspecifica el nivel de log. "
					"Por defecto es 3.\n\n"
				, argv[0]);
			return 1;
		}
		else if (strcmp(argv[argi], "-logfile") == 0
			|| strcmp(argv[argi], "-l") == 0)
		{
			argi++;
			if(argi < argc) logfilename = argv[argi];
			else
			{
				fprintf(stderr, "Se debe especificar un nombre"
					      " de archivo\n");
				return 1;
			}
			loglevel=1; /* Para evitar malos usos */
		}
		else if (strcmp(argv[argi], "-loglevel") == 0
			 || strcmp(argv[argi], "-n") == 0)
		{
			argi++;
			if (argi < argc)
			{
				loglevel = (int)strtol(argv[argi], &tailptr, 10);
				if (tailptr == argv[argi])
				{
					fprintf(stderr,
						"El nivel de log debe"
						"ser ún numero\n");
					return 1;
				}
				if (loglevel < 0)
				{
					fprintf(stderr,
						"El nivel de log debe"
						"ser un número natural\n");
					return 1;
				}
			}
			else
			{
				fprintf(stderr, "Se debe especificar un nivel"
					      " de log\n");
				return 1;
			}
		}
		else if (argv[argi][0] == '-')
		{
				fprintf(stderr, "Opción no reconocida:"
					      "%s\n",argv[argi]);
				return 1;
		}
		else if (mountpoint == NULL)
		{
			mountpoint = argv[argi];
		}
		argi++;
	}
	if(mountpoint == NULL)
	{
		fprintf(stderr, "Argumentos insuficientes: "
				"hace falta un mountpoint\n");
		return 1;
	}
	cheat[i++] = mountpoint;
	if (loglevel != 0)
	{
		cheat[i++] = "-f";
	}
	cheat[i] = NULL;

	if (log_open(logfilename, loglevel)!=0)
	{
		fprintf(stderr, "Advertencia! no se pudo inicializar el log\n");
	}
	
	debug3("Los numeros a la izquierda indican el momento en que la "
	       "llamada a la\nfunción se efectuó según time(NULL).\n");

	fs = medianin_create();
	if (fs == NULL)
	{
		return ENOMEM;
	}
	
	fuse_main(i, cheat, &fs_oper, NULL); /* El corazón del programa */
	
	medianin_destroy(fs);
	log_close();

	return 0;
}