Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / backup_manager / server / include / bkup_util.h
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 #ifndef BACKUP_MANAGER_SERVER_INCLUDE_BKUP_UTIL_H_
18 #define BACKUP_MANAGER_SERVER_INCLUDE_BKUP_UTIL_H_
19
20 #include <sys/mman.h>
21 #include <ctime>
22 #include <cstring>
23
24 static inline size_t BkupStrlcpy(char *dst, const char *src, size_t siz) {
25   size_t ret = strlen(src);
26   if (dst == NULL) {
27     return -1;
28   }
29   if (src == NULL) {
30     return -1;
31   }
32   if (siz) {  // LCOV_EXCL_BR_LINE 6:double check
33     size_t len = (ret >= siz) ? siz - 1 : ret;  // LCOV_EXCL_BR_LINE 11:unexpected branch
34     memcpy(dst, src, len);
35     dst[len] = '\0';
36   }
37   return ret;
38 }
39
40 static inline char *BkupAnonMmap(size_t size) {
41   char *map = static_cast<char *>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
42   return map;
43 }
44
45 static inline uint64_t BkupTimerReal(void) {
46   struct timespec ts;
47   if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != -1) {  // LCOV_EXCL_BR_LINE 5:clock_gettime's error case.
48     return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
49   }
50   return -1;
51 }
52
53 #endif  // BACKUP_MANAGER_SERVER_INCLUDE_BKUP_UTIL_H_