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