/
movefile.cc
99 lines (86 loc) · 2.16 KB
/
movefile.cc
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
#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <node.h>
#include <v8.h>
using namespace v8;
using namespace std;
std::string argToString(const Arguments & args, int n)
{
char * buff;
std::string str;
buff = new char[args[n]->ToString()->Length() + 1];
memset(buff, 0, args[n]->ToString()->Length() + 1);
args[n]->ToString()->WriteAscii(buff);
str.assign(buff);
delete buff;
return (str);
}
Handle<Value> Move(const Arguments& args) {
HandleScope scope;
std::string from;
std::string to;
char buffer[1024];
int fromFile = -1;
int toFile = -1;
int rd = -1;
if (args.Length() != 2)
{
ThrowException(Exception::TypeError(
String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsString() || !args[1]->IsString())
{
ThrowException(Exception::TypeError(
String::New("Wrong arguments")));
return scope.Close(Undefined());
}
from = argToString(args, 0);
to = argToString(args, 1);
if (rename(from.c_str(), to.c_str()) == 0)
{
return scope.Close(Undefined());
}
fromFile = open(from.c_str(), O_RDONLY);
toFile = open(to.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0640);
if (fromFile == -1)
{
ThrowException(Exception::TypeError(
String::New("Can't open 'from' file")));
return scope.Close(Undefined());
}
if (toFile == -1)
{
ThrowException(Exception::TypeError(
String::New("Can't open 'to' file")));
return scope.Close(Undefined());
}
memset(buffer, 0, 1024);
do
{
rd = read(fromFile, buffer, 1023);
buffer[rd] = '\0';
write(toFile, buffer, rd);
} while (rd > 0);
close(fromFile);
close(toFile);
if (unlink(from.c_str()) != 0)
{
ThrowException(Exception::TypeError(
String::New("Can't unlink 'from' file")));
return scope.Close(Undefined());
}
return scope.Close(Undefined());
}
void init(Handle<Object> target) {
target->Set(String::NewSymbol("move"),
FunctionTemplate::New(Move)->GetFunction());
}
NODE_MODULE(movefile, init)