| 266 | } |
| 267 | |
| 268 | FileHandle FileSystem::open(const char* path, OpenFlags flags) |
| 269 | { |
| 270 | if(isRootPath(path)) { |
| 271 | return Error::BadParam; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * The file may be marked 'read-only' in its metadata, so avoid modifications |
| 276 | * (truncate) during the open phase until this has been checked. |
| 277 | */ |
| 278 | |
| 279 | spiffs_flags sflags; |
| 280 | if(mapFileOpenFlags(flags - OpenFlag::Truncate, sflags).any()) { |
| 281 | return FileHandle(Error::NotSupported); |
| 282 | } |
| 283 | |
| 284 | auto file = SPIFFS_open(handle(), path, sflags, 0); |
| 285 | if(file < 0) { |
| 286 | int err = translateSpiffsError(file); |
| 287 | debug_ifserr(err, "open('%s')", path); |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | auto smb = initMetaBuffer(file); |
| 292 | // If file is marked read-only, fail write requests |
| 293 | if(smb != nullptr) { |
| 294 | if(flags[OpenFlag::Write] && smb->meta.attr[FileAttribute::ReadOnly]) { |
| 295 | SPIFFS_close(handle(), file); |
| 296 | return Error::ReadOnly; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Now truncate the file if so requested |
| 301 | if(flags[OpenFlag::Truncate]) { |
| 302 | int err = SPIFFS_ftruncate(handle(), file, 0); |
| 303 | if(err < 0) { |
| 304 | SPIFFS_close(handle(), file); |
| 305 | return translateSpiffsError(err); |
| 306 | } |
| 307 | |
| 308 | // Update modification timestamp |
| 309 | touch(file); |
| 310 | } |
| 311 | |
| 312 | return file; |
| 313 | } |
| 314 | |
| 315 | int FileSystem::close(FileHandle file) |
| 316 | { |
no test coverage detected