Supporting Internal Media Path Compatibility
[platform/core/system/libstorage.git] / src / storage-internal.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <sys/statvfs.h>
24 #include <tzplatform_config.h>
25 #include <libmount.h>
26
27 #include "common.h"
28 #include "log.h"
29
30 #define COMPAT_DIR "/opt/usr/media"
31
32 #ifndef __USE_FILE_OFFSET64
33 int __WEAK__ storage_get_internal_memory_size(struct statvfs *buf);
34 #else
35 int __WEAK__ storage_get_internal_memory_size64(struct statvfs *buf);
36 #endif
37
38 static int internal_get_state(void)
39 {
40         return STORAGE_STATE_MOUNTED;
41 }
42
43 static int internal_get_space(unsigned long long *total, unsigned long long *available)
44 {
45         struct statvfs s;
46         int ret;
47
48 #ifndef __USE_FILE_OFFSET64
49         ret = storage_get_internal_memory_size(&s);
50 #else
51         ret = storage_get_internal_memory_size64(&s);
52 #endif
53         if (ret < 0)
54                 return -EPERM;
55
56         if (total)
57                 *total = (unsigned long long)s.f_frsize*s.f_blocks;
58         if (available)
59                 *available = (unsigned long long)s.f_bsize*s.f_bavail;
60         return 0;
61 }
62
63 static const char *internal_get_root(void)
64 {
65         struct libmnt_table *t = NULL;
66         int r = 0;
67         struct libmnt_fs *fs;
68         const char *ret;
69
70         ret = tzplatform_getenv(TZ_USER_CONTENT);
71
72         t = mnt_new_table();
73         if(!t)
74                 return ret;
75
76         r = mnt_table_parse_mtab(t, NULL);
77         if(r < 0){
78                 mnt_free_table(t);
79                 return ret;
80         }
81
82         fs = mnt_table_find_target(t,COMPAT_DIR,MNT_ITER_BACKWARD);
83         if(fs){
84                 // TODO : mnt_fs_get_root(fs) should be matched to tzplatform_getenv(TZ_USER_CONTENT).
85                 ret = COMPAT_DIR;
86         }
87
88         mnt_free_table(t);
89
90         return ret;
91 }
92
93 const struct storage_ops internal = {
94         .type = STORAGE_TYPE_INTERNAL,
95         .root = internal_get_root,
96         .get_state = internal_get_state,
97         .get_space = internal_get_space,
98         .storage_id = 0,
99 };
100
101 STORAGE_OPS_REGISTER(&internal)