Project

General

Profile

Bug #13941 ยป blocking_fs.c

Blocking filesystem - graywolf (Gray Wolf), 09/26/2017 12:10 AM

 
1
#define FUSE_USE_VERSION 30
2

    
3
#include <fuse.h>
4
#include <stdio.h>
5
#include <unistd.h>
6
#include <sys/types.h>
7
#include <time.h>
8
#include <string.h>
9
#include <stdlib.h>
10

    
11
static void sleep_for(int s) {
12
	printf("\tsleep ");
13
	for (int i = 0; i < s; ++i) {
14
		printf(".");
15
		sleep(1);
16
	}
17
	printf("\n");
18
}
19

    
20
static int do_getattr(const char *path, struct stat *st) {
21
	printf("[getattr] Called\n");
22
	printf("\tAttributes of %s requested\n", path);
23

    
24
	sleep_for(30);
25

    
26
	st->st_uid = getuid();
27
	st->st_gid = getgid();
28
	st->st_atime = time(NULL);
29
	st->st_mtime = time(NULL);
30

    
31
	if (strcmp(path, "/") == 0) {
32
		st->st_mode = S_IFDIR | 0755;
33
		st->st_nlink = 2;
34
	} else {
35
		st->st_mode = S_IFREG | 0644;
36
		st->st_nlink = 1;
37
		st->st_size = 1024;
38
	}
39

    
40
	return 0;
41
}
42

    
43
static int do_readdir(
44
	const char *path,
45
	void *buffer,
46
	fuse_fill_dir_t filler,
47
	off_t offset,
48
	struct fuse_file_info *fi
49
) {
50
	printf("[readdir] Called\n");
51
	printf("\tGetting The List of Files of %s\n", path);
52

    
53
	sleep_for(30);
54

    
55
	filler(buffer, ".", NULL, 0); // Current Directory
56
	filler(buffer, "..", NULL, 0); // Parent Directory
57

    
58
	// If the user is trying to show the files/directories of the root
59
	// directory show the following
60
	if (strcmp(path, "/") == 0) {
61
		filler(buffer, "file54", NULL, 0);
62
		filler(buffer, "file349", NULL, 0);
63
	}
64

    
65
	return 0;
66
}
67

    
68
static int do_read(
69
	const char *path,
70
	char *buffer,
71
	size_t size,
72
	off_t offset,
73
	struct fuse_file_info *fi
74
) {
75
	printf("[read] Called\n");
76
	printf("\tGetting The Content of %s, size: %zu, off: %zu \n", path, size, offset);
77

    
78
	sleep_for(30);
79

    
80
	char file54Text[] = "Hello World From File54!";
81
	char file349Text[] = "Hello World From File349!";
82
	char *selectedText = NULL;
83

    
84
	if ( strcmp( path, "/file54" ) == 0 ) {
85
		selectedText = file54Text;
86
	} else if ( strcmp( path, "/file349" ) == 0 ) {
87
		selectedText = file349Text;
88
	} else {
89
		return -1;
90
	}
91

    
92
	memcpy(buffer, selectedText + offset, size);
93

    
94
	return strlen(selectedText) - offset;
95
}
96

    
97
static struct fuse_operations operations = {
98
	.getattr = do_getattr,
99
	.readdir = do_readdir,
100
	.read    = do_read,
101
};
102

    
103
int main(int argc, char *argv[]) {
104
	setbuf(stdout, NULL);
105

    
106
	return fuse_main(argc, argv, &operations, NULL);
107
}