1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "fdopen_stream_core.h" 17 18 #include <memory> 19 20 #include "file_utils.h" 21 #include "filemgmt_libhilog.h" 22 #include "fs_utils.h" 23 #include "stream_entity.h" 24 #include "stream_instantiator.h" 25 26 namespace OHOS { 27 namespace FileManagement { 28 namespace ModuleFileIO { 29 using namespace std; 30 DoFdopenStream(const int & fd,const string & mode)31FsResult<FsStream *> FdopenStreamCore::DoFdopenStream(const int &fd, const string &mode) 32 { 33 if (fd < 0) { 34 HILOGE("Invalid fd"); 35 return FsResult<FsStream *>::Error(EINVAL); 36 } 37 38 FILE *file = fdopen(fd, mode.c_str()); 39 if (!file) { 40 HILOGE("Failed to fopen file by fd"); 41 return FsResult<FsStream *>::Error(errno); 42 } 43 44 return StreamInstantiator::InstantiateStream(move(file)); 45 } 46 47 } // namespace ModuleFileIO 48 } // namespace FileManagement 49 } // namespace OHOS