3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 #include <sys/statvfs.h>
28 #include <tzplatform_config.h>
32 #include "storage-external.h"
34 #define MEMORY_GIGABYTE_VALUE 1073741824
35 #define MEMORY_MEGABYTE_VALUE 1048576
37 #define EXTERNAL_MEMORY_NODE "sdcard"
38 #define STORAGE_CONF_FILE "/etc/storage/libstorage.conf"
40 /* it's for 32bit file offset */
42 unsigned long int f_bsize;
43 unsigned long int f_frsize;
44 unsigned long int f_blocks;
45 unsigned long int f_bfree;
46 unsigned long int f_bavail;
47 unsigned long int f_files;
48 unsigned long int f_ffree;
49 unsigned long int f_favail;
50 unsigned long int f_fsid;
51 #ifdef _STATVFSBUF_F_UNUSED
54 unsigned long int f_flag;
55 unsigned long int f_namemax;
60 #define MAX_SECTION 64
61 #define WHITESPACE " \t"
62 #define NEWLINE "\n\r"
65 #define MATCH(a, b) (!strncmp(a, b, strlen(a)))
66 #define SET_CONF(a, b) (a = (b > 0.0 ? b : a))
74 struct storage_config_info {
80 static struct storage_config_info storage_info;
82 static inline char *trim_str(char *s)
86 s += strspn(s, WHITESPACE);
89 for (t = strchr(s, 0); t > s; t--)
90 if (!strchr(WHITESPACE, t[-1]))
96 static int config_parse(const char *file_name, int cb(struct parse_result *result,
97 void *user_data), void *user_data)
100 struct parse_result result;
101 /* use stack for parsing */
103 char section[MAX_SECTION];
104 char *start, *end, *name, *value;
105 int lineno = 0, ret = 0;
107 if (!file_name || !cb) {
113 f = fopen(file_name, "r");
115 _E("Failed to open file %s", file_name); //LCOV_EXCL_LINE
120 /* parsing line by line */
121 while (fgets(line, MAX_LINE, f) != NULL) {
125 start[strcspn(start, NEWLINE)] = '\0';
126 start = trim_str(start);
128 if (*start == COMMENT) {
130 } else if (*start == '[') {
132 end = strchr(start, ']');
133 if (!end || *end != ']') {
139 strncpy(section, start + 1, sizeof(section));
140 section[MAX_SECTION-1] = '\0';
142 /* parse name & value */
143 end = strchr(start, '=');
144 if (!end || *end != '=') {
149 name = trim_str(start);
150 value = trim_str(end + 1);
151 end = strchr(value, COMMENT);
152 if (end && *end == COMMENT) {
154 value = trim_str(value);
157 result.section = section;
159 result.value = value;
160 /* callback with parse result */
161 ret = cb(&result, user_data);
168 _D("Success to load %s", file_name);
175 _E("Failed to read %s:%d!", file_name, lineno); //LCOV_EXCL_LINE
179 static int load_config(struct parse_result *result, void *user_data)
181 static int check_size = -1;
182 struct storage_config_info *info = (struct storage_config_info *)user_data;
189 if (!MATCH(result->section, "STORAGE"))
193 value = result->value;
195 if (info->check_size > 0 && check_size < 0)
196 check_size = (storage_info.total_size < info->check_size) ? 1 : 0;
197 if (MATCH(name, "CHECK_SIZE"))
198 info->check_size = atoi(value);
199 else if (check_size == 0 && MATCH(name, "RESERVE"))
200 info->reserved_size = atoi(value);
201 else if (check_size == 1 && MATCH(name, "RESERVE_LITE"))
202 info->reserved_size = atoi(value);
207 static void storage_config_load(struct storage_config_info *info)
211 ret = config_parse(STORAGE_CONF_FILE, load_config, info);
213 _E("Failed to load %s, %d Use default value!", STORAGE_CONF_FILE, ret); //LCOV_EXCL_LINE
216 static int get_memory_size(const char *path, struct statvfs_32 *buf)
223 ret = statvfs(path, &s);
225 return -errno; //LCOV_EXCL_LINE System Error
227 memset(buf, 0, sizeof(struct statvfs_32));
229 buf->f_bsize = s.f_bsize;
230 buf->f_frsize = s.f_frsize;
231 buf->f_blocks = (unsigned long)s.f_blocks;
232 buf->f_bfree = (unsigned long)s.f_bfree;
233 buf->f_bavail = (unsigned long)s.f_bavail;
234 buf->f_files = (unsigned long)s.f_files;
235 buf->f_ffree = (unsigned long)s.f_ffree;
236 buf->f_favail = (unsigned long)s.f_favail;
237 buf->f_fsid = s.f_fsid;
238 buf->f_flag = s.f_flag;
239 buf->f_namemax = s.f_namemax;
244 /* This api is intended for binaries built with _FILE_OFFSET_BITS=32 */
245 API int storage_get_internal_memory_size(struct statvfs *buf)
247 struct statvfs_32 temp;
248 static unsigned long reserved = 0;
252 _E("input param error");
253 return STORAGE_ERROR_INVALID_PARAMETER;
256 ret = get_memory_size(tzplatform_getenv(TZ_SYS_HOME), &temp);
257 if (ret || temp.f_bsize == 0) {
258 _E("fail to get memory size %d", ret); //LCOV_EXCL_LINE
259 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
263 storage_info.total_size = (double)temp.f_frsize * temp.f_blocks;
264 storage_config_load(&storage_info);
265 reserved = (unsigned long)storage_info.reserved_size;
266 reserved = reserved/temp.f_bsize;
267 _I("total %4.4lf check %4.4lf reserved %4.4lf",
268 storage_info.total_size, storage_info.check_size, storage_info.reserved_size);
270 if (temp.f_bavail < reserved)
273 temp.f_bavail -= reserved;
275 memcpy(buf, &temp, sizeof(temp));
276 return STORAGE_ERROR_NONE;
279 /* This api is intended for binaries built with __USE_FILE_OFFSET64(_FILE_OFFSET_BITS=64) */
280 API int storage_get_internal_memory_size64(struct statvfs *buf)
282 static unsigned long reserved = 0;
286 _E("input param error"); //LCOV_EXCL_LINE
287 return STORAGE_ERROR_INVALID_PARAMETER;
290 ret = statvfs(tzplatform_getenv(TZ_SYS_HOME), buf);
292 _E("fail to get memory size"); //LCOV_EXCL_LINE
293 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
297 storage_info.total_size = (double)(buf->f_frsize * buf->f_blocks);
298 storage_config_load(&storage_info);
299 reserved = (unsigned long)storage_info.reserved_size;
300 reserved = reserved/buf->f_bsize;
301 _I("total %4.4lf check %4.4lf reserved %4.4lf",
302 storage_info.total_size, storage_info.check_size, storage_info.reserved_size);
304 if (buf->f_bavail < reserved)
307 buf->f_bavail -= reserved;
308 return STORAGE_ERROR_NONE;
311 static int mount_check(char *path)
315 const char *table = "/etc/mtab";
318 fp = setmntent(table, "r");
321 while ((mnt = getmntent(fp))) {
322 if (!strcmp(mnt->mnt_dir, path)) {
331 static int get_external_path(char *path, size_t len)
333 return storage_ext_get_primary_mmc_path(path, len);
336 /* This api is intended for binaries built with _FILE_OFFSET_BITS=32 */
337 int storage_get_external_memory_size_with_path(char *path, struct statvfs *buf)
339 struct statvfs_32 temp;
343 _D("storage_get_external_memory_size");
345 _E("input param error");
346 return STORAGE_ERROR_INVALID_PARAMETER;
350 snprintf(ext_path, sizeof(ext_path), "%s", path);
352 if (!storage_ext_is_supported()) {
353 _D("Block module is not enabled");
356 ret = get_external_path(ext_path, sizeof(ext_path));
360 _E("Failed to get external path(%d)", ret); //LCOV_EXCL_LINE
361 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
365 if (!mount_check(ext_path))
368 ret = get_memory_size(ext_path, &temp);
370 _E("fail to get memory size"); //LCOV_EXCL_LINE
371 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
374 memcpy(buf, &temp, sizeof(temp));
378 memset(buf, 0, sizeof(struct statvfs_32));
379 return STORAGE_ERROR_NONE;
382 /* This api is intended for binaries built with __USE_FILE_OFFSET64(_FILE_OFFSET_BITS=64) */
383 int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf)
388 _D("storage_get_external_memory_size64");
390 _E("input param error");
391 return STORAGE_ERROR_INVALID_PARAMETER;
395 snprintf(ext_path, sizeof(ext_path), "%s", path);
397 if (!storage_ext_is_supported()) {
398 _D("Block module is not enabled");
401 ret = get_external_path(ext_path, sizeof(ext_path));
405 _E("Failed to get external path(%d)", ret);
406 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
410 if (!mount_check(ext_path))
413 ret = statvfs(ext_path, buf);
415 //LCOV_EXCL_START System Error
416 _E("fail to get memory size");
417 return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
421 return STORAGE_ERROR_NONE;
424 memset(buf, 0, sizeof(struct statvfs));
428 API int storage_get_external_memory_size(struct statvfs *buf)
430 return storage_get_external_memory_size_with_path(NULL, buf);
433 API int storage_get_external_memory_size64(struct statvfs *buf)
435 return storage_get_external_memory_size64_with_path(NULL, buf);