File System Error Handling

Overview

Result Handling

FS functions return many different types of Result values. These are handled by the following mechanism rather than one value at a time. The following example shows how to handle all errors belonging to the NotFound category.

if(result <= nn::fs::ResultNotFound())
{
    ...
}

Result objects returned by fs functions are forbidden from using the arithmetic operators == or !=. Using these causes a build error. The following two examples are equivalent.

if(nn::fs::ResultNotFound::Includes(result))
{
    ...
}
if(nn::fs::ResultNotFound() >= result)
{
    ...
}

Do not directly handle any Result values that are not mentioned in the Function Reference Manual.

Handling Mount Errors

The following simplified C++ code provides guidelines for handling mount errors.

ROM Archives

nn::Result result = nn::fs::MountRom();
if( result.IsFailure() )
{
    // A ROM archive never fails to be mounted. Any failure indicates an abnormal condition.
    // NOTE: When developing an application, confirm that you are passing enough working memory as an argument.
    //       Also check your Omakefile and RSF file to determine whether a ROMFS is created at build time.
}

Save Data Archives

nn::Result result = nn::fs::MountSaveData();

if( result.IsFailure() )
{
    if(result <= nn::fs::ResultNotFound())
    {
        // The application cannot create a save data region.
        // However, do not allow the application to stop on this error.
    }
    else if(result <= nn::fs::ResultNotFormatted())
    {
        // Data must be formatted.
        // CAUSE: Unformatted save data region.
        // NOTE: You must format a save data region before mounting it for the first time.
        result = nn::fs::FormatSaveData();
    }
    else if(result <= nn::fs::ResultBadFormat())
    {
        // Data must be formatted.
        // CAUSE: Invalid file format.
        result = nn::fs::FormatSaveData();
    }
    else if(result <= nn::fs::ResultVerificationFailed())
    {
        // Data must be formatted.
        // CAUSE: Corrupted or altered save data.
        // NOTE: This state is never the result of user operations when data is mirrored (duplicated).
        //       This error may be caused by user operations (such as removing a card or turning off the system while data is being written) when data is not mirrored.
        result = nn::fs::FormatSaveData();
    }
    else
    {
        // Some other error has occurred.
    }

    // Troubleshooting after data has been formatted 
    if( result.IsFailure() )
    {
        // The act of formatting save data never fails.
        // NOTE: When developing your application, confirm that the maxFiles and maxDirectories arguments are not too big.
        //       Also confirm that the content of the RSF file is correct.
    }
    else
    {
        // Re-mount
        result = nn::fs::MountSaveData();
        if( result.IsFailure() )
        {
            // After data has been successfully formatted, the act of mounting it never fails.
        }
    }
}

Expanded Save Data Archives

nn::Result result = nn::fs::MountExtSaveData();

if( result.IsFailure() )
{
    if(result <= nn::fs::ResultNotFound())
    {
        if(result <= nn::fs::ResultMediaNotFound())
        {
            // An SD Card has not been inserted.
        }
        else
        {
            // The specified expanded save data does not exist and must be created.
            // NOTE: Confirm that the specified ID is correct when developing your application.
            result = nn::fs::CreateExtSaveData();
        }
    }
    else if(result <= nn::fs::ResultBadFormat())
    {
        // You must recreate the expanded save data.
        // CAUSE: Invalid file format.
        result = nn::fs::DeleteExtSaveData();
        if(result.IsSuccess())
        {
            result = nn::fs::CreateExtSaveData();
        }
    }
    else if(result <= nn::fs::ResultVerificationFailed())
    {
        // You must recreate the expanded save data.
        // CAUSE: Corrupted or altered save data.
        // NOTE: This state is never the result of user operations when data is mirrored (duplicated).
        //       This error may be caused by user operations (such as removing a card or turning off the system while data is being written) when data is not mirrored.
        result = nn::fs::DeleteExtSaveData();
        if(result.IsSuccess())
        {
            result = nn::fs::CreateExtSaveData();
        }
    }
    else if(result <= nn::fs::ResultArchiveInvalidated())
    {
        // The SD Card may have been removed while it was being mounted.
        // Handle this just like nn::fs::ResultMediaNotFound().
    }
    else if(result <= nn::fs::ResultOperationDenied())
    {
        // Either the inserted card is not an SD Card or the SD Card is corrupted.
    }
    else
    {
        // Some other error has occurred.
    }

    // Troubleshooting after data has been created 
    if( result.IsFailure() )
    {
        if(result <= nn::fs::ResultNotEnoughSpace())
        {
            // There is no free space on the SD Card.
        }
        else
        {
            // As long as the SD Card is not removed while data is being created, the operation will not fail.
            // NOTE: When developing your application, confirm that the entryFile and entryDirectory arguments are not too big.
            //       Also confirm that valid values are being passed to iconData and iconDataSize.
        }
    }
    else
    {
        // Re-mount
        result = nn::fs::MountExtSaveData();
        if( result.IsFailure() )
        {
            // As long as the SD Card is not removed, the mount operation will not fail immediately after data is created.
        }

    }
}

Handling Errors During File and Directory Operations

if(result.IsFailure())
{
    if(result <= nn::fs::ResultNotFound())
    {
        if(result <= nn::fs::ResultMediaNotFound())
        {
            // An SD Card has not been inserted.
        }
        else
        {
            // The specified path does not exist.
            // NOTE: This error is not returned when files and directories are created.
        }
    }
    else if(result <= nn::fs::ResultAlreadyExists())
    {
        // The specified path already exists.
        // NOTE: This error is not returned when files and directories are deleted.
    }
    else if(result <= nn::fs::ResultBadFormat())
    {
        // You must delete and recreate this file (directory).
        // CAUSE: Invalid file format.
        // NOTE: If this does not solve the problem, you need to format or recreate the archive.
    }
    else if(result <= nn::fs::ResultVerificationFailed())
    {
        // You must delete and recreate this file (directory).
        // CAUSE: Corrupted or altered save data.
        // NOTE: This state is never the result of user operations when data is mirrored (duplicated).
        //       This error may be caused by user operations (such as removing a card or turning off the system while data is being written) when data is not mirrored.
        // If this does not solve the problem, you need to format or recreate the archive.
    }
    else if(result <= nn::fs::ResultArchiveInvalidated())
    {
        // You must remount the archive.
        // CAUSE: The SD Card was removed, making the archive invalid.
    }
    else if(result <= nn::fs::ResultOperationDenied())
    {
        if(result <= nn::fs::ResultWriteProtected())
        {
            // The SD Card is write-locked.
        }
        else if(result <= nn::fs::ResultMediaAccessError())
        {
            // Either the inserted card is not an SD Card or the SD Card is corrupted.
            // Backup memory may be corrupted while a CTR Card is being accessed.
        }
        else
        {
            // Access was denied for some reason other than those above.
            // EXAMPLES: You attempted to write to a file that cannot be written.
            //           You attempted to delete a file that was open elsewhere.
            //           You attempted to move a subdirectory that cannot be moved.
            //           You attempted to move a file or directory between archives.
            //           You attempted to delete the root directory.
        }
    }
    else
    {
        // Some other error has occurred.
    }
}

Revision History

2010/11/13
Initial version.

CONFIDENTIAL