forked from liexusong/php-beast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpfile_file_handler.c
90 lines (69 loc) · 1.45 KB
/
tmpfile_file_handler.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
#include <stdlib.h>
#include <stdio.h>
#include "file_handler.h"
struct tmpfile_handler_ctx {
FILE *fp;
};
int tmpfile_handler_check()
{
return 0;
}
int tmpfile_handler_open(struct file_handler *self)
{
struct tmpfile_handler_ctx *ctx = self->ctx;
ctx->fp = tmpfile();
if (!ctx->fp) {
return -1;
}
return 0;
}
int tmpfile_handler_write(struct file_handler *self, char *buf, int size)
{
struct tmpfile_handler_ctx *ctx = self->ctx;
if (fwrite(buf, 1, size, ctx->fp) == size) {
return 0;
}
return -1;
}
int tmpfile_handler_rewind(struct file_handler *self)
{
struct tmpfile_handler_ctx *ctx = self->ctx;
rewind(ctx->fp);
return 0;
}
FILE *tmpfile_handler_get_fp(struct file_handler *self)
{
struct tmpfile_handler_ctx *ctx = self->ctx;
FILE *retval;
retval = ctx->fp;
ctx->fp = NULL;
return retval;
}
int tmpfile_handler_get_fd(struct file_handler *self)
{
return -1;
}
int tmpfile_handler_destroy(struct file_handler *self)
{
struct tmpfile_handler_ctx *ctx = self->ctx;
if (ctx->fp) {
fclose(ctx->fp);
}
ctx->fp = NULL;
return 0;
}
static struct tmpfile_handler_ctx _ctx = {
NULL
};
struct file_handler tmpfile_handler = {
"tmpfile",
BEAST_FILE_HANDLER_FP,
&_ctx,
tmpfile_handler_check,
tmpfile_handler_open,
tmpfile_handler_write,
tmpfile_handler_rewind,
tmpfile_handler_get_fd,
tmpfile_handler_get_fp,
tmpfile_handler_destroy
};