~james-w/+junk/fuse-debian-upstream

« back to all changes in this revision

Viewing changes to example/hello.c

  • Committer: James Westby
  • Date: 2008-05-16 12:57:40 UTC
  • Revision ID: jw+debian@jameswestby.net-20080516125740-fn2iqsxtfd3olmib
Tags: upstream-debian-2.2.1
Import upstream from fuse_2.2.1.orig.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    FUSE: Filesystem in Userspace
 
3
    Copyright (C) 2001-2005  Miklos Szeredi <miklos@szeredi.hu>
 
4
 
 
5
    This program can be distributed under the terms of the GNU GPL.
 
6
    See the file COPYING.
 
7
*/
 
8
 
 
9
#include <fuse.h>
 
10
#include <stdio.h>
 
11
#include <string.h>
 
12
#include <errno.h>
 
13
#include <fcntl.h>
 
14
 
 
15
static const char *hello_str = "Hello World!\n";
 
16
static const char *hello_path = "/hello";
 
17
 
 
18
static int hello_getattr(const char *path, struct stat *stbuf)
 
19
{
 
20
    int res = 0;
 
21
 
 
22
    memset(stbuf, 0, sizeof(struct stat));
 
23
    if(strcmp(path, "/") == 0) {
 
24
        stbuf->st_mode = S_IFDIR | 0755;
 
25
        stbuf->st_nlink = 2;
 
26
    }
 
27
    else if(strcmp(path, hello_path) == 0) {
 
28
        stbuf->st_mode = S_IFREG | 0444;
 
29
        stbuf->st_nlink = 1;
 
30
        stbuf->st_size = strlen(hello_str);
 
31
    }
 
32
    else
 
33
        res = -ENOENT;
 
34
 
 
35
    return res;
 
36
}
 
37
 
 
38
static int hello_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
 
39
{
 
40
    if(strcmp(path, "/") != 0)
 
41
        return -ENOENT;
 
42
 
 
43
    filler(h, ".", 0, 0);
 
44
    filler(h, "..", 0, 0);
 
45
    filler(h, hello_path + 1, 0, 0);
 
46
 
 
47
    return 0;
 
48
}
 
49
 
 
50
static int hello_open(const char *path, struct fuse_file_info *fi)
 
51
{
 
52
    if(strcmp(path, hello_path) != 0)
 
53
        return -ENOENT;
 
54
 
 
55
    if((fi->flags & 3) != O_RDONLY)
 
56
        return -EACCES;
 
57
 
 
58
    return 0;
 
59
}
 
60
 
 
61
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
 
62
                      struct fuse_file_info *fi)
 
63
{
 
64
    size_t len;
 
65
    (void) fi;
 
66
    if(strcmp(path, hello_path) != 0)
 
67
        return -ENOENT;
 
68
 
 
69
    len = strlen(hello_str);
 
70
    if (offset < len) {
 
71
        if (offset + size > len)
 
72
            size = len - offset;
 
73
        memcpy(buf, hello_str + offset, size);
 
74
    } else
 
75
        size = 0;
 
76
 
 
77
    return size;
 
78
}
 
79
 
 
80
static struct fuse_operations hello_oper = {
 
81
    .getattr    = hello_getattr,
 
82
    .getdir     = hello_getdir,
 
83
    .open       = hello_open,
 
84
    .read       = hello_read,
 
85
};
 
86
 
 
87
int main(int argc, char *argv[])
 
88
{
 
89
    return fuse_main(argc, argv, &hello_oper);
 
90
}