Skip to content

Write does not work on DOSBox-X #22

@ApprenticeofEnder

Description

@ApprenticeofEnder

It looks like the actual file write function does not work with what I've tested. It looks like the write function makes no actual modifications to the file. I'm not entirely sure I'm doing anything wrong. I noticed the file tests notably lacked any write tests, though, so this might be relatively recent functionality.

I wonder if there's a call missing somewhere, maybe to actually flush the buffer? Assembly is pretty far from my domain of expertise, sadly.

I tried this on DOSBox-X on Windows (version 2023.10.06 specifically, if that makes a difference). I tried a bunch of different things:

  • Using the same file object for read, seek, and write (nope)
  • Using the same file object but opening and closing it between operations (nope)
  • Using two different file objects operating on the same file (nope)
  • Using two different file objects, one for a source file and one for a destination (nope, this actually caused a panic if the second file didn't exist so no programmatic creation here)

With the last test, I created a completely blank file in DOSBox, which after the modifications should have been 13000 bytes in size, yet remained at size 0 after the program finished running.

For reference, here's the relevant part of main.rs (this is for TryHackMe's Advent of Cyber 2023):

use day5_payload::{*, dos::file::File};

entry!(main);

fn main() {
    let filename_src: &str = "TEST.TXT";
    let filename_out: &str = "TEST_2.TXT";

    let fix_bytes: [u8; 2] = [0x41, 0x43];

    println!("Opening: {}", filename_src);
    let src_file: File = File::open(filename_src).expect("Check file name and try again?");

    println!("Reading...");
    let mut buf = [0; 13000]; //Target file is a little over 12,700 bytes
    let bytes_read = src_file.read(&mut buf).unwrap();
    println!("Read {} bytes", bytes_read);

    src_file.close().unwrap();
    println!("Closed source file.");

    println!("Opening for write: {}", filename_out);
    let out_file: File = File::open(filename_out).expect("Error opening outfile.");

    fix_bytes.iter().enumerate().for_each(|(index, byte)| {
        buf[index] = *byte;
    });

    println!("First two bytes of buffer: {} {}", buf[0], buf[1]);
    println!(
        "First two bytes of patch buffer: {} {}",
        fix_bytes[0], fix_bytes[1]
    );
    assert_eq!((buf[0], buf[1]), (fix_bytes[0], fix_bytes[1]));

    let bytes_written = out_file.write(&buf).unwrap();
    println!("Wrote {} bytes", bytes_written);

    out_file.close().unwrap();
    println!("Modification complete. Please confirm modifications manually.");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions