操作记录:在ubuntu16.04.1配置fuse开发环境及fuse开发规范测试

简介:

1、使用ssh客户端,登陆ubuntu

1
ssh  -p 2206 root@127.0.0.1

2、下载fuse源码,进行编译,安装(当前工作目录为~)

1
2
3
4
5
6
7
8
wget https: //github .com /libfuse/libfuse/releases/download/fuse-3 .0.0rc3 /fuse-3 .0.0rc3. tar .gz
tar  xvf fuse-3.0.0rc3. tar .gz
cd  fuse-3.0.0rc3/
. /configure
make  -j8
make  install
depmod
modprobe fuse

3、测试example\hello_ll是否能正常工作

1
2
3
4
5
6
7
8
9
cd  example/
. /hello_ll  --help
. /hello_ll  /mnt
mount
cd  /mnt
ls
cat  hello
cd
umount  /mnt

  • 针对example\hello_ll.c,进行改动测试:


1、在根目录下多创建一个文件出来,节点号定为5,名称为frombyte,内容为文本"http://www.datahf.net"

改动如下:

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
/*
   FUSE: Filesystem in Userspace
   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
 
   This program can be distributed under the terms of the GNU GPL.
   See the file COPYING.
*/
 
/** @file
  *
  * minimal example filesystem using low-level API
  *
  * Compile with:
  *
  *     gcc -Wall hello_ll.c `pkg-config fuse3 --cflags --libs` -o hello_ll
  *
  * ## Source code ##
  * \include hello_ll.c
  */
 
#define FUSE_USE_VERSION 30
 
#include <config.h>
 
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
 
static  const  char  *hello_str =  "Hello World!\n" ;
static  const  char  *hello_name =  "hello" ;
//change1 #0
static  const  char  *inode5_str =  "http://www.datahf.net!\n" ;
static  const  char  *inode5_name =  "frombyte" ;
//change end
static  int  hello_stat(fuse_ino_t ino,  struct  stat *stbuf)
{
     stbuf->st_ino = ino;
     switch  (ino) {
     case  1:
         stbuf->st_mode = S_IFDIR | 0755;
         stbuf->st_nlink = 2;
         break ;
 
     case  2:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size =  strlen (hello_str);
         break ;
 
//change1  #1
     case  5:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size =  strlen (inode5_str);
         break ;
//change1 end
             
     default :
         return  -1;
     }
     return  0;
}
 
static  void  hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
                  struct  fuse_file_info *fi)
{
     struct  stat stbuf;
 
     ( void ) fi;
 
     memset (&stbuf, 0,  sizeof (stbuf));
     if  (hello_stat(ino, &stbuf) == -1)
         fuse_reply_err(req, ENOENT);
     else
         fuse_reply_attr(req, &stbuf, 1.0);
}
 
static  void  hello_ll_lookup(fuse_req_t req, fuse_ino_t parent,  const  char  *name)
{
     struct  fuse_entry_param e;
     //change1  #2
     /*
     if (parent != 1 || strcmp(name, hello_name) != 0)
         fuse_reply_err(req, ENOENT);
     else {
         memset(&e, 0, sizeof(e));
         e.ino = 2;
         e.attr_timeout = 1.0;
         e.entry_timeout = 1.0;
         hello_stat(e.ino, &e.attr);
         
         fuse_reply_entry(req, &e);
     }*/
 
 
     if  (parent != 1)
         fuse_reply_err(req, ENOENT);
     else  if ( strcmp (name, hello_name) == 0){
         memset (&e, 0,  sizeof (e));
         e.ino = 2;
         e.attr_timeout = 1.0;
         e.entry_timeout = 1.0;
         hello_stat(e.ino, &e.attr);
         fuse_reply_entry(req, &e);
     }
     else  if ( strcmp (name, inode5_name) == 0){
         memset (&e, 0,  sizeof (e));
         e.ino = 5;
         e.attr_timeout = 1.0;
         e.entry_timeout = 1.0;
         hello_stat(e.ino, &e.attr);
         fuse_reply_entry(req, &e);
     }
     else
         fuse_reply_err(req, ENOENT);
     //change1 end
}
 
