00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <glib.h>
00034 #include <dbus/dbus.h>
00035 #include <dbus/dbus-glib.h>
00036
00037 #include <libhal.h>
00038 #include <libmnt/libmnt.h>
00039
00040 #include "mntd_volume_manager_helper.h"
00041 #include "mntd_volume_manager.h"
00042 #include "mntd_volume_hash.h"
00043 #include "mntd_volume_config.h"
00044 #include "mntd_volume.h"
00045 #include "mntd_hal.h"
00046 #include "mntd_file.h"
00047 #include "errmanager.h"
00048
00049
00050
00051 int mntd_volume_manager_init(PVOLUMEMANAGER pvm);
00052 void mntd_volume_manager_destroy(PVOLUMEMANAGER pvm);
00053 void mntd_volume_manager_func_free(void *userdata);
00054 void mntd_volume_manager_add_volume(PVOLUMEMANAGER pvm, const char *udi);
00055 void mntd_volume_manager_remove_volume(PVOLUMEMANAGER pvm, const char *udi);
00056 void mntd_volume_manager_new_volume_capability(PVOLUMEMANAGER pvm, const char *udi, const char *capability);
00057 void mntd_volume_manager_add_volume_property(PVOLUMEMANAGER pvm, const char *udi, const char *property);
00058 void mntd_volume_manager_remove_volume_property(PVOLUMEMANAGER pvm, const char *udi, const char *property);
00059 void mntd_volume_manager_change_volume_property(PVOLUMEMANAGER pvm, const char *udi, const char *property);
00060 void mntd_volume_manager_rescan(PVOLUMEMANAGER pvm);
00061 void mntd_volume_manager_foreach_mounted(PVOLUMEMANAGER pvm,
00062 int (*func)(void *data, void *userdata),
00063 void *userdata);
00064 char *mntd_volume_manager_get_mntpnt(PVOLUMEMANAGER pvm, const char *udi);
00065 int mntd_volume_manager_contains(PVOLUMEMANAGER pvm, const char *udi);
00066
00067
00082 PVOLUMEMANAGER new_VolumeManager(const char *config)
00083 {
00084 PVOLUMEMANAGER pvm = NULL;
00085 PHASHMAP phm = NULL;
00086 char *s_config = NULL;
00087
00088
00089 g_assert(config!=NULL);
00090
00091
00092 pvm = (PVOLUMEMANAGER) malloc(sizeof(VOLUMEMANAGER));
00093 if (pvm == NULL) {
00094 return NULL;
00095 }
00096
00097
00098 memset(pvm, 0, sizeof(VOLUMEMANAGER));
00099
00100
00101 phm = new_HashMap(DEFAULT_BUCKETS, mntd_volume_manager_func_free);
00102 if (phm == NULL) {
00103 goto error;
00104 }
00105 pvm->phm = phm;
00106
00107
00108 s_config = strdup(config);
00109 if (s_config == NULL) {
00110 goto error;
00111 }
00112 pvm->config = s_config;
00113
00114
00115 pvm->init = mntd_volume_manager_init;
00116
00117
00118 pvm->destroy = mntd_volume_manager_destroy;
00119 pvm->add_volume = mntd_volume_manager_add_volume;
00120 pvm->remove_volume = mntd_volume_manager_remove_volume;
00121 pvm->new_volume_capability = mntd_volume_manager_new_volume_capability;
00122 pvm->add_volume_property = mntd_volume_manager_add_volume_property;
00123 pvm->remove_volume_property = mntd_volume_manager_remove_volume_property;
00124 pvm->change_volume_property = mntd_volume_manager_change_volume_property;
00125 pvm->rescan = mntd_volume_manager_rescan;
00126 pvm->foreach_mounted = mntd_volume_manager_foreach_mounted;
00127 pvm->get_mntpnt = mntd_volume_manager_get_mntpnt;
00128 pvm->contains = mntd_volume_manager_contains;
00129
00130
00131 if(pvm->init(pvm) == -1) {
00132 goto error;
00133 }
00134
00135 goto exit;
00136
00137 error:
00138 pvm->destroy(pvm);
00139 pvm = NULL;
00140
00141 exit:
00142
00143 return pvm;
00144 }
00145
00146
00152 int mntd_volume_manager_init(PVOLUMEMANAGER pvm)
00153 {
00154 g_assert(pvm!=NULL);
00155
00156 return mntd_volume_config_parse(pvm->config, pvm);
00157 }
00158
00159
00164 void mntd_volume_manager_destroy(PVOLUMEMANAGER pvm)
00165 {
00166 PHASHMAP phm = NULL;
00167
00168 g_assert(pvm!=NULL);
00169
00170
00171 if (pvm->base != NULL) {
00172 free(pvm->base);
00173 pvm->base = NULL;
00174 }
00175
00176
00177 if (pvm->config != NULL) {
00178 free(pvm->config);
00179 pvm->config = NULL;
00180 }
00181
00182
00183 phm = pvm->phm;
00184 if (phm != NULL) {
00185 phm->destroy(phm);
00186 }
00187 pvm->phm = NULL;
00188
00189
00190 memset(pvm, 0, sizeof(VOLUMEMANAGER));
00191 free(pvm);
00192
00193 return;
00194 }
00195
00196
00201 void mntd_volume_manager_func_free(void *userdata)
00202 {
00203 PVOLUME pv = NULL;
00204
00205 if (userdata != NULL) {
00206
00207
00208
00209 pv = (PVOLUME) userdata;
00210 pv->destroy(pv);
00211 pv = NULL;
00212 } else {
00213
00214 }
00215
00216 return;
00217 }
00218
00219
00225 void mntd_volume_manager_add_volume(PVOLUMEMANAGER pvm, const char *udi)
00226 {
00227 PVOLUME pv = NULL;
00228 PHASHMAP phm = NULL;
00229
00230 g_assert(pvm!=NULL);
00231 g_assert(udi!=NULL);
00232 g_assert((pvm->phm)!=NULL);
00233 phm = pvm->phm;
00234
00235 if (vmh_hal_device_is_volume(udi)) {
00236
00237
00238
00239
00240 pv = new_Volume(udi, pvm->base);
00241
00242 if (pv!=NULL) {
00243 if (phm->add(phm, udi, pv) == 0) {
00244 vmh_try_n_set_volumes_data(pv, udi);
00245 } else {
00246 pv->destroy(pv);
00247 pv = NULL;
00248
00249 }
00250 }
00251 }
00252
00253 return;
00254 }
00255
00256
00262 void mntd_volume_manager_remove_volume(PVOLUMEMANAGER pvm, const char *udi)
00263 {
00264 PHASHMAP phm = NULL;
00265
00266 g_assert(pvm!=NULL);
00267 g_assert(udi!=NULL);
00268
00269 phm = pvm->phm;
00270 if (phm!=NULL) {
00271 if (phm->contains(phm, udi)) {
00272
00273
00274
00275 phm->remove(phm, udi);
00276 }
00277 } else {
00278
00279 }
00280
00281 return;
00282 }
00283
00284
00291 void mntd_volume_manager_new_volume_capability(PVOLUMEMANAGER pvm, const char *udi, const char *capability)
00292 {
00293 PHASHMAP phm = NULL;
00294 PVOLUME pv = NULL;
00295 void *data = NULL;
00296
00297 g_assert(pvm!=NULL);
00298 g_assert(udi!=NULL);
00299 g_assert((pvm->phm)!=NULL);
00300 phm = pvm->phm;
00301
00302 if (phm->get(phm, udi, &data) == 0) {
00303
00304
00305
00306 pv = (PVOLUME) data;
00307
00308 if (pv!=NULL) {
00309 vmh_try_n_set_volumes_data_by_property(pv, udi, capability);
00310 }
00311 }
00312
00313 return;
00314 }
00315
00316
00323 void mntd_volume_manager_add_volume_property(PVOLUMEMANAGER pvm, const char *udi, const char *property)
00324 {
00325 PHASHMAP phm = NULL;
00326 PVOLUME pv = NULL;
00327 void *data = NULL;
00328
00329 g_assert(pvm!=NULL);
00330 g_assert(udi!=NULL);
00331 g_assert((pvm->phm)!=NULL);
00332 phm = pvm->phm;
00333
00334 if (phm->get(phm, udi, &data) == 0) {
00335
00336
00337
00338 pv = (PVOLUME) data;
00339
00340 if (pv!=NULL) {
00341 vmh_try_n_set_volumes_data_by_property(pv, udi, property);
00342 }
00343 }
00344
00345 return;
00346 }
00347
00348
00355 void mntd_volume_manager_remove_volume_property(PVOLUMEMANAGER pvm, const char *udi, const char *property)
00356 {
00357 PHASHMAP phm = NULL;
00358 PVOLUME pv = NULL;
00359 void *data = NULL;
00360
00361 g_assert(pvm!=NULL);
00362 g_assert(udi!=NULL);
00363 g_assert((pvm->phm)!=NULL);
00364 phm = pvm->phm;
00365
00366 if (phm->get(phm, udi, &data) == 0) {
00367
00368
00369
00370 pv = (PVOLUME) data;
00371
00372 if (pv!=NULL) {
00373 vmh_try_n_set_volumes_data_by_property(pv, udi, property);
00374 }
00375 }
00376
00377 return;
00378 }
00379
00380
00387 void mntd_volume_manager_change_volume_property(PVOLUMEMANAGER pvm, const char *udi, const char *property)
00388 {
00389 PHASHMAP phm = NULL;
00390 PVOLUME pv = NULL;
00391 void *data = NULL;
00392
00393 g_assert(pvm!=NULL);
00394 g_assert(udi!=NULL);
00395 g_assert((pvm->phm)!=NULL);
00396 phm = pvm->phm;
00397
00398 if (phm->get(phm, udi, &data) == 0) {
00399
00400
00401
00402 pv = (PVOLUME) data;
00403
00404 if (pv != NULL) {
00405 vmh_try_n_set_volumes_data_by_property(pv, udi, property);
00406 }
00407 }
00408
00409 return;
00410 }
00411
00412
00417 void mntd_volume_manager_rescan(PVOLUMEMANAGER pvm)
00418 {
00419 int i = 0;
00420 int num_devices = 0;
00421 char **device_names = NULL;
00422 char *udi = NULL;
00423
00424 g_assert(pvm!=NULL);
00425
00426 device_names = hal_get_all_devices(&num_devices);
00427
00428 if (device_names == NULL) {
00429 MSG_ERR("Couldn't get devices from HAL (hald not running ?).");
00430 return;
00431 }
00432
00433 for (i=0; i<num_devices; i++) {
00434 udi = device_names[i];
00435 if(vmh_hal_device_is_safe(udi)) {
00436 if (vmh_hal_device_is_volume(udi)) {
00437 pvm->add_volume(pvm, udi);
00438 }
00439 }
00440 }
00441
00442 return;
00443 }
00444
00445
00452 void mntd_volume_manager_foreach_mounted(PVOLUMEMANAGER pvm,
00453 int (*func)(void *data, void *userdata),
00454 void *userdata)
00455 {
00456 PHASHMAP phm = NULL;
00457
00458 g_assert(pvm!=NULL);
00459 g_assert(func!=NULL);
00460 g_assert((pvm->phm)!=NULL);
00461 phm = pvm->phm;
00462
00463 phm->foreach(phm, func, userdata);
00464
00465 return;
00466 }
00467
00468
00475 char *mntd_volume_manager_get_mntpnt(PVOLUMEMANAGER pvm, const char *udi)
00476 {
00477 PHASHMAP phm = NULL;
00478 PVOLUME pv = NULL;
00479 void *data = NULL;
00480 char *mntpnt = NULL;
00481
00482 g_assert(pvm!=NULL);
00483 g_assert(udi!=NULL);
00484 g_assert((pvm->phm)!=NULL);
00485 phm = pvm->phm;
00486
00487 if (phm->get(phm, udi, &data) == 0) {
00488 pv = (PVOLUME) data;
00489 if (pv!=NULL) {
00490
00491 if (pv->is_in_mntpath(pv)) {
00492 mntpnt = pv->get_mntpnt(pv);
00493 if (mntpnt!=NULL) {
00494 return mntpnt;
00495 }
00496 }
00497 }
00498 }
00499
00500 return NULL;
00501 }
00502
00503
00510 int mntd_volume_manager_contains(PVOLUMEMANAGER pvm, const char *udi)
00511 {
00512 PHASHMAP phm = NULL;
00513
00514 g_assert(pvm!=NULL);
00515 g_assert(udi!=NULL);
00516 g_assert((pvm->phm)!=NULL);
00517 phm = pvm->phm;
00518
00519 return phm->contains(phm, udi);
00520 }
00521
00522