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 API int storage_get_internal_memory_size(struct statvfs *buf)
246 struct statvfs_32 temp;
247 static unsigned long reserved = 0;
251 _E("input param error");
255 ret = get_memory_size(tzplatform_getenv(TZ_SYS_HOME), &temp);
256 if (ret || temp.f_bsize == 0) {
257 _E("fail to get memory size"); //LCOV_EXCL_LINE
258 return -errno; //LCOV_EXCL_LINE System Error
262 storage_info.total_size = (double)temp.f_frsize * temp.f_blocks;
263 storage_config_load(&storage_info);
264 reserved = (unsigned long)storage_info.reserved_size;
265 reserved = reserved/temp.f_bsize;
266 _I("total %4.4lf check %4.4lf reserved %4.4lf",
267 storage_info.total_size, storage_info.check_size, storage_info.reserved_size);
269 if (temp.f_bavail < reserved)
272 temp.f_bavail -= reserved;
274 memcpy(buf, &temp, sizeof(temp));
278 API int storage_get_internal_memory_size64(struct statvfs *buf)
280 static unsigned long reserved = 0;
284 _E("input param error"); //LCOV_EXCL_LINE
288 ret = statvfs(tzplatform_getenv(TZ_SYS_HOME), buf);
290 _E("fail to get memory size"); //LCOV_EXCL_LINE
291 return -errno; //LCOV_EXCL_LINE System Error
295 storage_info.total_size = (double)(buf->f_frsize * buf->f_blocks);
296 storage_config_load(&storage_info);
297 reserved = (unsigned long)storage_info.reserved_size;
298 reserved = reserved/buf->f_bsize;
299 _I("total %4.4lf check %4.4lf reserved %4.4lf",
300 storage_info.total_size, storage_info.check_size, storage_info.reserved_size);
302 if (buf->f_bavail < reserved)
305 buf->f_bavail -= reserved;
309 static int mount_check(char *path)
313 const char *table = "/etc/mtab";
316 fp = setmntent(table, "r");
319 while ((mnt = getmntent(fp))) {
320 if (!strcmp(mnt->mnt_dir, path)) {
329 static int get_external_path(char *path, size_t len)
331 return storage_ext_get_primary_mmc_path(path, len);
334 int storage_get_external_memory_size_with_path(char *path, struct statvfs *buf)
336 struct statvfs_32 temp;
340 _D("storage_get_external_memory_size");
342 _E("input param error");
347 snprintf(ext_path, sizeof(ext_path), "%s", path);
349 if (!storage_ext_is_supported()) {
350 _D("Block module is not enabled");
353 ret = get_external_path(ext_path, sizeof(ext_path));
357 _E("Failed to get external path(%d)", ret); //LCOV_EXCL_LINE
362 if (!mount_check(ext_path))
365 ret = get_memory_size(ext_path, &temp);
367 _E("fail to get memory size"); //LCOV_EXCL_LINE
368 return -errno; //LCOV_EXCL_LINE System Error
371 memcpy(buf, &temp, sizeof(temp));
375 memset(buf, 0, sizeof(struct statvfs_32));
379 int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf)
384 _D("storage_get_external_memory_size64");
386 _E("input param error");
391 snprintf(ext_path, sizeof(ext_path), "%s", path);
393 if (!storage_ext_is_supported()) {
394 _D("Block module is not enabled");
397 ret = get_external_path(ext_path, sizeof(ext_path));
401 _E("Failed to get external path(%d)", ret);
406 if (!mount_check(ext_path))
409 ret = statvfs(ext_path, buf);
411 //LCOV_EXCL_START System Error
412 _E("fail to get memory size");
420 memset(buf, 0, sizeof(struct statvfs));
424 API int storage_get_external_memory_size(struct statvfs *buf)
426 return storage_get_external_memory_size_with_path(NULL, buf);
429 API int storage_get_external_memory_size64(struct statvfs *buf)
431 return storage_get_external_memory_size64_with_path(NULL, buf);