pspdev / newlib

A fork of newlib for the PSP.
GNU General Public License v2.0
8 stars 5 forks source link

Missing ::truncate and friends #1

Closed glebm closed 2 years ago

glebm commented 3 years ago

Would it be possible to implement truncate?

@AJenbo is looking into trying to build DevilutionX (for Diablo 1) and this function is used to size save files correctly.

AJenbo commented 3 years ago

I think stormlib requires ftruncate and the project uses ::truncate

sajattack commented 3 years ago

I believe ftruncate can be implemented through a combination of SceIoGetStat and SceIoChStat.

sajattack commented 3 years ago

http://pspdev.github.io/pspsdk/group__FileIO.html#ga89361a511354826e6ac54ff587368e33

sajattack commented 3 years ago

This issue should really be on the newlib repo though.

sajattack commented 3 years ago

I implemented it for Rust and forgot :sweat_smile:

    pub fn truncate(&self, size: u64) -> io::Result<()> {
        let mut stat: libc::SceIoStat = unsafe { core::mem::zeroed() };
        stat.st_size = size as i64;
        let result = unsafe { libc::sceIoChstat(self.path.as_c_str().as_ptr() as *const u8, &mut stat, 0x0004) };
        if result < 0 {
            return Err(cvt_io_error(result));
        } else {
            Ok(())
        }
    }

https://github.com/overdrivenpotato/rust/blob/psp_std_saj/library/std/src/sys/psp/fs.rs#L283

sajattack commented 3 years ago

In C, that's roughly


int truncate(char* path, size_t size) {
   SceIoStat stat;
   stat.st_size = size;
   int result = sceIoChstat(path, &stat, 0x0004);
   return result;
}
AJenbo commented 3 years ago

@sajattack just fyi @glebm isn't the guy who wouldn't build with SDL1 :D

He was just using glebm's repo as a base.

glebm commented 3 years ago

@sajattack While you're at it could you please also do ftruncate? I see there is sceIoChstatByFd

sajattack commented 3 years ago

I see there is sceIoChstatByFd

I believe that's a vita function, not sure if psp has it.

glebm commented 3 years ago

I guess we can stub out ftruncate for Diablo because it's used in a code path that we probably never invoke, so we should be OK with just truncate

sajattack commented 3 years ago

You could probably do ftruncate similarly to fstat https://github.com/pspdev/newlib/blob/7b50ff4d76f58e26fb33f6a4ea5f3a067ffe95be/newlib/libc/sys/psp/libcglue.c#L586

glebm commented 2 years ago

@fjtrujy Is this also fixed in the latest newlib?

fjtrujy commented 2 years ago

truncate is for sure, ftruncate, I'm not sure, but it is easily doable. Please check it, and if it is not implemented, create the issue in pspssdk because is over there where we will implement it

glebm commented 2 years ago

We no longer use ftruncate in DevilutionX, only truncate, so we can close this one