files.anubis
3.42 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
*Project* Anubis
*Title* generic files functions.
*Copyright* Copyright (c) David René 2006-2007.
*Author* David René
*Created* May 2005
*Status* Released
*Overview*
read tools/connections.anubis
read tools/streams.anubis
read tools/basis.anubis
public type ResultCopy:
cant_read_file,
cant_create_file,
copy_error,
copy_file_mode_error,
copy_file_times_error,
copy_ok.
define ResultCopy
copy_file
(
RAddr(Int8) source,
WAddr(Int8) target,
Int32 length
) =
with read_length = min(length, 65536),
if read_length = 0 then
copy_ok
else if read(source, read_length, 10) is
{
failure then copy_error,
success(buffer) then
if write( target , buffer) is
{
failure then copy_error,
success(how_many) then
if read_length = how_many then
copy_file(source, target, length - how_many)
else
copy_error
}
}
.
define ResultCopy
copy_file_data
(
String source_file,
String target_file
) =
//open the source file
if (Maybe(RAddr(Int8)))file(source_file, read) is
{
failure then cant_read_file,
success(source) then
//open the target file
if (Maybe(RWAddr(Int8)))file(target_file, new) is
{
failure then print("can't create target file\n");cant_create_file, //nothing to write
success(target) then copy_file(source, weaken(target), file_size(source))
}
}.
public define ResultCopy
copy_file
(
String source_file,
String target_file
) =
// copy only the data of the file
with result = copy_file_data(source_file, target_file),
if result = copy_ok then
// copy date and times
if get_file_times(source_file) is
{
failure then print("get copy_file_times_error\n");copy_file_times_error,
success(ftimes) then
if ftimes is times(modif, access) then
if set_file_times(target_file, ftimes) is
{
failure then print("set copy_file_times_error\n");copy_file_times_error,
success(_) then
//copy file_mode
if get_file_mode(source_file) is
{
failure then copy_file_mode_error,
success (file_mode) then
if set_file_mode(target_file, file_mode) is
{
failure then copy_file_mode_error,
success(_) then copy_ok
}
}
}
}
else
result
.
public define Bool
move_file
(
String source_file,
String target_file
)=
if copy_file(source_file, target_file) is copy_ok then
remove(source_file)
else
false.
/** copy file into the socket
*/
public define Maybe(One)
copy_file_to_stream
(
RAddr(Int8) source_file,
RWAddr(Int8) socket,
Int32 length
) =
//we send the mail content by 16KB buffer
with read_length = min(length, 16384),
if read_length = 0 then
success(unique)
else if read(source_file, read_length, 10) is
{
failure then failure,
success(buffer) then
if reliable_write(tcp(socket), buffer) is
{
failure then failure,
success(how_many) then
if read_length = how_many then
copy_file_to_stream(source_file, socket, length - how_many)
else
failure
}
}.