Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What does System.IO.File.Replace turn into with IL2CPP?

Discussion in 'Android' started by johaga, Apr 26, 2022.

  1. johaga

    johaga

    Joined:
    Oct 10, 2019
    Posts:
    10
    I'm actually curious with all platforms, but I thought I'd start by asking about Android.
    In .NET, File.Replace is pretty close to atomic. It's not really, because it depends on the platform. In Win32, it's atomic. In Unix it's not quite.

    It does seem that regardless of platform, it's safer to save everything to FileB, and then replace FileA with FileB, than to try to write to FileA directly.

    So I wanted to use File.Replace ... however, I have no idea how IL2CPP handles it. Is there any place we can see what IL2CPP does with System.IO.File.Replace?
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    If you're using Unity 2020.3 or up, you can do export project and then look at il2cpp cpp files, I am guessing System.IO.File.Replace will eventually call into <project>unityLibrary\src\main\Il2CppOutputProject\IL2CPP\libil2cpp\os\Posix\File.cpp File::ReplaceFile
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
  4. johaga

    johaga

    Joined:
    Oct 10, 2019
    Posts:
    10
    Oh we're not worried about multiple access, we've had some issues with corrupted saves, so we're trying to make sure the game isn't quit out of mid-write.
     
  5. johaga

    johaga

    Joined:
    Oct 10, 2019
    Posts:
    10
    Or rather, if they do quit mid-write, it doesn't break the file.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    Then most certainly write new file and rename it afterwards. I don't think you can assume safety anywhere for File.Replace, just on some platforms the risk is very small.