struct  dirbuf {
     char  *p;
     size_t  size;
};
 
static  void  dirbuf_add(fuse_req_t req,  struct  dirbuf *b,  const  char  *name,
                fuse_ino_t ino)
{
     struct  stat stbuf;
     size_t  oldsize = b->size;
     b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
     b->p = ( char  *)  realloc (b->p, b->size);
     memset (&stbuf, 0,  sizeof (stbuf));
     stbuf.st_ino = ino;
     fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
               b->size);
}
 
#define min(x, y) ((x) < (y) ? (x) : (y))
 
static  int  reply_buf_limited(fuse_req_t req,  const  char  *buf,  size_t  bufsize,
                  off_t off,  size_t  maxsize)
{
     if  (off < bufsize)
         return  fuse_reply_buf(req, buf + off,
                       min(bufsize - off, maxsize));
     else
         return  fuse_reply_buf(req, NULL, 0);
}
 
static  void  hello_ll_readdir(fuse_req_t req, fuse_ino_t ino,  size_t  size,
                  off_t off,  struct  fuse_file_info *fi)
{
     ( void ) fi;
 
     if  (ino != 1)
         fuse_reply_err(req, ENOTDIR);
     else  {
         struct  dirbuf b;
 
         memset (&b, 0,  sizeof (b));
         dirbuf_add(req, &b,  "." , 1);
         dirbuf_add(req, &b,  ".." , 1);
         dirbuf_add(req, &b, hello_name, 2);
         //change1
         dirbuf_add(req, &b, inode5_name, 5);
         //end
         reply_buf_limited(req, b.p, b.size, off, size);
         free (b.p);
     }
}
 
static  void  hello_ll_open(fuse_req_t req, fuse_ino_t ino,
               struct  fuse_file_info *fi)
{
     //change1
     //if (ino != 2)
     if  ( (ino !=2) && (ino != 5) )
     //end
         fuse_reply_err(req, EISDIR);
     else  if  ((fi->flags & 3) != O_RDONLY)
         fuse_reply_err(req, EACCES);
     else
         fuse_reply_open(req, fi);
}
 
static  void  hello_ll_read(fuse_req_t req, fuse_ino_t ino,  size_t  size,
               off_t off,  struct  fuse_file_info *fi)
{
     ( void ) fi;
 
     //change1
     //assert(ino == 2);
     //reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
     switch  (ino) {
         case  2:
             reply_buf_limited(req, hello_str,  strlen (hello_str), off, size);
             break ;
         case  5:
             reply_buf_limited(req, inode5_str,  strlen (inode5_str), off, size);
             break ;
         default :
             ;
     }
     //end
}
 
static  struct  fuse_lowlevel_ops hello_ll_oper = {
     .lookup     = hello_ll_lookup,
     .getattr    = hello_ll_getattr,
     .readdir    = hello_ll_readdir,
     .open       = hello_ll_open,
     .read       = hello_ll_read,
};
 
