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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libfuse: example/hello.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">libfuse
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="dir_cfafba98a580ce4b62f8a6fa96d7cbb0.html">example</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">hello.c File Reference</div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include <<a class="el" href="fuse_8h_source.html">fuse.h</a>></code><br />
<code>#include <stdio.h></code><br />
<code>#include <string.h></code><br />
<code>#include <errno.h></code><br />
<code>#include <fcntl.h></code><br />
<code>#include <stddef.h></code><br />
<code>#include <assert.h></code><br />
</div>
<p><a href="hello_8c_source.html">Go to the source code of this file.</a></p>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>minimal example filesystem using high-level API</p>
<p>Compile with: </p><pre class="fragment">gcc -Wall hello.c `pkg-config fuse3 --cflags --libs` -o hello
</pre><h2>Source code</h2>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> FUSE: Filesystem in Userspace</span></div><div class="line"><span class="comment"> Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu></span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"> This program can be distributed under the terms of the GNU GPL.</span></div><div class="line"><span class="comment"> See the file COPYING.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define FUSE_USE_VERSION 31</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <<a class="code" href="fuse_8h.html">fuse.h</a>></span></div><div class="line"><span class="preprocessor">#include <stdio.h></span></div><div class="line"><span class="preprocessor">#include <string.h></span></div><div class="line"><span class="preprocessor">#include <errno.h></span></div><div class="line"><span class="preprocessor">#include <fcntl.h></span></div><div class="line"><span class="preprocessor">#include <stddef.h></span></div><div class="line"><span class="preprocessor">#include <assert.h></span></div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Command line options</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * We can't set default values for the char* fields here because</span></div><div class="line"><span class="comment"> * fuse_opt_parse would attempt to free() them when the user specifies</span></div><div class="line"><span class="comment"> * different values on the command line.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span>options {</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span> *filename;</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span> *contents;</div><div class="line"> <span class="keywordtype">int</span> show_help;</div><div class="line">} options;</div><div class="line"></div><div class="line"><span class="preprocessor">#define OPTION(t, p) \</span></div><div class="line"><span class="preprocessor"> { t, offsetof(struct options, p), 1 }</span></div><div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">struct </span><a class="code" href="structfuse__opt.html">fuse_opt</a> option_spec[] = {</div><div class="line"> OPTION(<span class="stringliteral">"--name=%s"</span>, filename),</div><div class="line"> OPTION(<span class="stringliteral">"--contents=%s"</span>, contents),</div><div class="line"> OPTION(<span class="stringliteral">"-h"</span>, show_help),</div><div class="line"> OPTION(<span class="stringliteral">"--help"</span>, show_help),</div><div class="line"> <a class="code" href="fuse__opt_8h.html#aca35962e17d189ceb5447f8eea11bd33">FUSE_OPT_END</a></div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> *hello_init(<span class="keyword">struct</span> <a class="code" href="structfuse__conn__info.html">fuse_conn_info</a> *conn,</div><div class="line"> <span class="keyword">struct</span> <a class="code" href="structfuse__config.html">fuse_config</a> *cfg)</div><div class="line">{</div><div class="line"> (void) conn;</div><div class="line"> cfg-><a class="code" href="structfuse__config.html#ac055a1643c6c7aa7fb19341cf7f39aad">kernel_cache</a> = 1;</div><div class="line"> <span class="keywordflow">return</span> NULL;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> hello_getattr(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> stat *stbuf,</div><div class="line"> <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line"> (void) fi;</div><div class="line"> <span class="keywordtype">int</span> res = 0;</div><div class="line"></div><div class="line"> memset(stbuf, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> stat));</div><div class="line"> <span class="keywordflow">if</span> (strcmp(path, <span class="stringliteral">"/"</span>) == 0) {</div><div class="line"> stbuf->st_mode = S_IFDIR | 0755;</div><div class="line"> stbuf->st_nlink = 2;</div><div class="line"> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(path+1, options.filename) == 0) {</div><div class="line"> stbuf->st_mode = S_IFREG | 0444;</div><div class="line"> stbuf->st_nlink = 1;</div><div class="line"> stbuf->st_size = strlen(options.contents);</div><div class="line"> } <span class="keywordflow">else</span></div><div class="line"> res = -ENOENT;</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> res;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> hello_readdir(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">void</span> *buf, <a class="code" href="fuse_8h.html#a7dd132de66a5cc2add2a4eff5d435660">fuse_fill_dir_t</a> filler,</div><div class="line"> off_t offset, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi,</div><div class="line"> <span class="keyword">enum</span> <a class="code" href="fuse_8h.html#af2bcf2a473b41b3cc8da8c079656a074">fuse_readdir_flags</a> flags)</div><div class="line">{</div><div class="line"> (void) offset;</div><div class="line"> (void) fi;</div><div class="line"> (void) flags;</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (strcmp(path, <span class="stringliteral">"/"</span>) != 0)</div><div class="line"> <span class="keywordflow">return</span> -ENOENT;</div><div class="line"></div><div class="line"> filler(buf, <span class="stringliteral">"."</span>, NULL, 0, 0);</div><div class="line"> filler(buf, <span class="stringliteral">".."</span>, NULL, 0, 0);</div><div class="line"> filler(buf, options.filename, NULL, 0, 0);</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> hello_open(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> (strcmp(path+1, options.filename) != 0)</div><div class="line"> <span class="keywordflow">return</span> -ENOENT;</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> ((fi-><a class="code" href="structfuse__file__info.html#ae7d31802727be19670193a411647bca5">flags</a> & O_ACCMODE) != O_RDONLY)</div><div class="line"> <span class="keywordflow">return</span> -EACCES;</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> hello_read(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> size, off_t offset,</div><div class="line"> <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line"> <span class="keywordtype">size_t</span> len;</div><div class="line"> (void) fi;</div><div class="line"> <span class="keywordflow">if</span>(strcmp(path+1, options.filename) != 0)</div><div class="line"> <span class="keywordflow">return</span> -ENOENT;</div><div class="line"></div><div class="line"> len = strlen(options.contents);</div><div class="line"> <span class="keywordflow">if</span> (offset < len) {</div><div class="line"> <span class="keywordflow">if</span> (offset + size > len)</div><div class="line"> size = len - <a class="code" href="structfuse__opt.html#a333dcd849a3bd54df06084a69b5a8d39">offset</a>;</div><div class="line"> memcpy(buf, options.contents + offset, size);</div><div class="line"> } <span class="keywordflow">else</span></div><div class="line"> size = 0;</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> size;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structfuse__operations.html">fuse_operations</a> hello_oper = {</div><div class="line"> .<a class="code" href="structfuse__operations.html#a0ad1f7c4105ee062528c767da88060f0">init</a> = hello_init,</div><div class="line"> .getattr = hello_getattr,</div><div class="line"> .readdir = hello_readdir,</div><div class="line"> .open = hello_open,</div><div class="line"> .read = hello_read,</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> show_help(<span class="keyword">const</span> <span class="keywordtype">char</span> *progname)</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">"usage: %s [options] <mountpoint>\n\n"</span>, progname);</div><div class="line"> printf(<span class="stringliteral">"File-system specific options:\n"</span></div><div class="line"> <span class="stringliteral">" --name=<s> Name of the \"hello\" file\n"</span></div><div class="line"> <span class="stringliteral">" (default: \"hello\")\n"</span></div><div class="line"> <span class="stringliteral">" --contents=<s> Contents \"hello\" file\n"</span></div><div class="line"> <span class="stringliteral">" (default \"Hello, World!\\n\")\n"</span></div><div class="line"> <span class="stringliteral">"\n"</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line"> <span class="keywordtype">int</span> ret;</div><div class="line"> <span class="keyword">struct </span><a class="code" href="structfuse__args.html">fuse_args</a> args = <a class="code" href="fuse__opt_8h.html#a9bea40fe56b18be9aa110185ab7387ed">FUSE_ARGS_INIT</a>(argc, argv);</div><div class="line"></div><div class="line"> <span class="comment">/* Set defaults -- we have to use strdup so that</span></div><div class="line"><span class="comment"> fuse_opt_parse can free the defaults if other</span></div><div class="line"><span class="comment"> values are specified */</span></div><div class="line"> options.filename = strdup(<span class="stringliteral">"hello"</span>);</div><div class="line"> options.contents = strdup(<span class="stringliteral">"Hello World!\n"</span>);</div><div class="line"></div><div class="line"> <span class="comment">/* Parse options */</span></div><div class="line"> <span class="keywordflow">if</span> (<a class="code" href="fuse__opt_8h.html#a539ef1f571c34f516c60c4cbe2901c0e">fuse_opt_parse</a>(&args, &options, option_spec, NULL) == -1)</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"></div><div class="line"> <span class="comment">/* When --help is specified, first print our own file-system</span></div><div class="line"><span class="comment"> specific help text, then signal fuse_main to show</span></div><div class="line"><span class="comment"> additional help (by adding `--help` to the options again)</span></div><div class="line"><span class="comment"> without usage: line (by setting argv[0] to the empty</span></div><div class="line"><span class="comment"> string) */</span></div><div class="line"> <span class="keywordflow">if</span> (options.show_help) {</div><div class="line"> show_help(argv[0]);</div><div class="line"> assert(<a class="code" href="fuse__opt_8h.html#a21602e6a0fff64fc9c7b642d12382094">fuse_opt_add_arg</a>(&args, <span class="stringliteral">"--help"</span>) == 0);</div><div class="line"> args.<a class="code" href="structfuse__args.html#a6f90bec2356aff03de79baaca65276b6">argv</a>[0][0] = <span class="charliteral">'\0'</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> ret = <a class="code" href="fuse_8h.html#ac99b844cee7aaa8fb4e35df5b5488d82">fuse_main</a>(args.<a class="code" href="structfuse__args.html#a501883d1eababa08e05b0c802b31e11d">argc</a>, args.<a class="code" href="structfuse__args.html#a6f90bec2356aff03de79baaca65276b6">argv</a>, &hello_oper, NULL);</div><div class="line"> <a class="code" href="fuse__opt_8h.html#a2cdf272429ab3869a5162976141b287d">fuse_opt_free_args</a>(&args);</div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line">}</div></div><!-- fragment -->
<p class="definition">Definition in file <a class="el" href="hello_8c_source.html">hello.c</a>.</p>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
|