1 /*
2  * Copyright (C) 2022 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 "file_packer_stream.h"
17 
18 #include <cerrno>
19 
20 #include "directory_ex.h"
21 #include "image_log.h"
22 #include "image_utils.h"
23 
24 #undef LOG_DOMAIN
25 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
26 
27 #undef LOG_TAG
28 #define LOG_TAG "FilePackerStream"
29 
30 namespace OHOS {
31 namespace Media {
32 
FilePackerStream(const std::string & filePath)33 FilePackerStream::FilePackerStream(const std::string &filePath)
34 {
35     int lastSlash = static_cast<int>(filePath.rfind("/"));
36     std::string dirPath;
37     std::string fileName;
38     if (lastSlash >= 0) {
39         dirPath = ExtractFilePath(filePath);
40         fileName = ExtractFileName(filePath);
41     } else {
42         dirPath = "";
43         fileName = filePath;
44     }
45     std::string realPath;
46     if (!ImageUtils::PathToRealPath(dirPath, realPath)) {
47         file_ = nullptr;
48         IMAGE_LOGE("convert to real path failed.");
49         return;
50     }
51 
52     if (!ForceCreateDirectory(realPath)) {
53         file_ = nullptr;
54         IMAGE_LOGE("create directory failed.");
55         return;
56     }
57 
58     std::string fullPath = realPath + "/" + fileName;
59     file_ = fopen(fullPath.c_str(), "wb");
60     if (file_ == nullptr) {
61         IMAGE_LOGE("fopen file failed, error:%{public}d", errno);
62         return;
63     }
64 }
FilePackerStream(const int fd)65 FilePackerStream::FilePackerStream(const int fd)
66 {
67     int dupFd = dup(fd);
68     if (dupFd < 0) {
69         IMAGE_LOGE("[FilePackerStream]Fail to dup fd.");
70         return;
71     }
72 
73     file_ = fdopen(dupFd, "wb");
74     if (file_ == nullptr) {
75         IMAGE_LOGE("[FilePackerStream]open file fail. error:%{public}d", errno);
76     }
77 }
~FilePackerStream()78 FilePackerStream::~FilePackerStream()
79 {
80     if (file_ != nullptr) {
81         fclose(file_);
82         file_ = nullptr;
83     }
84 }
85 
Write(const uint8_t * buffer,uint32_t size)86 bool FilePackerStream::Write(const uint8_t *buffer, uint32_t size)
87 {
88     if ((buffer == nullptr) || (size == 0)) {
89         IMAGE_LOGE("input parameter invalid.");
90         return false;
91     }
92     if (file_ == nullptr) {
93         IMAGE_LOGE("output file is null.");
94         return false;
95     }
96     if (fwrite(buffer, sizeof(uint8_t), size, file_) != size) {
97         IMAGE_LOGE("write %{public}u bytes failed. error:%{public}d", size, errno);
98         fclose(file_);
99         file_ = nullptr;
100         return false;
101     }
102     return true;
103 }
104 
Flush()105 void FilePackerStream::Flush()
106 {
107     if (file_ != nullptr) {
108         fflush(file_);
109     }
110 }
111 
BytesWritten()112 int64_t FilePackerStream::BytesWritten()
113 {
114     return (file_ != nullptr) ? ftell(file_) : 0;
115 }
116 
GetType()117 ImagePlugin::OutputStreamType FilePackerStream::GetType()
118 {
119     return ImagePlugin::OutputStreamType::FILE_PACKER;
120 }
121 
GetFd()122 int FilePackerStream::GetFd()
123 {
124     return fileno(file_);
125 }
126 } // namespace Media
127 } // namespace OHOS
128