int  main( int  argc,  char  *argv[])
{
     struct  fuse_args args = FUSE_ARGS_INIT(argc, argv);
     struct  fuse_session *se;
     struct  fuse_cmdline_opts opts;
     int  ret = -1;
 
     if  (fuse_parse_cmdline(&args, &opts) != 0)
         return  1;
     if  (opts.show_help) {
         printf ( "usage: %s [options] <mountpoint>\n\n" , argv[0]);
         fuse_cmdline_help();
         fuse_lowlevel_help();
         ret = 0;
         goto  err_out1;
     else  if  (opts.show_version) {
         printf ( "FUSE library version %s\n" , fuse_pkgversion());
         fuse_lowlevel_version();
         ret = 0;
         goto  err_out1;
     }
 
     se = fuse_session_new(&args, &hello_ll_oper,
                   sizeof (hello_ll_oper), NULL);
     if  (se == NULL)
         goto  err_out1;
 
     if  (fuse_set_signal_handlers(se) != 0)
         goto  err_out2;
 
     if  (fuse_session_mount(se, opts.mountpoint) != 0)
         goto  err_out3;
 
     fuse_daemonize(opts.foreground);
 
     /* Block until ctrl+c or fusermount -u */
     if  (opts.singlethread)
         ret = fuse_session_loop(se);
     else
         ret = fuse_session_loop_mt(se, opts.clone_fd);
 
     fuse_session_unmount(se);
err_out3:
     fuse_remove_signal_handlers(se);
err_out2:
     fuse_session_destroy(se);
err_out1:
     free (opts.mountpoint);
     fuse_opt_free_args(&args);
 
     return  ret ? 1 : 0;
}

结果测试:

1
2
3
4
5
6
cd  ~ /fuse-3 .0.0rc3 /example/
make
. /hello_ll  /mnt
ll -i  /mnt
cat  /mnt/frombyte 
umount  /mnt

2、增加目录、增加二进制文件测试:

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
/*
   FUSE: Filesystem in Userspace
   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
 
   This program can be distributed under the terms of the GNU GPL.
   See the file COPYING.
*/
 
/** @file
  *
  * minimal example filesystem using low-level API
  *
  * Compile with:
  *
  *     gcc -Wall hello_ll.c `pkg-config fuse3 --cflags --libs` -o hello_ll
  *
  * ## Source code ##
  * \include hello_ll.c
  */
 
#define FUSE_USE_VERSION 30
 
#include <config.h>
 
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
 
static  const  char  *hello_str =  "Hello World!\n" ;
static  const  char  *hello_name =  "hello" ;
//change1 #0
static  const  char  *inode5_str =  "http://www.datahf.net!\n" ;
static  const  char  *inode5_name =  "frombyte" ;
 
static  const  char  *inode3_name =  "frombyte.dir" ;
static  const  char  *inode6_name =  "inode6.img" ;
//change end
static  int  hello_stat(fuse_ino_t ino,  struct  stat *stbuf)
{
     stbuf->st_ino = ino;
     switch  (ino) {
     case  1:
         stbuf->st_mode = S_IFDIR | 0755;
         stbuf->st_nlink = 3;
         break ;
 
     case  2:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size =  strlen (hello_str);
         break ;
 
//change1  #1
     case  3:
         stbuf->st_mode = S_IFDIR | 0755;
         stbuf->st_nlink = 2;
         break ;
             
     case  5:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size =  strlen (inode5_str);
         break ;
             
     case  6:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size = 1024 * 1024 * 1024;
         break ;
     
//change1 end
             
     default :
         return  -1;
     }
     return  0;
}
 
static  void  hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
                  struct  fuse_file_info *fi)
{
     struct  stat stbuf;
 
     ( void ) fi;
 
     memset (&stbuf, 0,  sizeof (stbuf));
     if  (hello_stat(ino, &stbuf) == -1)
         fuse_reply_err(req, ENOENT);
     else
         fuse_reply_attr(req, &stbuf, 1.0);
}
 
