forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_file.h
More file actions
127 lines (103 loc) · 3.87 KB
/
node_file.h
File metadata and controls
127 lines (103 loc) · 3.87 KB
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
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#ifndef SRC_FILE_H_
#define SRC_FILE_H_
#include <node.h>
#include <node_events.h>
#include <v8.h>
namespace node {
/* Are you missing your favorite POSIX function? It might be very easy to
* add a wrapper. Take a look in deps/libeio/eio.h at the list of wrapper
* functions. If your function is in that list, just follow the lead of
* EIOPromise::Open. You'll need to add two functions, one static function
* in EIOPromise, and one static function which interprets the javascript
* args in src/file.cc. Then just a reference to that function in
* File::Initialize() and you should be good to go.
* Don't forget to add a test to test/mjsunit.
*/
#define NODE_V8_METHOD_DECLARE(name) \
static v8::Handle<v8::Value> name(const v8::Arguments& args)
class File {
public:
static void Initialize(v8::Handle<v8::Object> target);
};
class EIOPromise : public Promise {
public:
static EIOPromise* Create();
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Object> Open(const char *path, int flags, mode_t mode) {
EIOPromise *p = Create();
p->req_ = eio_open(path, flags, mode, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> Close(int fd) {
EIOPromise *p = Create();
p->req_ = eio_close(fd, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> Write(int fd,
void *buf,
size_t count,
off_t offset) {
EIOPromise *p = Create();
p->req_ = eio_write(fd, buf, count, offset, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> Read(int fd,
size_t count,
off_t offset,
enum encoding encoding) {
EIOPromise *p = Create();
p->encoding_ = encoding;
// NOTE: 2nd param: NULL pointer tells eio to allocate it itself
p->req_ = eio_read(fd, NULL, count, offset, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> Stat(const char *path) {
EIOPromise *p = Create();
p->req_ = eio_stat(path, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> Rename(const char *path, const char *new_path) {
EIOPromise *p = Create();
p->req_ = eio_rename(path, new_path, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> Unlink(const char *path) {
EIOPromise *p = Create();
p->req_ = eio_unlink(path, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> RMDir(const char *path) {
EIOPromise *p = Create();
p->req_ = eio_rmdir(path, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> MKDir(const char *path, mode_t mode) {
EIOPromise *p = Create();
p->req_ = eio_mkdir(path, mode, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> ReadDir(const char *path) {
EIOPromise *p = Create();
// By using EIO_READDIR_DENTS (or other flags), more complex results can
// be had from eio_readdir. Doesn't seem that we need that though.
p->req_ = eio_readdir(path, 0, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
static v8::Handle<v8::Object> SendFile(int out_fd, int in_fd, off_t in_offset, size_t length) {
EIOPromise *p = Create();
p->req_ = eio_sendfile(out_fd, in_fd, in_offset, length, EIO_PRI_DEFAULT, After, p);
return p->handle_;
}
protected:
void Attach();
void Detach();
EIOPromise() : Promise() { }
private:
static int After(eio_req *req);
eio_req *req_;
enum encoding encoding_;
};
} // namespace node
#endif // SRC_FILE_H_