vb6runtime 0.2.0

VB6 runtime library - value system, type conversions, and standard library implementations
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Native file I/O backend for Windows, Linux, and macOS.
//!
//! Uses actual file system calls through `std::fs::File`.

use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use super::backend::{AccessMode, FileBackend, LockMode, OpenFile, OpenMode};

/// A locked range: `(start, end)` inclusive, both 1-based.
type LockRange = (i32, i32);

/// Native file I/O backend using the real file system.
pub struct NativeBackend {
    /// Map from path to the actual file handle.
    files: HashMap<String, File>,
    /// Current working directory.
    current_dir: PathBuf,
    /// Current drive letter (tracked for VB6 semantics).
    current_drive: char,
    /// Locked regions per file path. `None` means the entire file is locked.
    locks: HashMap<String, Vec<Option<LockRange>>>,
}

impl NativeBackend {
    /// Create a new native backend.
    pub fn new() -> Self {
        let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/"));
        Self {
            files: HashMap::new(),
            current_dir: cwd,
            current_drive: 'C',
            locks: HashMap::new(),
        }
    }
}

impl Default for NativeBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl FileBackend for NativeBackend {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn open(
        &mut self,
        path: &Path,
        mode: OpenMode,
        access: AccessMode,
        lock: LockMode,
        record_length: i32,
    ) -> io::Result<OpenFile> {
        // Create parent directories if they don't exist for Output/Append modes
        if mode == OpenMode::Output || mode == OpenMode::Append {
            if let Some(parent) = path.parent() {
                if !parent.exists() {
                    std::fs::create_dir_all(parent)?;
                }
            }
        }

        let file = match mode {
            OpenMode::Input => OpenOptions::new()
                .read(true)
                .write(false)
                .create(false)
                .truncate(false)
                .open(path)?,
            OpenMode::Output => OpenOptions::new()
                .read(false)
                .write(true)
                .create(true)
                .truncate(true)
                .open(path)?,
            OpenMode::Append => OpenOptions::new()
                .read(false)
                .create(true)
                .append(true)
                .open(path)?,
            OpenMode::Random => OpenOptions::new()
                .read(true)
                .write(true)
                .create(true)
                .truncate(false)
                .open(path)?,
            OpenMode::Binary => OpenOptions::new()
                .read(true)
                .write(true)
                .create(true)
                .truncate(false)
                .open(path)?,
        };

        // Get initial position for Append mode
        let initial_position = if mode == OpenMode::Append {
            file.metadata()?.len() as i64
        } else {
            0
        };

        let path_str = path.to_string_lossy().to_string();

        // Store the file handle for later read/write/seek operations
        self.files.insert(path_str.clone(), file);

        Ok(OpenFile {
            number: 0, // Will be set by the caller
            path: path_str,
            mode,
            access,
            lock,
            record_length,
            position: initial_position,
            width: 0,        // Default: no line length limit
            print_column: 1, // Start of line
        })
    }

    fn close(&mut self, file: &OpenFile) -> io::Result<()> {
        self.files.remove(&file.path);
        Ok(())
    }

