2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <app_storage.h>
28 #include <app_storage_private.h>
34 #define LOG_TAG "CAPI_APPFW_APPLICATION_STORAGE"
36 static int storage_initialize();
37 static int storage_register_device(storage_device_h device);
38 static int storage_get_storage(int id, storage_info_h* storage_info);
40 extern storage_device_h storage_internal_device();
41 extern storage_device_h storage_sdcard_device();
42 extern storage_device_h storage_usbhost_device();
44 extern int storage_internal_get_state();
45 extern int storage_sdcard_get_state();
46 extern int storage_usbhost_get_state();
49 static struct storage_info_s storage_info_table[STORAGE_MAX];
50 static int storage_num = 0;
52 static int storage_register_device(storage_device_h device)
59 if (storage_num >= STORAGE_MAX)
61 LOGE("failed to register device : not enough device table");
65 storage_info_table[storage_num].id = storage_num;
66 storage_info_table[storage_num].device = device;
67 storage_info_table[storage_num].state = device->get_state();
68 storage_info_table[storage_num].state_cb = NULL;
69 storage_info_table[storage_num].state_cb_data = NULL;
76 static int storage_initialize()
78 storage_device_h dev_internal;
79 storage_device_h dev_sdcard;
80 storage_device_h dev_usbhost;
82 dev_internal = storage_internal_device();
83 storage_register_device(dev_internal);
85 dev_sdcard = storage_sdcard_device();
86 storage_register_device(dev_sdcard);
88 dev_usbhost = storage_usbhost_device();
89 storage_register_device(dev_usbhost);
94 static int storage_get_storage(int id, storage_info_h* storage_info)
96 int device_state = -1;
100 if (storage_initialize() != 0)
102 return STORAGE_ERROR_NOT_SUPPORTED;
106 if (id < 0 || id >= storage_num)
108 return STORAGE_ERROR_NOT_SUPPORTED;
114 device_state = storage_internal_get_state();
117 device_state = storage_sdcard_get_state();
120 device_state = storage_usbhost_get_state();
123 LOGE("Device statei is invalid");
126 storage_info_table[id].state = device_state;
127 *storage_info = &(storage_info_table[id]);
129 return STORAGE_ERROR_NONE;
132 int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
135 storage_info_h storage_info = NULL;
136 bool foreach_next = false;
138 if (callback == NULL)
140 LOGE("INVALID_PARAMETER(0x%08x) : invalid callback", STORAGE_ERROR_INVALID_PARAMETER);
141 return STORAGE_ERROR_INVALID_PARAMETER;
146 if (storage_get_storage(storage_id, &storage_info) != 0)
153 foreach_next = callback(storage_info->id, storage_info->device->type, storage_info->state, storage_info->device->path, user_data);
155 if (foreach_next == false)
161 return STORAGE_ERROR_NONE;
165 int storage_get_root_directory(int storage, char **path)
167 storage_info_h storage_info;
171 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", STORAGE_ERROR_INVALID_PARAMETER);
172 return STORAGE_ERROR_INVALID_PARAMETER;
175 if (storage_get_storage(storage, &storage_info) != 0)
177 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
178 return STORAGE_ERROR_NOT_SUPPORTED;
181 *path = strdup(storage_info->device->path);
183 return STORAGE_ERROR_NONE;
187 int storage_get_type(int storage, storage_type_e *type)
189 storage_info_h storage_info;
193 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", STORAGE_ERROR_INVALID_PARAMETER);
194 return STORAGE_ERROR_INVALID_PARAMETER;
197 if (storage_get_storage(storage, &storage_info) != 0)
199 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
200 return STORAGE_ERROR_NOT_SUPPORTED;
203 *type = storage_info->device->type;
205 return STORAGE_ERROR_NONE;
209 int storage_get_state(int storage, storage_state_e *state)
211 storage_info_h storage_info;
212 storage_dev_get_state get_state;
216 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", STORAGE_ERROR_INVALID_PARAMETER);
217 return STORAGE_ERROR_INVALID_PARAMETER;
220 if (storage_get_storage(storage, &storage_info) != 0)
222 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
223 return STORAGE_ERROR_NOT_SUPPORTED;
226 get_state = storage_info->device->get_state;
228 if (get_state == NULL)
230 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
231 return STORAGE_ERROR_NOT_SUPPORTED;
234 storage_info->state = get_state();
236 *state = storage_info->state;
238 return STORAGE_ERROR_NONE;
242 void storage_dispatch_state_event(storage_state_e state, void* data)
244 storage_info_h storage_info;
245 storage_state_changed_cb state_cb;
249 if (storage_info == NULL)
251 LOGE("INVALID_PARAMETER(0x%08x) : invalid storage information", STORAGE_ERROR_INVALID_PARAMETER);
255 storage_info->state = state;
256 state_cb = storage_info->state_cb;
258 if (state_cb != NULL)
260 state_cb(storage_info->id, state, storage_info->state_cb_data);
265 int storage_set_state_changed_cb(int storage, storage_state_changed_cb callback, void *user_data)
267 storage_info_h storage_info;
268 storage_dev_set_state_cb set_state_cb;
270 if (callback == NULL)
272 LOGE("INVALID_PARAMETER(0x%08x) : invalid callback", STORAGE_ERROR_INVALID_PARAMETER);
273 return STORAGE_ERROR_INVALID_PARAMETER;
276 if (storage_get_storage(storage, &storage_info) != 0)
278 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
279 return STORAGE_ERROR_NOT_SUPPORTED;
282 storage_info->state_cb = callback;
283 storage_info->state_cb_data = user_data;
285 set_state_cb = storage_info->device->set_state_cb;
287 if (set_state_cb == NULL)
289 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
290 return STORAGE_ERROR_NOT_SUPPORTED;
293 if (set_state_cb(storage_info) != 0)
295 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
296 return STORAGE_ERROR_NOT_SUPPORTED;
299 return STORAGE_ERROR_NONE;
303 int storage_unset_state_changed_cb(int storage)
305 storage_info_h storage_info;
306 storage_dev_unset_state_cb unset_state_cb;
308 if (storage_get_storage(storage, &storage_info) != 0)
310 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
311 return STORAGE_ERROR_NOT_SUPPORTED;
314 storage_info->state_cb = NULL;
315 unset_state_cb = storage_info->device->unset_state_cb;
317 if (unset_state_cb != NULL)
322 return STORAGE_ERROR_NONE;
326 int storage_get_total_space(int storage, unsigned long long *bytes)
328 storage_info_h storage_info;
329 storage_dev_get_space get_space;
333 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", STORAGE_ERROR_INVALID_PARAMETER);
334 return STORAGE_ERROR_INVALID_PARAMETER;
337 if (storage_get_storage(storage, &storage_info) != 0)
339 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
340 return STORAGE_ERROR_NOT_SUPPORTED;
343 get_space = storage_info->device->get_space;
345 if (get_space == NULL)
347 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
348 return STORAGE_ERROR_NOT_SUPPORTED;
351 if (get_space(bytes, NULL) != 0)
353 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
354 return STORAGE_ERROR_NOT_SUPPORTED;
357 return STORAGE_ERROR_NONE;
360 int storage_get_available_space(int storage, unsigned long long *bytes)
362 storage_info_h storage_info;
363 storage_dev_get_space get_space;
367 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", STORAGE_ERROR_INVALID_PARAMETER);
368 return STORAGE_ERROR_INVALID_PARAMETER;
371 if (storage_get_storage(storage, &storage_info) != 0)
373 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
374 return STORAGE_ERROR_NOT_SUPPORTED;
377 get_space = storage_info->device->get_space;
379 if (get_space == NULL)
381 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
382 return STORAGE_ERROR_NOT_SUPPORTED;
385 if (get_space(NULL, bytes) != 0)
387 LOGE("NOT_SUPPORTED(0x%08x) : storage(%d)", STORAGE_ERROR_NOT_SUPPORTED, storage);
388 return STORAGE_ERROR_NOT_SUPPORTED;
391 return STORAGE_ERROR_NONE;
394 int storage_statfs(const char *directory, unsigned long long *total, unsigned long long *available)
398 if (statfs(directory, &fs) < 0)
400 LOGE("statfs returns error(%d) directory(%s)\n", errno, directory);
406 *total = (unsigned long long)fs.f_bsize * (unsigned long long)fs.f_blocks;
409 if (available != NULL)
411 *available = (unsigned long long)fs.f_bsize * (unsigned long long)fs.f_bavail;