The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / src / file.cc @ 63a9cd38

History | View | Annotate | Download (3.91 KB)

1
#include "node.h"
2
#include <string.h>
3

    
4
using namespace v8;
5

    
6
class Callback {
7
  public:
8
    Callback(Handle<Value> v);
9
    ~Callback();
10
    Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
11
  private:
12
    Persistent<Function> handle;
13
};
14

    
15

    
16
Callback::Callback (Handle<Value> v)
17
{
18
  HandleScope scope;
19
  Handle<Function> f = Handle<Function>::Cast(v);
20
  handle = Persistent<Function>::New(f);
21
}
22

    
23
Callback::~Callback ()
24
{
25
  handle.Dispose();
26
  handle.Clear(); // necessary? 
27
}
28

    
29
Local<Value>
30
Callback::Call (Handle<Object> recv, int argc, Handle<Value> argv[])
31
{
32
  HandleScope scope;
33
  Local<Value> r = handle->Call(recv, argc, argv);
34
  return scope.Close(r);
35
}
36

    
37
static int
38
after_rename (eio_req *req)
39
{
40
  Callback *callback = static_cast<Callback*>(req->data);
41
  if (callback != NULL) {
42
    HandleScope scope;
43
    const int argc = 2;
44
    Local<Value> argv[argc];
45

    
46
    argv[0] = Integer::New(req->errorno);
47
    argv[1] = String::New(strerror(req->errorno));
48
    
49
    callback->Call(Context::GetCurrent()->Global(), argc, argv);
50
    delete callback;
51
  }
52
  return 0;
53
}
54

    
55
JS_METHOD(rename) 
56
{
57
  if (args.Length() < 2)
58
    return Undefined();
59

    
60
  HandleScope scope;
61

    
62
  String::Utf8Value path(args[0]->ToString());
63
  String::Utf8Value new_path(args[1]->ToString());
64

    
65
  Callback *callback = NULL;
66
  if (!args[2]->IsUndefined()) callback = new Callback(args[2]);
67

    
68
  eio_req *req = eio_rename(*path, *new_path, EIO_PRI_DEFAULT, after_rename, callback);
69
  node_eio_submit(req);
70

    
71
  return Undefined();
72
}
73

    
74
static int
75
after_stat (eio_req *req)
76
{
77
  Callback *callback = static_cast<Callback*>(req->data);
78
  if (callback != NULL) {
79
    HandleScope scope;
80
    const int argc = 3;
81
    Local<Value> argv[argc];
82

    
83
    Local<Object> stats = Object::New();
84
    argv[0] = stats;
85
    argv[1] = Integer::New(req->errorno);
86
    argv[2] = String::New(strerror(req->errorno));
87

    
88
    if (req->result == 0) {
89
      struct stat *s = static_cast<struct stat*>(req->ptr2);
90

    
91
      /* ID of device containing file */
92
      stats->Set(JS_SYMBOL("dev"), Integer::New(s->st_dev));
93
      /* inode number */
94
      stats->Set(JS_SYMBOL("ino"), Integer::New(s->st_ino));
95
      /* protection */
96
      stats->Set(JS_SYMBOL("mode"), Integer::New(s->st_mode));
97
      /* number of hard links */
98
      stats->Set(JS_SYMBOL("nlink"), Integer::New(s->st_nlink));
99
      /* user ID of owner */
100
      stats->Set(JS_SYMBOL("uid"), Integer::New(s->st_uid));
101
      /* group ID of owner */
102
      stats->Set(JS_SYMBOL("gid"), Integer::New(s->st_gid));
103
      /* device ID (if special file) */
104
      stats->Set(JS_SYMBOL("rdev"), Integer::New(s->st_rdev));
105
      /* total size, in bytes */
106
      stats->Set(JS_SYMBOL("size"), Integer::New(s->st_size));
107
      /* blocksize for filesystem I/O */
108
      stats->Set(JS_SYMBOL("blksize"), Integer::New(s->st_blksize));
109
      /* number of blocks allocated */
110
      stats->Set(JS_SYMBOL("blocks"), Integer::New(s->st_blocks));
111
      /* time of last access */
112
      stats->Set(JS_SYMBOL("atime"), Date::New(1000*static_cast<double>(s->st_atime)));
113
      /* time of last modification */
114
      stats->Set(JS_SYMBOL("mtime"), Date::New(1000*static_cast<double>(s->st_mtime)));
115
      /* time of last status change */
116
      stats->Set(JS_SYMBOL("ctime"), Date::New(1000*static_cast<double>(s->st_ctime)));
117
    }
118
    
119
    callback->Call(Context::GetCurrent()->Global(), argc, argv);
120
    delete callback;
121
  }
122
  return 0;
123
}
124

    
125
JS_METHOD(stat) 
126
{
127
  if (args.Length() < 1)
128
    return v8::Undefined();
129

    
130
  HandleScope scope;
131

    
132
  String::Utf8Value path(args[0]->ToString());
133

    
134
  Callback *callback = NULL;
135
  if (!args[1]->IsUndefined()) callback = new Callback(args[1]);
136

    
137
  eio_req *req = eio_stat(*path, EIO_PRI_DEFAULT, after_stat, callback);
138
  node_eio_submit(req);
139

    
140
  return Undefined();
141
}
142

    
143
void
144
NodeInit_file (Handle<Object> target)
145
{
146
  HandleScope scope;
147

    
148
  Local<Object> fs = Object::New();
149
  target->Set(String::NewSymbol("fs"), fs);
150
  
151
  JS_SET_METHOD(fs, "rename", rename);
152
  JS_SET_METHOD(fs, "stat", stat);
153
}