diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp index 8d8b149f62ab2ec10d5ee9ee42d29d0823f99fd7..a101a39300a356735dc79f1d1e903a145291bd58 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp @@ -266,20 +266,20 @@ FsResult FsRandomAccessFile::CloseSync() const return FsResult::Success(); } -FsResult FsRandomAccessFile::Constructor() +FsResult> FsRandomAccessFile::Constructor() { auto rafEntity = CreateUniquePtr(); if (rafEntity == nullptr) { HILOGE("Failed to request heap memory."); - return FsResult::Error(ENOMEM); + return FsResult>::Error(ENOMEM); } - FsRandomAccessFile *randomAccessFilePtr = new FsRandomAccessFile(move(rafEntity)); + auto randomAccessFilePtr = CreateUniquePtr(move(rafEntity)); if (randomAccessFilePtr == nullptr) { HILOGE("INNER BUG. Failed to wrap entity for obj RandomAccessFile"); - return FsResult::Error(EIO); + return FsResult>::Error(EIO); } - return FsResult::Success(move(randomAccessFilePtr)); + return FsResult>::Success(move(randomAccessFilePtr)); } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h index 024cf25b7db7d2bfdfaae1b591abf1a6c4bd8664..47b8941c418e13ecb91db157519754bc46933d7e 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h @@ -72,11 +72,11 @@ public: FsResult GetFD() const; FsResult GetFPointer() const; - static FsResult Constructor(); - + static FsResult> Constructor(); + explicit FsRandomAccessFile(unique_ptr entity) : rafEntity(move(entity)) {} + private: unique_ptr rafEntity; - explicit FsRandomAccessFile(unique_ptr entity) : rafEntity(move(entity)) {} }; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp index 17fe296a3fc95112d2b9d1c8441e8cb45ea05f7b..fac61886d61691f51e1f46a9d6ee0e38c4617a26 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp @@ -30,7 +30,7 @@ using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; -static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) +static ani_object Wrap(ani_env *env, unique_ptr rafFile) { if (rafFile == nullptr) { HILOGE("FsRandomAccessFile pointer is null!"); @@ -52,7 +52,7 @@ static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) return nullptr; } - ani_long ptr = static_cast(reinterpret_cast(rafFile)); + ani_long ptr = static_cast(reinterpret_cast(rafFile.get())); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { HILOGE("New %s obj Failed!", classDesc); @@ -148,18 +148,17 @@ static ani_object CreateRandomAccessFileByString( ErrorHandler::Throw(env, EINVAL); return nullptr; } - FsResult ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, modeOp, op); + FsResult> ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, + modeOp, op); if (!ret.IsSuccess()) { HILOGE("CreateRandomAccessFile failed"); const auto &err = ret.GetError(); ErrorHandler::Throw(env, err); return nullptr; } - const FsRandomAccessFile *refFile = ret.GetData().value(); + unique_ptr refFile = move(ret.GetData().value()); auto result = Wrap(env, move(refFile)); if (result == nullptr) { - delete refFile; - refFile = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } @@ -193,18 +192,16 @@ ani_object CreateRandomAccessFileAni::CreateRandomAccessFileSync( return nullptr; } int32_t fd = static_cast(fdOp); - FsResult ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, op); + FsResult> ret = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, op); if (!ret.IsSuccess()) { HILOGE("CreateRandomAccessFile failed"); const auto &err = ret.GetError(); ErrorHandler::Throw(env, err); return nullptr; } - const FsRandomAccessFile *refFile = ret.GetData().value(); + unique_ptr refFile = move(ret.GetData().value()); auto result = Wrap(env, move(refFile)); if (result == nullptr) { - delete refFile; - refFile = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp index e4a1969f3e493328008e81f49cd5fafe169c3a13..7ef147c78885db1a5ed0fda9143571c505eb5def 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.cpp @@ -123,58 +123,58 @@ static tuple ValidAndConvertFlags(const option return {true, flags, start, end}; } -static FsResult InstantiateRandomAccessFile(unique_ptr fdg, - int64_t fp, - int64_t start = INVALID_POS, - int64_t end = INVALID_POS) +static FsResult> InstantiateRandomAccessFile(unique_ptr fdg, + int64_t fp, + int64_t start = INVALID_POS, + int64_t end = INVALID_POS) { - FsResult result = FsRandomAccessFile::Constructor(); + FsResult> result = FsRandomAccessFile::Constructor(); if (!result.IsSuccess()) { HILOGE("Failed to instantiate class"); - return FsResult::Error(EIO); + return result; } - const FsRandomAccessFile *objRAF = result.GetData().value(); + unique_ptr objRAF = move(result.GetData().value()); if (!objRAF) { HILOGE("Cannot instantiate randomaccessfile"); - return FsResult::Error(EIO); + return FsResult>::Error(EIO); } auto *rafEntity = objRAF->GetRAFEntity(); if (!rafEntity) { HILOGE("Cannot instantiate randomaccessfile because of void entity"); - return FsResult::Error(EIO); + return FsResult>::Error(EIO); } rafEntity->fd.swap(fdg); rafEntity->filePointer = fp; rafEntity->start = start; rafEntity->end = end; - return result; + return FsResult>::Success(move(objRAF)); } -FsResult CreateRandomAccessFileCore::DoCreateRandomAccessFile( +FsResult> CreateRandomAccessFileCore::DoCreateRandomAccessFile( const string &path, const optional &mode, const optional &options) { auto [succ, fileInfo, err] = ParseStringToFileInfo(path); if (!succ) { - return FsResult::Error(err); + return FsResult>::Error(err); } auto [succFlags, flags, ignoreStart, ignoreEnd] = ValidAndConvertFlags(mode, options, fileInfo); if (!succFlags) { - return FsResult::Error(EINVAL); + return FsResult>::Error(EINVAL); } unique_ptr openReq = { new uv_fs_t, FsUtils::FsReqCleanup }; if (!openReq) { HILOGE("Failed to request heap memory."); - return FsResult::Error(ENOMEM); + return FsResult>::Error(ENOMEM); } int ret = uv_fs_open(nullptr, openReq.get(), fileInfo.path.get(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL); if (ret < 0) { - return FsResult::Error(ret); + return FsResult>::Error(ret); } fileInfo.fdg->SetFD(openReq.get()->result, false); @@ -188,12 +188,12 @@ FsResult CreateRandomAccessFileCore::DoCreateRandomAccessF return InstantiateRandomAccessFile(move(fileInfo.fdg), 0); } -FsResult CreateRandomAccessFileCore::DoCreateRandomAccessFile( +FsResult> CreateRandomAccessFileCore::DoCreateRandomAccessFile( const int32_t &fd, const optional &options) { auto [succ, fileInfo, err] = ParseFdToFileInfo(fd); if (!succ) { - return FsResult::Error(err); + return FsResult>::Error(err); } if (options.has_value()) { auto [succ, start, end] = ValidRafOptions(options); diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h index 0900891bae980358ec50348db1ee807f88c6149f..ba297080da83280bcaa7fa2644c0e83742897cb5 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile_core.h @@ -32,10 +32,10 @@ struct RandomAccessFileOptions { class CreateRandomAccessFileCore final { public: - static FsResult DoCreateRandomAccessFile( + static FsResult> DoCreateRandomAccessFile( const string &path, const optional &mode = nullopt, const optional &options = nullopt); - static FsResult DoCreateRandomAccessFile( + static FsResult> DoCreateRandomAccessFile( const int32_t &fd, const optional &options = nullopt); };