| 1 |
/* |
| 2 |
* This file is part of libbdplus |
| 3 |
* Copyright (C) 2013 VideoLAN |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library. If not, see |
| 17 |
* <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef HAVE_CONFIG_H |
| 21 |
# include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "configfile.h" |
| 25 |
|
| 26 |
#include "file.h" |
| 27 |
#include "dirs.h" |
| 28 |
#include "util/logging.h" |
| 29 |
#include "util/macro.h" |
| 30 |
#include "util/strutl.h" |
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
|
| 37 |
#define MIN_FILE_SIZE 1 |
| 38 |
#define MAX_FILE_SIZE 0xffffff |
| 39 |
|
| 40 |
|
| 41 |
char *file_get_cache_dir(void) |
| 42 |
{ |
| 43 |
char *cache = file_get_cache_home(); |
| 44 |
char *dir; |
| 45 |
|
| 46 |
if (!cache) { |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
|
| 50 |
dir = str_printf("%s/%s/", cache, BDPLUS_DIR); |
| 51 |
X_FREE(cache); |
| 52 |
file_mkdirs(dir); |
| 53 |
|
| 54 |
return dir; |
| 55 |
} |
| 56 |
|
| 57 |
static char *_probe_config_dir(const char *base, const char *vm, const char *file) |
| 58 |
{ |
| 59 |
char *dir = str_printf("%s/%s/%s/%s", base, BDPLUS_DIR, vm, file); |
| 60 |
BDPLUS_FILE_H *fp; |
| 61 |
|
| 62 |
if (!dir) { |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
fp = file_open_default()(NULL, dir); |
| 67 |
|
| 68 |
if (fp) { |
| 69 |
file_close(fp); |
| 70 |
*(strrchr(dir, '/') + 1) = 0; |
| 71 |
BD_DEBUG(DBG_BDPLUS, "Found VM config from %s\n", dir); |
| 72 |
return dir; |
| 73 |
} |
| 74 |
|
| 75 |
BD_DEBUG(DBG_BDPLUS, "VM config not found from %s\n", dir); |
| 76 |
free(dir); |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
|
| 80 |
char *file_get_config_dir(const char *file) |
| 81 |
{ |
| 82 |
char *dir = NULL; |
| 83 |
const char *vm; |
| 84 |
char *config_home; |
| 85 |
const char *base; |
| 86 |
|
| 87 |
vm = getenv("BDPLUS_VM_ID"); |
| 88 |
if (!vm) { |
| 89 |
vm = "vm0"; |
| 90 |
} |
| 91 |
|
| 92 |
/* try home directory */ |
| 93 |
config_home = file_get_config_home(); |
| 94 |
if (!config_home) { |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
dir = _probe_config_dir(config_home, vm, file); |
| 98 |
X_FREE(config_home); |
| 99 |
if (dir) { |
| 100 |
return dir; |
| 101 |
} |
| 102 |
|
| 103 |
/* try system config dirs */ |
| 104 |
base = file_get_config_system(NULL); |
| 105 |
while (base) { |
| 106 |
dir = _probe_config_dir(base, vm, file); |
| 107 |
if (dir) { |
| 108 |
return dir; |
| 109 |
} |
| 110 |
base = file_get_config_system(base); |
| 111 |
} |
| 112 |
|
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
static char *_load_fp(BDPLUS_FILE_H *fp, uint32_t *p_size) |
| 117 |
{ |
| 118 |
char *data = NULL; |
| 119 |
int64_t size, read_size; |
| 120 |
|
| 121 |
size = file_size(fp); |
| 122 |
|
| 123 |
if (size < MIN_FILE_SIZE || size > MAX_FILE_SIZE) { |
| 124 |
BD_DEBUG(DBG_FILE, "Invalid file size\n"); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
data = malloc(size + 1); |
| 129 |
if (!data) { |
| 130 |
BD_DEBUG(DBG_FILE, "Out of memory\n"); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
read_size = file_read(fp, (void *)data, size); |
| 135 |
|
| 136 |
if (read_size != size) { |
| 137 |
BD_DEBUG(DBG_FILE, "Error reading file\n"); |
| 138 |
free(data); |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
data[size] = 0; |
| 143 |
|
| 144 |
if (p_size) { |
| 145 |
*p_size = size; |
| 146 |
} |
| 147 |
|
| 148 |
return data; |
| 149 |
} |
| 150 |
|
| 151 |
char *file_load(const char *path, uint32_t *p_size) |
| 152 |
{ |
| 153 |
char *mem; |
| 154 |
BDPLUS_FILE_H *fp; |
| 155 |
|
| 156 |
if (!path) { |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
fp = file_open_default()(NULL, path); |
| 161 |
|
| 162 |
if (!fp) { |
| 163 |
BD_DEBUG(DBG_FILE | DBG_CRIT, "Error loading %s\n", path); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
mem = _load_fp(fp, p_size); |
| 168 |
|
| 169 |
file_close(fp); |
| 170 |
|
| 171 |
return mem; |
| 172 |
} |