Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 4 | #include <string.h> |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 5 | #include <errno.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 6 | #include <pthread.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 7 | #include "dvr_segment.h" |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 8 | #include <segment.h> |
Zhiqiang Han | 02e890c | 2023-04-25 14:55:02 +0800 | [diff] [blame^] | 9 | #include <dirent.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 10 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 11 | /**\brief DVR segment file information*/ |
| 12 | typedef struct { |
| 13 | char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/ |
| 14 | uint64_t id; /**< DVR Segment id*/ |
| 15 | } DVR_SegmentFile_t; |
| 16 | |
| 17 | void *dvr_segment_thread(void *arg) |
| 18 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 19 | int ret; |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 20 | DVR_SegmentFile_t *segment_file = (DVR_SegmentFile_t*)arg; |
Wentao MA | 16f870e | 2022-09-09 11:00:22 +0800 | [diff] [blame] | 21 | if (segment_file == NULL) { |
| 22 | DVR_ERROR("Invalid segment_file pointer"); |
| 23 | return NULL; |
| 24 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 25 | |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 26 | pthread_detach(pthread_self()); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 27 | DVR_INFO("%s try to delete [%s-%lld]", __func__, segment_file->location, segment_file->id); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 28 | ret = segment_delete(segment_file->location, segment_file->id); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 29 | DVR_INFO("%s delete segment [%s-%lld] %s", __func__, segment_file->location, segment_file->id, |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 30 | ret == DVR_SUCCESS ? "success" : "failed"); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 31 | if (segment_file != NULL) { |
| 32 | //malloc at delete api.free at this |
| 33 | free(segment_file); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 34 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 35 | return NULL; |
| 36 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 37 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 38 | int dvr_segment_delete(const char *location, uint64_t segment_id) |
| 39 | { |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 40 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 41 | pthread_t thread; |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 42 | DVR_SegmentFile_t *segment; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 43 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 44 | DVR_RETURN_IF_FALSE(location); |
| 45 | DVR_RETURN_IF_FALSE(strlen(location) < DVR_MAX_LOCATION_SIZE); |
Wentao MA | 4b8d475 | 2022-09-14 19:03:45 +0800 | [diff] [blame] | 46 | DVR_INFO("In function %s, segment %s's id is %lld", __func__, location, segment_id); |
| 47 | |
| 48 | // Memory allocated here will be freed in segment deletion thread under normal conditions. |
| 49 | // In case of thread creation failure, it will be freed right away. |
| 50 | segment = (DVR_SegmentFile_t *)malloc(sizeof(DVR_SegmentFile_t)); |
| 51 | DVR_RETURN_IF_FALSE(segment != NULL); |
| 52 | |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 53 | memset(segment->location, 0, sizeof(segment->location)); |
| 54 | memcpy(segment->location, location, strlen(location)); |
| 55 | segment->id = segment_id; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 56 | |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 57 | int ret = pthread_create(&thread, NULL, dvr_segment_thread, segment); |
| 58 | if (ret != 0) { |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 59 | if (segment != NULL) { |
| 60 | free(segment); |
hualing chen | e83bf81 | 2020-04-23 13:42:37 +0800 | [diff] [blame] | 61 | } |
| 62 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 63 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 64 | } |
| 65 | |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 66 | int dvr_segment_del_by_location(const char *location) |
| 67 | { |
Zhiqiang Han | 02e890c | 2023-04-25 14:55:02 +0800 | [diff] [blame^] | 68 | #if 0 |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 69 | FILE *fp; |
Weiwei Wang | 2ca253d | 2022-11-01 15:49:10 +0800 | [diff] [blame] | 70 | char cmd[DVR_MAX_LOCATION_SIZE * 2 + 64]; |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 71 | |
| 72 | DVR_RETURN_IF_FALSE(location); |
| 73 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 74 | DVR_INFO("%s location:%s", __func__, location); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 75 | { |
| 76 | /* del file */ |
| 77 | memset(cmd, 0, sizeof(cmd)); |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 78 | sprintf(cmd, "rm %s-* %s.list %s.stats %s.odb %s.dat", location, location, location, location, location); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 79 | fp = popen(cmd, "r"); |
| 80 | DVR_RETURN_IF_FALSE(fp); |
| 81 | } |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 82 | pclose(fp); |
Zhiqiang Han | 02e890c | 2023-04-25 14:55:02 +0800 | [diff] [blame^] | 83 | #else |
| 84 | DVR_RETURN_IF_FALSE(location); |
| 85 | |
| 86 | DVR_INFO("%s location:%s", __func__, location); |
| 87 | |
| 88 | DIR *dir; // pointer to directory |
| 89 | struct dirent *entry; // pointer to file entry |
| 90 | char *ext; // pointer to file extension |
| 91 | char *loc_dname; |
| 92 | int loc_dname_len; |
| 93 | char *loc_fname; |
| 94 | int loc_fname_len; |
| 95 | char *path; |
| 96 | int path_size; |
| 97 | |
| 98 | /*get the dirname and filename*/ |
| 99 | loc_fname = strrchr(location, '/');// fine last slash |
| 100 | loc_fname_len = strlen(loc_fname); |
| 101 | DVR_RETURN_IF_FALSE(loc_fname_len != 0); |
| 102 | |
| 103 | loc_dname_len = loc_fname - location; |
| 104 | loc_dname = malloc(loc_dname_len + 1); |
| 105 | DVR_RETURN_IF_FALSE(loc_dname != NULL); |
| 106 | memcpy(loc_dname, location, loc_dname_len); |
| 107 | loc_dname[loc_dname_len] = '\0'; |
| 108 | |
| 109 | path_size = strlen(location) + 32;//assume the file ext is no more than 32 bytes |
| 110 | path = malloc(path_size); |
| 111 | if (path) { |
| 112 | dir = opendir(loc_dname); // open directory |
| 113 | if (dir != NULL) { |
| 114 | while ((entry = readdir(dir)) != NULL) { // read each file entry |
| 115 | if (entry->d_type == DT_REG) { // only regular files |
| 116 | if (strncmp(entry->d_name, loc_fname, loc_fname_len) == 0) { |
| 117 | snprintf(path, path_size, "%s/%s", loc_dname, entry->d_name); |
| 118 | if (remove(path) != 0) { |
| 119 | DVR_INFO("%s cannot delete file:%s", __func__, path); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | closedir(dir); // close directory |
| 125 | } else { |
| 126 | DVR_INFO("%s location:%s canot open", __func__, location); |
| 127 | } |
| 128 | free(path); |
| 129 | |
| 130 | } else { |
| 131 | |
| 132 | free(loc_dname); |
| 133 | DVR_INFO("%s mem fail", __func__); |
| 134 | } |
| 135 | |
| 136 | free(loc_dname); |
| 137 | #endif |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 138 | DVR_INFO("%s location:%s end", __func__, location); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 139 | return DVR_SUCCESS; |
| 140 | } |
| 141 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 142 | int dvr_segment_get_list(const char *location, uint32_t *p_segment_nb, uint64_t **pp_segment_ids) |
| 143 | { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 144 | FILE *fp; |
| 145 | char fpath[DVR_MAX_LOCATION_SIZE]; |
Zhiqiang Han | ee5ac41 | 2020-05-07 20:54:32 +0800 | [diff] [blame] | 146 | uint32_t i = 0, j = 0, n = 0; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 147 | char buf[DVR_MAX_LOCATION_SIZE + 10]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 148 | uint64_t *p = NULL; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 149 | char cmd[DVR_MAX_LOCATION_SIZE + 64]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 150 | |
| 151 | DVR_RETURN_IF_FALSE(location); |
| 152 | DVR_RETURN_IF_FALSE(p_segment_nb); |
| 153 | DVR_RETURN_IF_FALSE(pp_segment_ids); |
| 154 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 155 | memset(fpath, 0, sizeof(fpath)); |
| 156 | sprintf(fpath, "%s.list", location); |
| 157 | |
Wentao MA | b207081 | 2022-09-02 13:10:03 +0800 | [diff] [blame] | 158 | fp = fopen(fpath, "r"); |
| 159 | if (fp != NULL) { /*the list file exists*/ |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 160 | /*get segment numbers*/ |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 161 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 162 | i++; |
| 163 | } |
| 164 | *p_segment_nb = i; |
| 165 | rewind(fp); |
| 166 | /*malloc*/ |
| 167 | p = malloc(i * sizeof(uint64_t)); |
| 168 | i = 0; |
| 169 | /*set value*/ |
| 170 | memset(buf, 0, sizeof(buf)); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 171 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 172 | p[i++] = strtoull(buf, NULL, 10); |
| 173 | memset(buf, 0, sizeof(buf)); |
| 174 | } |
| 175 | *pp_segment_ids = p; |
| 176 | fclose(fp); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 177 | DVR_INFO("%s location:%s segments:%d", __func__, location, i); |
Wentao MA | b207081 | 2022-09-02 13:10:03 +0800 | [diff] [blame] | 178 | } else { /*the list file does not exist*/ |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 179 | uint32_t id = 0; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 180 | memset(cmd, 0, sizeof(cmd)); |
Zhiqiang Han | ade34ff | 2020-04-17 16:50:59 +0800 | [diff] [blame] | 181 | sprintf(cmd, "ls -l %s-*.ts | wc -l", location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 182 | fp = popen(cmd, "r"); |
| 183 | DVR_RETURN_IF_FALSE(fp); |
| 184 | memset(buf, 0, sizeof(buf)); |
| 185 | if (fgets(buf, sizeof(buf), fp) != NULL) { |
Wentao MA | b207081 | 2022-09-02 13:10:03 +0800 | [diff] [blame] | 186 | i = strtoul(buf, NULL, 10); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 187 | pclose(fp); |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 188 | DVR_RETURN_IF_FALSE(i>0); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 189 | } else { |
| 190 | pclose(fp); |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 191 | DVR_ERROR("%s location:%s get null", __func__, location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 192 | return DVR_FAILURE; |
| 193 | } |
Zhiqiang Han | ee5ac41 | 2020-05-07 20:54:32 +0800 | [diff] [blame] | 194 | n = i; |
Wentao MA | 4b8d475 | 2022-09-14 19:03:45 +0800 | [diff] [blame] | 195 | |
Zhiqiang Han | 8dd08b3 | 2020-05-13 16:50:04 +0800 | [diff] [blame] | 196 | /*try to get the 1st segment id*/ |
| 197 | memset(cmd, 0, sizeof(cmd)); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 198 | sprintf(cmd, "ls %s-*.ts", location); |
Zhiqiang Han | 8dd08b3 | 2020-05-13 16:50:04 +0800 | [diff] [blame] | 199 | fp = popen(cmd, "r"); |
| 200 | DVR_RETURN_IF_FALSE(fp); |
| 201 | memset(buf, 0, sizeof(buf)); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 202 | j = 0; |
| 203 | snprintf(fpath, sizeof(fpath), "%s-%%d.ts", location); |
Wentao MA | 4b8d475 | 2022-09-14 19:03:45 +0800 | [diff] [blame] | 204 | |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 205 | // Tainted data issue originating from fgets seem false positive, so we |
| 206 | // just suppress it here. |
| 207 | // coverity[tainted_data] |
Wentao MA | 4b8d475 | 2022-09-14 19:03:45 +0800 | [diff] [blame] | 208 | p = malloc(n * sizeof(uint64_t)); |
Wentao MA | 4d85ff3 | 2022-09-23 11:36:18 +0800 | [diff] [blame] | 209 | if (p == NULL) { |
| 210 | DVR_ERROR("%s, Failed to allocate memory with errno:%d (%s)", |
| 211 | __func__,errno,strerror(errno)); |
| 212 | pclose(fp); |
| 213 | return DVR_FAILURE; |
| 214 | } |
Wentao MA | 4b8d475 | 2022-09-14 19:03:45 +0800 | [diff] [blame] | 215 | |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 216 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 217 | if (sscanf(buf, fpath, &id) != 1) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 218 | DVR_INFO("%s location:%s buf:%s not get id", __func__, location, buf); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 219 | id = 0; |
| 220 | n = n -1; |
| 221 | } else { |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 222 | p[j++] = id; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 223 | } |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 224 | memset(buf, 0, sizeof(buf)); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 225 | } |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 226 | DVR_DEBUG("%s location:%s n=%d j=%d end", __func__, location, n, j); |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 227 | pclose(fp); |
| 228 | *p_segment_nb = n; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 229 | *pp_segment_ids = p; |
| 230 | } |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 231 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 232 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 235 | int dvr_segment_get_info(const char *location, uint64_t segment_id, DVR_RecordSegmentInfo_t *p_info) |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 236 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 237 | int ret; |
| 238 | Segment_OpenParams_t open_params; |
| 239 | Segment_Handle_t segment_handle; |
| 240 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 241 | DVR_RETURN_IF_FALSE(location); |
| 242 | DVR_RETURN_IF_FALSE(p_info); |
| 243 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 244 | |
| 245 | memset(&open_params, 0, sizeof(open_params)); |
| 246 | memcpy(open_params.location, location, strlen(location)); |
| 247 | open_params.segment_id = segment_id; |
| 248 | open_params.mode = SEGMENT_MODE_READ; |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 249 | |
| 250 | // Previous location strlen checking againest DVR_MAX_LOCATION_SIZE and |
wentao.ma | fd5283f | 2022-10-14 09:51:13 +0800 | [diff] [blame] | 251 | // latter memset on open_params ensure that open_params.location is |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 252 | // null-terminated, so the Coverity STRING_NULL error is suppressed here. |
| 253 | // coverity[string_null] |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 254 | ret = segment_open(&open_params, &segment_handle); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 255 | if (ret == DVR_SUCCESS) { |
| 256 | ret = segment_load_info(segment_handle, p_info); |
Wentao MA | 07d3d74 | 2022-09-06 09:58:05 +0800 | [diff] [blame] | 257 | if (ret != DVR_SUCCESS) { |
| 258 | DVR_ERROR("segment_load_info failed with return value %d",ret); |
| 259 | } |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 260 | } |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 261 | DVR_DEBUG("%s, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 262 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
hualing chen | b224235 | 2021-05-18 17:16:16 +0800 | [diff] [blame] | 263 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 264 | ret = segment_close(segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 265 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 266 | |
| 267 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 268 | } |
| 269 | |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 270 | int dvr_segment_get_allInfo(const char *location, struct list_head *list) |
| 271 | { |
| 272 | int ret; |
| 273 | Segment_OpenParams_t open_params; |
| 274 | Segment_Handle_t segment_handle; |
| 275 | |
| 276 | DVR_RETURN_IF_FALSE(location); |
| 277 | DVR_RETURN_IF_FALSE(list); |
| 278 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
| 279 | |
| 280 | memset(&open_params, 0, sizeof(open_params)); |
| 281 | memcpy(open_params.location, location, strlen(location)); |
| 282 | open_params.segment_id = 0; |
| 283 | open_params.mode = SEGMENT_MODE_READ; |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 284 | |
| 285 | // Previous location strlen checking againest DVR_MAX_LOCATION_SIZE and |
wentao.ma | fd5283f | 2022-10-14 09:51:13 +0800 | [diff] [blame] | 286 | // latter memset on open_params ensure that open_params.location is |
wentao.ma | f57dd23 | 2022-10-08 16:07:29 +0800 | [diff] [blame] | 287 | // null-terminated, so the Coverity STRING_NULL error is suppressed here. |
| 288 | // coverity[string_null] |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 289 | ret = segment_open(&open_params, &segment_handle); |
| 290 | if (ret == DVR_SUCCESS) { |
| 291 | ret = segment_load_allInfo(segment_handle, list); |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 292 | if (ret == DVR_FAILURE) { |
| 293 | segment_close(segment_handle); |
| 294 | return DVR_FAILURE; |
| 295 | } |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 296 | } |
| 297 | ret = segment_close(segment_handle); |
| 298 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 299 | |
| 300 | return DVR_SUCCESS; |
| 301 | } |
| 302 | |
| 303 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 304 | int dvr_segment_link(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids) |
| 305 | { |
Wentao MA | e8ba517 | 2022-08-09 11:18:17 +0800 | [diff] [blame] | 306 | return dvr_segment_link_op(location, nb_segments, p_segment_ids, SEGMENT_OP_NEW); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int dvr_segment_link_op(const char *location, uint32_t nb_segments, uint64_t *p_segment_ids, int op) |
| 310 | { |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 311 | FILE *fp; |
| 312 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 313 | uint32_t i; |
hualing chen | b270cfa | 2020-08-19 15:04:35 +0800 | [diff] [blame] | 314 | char buf[DVR_MAX_LOCATION_SIZE + 64]; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 315 | |
| 316 | DVR_RETURN_IF_FALSE(location); |
| 317 | DVR_RETURN_IF_FALSE(p_segment_ids); |
| 318 | DVR_RETURN_IF_FALSE(strlen((const char *)location) < DVR_MAX_LOCATION_SIZE); |
| 319 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 320 | DVR_INFO("%s op[%d] location:%s, nb_segments:%d", __func__, op, location, nb_segments); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 321 | memset(fpath, 0, sizeof(fpath)); |
| 322 | sprintf(fpath, "%s.list", location); |
Wentao MA | e8ba517 | 2022-08-09 11:18:17 +0800 | [diff] [blame] | 323 | fp = fopen(fpath, (op == SEGMENT_OP_ADD) ? "a+" : "w+"); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 324 | if (!fp) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 325 | DVR_INFO("failed to open list file, err:%d:%s", errno, strerror(errno)); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 326 | return DVR_FAILURE; |
| 327 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 328 | for (i = 0; i< nb_segments; i++) { |
| 329 | memset(buf, 0, sizeof(buf)); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 330 | sprintf(buf, "%lld\n", p_segment_ids[i]); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 331 | fwrite(buf, 1, strlen(buf), fp); |
| 332 | } |
| 333 | |
| 334 | fflush(fp); |
| 335 | fsync(fileno(fp)); |
| 336 | fclose(fp); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 337 | return DVR_SUCCESS; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 338 | } |