Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / src / file_writer.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_SS_LoggerService
19 /// \brief    TODO
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "readerWriter/writer/file_writer.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <string>
27
28 namespace ReaderWriter {
29 CFileWriter::CFileWriter()
30     : m_FileHandle(-1),
31       m_fileposn(-1),
32       m_maxFileSize(0) {
33 }
34
35 CFileWriter::~CFileWriter() {
36   if (this->m_FileHandle != -1) {  // LCOV_EXCL_BR_LINE 200: it aways open, so m_FileHandle can not be -1
37     this->Close();
38     this->m_FileHandle = -1;
39   }
40 }
41
42 EFrameworkunifiedStatus CFileWriter::Initialize(CLoggerCfg* f_pLoggerCfg,
43                                    std::string f_Name1, UI_32 f_size1,
44                                    std::string f_Name2, UI_32 f_size2) {
45   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
46   this->m_pLoggerCfg = f_pLoggerCfg;
47   this->m_maxFileSize = f_size1;
48   if ((f_Name1.length() != 0) && (this->m_FileHandle == -1)) {  // LCOV_EXCL_BR_LINE 6:Due to the initial status
49     m_filename = f_Name1;
50     l_eStatus = this->Open();
51   }
52   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
53   return (l_eStatus);
54 }
55
56 EFrameworkunifiedStatus CFileWriter::Open(void) {
57   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
58   if (this->m_FileHandle != -1) {  // LCOV_EXCL_BR_LINE 6:Due to the initial status
59     // File already opened, no action required.
60     l_eStatus = eFrameworkunifiedStatusOK;
61   } else if (m_filename.length() != 0) {  // LCOV_EXCL_BR_LINE 6:Because the applicable variable cannot be changed from the external API
62     this->m_FileHandle = open(m_filename.c_str(),
63     O_WRONLY | O_CREAT | O_TRUNC,
64                               0644);
65     if (-1 != this->m_FileHandle) {  // LCOV_EXCL_BR_LINE 5:The open cannot pass because it cannot be mock
66       l_eStatus = eFrameworkunifiedStatusOK;
67       m_fileposn = lseek(m_FileHandle, 0L, SEEK_CUR);
68     }
69   }
70   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
71   return (l_eStatus);
72 }
73
74 BOOL CFileWriter::IsOpen(void) {
75   return (-1 == m_FileHandle) ? FALSE : TRUE;
76 }
77
78 EFrameworkunifiedStatus CFileWriter::Write(UI_8* f_data, UI_32 f_length,
79                               SI_32& f_bytesWritten) {
80   return (this->WriteData(f_data, f_length, f_bytesWritten));
81 }
82
83 EFrameworkunifiedStatus CFileWriter::WriteData(UI_8* f_data, UI_32 f_length,
84                                   SI_32& f_bytesWritten) {
85   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
86   f_bytesWritten = 0;
87   if (-1 == m_FileHandle) {  // LCOV_EXCL_BR_LINE 200: m_FileHandle can not be null
88     l_eStatus = eFrameworkunifiedStatusFail;
89   }
90
91   while ((eFrameworkunifiedStatusOK == l_eStatus)
92       && ((f_bytesWritten >= 0) && ((UI_32) f_bytesWritten < f_length))) {
93     SI_32 l_bytesWritten = -1;
94     m_fileposn = lseek(m_FileHandle, 0L, SEEK_CUR);
95     if ((m_fileposn + (UI_32) f_length) >= this->m_maxFileSize) {
96       SI_32 l_deltaLength = static_cast<SI_32>(this->m_maxFileSize - m_fileposn);
97       l_bytesWritten = static_cast<SI_32>(write(m_FileHandle, &f_data[f_bytesWritten],
98                              l_deltaLength));
99       f_bytesWritten += l_bytesWritten;
100       if ((l_bytesWritten >= 0) && (l_deltaLength == l_bytesWritten)) {
101         m_fileposn = lseek(m_FileHandle, 0L, SEEK_SET);
102       }
103     } else {
104       l_bytesWritten = static_cast<SI_32>(write(m_FileHandle, &f_data[f_bytesWritten],
105                              f_length - f_bytesWritten));
106       f_bytesWritten += l_bytesWritten;
107     }
108     l_eStatus = (l_bytesWritten > -1) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusFail;
109   }
110
111   return (l_eStatus);
112 }
113
114 void CFileWriter::Close() {  // LCOV_EXCL_START 6:Because the condition cannot be set
115   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
116   if (this->m_FileHandle != -1) {
117     (void) close(this->m_FileHandle);
118     this->m_FileHandle = -1;
119   }
120 }
121 // LCOV_EXCL_STOP
122 }  // namespace ReaderWriter