| 140 |
{ |
{ |
| 141 |
char data_file[FILE_PATH_LEN]; |
char data_file[FILE_PATH_LEN]; |
| 142 |
int fd; |
int fd; |
| 143 |
|
struct stat sb; |
| 144 |
|
void *p_mmap; |
| 145 |
|
size_t mmap_len; |
| 146 |
|
|
| 147 |
if (p_cache == NULL || cache_dir == NULL || p_article == NULL) |
if (p_cache == NULL || cache_dir == NULL || p_article == NULL) |
| 148 |
{ |
{ |
| 162 |
return -2; |
return -2; |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
if (read(fd, (void *)p_cache, sizeof(ARTICLE_CACHE)) == -1) |
if (fstat(fd, &sb) < 0) |
| 166 |
{ |
{ |
| 167 |
log_error("read(%s, cache) error (%d)\n", data_file, errno); |
log_error("fstat(fd) error (%d)\n", errno); |
| 168 |
close(fd); |
return -2; |
|
return -3; |
|
| 169 |
} |
} |
| 170 |
|
|
| 171 |
p_cache->p_data = malloc(p_cache->data_len + 1); |
mmap_len = (size_t)sb.st_size; |
|
if (p_cache->p_data == NULL) |
|
|
{ |
|
|
log_error("malloc(size=%ld) error OOM\n", p_cache->data_len + 1); |
|
|
close(fd); |
|
|
return -1; |
|
|
} |
|
| 172 |
|
|
| 173 |
if (read(fd, p_cache->p_data, p_cache->data_len) == -1) |
p_mmap = mmap(NULL, mmap_len, PROT_READ, MAP_SHARED, fd, 0L); |
| 174 |
|
if (p_mmap == MAP_FAILED) |
| 175 |
{ |
{ |
| 176 |
log_error("read(%s, data) error (%d)\n", data_file, errno); |
log_error("mmap(%s) error (%d)\n", data_file, errno); |
| 177 |
close(fd); |
return -2; |
|
return -3; |
|
| 178 |
} |
} |
|
p_cache->p_data[p_cache->data_len] = '\0'; |
|
| 179 |
|
|
| 180 |
if (close(fd) == -1) |
if (close(fd) == -1) |
| 181 |
{ |
{ |
| 183 |
return -2; |
return -2; |
| 184 |
} |
} |
| 185 |
|
|
| 186 |
|
memcpy((void *)p_cache, p_mmap, sizeof(ARTICLE_CACHE)); |
| 187 |
|
|
| 188 |
|
if (sizeof(ARTICLE_CACHE) + p_cache->data_len != mmap_len) |
| 189 |
|
{ |
| 190 |
|
log_error("Inconsistent length, cache_len(%ld) + data_len(%ld) != mmap_len(%ld)\n", |
| 191 |
|
sizeof(ARTICLE_CACHE), p_cache->data_len, mmap_len); |
| 192 |
|
|
| 193 |
|
if (munmap(p_mmap, mmap_len) < 0) |
| 194 |
|
{ |
| 195 |
|
log_error("munmap() error (%d)\n", errno); |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
return -3; |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
p_cache->p_mmap = p_mmap; |
| 202 |
|
p_cache->p_data = p_mmap + sizeof(ARTICLE_CACHE); |
| 203 |
|
|
| 204 |
return 0; |
return 0; |
| 205 |
} |
} |
| 206 |
|
|
| 207 |
int article_cache_unload(ARTICLE_CACHE *p_cache) |
int article_cache_unload(ARTICLE_CACHE *p_cache) |
| 208 |
{ |
{ |
| 209 |
if (p_cache == NULL || p_cache->p_data == NULL) |
if (p_cache == NULL || p_cache->p_mmap == NULL) |
| 210 |
{ |
{ |
| 211 |
log_error("article_cache_unload() NULL pointer error\n"); |
log_error("article_cache_unload() NULL pointer error\n"); |
| 212 |
return -1; |
return -1; |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
free(p_cache->p_data); |
if (munmap(p_cache->p_mmap, sizeof(ARTICLE_CACHE) + p_cache->data_len) < 0) |
| 216 |
|
{ |
| 217 |
|
log_error("munmap() error (%d)\n", errno); |
| 218 |
|
return -2; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
p_cache->p_mmap = NULL; |
| 222 |
p_cache->p_data = NULL; |
p_cache->p_data = NULL; |
| 223 |
|
|
| 224 |
return 0; |
return 0; |