-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathmod.rs
369 lines (331 loc) · 11.6 KB
/
mod.rs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
mod utils;
use std::{fs, io::ErrorKind};
use crate::*;
use glob::glob;
use std::io::Write;
use std::path::Path;
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_read(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(x) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
let contents = fs::read_to_string(&x)
.unwrap_or_else(|e| panic!("failed to access the file '{}': {}", x, e));
let s = ValueRef::str(contents.as_ref());
return s.into_raw(ctx);
}
panic!("read() takes exactly one argument (0 given)");
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_glob(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
let pattern = get_call_arg_str(args, kwargs, 0, Some("pattern"))
.expect("glob() takes exactly one argument (0 given)");
let mut matched_paths = vec![];
for entry in glob(&pattern).unwrap_or_else(|e| panic!("Failed to read glob pattern: {}", e)) {
match entry {
Ok(path) => matched_paths.push(path.display().to_string()),
Err(e) => panic!("failed to access the file matching '{}': {}", pattern, e),
}
}
ValueRef::list_str(matched_paths.as_slice()).into_raw(ctx)
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_modpath(
ctx: *mut kclvm_context_t,
_args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let ctx = mut_ptr_as_ref(ctx);
let s = ValueRef::str(ctx.module_path.as_ref());
s.into_raw(ctx)
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_workdir(
ctx: *mut kclvm_context_t,
_args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let ctx = mut_ptr_as_ref(ctx);
let s = ValueRef::str(ctx.workdir.as_ref());
s.into_raw(ctx)
}
/// Read the path of the current script or module that is being executed
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_current(
ctx: *mut kclvm_context_t,
_args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let ctx = mut_ptr_as_ref(ctx);
let s = ValueRef::str(ctx.panic_info.kcl_file.as_ref());
s.into_raw(ctx)
}
/// Whether this file path exists. Returns true if the path points at
/// an existing entity. This function will traverse symbolic links to
/// query information about the destination file.
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_exists(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
let exist = Path::new(&path).exists();
return ValueRef::bool(exist).into_raw(ctx);
}
panic!("read() takes exactly one argument (0 given)");
}
/// Returns the canonical, absolute form of the path with all intermediate
/// components normalized and symbolic links resolved.
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_abs(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
if let Ok(abs_path) = Path::new(&path).canonicalize() {
return ValueRef::str(abs_path.to_str().unwrap()).into_raw(ctx);
} else {
panic!("Could not get the absolute path of {path}");
}
}
panic!("read() takes exactly one argument (0 given)");
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_mkdir(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("directory")) {
let exists = get_call_arg_bool(args, kwargs, 1, Some("exists")).unwrap_or_default();
if let Err(e) = fs::create_dir_all(&path) {
// Ignore the file exists error.
if exists && matches!(e.kind(), ErrorKind::AlreadyExists) {
return ValueRef::none().into_raw(ctx);
}
panic!("Failed to create directory '{}': {}", path, e);
}
return ValueRef::none().into_raw(ctx);
}
panic!("mkdir() takes exactly one argument (0 given)");
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_delete(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
if let Err(e) = fs::remove_file(&path) {
match e.kind() {
std::io::ErrorKind::NotFound => {
// if file not found, try to remove it as a directory
if let Err(e) = fs::remove_dir(&path) {
panic!("failed to delete '{}': {}", path, e);
}
}
_ => {
panic!("failed to delete '{}': {}", path, e);
}
}
}
return ValueRef::none().into_raw(ctx);
}
panic!("delete() takes exactly one argument (0 given)");
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_cp(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(src_path) = get_call_arg_str(args, kwargs, 0, Some("src")) {
if let Some(dest_path) = get_call_arg_str(args, kwargs, 1, Some("dest")) {
let src_path = Path::new(&src_path);
let dest_path = Path::new(&dest_path);
let result = if src_path.is_dir() {
utils::copy_directory(&src_path, &dest_path)
} else {
fs::copy(&src_path, &dest_path).map(|_| ())
};
if let Err(e) = result {
panic!(
"Failed to copy from '{}' to '{}': {}",
src_path.display(),
dest_path.display(),
e
);
}
return ValueRef::none().into_raw(ctx);
} else {
panic!("cp() missing 'dest_path' argument");
}
} else {
panic!("cp() missing 'src_path' argument");
}
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_mv(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(src_path) = get_call_arg_str(args, kwargs, 0, Some("src")) {
if let Some(dest_path) = get_call_arg_str(args, kwargs, 1, Some("dest")) {
if let Err(e) = fs::rename(&src_path, &dest_path) {
panic!("Failed to move '{}' to '{}': {}", src_path, dest_path, e);
}
return ValueRef::none().into_raw(ctx);
} else {
panic!("mv() missing 'dest_path' argument");
}
} else {
panic!("mv() missing 'src_path' argument");
}
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_size(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
let metadata = fs::metadata(&path);
match metadata {
Ok(metadata) => {
let size = metadata.len();
let value = kclvm::ValueRef::int(size as i64);
return value.into_raw(ctx);
}
Err(e) => {
panic!("failed to get size of '{}': {}", path, e);
}
}
}
panic!("size() takes exactly one argument (0 given)");
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_write(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
if let Some(content) = get_call_arg_str(args, kwargs, 1, Some("content")) {
match fs::File::create(&path) {
Ok(mut file) => {
if let Err(e) = file.write_all(content.as_bytes()) {
panic!("Failed to write to '{}': {}", path, e);
}
return ValueRef::none().into_raw(ctx);
}
Err(e) => panic!("Failed to create file '{}': {}", path, e),
}
} else {
panic!("write() missing 'content' argument");
}
} else {
panic!("write() missing 'filepath' argument");
}
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_append(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
if let Some(content) = get_call_arg_str(args, kwargs, 1, Some("content")) {
// Open the file in append mode, creating it if it doesn't exist
match fs::OpenOptions::new().append(true).create(true).open(&path) {
Ok(mut file) => {
if let Err(e) = file.write_all(content.as_bytes()) {
panic!("Failed to append to file '{}': {}", path, e);
}
return ValueRef::none().into_raw(ctx);
}
Err(e) => {
panic!("Failed to open or create file '{}': {}", path, e);
}
}
} else {
panic!("append() requires 'content' argument");
}
} else {
panic!("append() requires 'filepath' argument");
}
}
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_read_env(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(key) = get_call_arg_str(args, kwargs, 0, Some("key")) {
match std::env::var(key) {
Ok(v) => ValueRef::str(&v).into_raw(ctx),
Err(_) => ValueRef::undefined().into_raw(ctx),
}
} else {
panic!("read_env() requires 'key' argument");
}
}