1<?xml version="1.0" encoding="utf-8"?> 2<html xml:lang="en-US" lang="en-US"> 3<head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <meta http-equiv="Content-Style-Type" content="text/css" /> 6 <link rel="stylesheet" href="../../../css/page.css" type="text/css" /> 7<title>File System: Error Handling</title> 8</head> 9<body> 10<h1>File System: Error Handling</h1> 11<h2>Table of Contents</h2> 12 <ul> 13<li><a href="#Summary">Overview</a></li> 14 <ul> 15<li><a href="#Summary_Result">Result Handling</a></li> 16<li><a href="#Summary_Error">Errors That Must Not Be Allowed to Occur in Retail Products</a></li> 17<li><a href="#Summary_Api">Functions for File and Directory Operations</a></li> 18 </ul> 19<li><a href="#Mounting">Handling Mount Errors</a></li> 20 <ul> 21<li><a href="#Mounting_RomArchive">ROM Archives</a></li> 22<li><a href="#Mounting_SaveDataArchive">Save Data Archives</a></li> 23<li><a href="#Mounting_ExtSaveDataArchive">Expanded Save Data Archives</a></li> 24 </ul> 25<li><a href="#FileAndDirectory">Handling Errors During File and Directory Operations</a></li> 26 <ul> 27<li><a href="#FileAndDirectory_RomArchive">ROM Archives</a></li> 28<li><a href="#FileAndDirectory_SaveDataArchive">Save Data Archives</a></li> 29<li><a href="#FileAndDirectory_ExtSaveDataArchive">Expanded Save Data Archives</a></li> 30 </ul> 31<li><a href="#History">Revision History</a></li> 32 </ul> 33<a name="Summary"><h2>Overview</h2></a> 34 <p> 35This page describes error handling for the file system. 36 </p> 37<a name="Summary_Result"><h3>Result Handling</h3></a> 38 <p> 39FS functions return many different types of <CODE>Result</CODE> 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 <CODE>NotFound</CODE> category. 40 </p> 41<pre> 42if(result <= nn::fs::ResultNotFound()) 43{ 44 // This can catch nn::fs::ResultMediaNotFound as well. 45} 46</pre> 47 <p> 48The <CODE>Result</CODE> objects returned by FS functions are forbidden from using the arithmetic operators <CODE>==</CODE> or <CODE>!=</CODE>. Using these causes a build error. Also note that the following two examples are equivalent. 49 </p> 50<pre> 51if(nn::fs::ResultNotFound::Includes(result)) 52{ 53 ... 54} 55</pre> 56<pre> 57if(nn::fs::ResultNotFound() >= result) 58{ 59 ... 60} 61</pre> 62<p><font color="red">Do not directly handle any <CODE>Result</CODE> values that are not mentioned in the Function Reference Manual.</font></p> 63<a name="Summary_Error"><h3>Errors That Must Not Be Allowed to Occur in Retail Products</h3></a> 64 <p> 65The following <CODE>Result</CODE> values indicate errors that are caused by application bugs and other such problems. You must fix your program so that these do not occur in retail products. 66 </p> 67 <ul> 68<li><a href="../../../nn/fs/ResultOutOfResource/Overview.html"><CODE>ResultOutOfResource</CODE></a></li> 69<li><a href="../../../nn/fs/ResultAccessDenied/Overview.html"><CODE>ResultAccessDenied</CODE></a></li> 70<li><a href="../../../nn/fs/ResultInvalidArgument/Overview.html"><CODE>ResultInvalidArgument</CODE></a></li> 71<li><a href="../../../nn/fs/ResultNotInitialized/Overview.html"><CODE>ResultNotInitialized</CODE></a></li> 72<li><a href="../../../nn/fs/ResultAlreadyInitialized/Overview.html"><CODE>ResultAlreadyInitialized</CODE></a></li> 73<li><a href="../../../nn/fs/ResultUnsupportedOperation/Overview.html"><CODE>ResultUnsupportedOperation</CODE></a></li> 74 </ul> 75 <p> 76The <CODE>Result</CODE> values listed above may be caused by user operations or it may be impossible to get rid of them entirely during development. You must handle errors that can be caused by the user. Some functions may also specify errors that must not occur in retail products, even though the corresponding <CODE>Result</CODE> values are not among those listed above. For more information, see the documentation for each of these functions in the Function Reference Manual. 77 </p> 78<a name="Summary_Api"><h3>Functions for File and Directory Operations</h3></a> 79 <p> 80There are two types of functions used to perform file and directory operations: <CODE>Try</CODE> functions (which have the prefix <CODE>Try</CODE> added to the function name), and non-<CODE>Try</CODE> functions (which do not contain this prefix). <CODE>Try</CODE> functions return a <CODE>Result</CODE> as the return value, unless an fatal internal error occurs. Instead of returning <CODE>Results</CODE>, non-<CODE>Try</CODE> functions are implemented to display an error screen whenever an internal error returns, regardless of whether the error is fatal. Control does not return from the function in this case.<br /> 81 </p> 82 <p> 83Internal errors never occur for ROM archives due to their characteristics. For ROM archives, implementation omissions in error handling can be avoided by using the non-<CODE>Try</CODE> functions. (During development, make sure to eliminate situations in which an error could occur because of an attempt to open a nonexistent file.) 84 </p> 85 <p> 86For archives other than ROM archives, you must always check for errors that can result from the user's operations. One typical example is removal of the Game Card by the user, which can lead to corrupted files. As a result, <CODE>Try</CODE> functions should always be used for error handling. Do not use non-<CODE>Try</CODE> functions because they can easily jump to error screens as a result of user operations. 87 </p> 88<a name="Mounting"><h2>Handling Mount Errors</h2></a> 89 <p> 90The following simplified C++ code provides guidelines for handling mount errors. 91 </p> 92<a name="Mounting_RomArchive"><h3>ROM Archives</h3></a> <code><pre> 93nn::Result result = nn::fs::MountRom(); 94if(result.IsFailure()) 95{ 96 // A ROM archive never fails to be mounted. Any failure indicates an abnormal condition. 97 // NOTE: When developing an application, confirm that you are passing enough working memory as an argument. 98 // Also check your Omakefile and RSF file to determine whether a ROMFS is created at build time. 99 NN_ERR_THROW_FATAL_ALL(result); 100} 101</pre></code> 102<a name="Mounting_SaveDataArchive"><h3>Save Data Archives</h3></a> <code><pre> 103nn::Result result = nn::fs::MountSaveData(); 104if(result.IsFailure()) 105{ 106 if(result <= nn::fs::ResultNotFormatted()) 107 { 108 // Formatting required. 109 // CAUSE: Unformatted save data region. 110 // NOTE: You must always format a save data region before mounting it for the first time. 111 result = nn::fs::FormatSaveData(); 112 } 113 else if(result <= nn::fs::ResultBadFormat()) 114 { 115 // Formatting required. 116 // CAUSE: Invalid file format. 117 result = nn::fs::FormatSaveData(); 118 } 119 else if(result <= nn::fs::ResultVerificationFailed()) 120 { 121 // Formatting required. 122 // CAUSE: Corrupted or tampered-with save data. 123 // NOTE: This state is never the result of user operations when there is automatic redundancy. 124 // This error may be caused by user operations (such as removing a card or turning off the system while data is being written) when there is no automatic redundancy. 125 result = nn::fs::FormatSaveData(); 126 } 127 else if(result <= nn::fs::ResultOperationDenied()) 128 { 129 // This error generally does not occur so no handling is required. 130 // NOTE: But <CODE>nn::fs::ResultMediaAccessError</CODE> is returned if failure is due to a bad connection or other hardware factor. 131 // In such cases, recovery may be possible by retrying, re-inserting the Game Card, restarting the system, or some other action. 132 } 133 else 134 { 135 // Unexpected error if other than the above 136 NN_ERR_THROW_FATAL_ALL(result); 137 } 138 // Troubleshooting after formatting 139 if(result.IsFailure()) 140 { 141 // Here, the act of formatting save data never fails. 142 // NOTE: When developing your application, confirm that the maxFiles and maxDirectories arguments are not too big. 143 // Also confirm that the content of the RSF file is correct. 144 NN_ERR_THROW_FATAL_ALL(result); 145 } 146 else 147 { 148 // Remount 149 result = nn::fs::MountSaveData(); 150 if(result.IsFailure()) 151 { 152 // The same as when making the first call. 153 // (Supplement) A mount operation never fails immediately after data has been successfully formatted. 154 } 155 } 156} 157</pre></code> 158<a name="Mounting_ExtSaveDataArchive"><h3>Expanded Save Data Archives</h3></a> <code><pre> 159nn::Result result = nn::fs::MountExtSaveData(); 160if(result.IsFailure()) 161{ 162 if(result <= nn::fs::ResultNotFound()) 163 { 164 if(result <= nn::fs::ResultMediaNotFound()) 165 { 166 // An SD Card has not been inserted. 167 // Note: This error is also returned if the SD Card is damaged or if some medium other than an SD Card is inserted. 168 // In such cases, an insertion event registered by <CODE>RegisterSdmcInsertedEvent</CODE> is signaled. 169 } 170 else 171 { 172 // The specified expanded save data does not exist and must be created. 173 // NOTE: Confirm that the ID you specified is correct when developing your application. 174 result = nn::fs::CreateExtSaveData(); 175 } 176 } 177 else if(result <= nn::fs::ResultNotFormatted()) 178 { 179 // You must recreate the expanded save data. 180 // CAUSE: Failed to create expanded save data. 181 result = nn::fs::DeleteExtSaveData(); 182 if(result.IsSuccess()) 183 { 184 result = nn::fs::CreateExtSaveData(); 185 } 186 } 187 else if(result <= nn::fs::ResultBadFormat()) 188 { 189 // You must format the SD Card. 190 } 191 else if(result <= nn::fs::ResultVerificationFailed()) 192 { 193 // You must recreate the expanded save data. 194 // CAUSE: Corrupted or tampered-with save data. 195 result = nn::fs::DeleteExtSaveData(); 196 if(result.IsSuccess()) 197 { 198 result = nn::fs::CreateExtSaveData(); 199 } 200 } 201 else if(result <= nn::fs::ResultOperationDenied()) 202 { 203 if(result <= nn::fs::ResultWriteProtected()) 204 { 205 // The SD Card is write-protected. 206 // NOTE: This occurs if the data recovery sequence is run during a mount operation. 207 } 208 else if(result <= nn::fs::ResultMediaAccessError()) 209 { 210 // This error is only returned when the cause is hardware-related, such as a loose connection. 211 // In such cases, recovery may be possible by retrying, re-inserting the Game Card, restarting the system, or some other action. 212 } 213 else 214 { 215 // The file or directory on the SD card may be read-only. 216 // NOTE: This occurs if the data recovery sequence is run during a mount operation. 217 } 218 } 219 else 220 { 221 // Unexpected error if other than the above 222 NN_ERR_THROW_FATAL_ALL(result); 223 } 224 // Troubleshooting after data has been created 225 if(result.IsFailure()) 226 { 227 if(result <= nn::fs::ResultNotEnoughSpace()) 228 { 229 // The SD Card does not have the necessary free space. 230 } 231 else if(result <= nn::fs::ResultArchiveInvalidated()) 232 { 233 // The SD Card may have been removed while data was being created. 234 } 235 else if(result <= nn::fs::ResultNotFormatted()) 236 { 237 // An error has occurred, interrupting the process of data creation. 238 } 239 else if(result <= nn::fs::ResultOperationDenied()) 240 { 241 if(result <= nn::fs::ResultWriteProtected()) 242 { 243 // The SD Card is write-protected. 244 } 245 else if(result <= nn::fs::ResultMediaAccessError()) 246 { 247 // This error is only returned when the cause is hardware-related, such as a loose connection. 248 // In such cases, recovery may be possible by retrying, re-inserting the Game Card, restarting the system, or some other action. 249 } 250 else 251 { 252 // The directory on the SD card may be read-only. 253 // NOTE: This occurs if the data recovery sequence is run during a mount operation. 254 } 255 } 256 else 257 { 258 // Unexpected error if other than the above 259 // NOTE: When developing your application, confirm that valid values are passed to iconData and iconDataSize. 260 NN_ERR_THROW_FATAL_ALL(result); 261 } 262 } 263 else 264 { 265 // Remount 266 result = nn::fs::MountExtSaveData(); 267 if(result.IsFailure()) 268 { 269 // The same as when making the first call. 270 // (Supplement) As long as the SD Card is not removed, mount operations carried out immediately after the data is created will never fail. 271 } 272 } 273} 274</pre></code> 275<a name="FileAndDirectory"><h2>Handling Errors During File and Directory Operations</h2></a> 276 <p> 277The following simplified C++ code provides guidelines for handling errors during file and directory operations. 278 </p> 279 <p> 280There are two types of functions used to perform file and directory operations: <CODE>Try</CODE> functions (which have the prefix <CODE>Try</CODE> added to the function name), and non-<CODE>Try</CODE> functions (which do not contain this prefix). For details, see <a href="#Summary_Api">Functions for File and Directory Operations</a>. 281 </p> 282<a name="FileAndDirectory_RomArchive"><h3>ROM Archives</h3></a> <code><pre> 283nn::fs::XXX(); 284// Because errors are handled internally by non-<CODE>Try</CODE> functions, you do not need to receive <CODE>Result</CODE> values. 285</pre> 286<a name="FileAndDirectory_SaveDataArchive"><h3>Save Data Archives</h3></a> 287<pre> 288result = nn::fs::TryXXX(); 289if(result.IsFailure()) 290{ 291 if(result <= nn::fs::ResultNotFound()) 292 { 293 // The specified path does not exist. 294 // NOTE: This error is returned when creating a file or directory only if the problem involves an incorrect archive name. 295 // You do not need to handle this if, by design, it does not occur. 296 } 297 else if(result <= nn::fs::ResultAlreadyExists()) 298 { 299 // The specified path already exists. 300 // NOTE: This error is not returned when files and directories are deleted. 301 // You do not need to handle this if, by design, it does not occur. 302 } 303 else if(result <= nn::fs::ResultVerificationFailed()) 304 { 305 // You must delete and recreate this file or directory. 306 // CAUSE: Corrupted or tampered-with save data. 307 // NOTE: This state will never be caused by user operations (such as removing a card or turning off the system while data is being written) when there is automatic redundancy. 308 // This error occurs even by user operations when there is no automatic redundancy. 309 // If this is not resolved, you must format the save data. 310 // NOTE: This error is returned when reading a region that has never been written to. 311 // Be careful after executing the functions <CODE>nn::fs::TryCreateFile</CODE> and <CODE>nn::fs::FileStream::TrySetSize</CODE>. 312 } 313 else if(result <= nn::fs::ResultOperationDenied()) 314 { 315 // Access was denied for some reason. 316 // EXAMPLE: You attempted to delete a file that was open elsewhere. 317 // You attempted to move a file or directory so that it would span archives. 318 // You attempted to delete the root directory. 319 // NOTE: You do not need handling for the above situations if, by design, they will not occur. 320 // But <CODE>nn::fs::ResultMediaAccessError</CODE> is returned if failure is due to a bad connection or other hardware factor. 321 // In such cases, recovery may be possible by retrying, re-inserting the Game Card, restarting the system, or some other action. 322 } 323 else if(result <= nn::fs::ResultNotEnoughSpace()) 324 { 325 // There is no available space in the archive or on the storage media. 326 // NOTE: This error may occur when creating a file or directory, or changing the file size. 327 // If this error occurs when you create a file, delete the file because it might be corrupted. 328 // If this error occurs when you change the size of a file, the file may have grown to the maximum possible size. 329 } 330 else 331 { 332 // Unexpected error if other than the above 333 NN_ERR_THROW_FATAL_ALL(result); 334 } 335} 336</pre></code> 337<a name="FileAndDirectory_ExtSaveDataArchive"><h3>Expanded Save Data Archives</h3></a> <code><pre> 338result = nn::fs::TryXXX(); 339if(result.IsFailure()) 340{ 341 if(result <= nn::fs::ResultNotFound()) 342 { 343 if(result <= nn::fs::ResultMediaNotFound()) 344 { 345 // An SD Card has not been inserted. 346 // Note: This error is also returned if the SD Card is damaged or if some medium other than an SD Card is inserted. 347 // In such cases, an insertion event registered by <CODE>RegisterSdmcInsertedEvent</CODE> is signaled. 348 } 349 else 350 { 351 // The specified path does not exist. 352 // NOTE: This error is returned when creating a file or directory only if the problem involves an incorrect archive name. 353 // NOTE: Expanded save data can also be accessed from other applications, 354 // so handling is not required for this error. 355 } 356 } 357 else if(result <= nn::fs::ResultAlreadyExists()) 358 { 359 // The specified path already exists. 360 // NOTE: This error is not returned when files and directories are deleted. 361 // NOTE: Expanded save data can also be accessed from other applications, 362 // so handling is not required for this error. 363 } 364 else if(result <= nn::fs::ResultVerificationFailed()) 365 { 366 // You must delete and recreate this file or directory. 367 // CAUSE: Corrupted or tampered-with save data. 368 // NOTE: If this is not resolved, you need to recreate the expanded save data. 369 // NOTE: This error is returned when reading a region that has never been written to. 370 // Be careful after executing the functions <CODE>nn::fs::TryCreateFile</CODE> and <CODE>nn::fs::FileStream::TrySetSize</CODE>. 371 } 372 else if(result <= nn::fs::ResultArchiveInvalidated()) 373 { 374 // You must remount the archive. 375 // CAUSE: The SD Card was removed, making the archive invalid. 376 // NOTE: Close all open files, call the <CODE>nn::fs::Unmount</CODE> function, and then remount the archive. 377 } 378 else if(result <= nn::fs::ResultOperationDenied()) 379 { 380 if(result <= nn::fs::ResultWriteProtected()) 381 { 382 // The SD Card is write-locked. 383 // NOTE: This error occurs when you write to an SD Card. This does not occur when you simply open a file. 384 } 385 else if(result <= nn::fs::ResultMediaAccessError()) 386 { 387 // This error is only returned when the cause is hardware-related, such as a loose connection. 388 // In such cases, recovery may be possible by retrying, re-inserting the Game Card, restarting the system, or some other action. 389 } 390 else 391 { 392 // Access was denied for some reason other than those above. 393 // EXAMPLES: You attempted to write to a file that is read-only. 394 // You attempted to delete a file that was open elsewhere. 395 // You attempted to move a file or directory so that it would span archives. 396 // You attempted to delete the root directory. 397 // (Note) Because the content of expanded save data can be modified even by other applications (depending on operations), 398 // or a user may have spoofed data on the SD card using a PC, this error must always be handled 399 } 400 } 401 else if(result <= nn::fs::ResultNotEnoughSpace()) 402 { 403 // There is no available space in the archive or on the storage media. 404 // NOTE: This error may occur when creating a file or directory, or changing the file size. 405 // If this error occurs when you create a file, delete the file because it might be corrupted. 406 // If this error occurs when you change the size of a file, the file may have grown to the maximum possible size. 407 } 408 else 409 { 410 // Unexpected error if other than the above 411 NN_ERR_THROW_FATAL_ALL(result); 412 } 413} 414</pre></code> 415<a name="History"><h2>Revision History</h2></a> 416 <div class="section"> 417 <dl class="history"> 418 <dt>2011/02/07</dt> 419 <dd> 420Changed how mounting expanded save data is handled from recreating the archive using <CODE>ResultBadFormat</CODE> to formatting the SD card. 421 </dd> 422 <dd> 423Deleted <CODE>ResultBadFormat</CODE> from how manipulating expanded save data files and directories are handled. 424 </dd> 425 <dd> 426Revised code so that unexpected errors now result in <CODE>NN_ERR_THROW_FATAL_ALL</CODE>. 427 </dd> 428 <dt>2011/01/05</dt> 429 <dd> 430Deleted <CODE>ResultArchiveInvalidated</CODE> from the errors that can occur when mounting the expanded save data. 431 </dd> 432 <dd> 433Changed the <CODE>Result</CODE> from <CODE>ResultMediaAccessError</CODE> to <CODE>ResultMediaNotFound</CODE> when a damaged SD Card is inserted. 434 </dd> 435 <dt>2010/12/14</dt> 436 <dd> 437Added errors that must not be allowed to occur in retail products. 438 </dd> 439 <dd> 440Added <CODE>ResultNotFormatted</CODE> and <CODE>ResultArchiveInvalidated</CODE> to error handling when expanded save data is created. 441 </dd> 442 <dt>2010/12/13</dt> 443 <dd> 444Split up error handling for file and directory operations by archive type. 445 </dd> 446 <dd> 447Changed how <CODE>ResultOperationDenied</CODE> is handled when expanded save data is mounted. 448 </dd> 449 <dt>2010/12/09</dt> 450 <dd> 451Added <B>Functions for File and Directory Operations</B>. 452 </dd> 453 <dt>2010/12/02</dt> 454 <dd> 455Removed <CODE>ResultNotFound</CODE> from save data error handling. 456 </dd> 457 <dt>2010/11/31</dt> 458 <dd> 459Added <CODE>ResultNotEnoughSpace</CODE> to the errors occurring during file operations. 460 </dd> 461 <dt>2010/11/25</dt> 462 <dd> 463Added <CODE>ResultNotFormatted</CODE> to error handling for expanded save data. 464 </dd> 465 <dt>2010/11/13</dt> 466 <dd> 467Initial version. 468 </dd> 469 </dl> 470 </div> 471<hr><p>CONFIDENTIAL</p></body> 472</html> 473