• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cerrno>
18 #include "directory_ex.h"
19 #include "image_utils.h"
20 
21 namespace OHOS {
22 namespace Media {
23 using namespace OHOS::HiviewDFX;
24 
FilePackerStream(const std::string & filePath)25 FilePackerStream::FilePackerStream(const std::string &filePath)
26 {
27     int lastSlash = filePath.rfind("/");
28     std::string dirPath;
29     std::string fileName;
30     if (lastSlash >= 0) {
31         dirPath = ExtractFilePath(filePath);
32         fileName = ExtractFileName(filePath);
33     } else {
34         dirPath = "";
35         fileName = filePath;
36     }
37     std::string realPath;
38     if (!ImageUtils::PathToRealPath(dirPath, realPath)) {
39         file_ = nullptr;
40         HiLog::Error(LABEL, "convert to real path failed.");
41         return;
42     }
43 
44     if (!ForceCreateDirectory(realPath)) {
45         file_ = nullptr;
46         HiLog::Error(LABEL, "create directory failed.");
47         return;
48     }
49 
50     std::string fullPath = realPath + "/" + fileName;
51     file_ = fopen(fullPath.c_str(), "wb");
52     if (file_ == nullptr) {
53         HiLog::Error(LABEL, "fopen file failed, error:%{public}d", errno);
54         return;
55     }
56 }
FilePackerStream(const int fd)57 FilePackerStream::FilePackerStream(const int fd)
58 {
59     file_ = fdopen(fd, "wb");
60     if (file_ == nullptr) {
61         HiLog::Error(LABEL, "fopen file failed, error:%{public}d", errno);
62         return;
63     }
64 }
~FilePackerStream()65 FilePackerStream::~FilePackerStream()
66 {
67     if (file_ != nullptr) {
68         fclose(file_);
69         file_ = nullptr;
70     }
71 }
72 
Write(const uint8_t * buffer,uint32_t size)73 bool FilePackerStream::Write(const uint8_t *buffer, uint32_t size)
74 {
75     if ((buffer == nullptr) || (size == 0)) {
76         HiLog::Error(LABEL, "input parameter invalid.");
77         return false;
78     }
79     if (file_ == nullptr) {
80         HiLog::Error(LABEL, "output file is null.");
81         return false;
82     }
83     if (fwrite(buffer, sizeof(uint8_t), size, file_) != size) {
84         HiLog::Error(LABEL, "write %{public}u bytes failed.", size);
85         fclose(file_);
86         file_ = nullptr;
87         return false;
88     }
89     return true;
90 }
91 
Flush()92 void FilePackerStream::Flush()
93 {
94     if (file_ != nullptr) {
95         fflush(file_);
96     }
97 }
98 
BytesWritten()99 int64_t FilePackerStream::BytesWritten()
100 {
101     return (file_ != nullptr) ? ftell(file_) : 0;
102 }
103 } // namespace Media
104 } // namespace OHOS
105