    fn read(&mut self, file: &mut OpenFile, buf: &mut [u8]) -> io::Result<usize> {
        let f = self
            .files
            .get_mut(&file.path)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "File not open"))?;

        // Seek to current position
        f.seek(SeekFrom::Start(file.position as u64))?;

        // Read data
        let bytes_read = f.read(buf)?;

        // Update position
        file.position += bytes_read as i64;

        Ok(bytes_read)
    }

    fn write(&mut self, file: &mut OpenFile, buf: &[u8]) -> io::Result<usize> {
        let f = self
            .files
            .get_mut(&file.path)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "File not open"))?;

        // Seek to current position
        f.seek(SeekFrom::Start(file.position as u64))?;

        // Write data
        let bytes_written = f.write(buf)?;

        // Update position
        file.position += bytes_written as i64;

        Ok(bytes_written)
    }

    fn seek(&mut self, file: &mut OpenFile, position: i64) -> io::Result<i64> {
        let _ = self
            .files
            .get(&file.path)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "File not open"))?;

        // Update the file's position
        file.position = position;

        Ok(file.position)
    }

    fn file_len(&mut self, path: &Path) -> io::Result<i64> {
        let metadata = std::fs::metadata(path)?;
        Ok(metadata.len() as i64)
    }

    fn file_exists(&mut self, path: &Path) -> bool {
        path.exists()
    }

    fn file_dir(&mut self, path: &Path, pattern: &str, attributes: i16) -> io::Result<Vec<String>> {
        let mut results = Vec::new();

        if let Ok(entries) = std::fs::read_dir(path) {
            for entry in entries.flatten() {
                let file_name = entry
                    .path()
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string();

                // Skip "." and ".."
                if file_name == "." || file_name == ".." {
                    continue;
                }

                // Check pattern match using utility function
                let matches_pattern = crate::state::file::matches_wildcard(&file_name, pattern);

                // Check attributes via filesystem metadata
                let metadata = std::fs::metadata(entry.path());
                let mut file_attrs: i16 = 0;
                if let Ok(meta) = metadata {
                    if meta.is_dir() {
                        file_attrs |= 16; // vbDirectory
                    }
                    if meta.is_file() {
                        file_attrs |= 32; // vbArchive
                    }
                }

                // Match attributes if specified (vbNormal=0 means no attribute filter)
                let attr_match = if attributes == 0 || attributes == file_attrs {
                    true
                } else {
                    (attributes & file_attrs) == attributes
                };

                if matches_pattern && attr_match {
                    results.push(file_name);
                }
            }
        }

        Ok(results)
    }

    fn position(&self, file: &OpenFile) -> i64 {
        // VB6 positions are 1-based
        file.position + 1
    }

    fn lof(&mut self, file: &OpenFile) -> io::Result<i64> {
        let path = Path::new(&file.path);
        let metadata = std::fs::metadata(path)?;
        Ok(metadata.len() as i64)
    }

    fn copy_file(&mut self, src: &Path, dst: &Path) -> io::Result<()> {
        std::fs::copy(src, dst)?;
        Ok(())
    }

    fn rename_file(&mut self, old_path: &Path, new_path: &Path) -> io::Result<()> {
        std::fs::rename(old_path, new_path)
    }

    fn remove_file(&mut self, path: &Path) -> io::Result<()> {
        std::fs::remove_file(path)
    }

    fn create_dir(&mut self, path: &Path) -> io::Result<()> {
        std::fs::create_dir(path)
    }

    fn remove_dir(&mut self, path: &Path) -> io::Result<()> {
        std::fs::remove_dir(path)
    }

    fn get_attrs(&mut self, path: &Path) -> io::Result<i16> {
        let metadata = std::fs::metadata(path)?;
        let perms = metadata.permissions();

        let mut attrs: i16 = 0;
        if perms.readonly() {
            attrs |= 1; // VB_READ_ONLY
        }
        if metadata.is_dir() {
            attrs |= 16; // VB_DIRECTORY
        }
        // Hidden/system require platform-specific APIs; skip for now.
        // Archive bit: set for all files on native (conservative default).
        if metadata.is_file() {
            attrs |= 32; // VB_ARCHIVE
        }
        Ok(attrs)
    }

    fn set_attrs(&mut self, path: &Path, attrs: i16) -> io::Result<()> {
        let metadata = std::fs::metadata(path)?;
        let mut perms = metadata.permissions();
        perms.set_readonly((attrs & 1) != 0);
        std::fs::set_permissions(path, perms)
    }

    fn file_datetime(&mut self, path: &Path) -> io::Result<std::time::SystemTime> {
        let metadata = std::fs::metadata(path)?;
        metadata.modified()
    }

    fn current_dir(&mut self) -> io::Result<PathBuf> {
        Ok(self.current_dir.clone())
    }

    fn set_current_dir(&mut self, path: &Path) -> io::Result<()> {
        // Resolve relative paths against the current directory
        let target = if path.is_relative() {
            self.current_dir.join(path)
        } else {
            path.to_path_buf()
        };
        // Verify the directory exists
        if !target.is_dir() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("Path not found: {}", path.display()),
            ));
        }
        self.current_dir = target;
        Ok(())
    }

    fn drives(&self) -> Vec<char> {
        vec!['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
    }

    fn current_dir_for_drive(&mut self, drive: char) -> io::Result<PathBuf> {
        if drive == self.current_drive {
            Ok(self.current_dir.clone())
        } else {
            // Non-current drives: return root as default
            Ok(PathBuf::from(format!("{drive}:\\")))
        }
    }

    fn set_current_drive(&mut self, drive: char) -> io::Result<()> {
        self.current_drive = drive.to_ascii_uppercase();
        Ok(())
    }

    fn lock_file(&mut self, path: &Path, record_range: Option<(i32, i32)>) -> io::Result<()> {
        let path_str = path.to_string_lossy().to_string();
        let existing = self.locks.entry(path_str).or_default();

        // Check for conflicts with existing locks
        for existing_lock in existing.iter() {
            match (existing_lock, &record_range) {
                // Entire file is already locked
                (None, _) | (_, None) => {
                    return Err(io::Error::new(
                        io::ErrorKind::PermissionDenied,
                        "File already locked",
                    ));
                }
                // Both are record ranges — check overlap
                (Some((e_start, e_end)), Some((n_start, n_end))) => {
                    if n_start <= e_end && n_end >= e_start {
                        return Err(io::Error::new(
                            io::ErrorKind::PermissionDenied,
                            "File already locked",
                        ));
                    }
                }
            }
        }

        existing.push(record_range);
        Ok(())
    }

    fn unlock_file(&mut self, path: &Path, record_range: Option<(i32, i32)>) -> io::Result<()> {
        let path_str = path.to_string_lossy().to_string();
        let existing = self
            .locks
            .get_mut(&path_str)
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not locked"))?;

        // Find and remove the matching lock
        let pos = existing
            .iter()
            .position(|lock| match (lock, &record_range) {
                (None, None) => true,
                (Some((a, b)), Some((c, d))) => a == c && b == d,
                _ => false,
            });

        match pos {
            Some(i) => {
                existing.remove(i);
                if existing.is_empty() {
                    self.locks.remove(&path_str);
                }
                Ok(())
            }
            None => Err(io::Error::new(io::ErrorKind::NotFound, "File not locked")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn open_and_close_file() {
        let mut backend = NativeBackend::new();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.txt");

        let mut file = backend
            .open(
                &path,
                OpenMode::Output,
                AccessMode::Write,
                LockMode::Shared,
                0,
            )
            .unwrap();
        file.number = 1;
        backend.close(&file).unwrap();
    }

    #[test]
    fn write_and_read_file() {
        let mut backend = NativeBackend::new();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.txt");

        // Write
        let mut file = backend
            .open(
                &path,
                OpenMode::Output,
                AccessMode::Write,
                LockMode::Shared,
                0,
            )
            .unwrap();
        file.number = 1;
        backend.write(&mut file, b"Hello, World!").unwrap();
        backend.close(&file).unwrap();

        // Read
        let mut file = backend
            .open(
                &path,
                OpenMode::Input,
                AccessMode::Read,
                LockMode::Shared,
                0,
            )
            .unwrap();
        file.number = 1;
        let mut buf = [0u8; 13];
        let bytes_read = backend.read(&mut file, &mut buf).unwrap();
        assert_eq!(bytes_read, 13);
        assert_eq!(&buf, b"Hello, World!");
        backend.close(&file).unwrap();
    }

    #[test]
    fn file_exists_check() {
        let mut backend = NativeBackend::new();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.txt");

        assert!(!backend.file_exists(&path));

        let file = backend
            .open(
                &path,
                OpenMode::Output,
                AccessMode::Write,
                LockMode::Shared,
                0,
            )
            .unwrap();
        backend.close(&file).unwrap();

        assert!(backend.file_exists(&path));
    }

    #[test]
    fn file_len_returns_correct_size() {
        let mut backend = NativeBackend::new();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.txt");

        let mut file = backend
            .open(
                &path,
                OpenMode::Output,
                AccessMode::Write,
                LockMode::Shared,
                0,
            )
            .unwrap();
        file.number = 1;
        backend.write(&mut file, b"12345").unwrap();

        let len = backend.file_len(&path).unwrap();
        assert_eq!(len, 5);
    }
}