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