static  void  hello_ll_lookup(fuse_req_t req, fuse_ino_t parent,  const  char  *name)
{
     struct  fuse_entry_param e;
     //change1  #2
     /*
     if (parent != 1 || strcmp(name, hello_name) != 0)
         fuse_reply_err(req, ENOENT);
     else {
         memset(&e, 0, sizeof(e));
         e.ino = 2;
         e.attr_timeout = 1.0;
         e.entry_timeout = 1.0;
         hello_stat(e.ino, &e.attr);
         
         fuse_reply_entry(req, &e);
     }*/
 
     switch  (parent) {
         case  1:
             if ( strcmp (name, hello_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 2;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else  if ( strcmp (name, inode3_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 3;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else
                 fuse_reply_err(req, ENOENT);
             break ;
         case  3:
             if ( strcmp (name, inode5_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 5;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else  if ( strcmp (name, inode6_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 6;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else
                 fuse_reply_err(req, ENOENT);
             break ;
             
         default :
             fuse_reply_err(req, ENOENT);
             break ;
     }
     //change1 end
}
 
struct  dirbuf {
     char  *p;
     size_t  size;
};
 
static  void  dirbuf_add(fuse_req_t req,  struct  dirbuf *b,  const  char  *name,
                fuse_ino_t ino)
{
     struct  stat stbuf;
     size_t  oldsize = b->size;
     b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
     b->p = ( char  *)  realloc (b->p, b->size);
     memset (&stbuf, 0,  sizeof (stbuf));
     stbuf.st_ino = ino;
     fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
               b->size);
}
 
#define min(x, y) ((x) < (y) ? (x) : (y))
 
static  int  reply_buf_limited(fuse_req_t req,  const  char  *buf,  size_t  bufsize,
                  off_t off,  size_t  maxsize)
{
     if  (off < bufsize)
         return  fuse_reply_buf(req, buf + off,
                       min(bufsize - off, maxsize));
     else
         return  fuse_reply_buf(req, NULL, 0);
}
 
static  void  hello_ll_readdir(fuse_req_t req, fuse_ino_t ino,  size_t  size,
                  off_t off,  struct  fuse_file_info *fi)
{
     ( void ) fi;
 
     /*
     if (ino != 1)
         fuse_reply_err(req, ENOTDIR);
     else {*/
     struct  dirbuf b;
     switch  (ino) {
         case  1:
             memset (&b, 0,  sizeof (b));
             dirbuf_add(req, &b,  "." , 1);
             dirbuf_add(req, &b,  ".." , 1);
             dirbuf_add(req, &b, hello_name, 2);
             //change1
             dirbuf_add(req, &b, inode3_name, 3);
             //end
             reply_buf_limited(req, b.p, b.size, off, size);
             free (b.p);
             break ;
         case  3:
             memset (&b, 0,  sizeof (b));
             dirbuf_add(req, &b,  "." , 3);
             dirbuf_add(req, &b,  ".." , 1);
             dirbuf_add(req, &b, inode5_name, 5);
             dirbuf_add(req, &b, inode6_name, 6);
             reply_buf_limited(req, b.p, b.size, off, size);
             free (b.p);
             break ;
         default :
             fuse_reply_err(req, ENOTDIR);
             break ;
     }
     //}
}
 
static  void  hello_ll_open(fuse_req_t req, fuse_ino_t ino,
               struct  fuse_file_info *fi)
{
     //change1
     //if (ino != 2)
     switch (ino)
     {
         case  2: case  5: case  6:
             if  ((fi->flags & 3) != O_RDONLY)
                 fuse_reply_err(req, EACCES);
             else
                 fuse_reply_open(req, fi);
             break ;
         default :
             fuse_reply_err(req, EISDIR);
             break ;
     }
}
 
static  void  hello_ll_read(fuse_req_t req, fuse_ino_t ino,  size_t  size,
               off_t off,  struct  fuse_file_info *fi)
{
     ( void ) fi;
     char  * tbuf;
 
     //change1
     //assert(ino == 2);
     //reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
     switch  (ino) {
         case  2:
             reply_buf_limited(req, hello_str,  strlen (hello_str), off, size);
             break ;
         case  5:
             reply_buf_limited(req, inode5_str,  strlen (inode5_str), off, size);
             break ;
         case  6:
             tbuf = ( char  *) malloc (size);
             
             for ( int  i = 0; i < size /  sizeof ( int ); i++)
                 (( int *)tbuf)[i] = size;
             fuse_reply_buf(req, tbuf,size);
             free (tbuf);
         default :
             ;
     }
     //end
}
 
static  struct  fuse_lowlevel_ops hello_ll_oper = {
     .lookup     = hello_ll_lookup,
     .getattr    = hello_ll_getattr,
     .readdir    = hello_ll_readdir,
     .open       = hello_ll_open,
     .read       = hello_ll_read,
};
 
int  main( int  argc,  char  *argv[])
{
     struct  fuse_args args = FUSE_ARGS_INIT(argc, argv);
     struct  fuse_session *se;
     struct  fuse_cmdline_opts opts;
     int  ret = -1;
 
     if  (fuse_parse_cmdline(&args, &opts) != 0)
         return  1;
     if  (opts.show_help) {
         printf ( "usage: %s [options] <mountpoint>\n\n" , argv[0]);
         fuse_cmdline_help();
         fuse_lowlevel_help();
         ret = 0;
         goto  err_out1;
     else  if  (opts.show_version) {
         printf ( "FUSE library version %s\n" , fuse_pkgversion());
         fuse_lowlevel_version();
         ret = 0;
         goto  err_out1;
     }
 
     se = fuse_session_new(&args, &hello_ll_oper,
                   sizeof (hello_ll_oper), NULL);
     if  (se == NULL)
         goto  err_out1;
 
     if  (fuse_set_signal_handlers(se) != 0)
         goto  err_out2;
 
     if  (fuse_session_mount(se, opts.mountpoint) != 0)
         goto  err_out3;
 
     fuse_daemonize(opts.foreground);
 
     /* Block until ctrl+c or fusermount -u */
     if  (opts.singlethread)
         ret = fuse_session_loop(se);
     else
         ret = fuse_session_loop_mt(se, opts.clone_fd);
 
     fuse_session_unmount(se);
err_out3:
     fuse_remove_signal_handlers(se);
err_out2:
     fuse_session_destroy(se);
err_out1:
     free (opts.mountpoint);
     fuse_opt_free_args(&args);
 
     return  ret ? 1 : 0;
}


3、fuse的根节点号是1,在源码中是以常量的方式定义的,本来想改成变量,估计工作量和稳定性会有麻烦,变通处理吧。


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
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
/*
   FUSE: Filesystem in Userspace
   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
 
   This program can be distributed under the terms of the GNU GPL.
   See the file COPYING.
*/
 
/** @file
  *
  * minimal example filesystem using low-level API
  *
  * Compile with:
  *
  *     gcc -Wall hello_ll.c `pkg-config fuse3 --cflags --libs` -o hello_ll
  *
  * ## Source code ##
  * \include hello_ll.c
  */
 
/*change2:
  定义一个idx文件,格式为typedef struct Str_idx {
  int low_off;
  short hige_off;
  short fileid;
  }str_idx;
 
  按每8字节一个索引的方式组合某个镜像文件,文件ID为0表示自由,用字符'F'填充。
  文件ID为1,表示使用当前目录下的$MFT文件做为数据源进行加工。
  测试代码,至少还需补全:
  1、配置文件(文件大小、源文件路径等)的参数指定
  2、idx结构的准确大小(int,short和机器位长有关系,需修改为与机器位长无关的数据类型)
  3、排错:文件打不开
  4、排错:文件大小与idx不符
  5、排错:文件溢出处理
  6、修改缓冲机制,不能先把源文件全读到内存,需要建立缓冲机制,应对读函数,或者简单地用fopen加缓冲处理。
  by www.datahf.net 张宇
  */
#define FUSE_USE_VERSION 30
 
#include <config.h>
 
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
 
static  const  char  *hello_str =  "Hello World!\n" ;
static  const  char  *hello_name =  "hello" ;
//change1 #0
static  const  char  *inode5_str =  "http://www.datahf.net!\n" ;
static  const  char  *inode5_name =  "frombyte" ;
 
static  const  char  *inode3_name =  "frombyte.dir" ;
static  const  char  *inode6_name =  "inode6.img" ;
unsigned  char  *databuf;
#define INODE6_SIZE (1024*1024*4)
//change end
static  int  hello_stat(fuse_ino_t ino,  struct  stat *stbuf)
{
     stbuf->st_ino = ino;
     switch  (ino) {
     case  1:
         stbuf->st_mode = S_IFDIR | 0755;
         stbuf->st_nlink = 3;
         break ;
 
     case  2:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size =  strlen (hello_str);
         break ;
 
//change1  #1
     case  3:
         stbuf->st_mode = S_IFDIR | 0755;
         stbuf->st_nlink = 2;
         break ;
             
     case  5:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size =  strlen (inode5_str);
         break ;
             
     case  6:
         stbuf->st_mode = S_IFREG | 0444;
         stbuf->st_nlink = 1;
         stbuf->st_size = INODE6_SIZE;
         break ;
     
//change1 end
             
     default :
         return  -1;
     }
     return  0;
}
 
static  void  hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
                  struct  fuse_file_info *fi)
{
     struct  stat stbuf;
 
     ( void ) fi;
 
     memset (&stbuf, 0,  sizeof (stbuf));
     if  (hello_stat(ino, &stbuf) == -1)
         fuse_reply_err(req, ENOENT);
     else
         fuse_reply_attr(req, &stbuf, 1.0);
}
 
static  void  hello_ll_lookup(fuse_req_t req, fuse_ino_t parent,  const  char  *name)
{
     struct  fuse_entry_param e;
     //change1  #2
     /*
     if (parent != 1 || strcmp(name, hello_name) != 0)
         fuse_reply_err(req, ENOENT);
     else {
         memset(&e, 0, sizeof(e));
         e.ino = 2;
         e.attr_timeout = 1.0;
         e.entry_timeout = 1.0;
         hello_stat(e.ino, &e.attr);
         
         fuse_reply_entry(req, &e);
     }*/
 
     switch  (parent) {
         case  1:
             if ( strcmp (name, hello_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 2;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else  if ( strcmp (name, inode3_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 3;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else
                 fuse_reply_err(req, ENOENT);
             break ;
         case  3:
             if ( strcmp (name, inode5_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 5;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else  if ( strcmp (name, inode6_name) == 0){
                 memset (&e, 0,  sizeof (e));
                 e.ino = 6;
                 e.attr_timeout = 1.0;
                 e.entry_timeout = 1.0;
                 hello_stat(e.ino, &e.attr);
                 fuse_reply_entry(req, &e);
             }
             else
                 fuse_reply_err(req, ENOENT);
             break ;
             
         default :
             fuse_reply_err(req, ENOENT);
             break ;
     }
     //change1 end
}
 
struct  dirbuf {
     char  *p;
     size_t  size;
};
//change2
typedef  struct  Str_idx {
     int  low_off;
     short  hige_off;
     short  fileid;
}str_idx;
 
static  void  dirbuf_add(fuse_req_t req,  struct  dirbuf *b,  const  char  *name,
                fuse_ino_t ino)
{
     struct  stat stbuf;
     size_t  oldsize = b->size;
     b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
     b->p = ( char  *)  realloc (b->p, b->size);
     memset (&stbuf, 0,  sizeof (stbuf));
     stbuf.st_ino = ino;
     fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
               b->size);
}
 
#define min(x, y) ((x) < (y) ? (x) : (y))
 
static  int  reply_buf_limited(fuse_req_t req,  const  char  *buf,  size_t  bufsize,
                  off_t off,  size_t  maxsize)
{
     if  (off < bufsize)
         return  fuse_reply_buf(req, buf + off,
                       min(bufsize - off, maxsize));
     else
         return  fuse_reply_buf(req, NULL, 0);
}
 
static  void  hello_ll_readdir(fuse_req_t req, fuse_ino_t ino,  size_t  size,
                  off_t off,  struct  fuse_file_info *fi)
{
     ( void ) fi;
 
     /*
     if (ino != 1)
         fuse_reply_err(req, ENOTDIR);
     else {*/
     struct  dirbuf b;
     switch  (ino) {
         case  1:
             memset (&b, 0,  sizeof (b));
             dirbuf_add(req, &b,  "." , 1);
             dirbuf_add(req, &b,  ".." , 1);
             dirbuf_add(req, &b, hello_name, 2);
             //change1
             dirbuf_add(req, &b, inode3_name, 3);
             //end
             reply_buf_limited(req, b.p, b.size, off, size);
             free (b.p);
             break ;
         case  3:
             memset (&b, 0,  sizeof (b));
             dirbuf_add(req, &b,  "." , 3);
             dirbuf_add(req, &b,  ".." , 1);
             dirbuf_add(req, &b, inode5_name, 5);
             dirbuf_add(req, &b, inode6_name, 6);
             reply_buf_limited(req, b.p, b.size, off, size);
             free (b.p);
             break ;
         default :
             fuse_reply_err(req, ENOTDIR);
             break ;
     }
     //}
}
 
static  void  hello_ll_open(fuse_req_t req, fuse_ino_t ino,
               struct  fuse_file_info *fi)
{
     //change1
     //if (ino != 2)
     switch (ino)
     {
         case  2: case  5: case  6:
             if  ((fi->flags & 3) != O_RDONLY)
                 fuse_reply_err(req, EACCES);
             else
                 fuse_reply_open(req, fi);
             break ;
         default :
             fuse_reply_err(req, EISDIR);
             break ;
     }
}
 
static  void  hello_ll_read(fuse_req_t req, fuse_ino_t ino,  size_t  size,
               off_t off,  struct  fuse_file_info *fi)
{
     ( void ) fi;
 
     //change1
     //assert(ino == 2);
     //reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
     switch  (ino) {
         case  2:
             reply_buf_limited(req, hello_str,  strlen (hello_str), off, size);
             break ;
         case  5:
             reply_buf_limited(req, inode5_str,  strlen (inode5_str), off, size);
             break ;
         case  6:
             fuse_reply_buf(req, databuf + off,size);
         default :
             ;
     }
     //end
}
 
static  struct  fuse_lowlevel_ops hello_ll_oper = {
     .lookup     = hello_ll_lookup,
     .getattr    = hello_ll_getattr,
     .readdir    = hello_ll_readdir,
     .open       = hello_ll_open,
     .read       = hello_ll_read,
};
 
 
int  main( int  argc,  char  *argv[])
{
     struct  fuse_args args = FUSE_ARGS_INIT(argc, argv);
     struct  fuse_session *se;
     struct  fuse_cmdline_opts opts;
     int  ret = -1;
 
     if  (fuse_parse_cmdline(&args, &opts) != 0)
         return  1;
     if  (opts.show_help) {
         printf ( "usage: %s [options] <mountpoint>\n\n" , argv[0]);
         fuse_cmdline_help();
         fuse_lowlevel_help();
         ret = 0;
         goto  err_out1;
     else  if  (opts.show_version) {
         printf ( "FUSE library version %s\n" , fuse_pkgversion());
         fuse_lowlevel_version();
         ret = 0;
         goto  err_out1;
     }
 
     //change2
     FILE  *f_idx,*f_img;
     f_idx =  fopen ( "./1.idx" , "rb" );
     f_img =  fopen ( "./$MFT" , "rb" );
     if  ( (f_idx == -1) || (f_img == -1) )
     {
         printf ( "f_idx is %d\nf_img is %d\n" ,f_idx,f_img);
         goto  err_out1;
     }
     size_t  s_idx,s_img;
     s_idx = 512;
     unsigned  char  idx_buf[512];
     databuf = NULL;
     databuf =  ( char  *) malloc (INODE6_SIZE);
     fread (idx_buf,512,1,f_idx);
 
     int  i=0;
     struct  Str_idx * tidx;
     tidx = ( struct  Str_idx*) idx_buf;
     for (i=0;i<4;i++)
     {
         if (tidx[i].fileid == 0)
             memset (databuf+ i * 1024 * 1024, 'F' ,1024*1024);
         else  if (tidx[i].fileid == 1)
         {
             fseek (f_img,
                  tidx[i].low_off * 1024 * 1024 + tidx[i].hige_off * 1024 * 1024,
                  SEEK_SET);
             fread (databuf + i * 1024 * 1024,1024*1024,1,f_img);
         }
         else
             ;
         printf ( "#i:%d,%d,%d\n" ,tidx[i].low_off,tidx[i].hige_off,tidx[i].fileid);
     }
     
     se = fuse_session_new(&args, &hello_ll_oper,
                   sizeof (hello_ll_oper), NULL);
     if  (se == NULL)
         goto  err_out1;
 
     if  (fuse_set_signal_handlers(se) != 0)
         goto  err_out2;
 
     if  (fuse_session_mount(se, opts.mountpoint) != 0)
         goto  err_out3;
 
     fuse_daemonize(opts.foreground);
 
     /* Block until ctrl+c or fusermount -u */
     if  (opts.singlethread)
         ret = fuse_session_loop(se);
     else
         ret = fuse_session_loop_mt(se, opts.clone_fd);
 
     fuse_session_unmount(se);
err_out3:
     fuse_remove_signal_handlers(se);
err_out2:
     fuse_session_destroy(se);
err_out1:
     free (opts.mountpoint);
     fuse_opt_free_args(&args);
 
     return  ret ? 1 : 0;
}

待续










本文转自 张宇 51CTO博客,原文链接:http://blog.51cto.com/zhangyu/1881032,如需转载请自行联系原作者
目录
相关文章
|
12月前
|
人工智能 Ubuntu 前端开发
Dify部署全栈指南:AI从Ubuntu配置到HTTPS自动化的10倍秘籍
本文档介绍如何部署Dify后端服务及前端界面,涵盖系统环境要求、依赖安装、代码拉取、环境变量配置、服务启动、数据库管理及常见问题解决方案,适用于开发与生产环境部署。
1935 1
|
Ubuntu Linux 网络安全
Linux服务器之Ubuntu的安装与配置
Ubuntu Desktop是目前最成功、最流行的图形界面的Linux发行版;而Ubuntu Server也在服务器端市场占据了较大的份额。今天为大家详细介绍了Ubuntu Server的安装与配置,希望对你能有所帮助。关于VMware、VirtualBox等虚拟化软件的使用,朱哥还会在后续的文章中为大家详细介绍,敬请关注!
|
12月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
593 18
|
12月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
978 17
|
12月前
|
Ubuntu 安全 关系型数据库
安装与配置MySQL 8 on Ubuntu,包括权限授予、数据库备份及远程连接指南
以上步骤提供了在Ubuntu上从头开始设置、配置、授权、备份及恢复一个基础但完整的MySQL环境所需知识点。
1270 7
|
缓存 Ubuntu Docker
Ubuntu环境下删除Docker镜像与容器、配置静态IP地址教程。
如果遇见问题或者想回滚改动, 可以重启系统.
739 16
|
12月前
|
Java 测试技术 数据安全/隐私保护
通过yaml文件配置自动化测试程序
通过yaml文件可以将自动化测试环境,测试数据和测试行为分开,请看一下案例
456 4
|
缓存 监控 Ubuntu
在Ubuntu 16.04上配置GitLab Runner以激活GitLab CI/CD流程
完成以上步骤后,每当代码被推送到远端仓库中相对应分支上时,GitLb CI / CD 流水线就会自动触发,并由之前注册好了 GitLb runner 的机器去完成定义好了 ` .gitlabcicd.yml ` 文件里面定义好各种任务(如编译测试部署等).
559 13
|
XML Ubuntu Java
如何在Ubuntu系统上安装和配置JMeter和Ant进行性能测试
进入包含 build.xml 的目录并执行:
600 13
|
消息中间件 人工智能 运维
Ubuntu环境下的 RabbitMQ 安装与配置详细教程
本文聚焦在Ubuntu下RabbitMQ安装与配置教程,旨在帮助读者快速构建稳定可用的消息队列服务。
1750 6