-
Notifications
You must be signed in to change notification settings - Fork 2
/
collfs_fopen.c
102 lines (76 loc) · 1.82 KB
/
collfs_fopen.c
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
/*
This file implements custom C string streams for read-only collective file system access. For more information about custom C strings, see:
http://www.gnu.org/software/libc/manual/html_node/Custom-Streams.html#Custom-Streams
*/
#define _GNU_SOURCE // needed to expose cookie_io_functions_t
#include <errno.h>
#include <libio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include "collfs-private.h"
typedef struct fcollfs_cookie_struct fcollfs_cookie_t;
struct fcollfs_cookie_struct
{
int fd;
};
static ssize_t
fcollfs_read (void *cookie, char *b, size_t s)
{
int fd;
fcollfs_cookie_t *c;
c = (fcollfs_cookie_t *) cookie;
fd = c->fd;
return __collfs_libc_read(fd, b, s);
}
static ssize_t
fcollfs_write (void *cookie __attribute__ ((unused)), const char *b __attribute__ ((unused)), size_t s __attribute__ ((unused)))
{
// this is an error!
return 0;
}
static int
fcollfs_seek (void *cookie, _IO_off64_t *p, int w)
{
int fd;
off_t new_p;
fcollfs_cookie_t *c;
c = (fcollfs_cookie_t *) cookie;
fd = c->fd;
new_p = __collfs_lseek(fd, *p, w);
if (new_p == -1) {
return -1;
}
*p = new_p;
return 0;
}
static int
fcollfs_close (void *cookie)
{
int fd;
fcollfs_cookie_t *c;
c = (fcollfs_cookie_t *) cookie;
fd = c->fd;
free (c);
return __collfs_close(fd);
}
FILE *
fcollfs_open (const char *pathname, const char *mode)
{
cookie_io_functions_t iof;
fcollfs_cookie_t *c;
if (mode[0] != 'r')
return NULL;
c = (fcollfs_cookie_t *) malloc (sizeof (fcollfs_cookie_t));
if (c == NULL)
return NULL;
c->fd = __collfs_open(pathname, O_RDONLY);
iof.read = fcollfs_read;
iof.write = fcollfs_write;
iof.seek = fcollfs_seek;
iof.close = fcollfs_close;
return fopencookie (c, mode, iof);
}