Skip to content

Commit dd50c1d

Browse files
author
Daniel Kang
committed
adding fs.h; fixed missing compiler preprocessors
1 parent 2fbd860 commit dd50c1d

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
CXXFLAGS = -std=gnu++0x -g -O0 -I$(LIBUV_PATH)/include -I$(HTTP_PARSER_PATH) -I.
1+
CXXFLAGS = -std=gnu++0x -g -O0 -I$(LIBUV_PATH)/include -I$(HTTP_PARSER_PATH) -I. -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
22

33
all: webclient webserver
44

55
webclient: webclient.cpp $(LIBUV_PATH)/uv.a $(HTTP_PARSER_PATH)/http_parser.o $(wildcard native/*.h)
6-
$(CXX) $(CXXFLAGS) -o webclient webclient.cpp $(LIBUV_PATH)/uv.a $(HTTP_PARSER_PATH)/http_parser.o -lrt -lm -lpthread
6+
$(CXX) $(CXXFLAGS) -o webclient webclient.cpp $(LIBUV_PATH)/uv.a $(HTTP_PARSER_PATH)/http_parser.o -lrt -lm -lpthread
77

88
webserver: webserver.cpp $(LIBUV_PATH)/uv.a $(HTTP_PARSER_PATH)/http_parser.o $(wildcard native/*.h)
99
$(CXX) $(CXXFLAGS) -o webserver webserver.cpp $(LIBUV_PATH)/uv.a $(HTTP_PARSER_PATH)/http_parser.o -lrt -lm -lpthread

native/fs.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#ifndef __FS_H__
2+
#define __FS_H__
3+
4+
#include "base.h"
5+
#include "callback.h"
6+
7+
namespace native
8+
{
9+
typedef uv_file file;
10+
11+
// TODO: implement functions that accept loop pointer as extra argument.
12+
namespace fs
13+
{
14+
template<typename callback_t>
15+
bool open(const char* path, int flags, int mode, callback_t callback)
16+
{
17+
auto req = new uv_fs_t;
18+
req->data = new callbacks(1);
19+
callbacks::store(req->data, 0, callback);
20+
return uv_fs_open(uv_default_loop(), req, path, flags, mode, [](uv_fs_t* req){
21+
assert(req->fs_type == UV_FS_OPEN);
22+
23+
if(req->errorno || req->result < 0)
24+
{
25+
// TODO: handle error
26+
}
27+
else
28+
{
29+
callbacks::invoke<callback_t>(req->data, 0, req->result);
30+
}
31+
32+
delete reinterpret_cast<callbacks*>(req->data);
33+
uv_fs_req_cleanup(req);
34+
delete req;
35+
})==0;
36+
}
37+
38+
template<typename callback_t>
39+
bool read(file f, void* buf, size_t len, off_t offset, callback_t callback)
40+
{
41+
auto req = new uv_fs_t;
42+
req->data = new callbacks(1);
43+
callbacks::store(req->data, 0, callback);
44+
return uv_fs_read(uv_default_loop(), req, f, buf, len, offset, [](uv_fs_t* req){
45+
assert(req->fs_type == UV_FS_READ);
46+
47+
if(req->errorno)
48+
{
49+
// TODO: handle error
50+
}
51+
else
52+
{
53+
callbacks::invoke<callback_t>(req->data, 0, req);
54+
}
55+
56+
delete reinterpret_cast<callbacks*>(req->data);
57+
uv_fs_req_cleanup(req);
58+
delete req;
59+
})==0;
60+
}
61+
62+
template<typename callback_t>
63+
bool mkdir(const char* path, int mode, callback_t callback)
64+
{
65+
auto req = new uv_fs_t;
66+
req->data = new callbacks(1);
67+
callbacks::store(req->data, 0, callback);
68+
return uv_fs_mkdir(uv_default_loop(), req, path, mode, [](uv_fs_t* req){
69+
assert(req->fs_type == UV_FS_MKDIR);
70+
71+
if(req->errorno)
72+
{
73+
// TODO: handle error
74+
}
75+
else
76+
{
77+
callbacks::invoke<callback_t>(req->data, 0, req->result);
78+
}
79+
80+
delete reinterpret_cast<callbacks*>(req->data);
81+
uv_fs_req_cleanup(req);
82+
delete req;
83+
})==0;
84+
}
85+
}
86+
}
87+
88+
#endif

native/native.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
#include "error.h"
77
#include "tcp.h"
88
#include "http.h"
9+
#include "fs.h"
910

1011
#endif

webclient.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
#include <native/native.h>
55
using namespace native;
66

7+
#include <fcntl.h>
8+
79
int main() {
10+
fs::open("Makefile", O_RDONLY, 0, [](file f){
11+
#if 0
12+
char buf[10000];
13+
fs::read(f, buf, 10000, 0, [](uv_fs_t* req){
14+
printf("test\n");
15+
});
16+
#endif
17+
});
18+
19+
return run();
20+
821
auto client = net::tcp::create();
922
client->connect("127.0.0.1", 8080, [=](error e){
1023
client->write("GET / HTTP/1.1\r\n\r\n", [=](error e){

0 commit comments

Comments
 (0)