Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

mntd_volume_manager.c

00001 /***************************************************************************
00002  * CVSID: $Id: mntd_volume_manager.c,v 1.38 2004/12/18 23:38:53 stefanb Exp $
00003  *
00004  * mntd_volume_manager.c : Volume Manager handler for MNT daemon
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <sbambach@gmx.net>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 <libmnt/libmnt.h>   /* for common defines etc. */
00038 
00039 #include "mntd_volume_manager.h"
00040 #include "mntd_volume_hash.h"
00041 #include "mntd_volume_config.h"
00042 #include "mntd_volume.h"
00043 #include "mntd_mount.h"
00044 #include "errmanager.h"
00045 #include "dbug_mem.h"
00046 
00047 
00048 /* Private Methods */
00049 int mntd_volume_manager_init(PVOLUMEMANAGER pvm);
00050 void mntd_volume_manager_destroy(PVOLUMEMANAGER pvm);
00051 void mntd_volume_manager_func_free(void *userdata);
00052 void mntd_volume_manager_add_volume(PVOLUMEMANAGER pvm, const char *udi);
00053 void mntd_volume_manager_remove_volume(PVOLUMEMANAGER pvm, const char *udi);
00054 void mntd_volume_manager_rescan(PVOLUMEMANAGER pvm);
00055 void mntd_volume_manager_foreach_mounted(PVOLUMEMANAGER pvm, 
00056                                                 int (*func)(void *data, void *userdata), 
00057                                                 void *userdata);
00058 char *mntd_volume_manager_get_mntpnt(PVOLUMEMANAGER pvm, const char *udi);
00059 int mntd_volume_manager_contains(PVOLUMEMANAGER pvm, const char *udi);
00060 int mntd_volume_manager_remount(PVOLUMEMANAGER pvm, const char *udi, int flag);
00061 
00062 
00078 PVOLUMEMANAGER new_VolumeManager(const char *base, const char *prefix)
00079 {
00080     PVOLUMEMANAGER pvm = NULL;
00081     PHASHMAP phm = NULL;
00082     char *s_base = NULL;
00083     char *s_prefix = NULL;
00084     
00085     // check for error
00086     g_assert(base!=NULL);
00087     g_assert(prefix!=NULL);
00088     
00089     // get mem for volume object
00090     pvm = (PVOLUMEMANAGER) malloc(sizeof(VOLUMEMANAGER));
00091     if (pvm == NULL) {
00092         return NULL;
00093     }
00094     
00095     // clear structure memory
00096     memset(pvm, 0, sizeof(VOLUMEMANAGER));
00097     
00098     // generate hashmap
00099     phm = new_HashMap(DEFAULT_BUCKETS, mntd_volume_manager_func_free);
00100     if (phm == NULL) {
00101         goto error;
00102     }
00103     pvm->phm = phm;
00104     
00105     // setting basepath
00106     s_base = strdup(base);
00107     if (s_base == NULL) {
00108         goto error;
00109     }
00110     pvm->base = s_base;
00111     
00112     // setting basepath
00113     s_prefix = strdup(prefix);
00114     if (s_prefix == NULL) {
00115         goto error;
00116     }
00117     pvm->prefix = s_prefix;
00118     
00119     // private methods
00120     pvm->init = mntd_volume_manager_init;
00121     
00122     // public methods
00123     pvm->destroy = mntd_volume_manager_destroy;
00124     pvm->add_volume = mntd_volume_manager_add_volume;
00125     pvm->remove_volume = mntd_volume_manager_remove_volume;
00126     pvm->rescan = mntd_volume_manager_rescan;
00127     pvm->foreach_mounted = mntd_volume_manager_foreach_mounted;
00128     pvm->get_mntpnt = mntd_volume_manager_get_mntpnt;
00129     pvm->contains = mntd_volume_manager_contains;
00130     pvm->remount = mntd_volume_manager_remount;
00131     
00132     // call constructor
00133     if(pvm->init(pvm) == -1) {
00134         goto error;
00135     }
00136     
00137     goto exit;
00138     
00139 error:
00140     pvm->destroy(pvm);
00141     pvm = NULL;
00142     
00143 exit:
00144     // return object
00145     return pvm;
00146 }
00147 
00148 
00154 int mntd_volume_manager_init(PVOLUMEMANAGER pvm)
00155 {
00156     g_assert(pvm!=NULL);
00157     
00158     return 0;
00159 }
00160 
00161 
00166 void mntd_volume_manager_destroy(PVOLUMEMANAGER pvm)
00167 {
00168     PHASHMAP phm = NULL;
00169     
00170     g_assert(pvm!=NULL);
00171     
00172     // destroy base path name
00173     if (pvm->base != NULL) {
00174         free(pvm->base);
00175         pvm->base = NULL;
00176     }
00177 
00178     // destroy base path prefix
00179     if (pvm->prefix != NULL) {
00180         free(pvm->prefix);
00181         pvm->prefix = NULL;
00182     }
00183 
00184     // destroy hash
00185     phm = pvm->phm;
00186     if (phm != NULL) {
00187         phm->destroy(phm);
00188     }
00189     pvm->phm = NULL;
00190     
00191     // free volume manager structure
00192     memset(pvm, 0, sizeof(VOLUMEMANAGER));
00193     free(pvm);
00194     
00195     return;
00196 }
00197 
00198 
00203 void mntd_volume_manager_func_free(void *userdata)
00204 {
00205     PVOLUME pv = NULL;
00206     
00207     if (userdata != NULL) {
00208         
00209         //MSG_DEBUG("----------==========  DESTROYING VOLUME OBJECT  ==========----------");
00210         
00211         pv = (PVOLUME) userdata;
00212         pv->destroy(pv);
00213         pv = NULL;
00214     } else {
00215         //MSG_ERR("No volume object to be freed given");
00216     }
00217     
00218     return;
00219 }
00220 
00221 
00227 void mntd_volume_manager_add_volume(PVOLUMEMANAGER pvm, const char *udi)
00228 {
00229     PVOLUME pv = NULL;
00230     PHASHMAP phm = NULL;
00231     
00232     g_assert(pvm!=NULL);
00233     g_assert(udi!=NULL);
00234     g_assert((pvm->phm)!=NULL);
00235     phm = pvm->phm;
00236 
00237     //MSG_DEBUG("----------==========  ADD VOLUME  ==========---------- (%s)", udi);
00238 
00239     // generate new volume object
00240     pv = new_Volume(udi, pvm->base, pvm->prefix);
00241     
00242     if (pv!=NULL) {
00243         if (phm->add(phm, udi, pv) == 0) {
00244             if ((pv->set_device(pv, udi)) == -1) {
00245                 MSG_WARNING("Couldn't set device '%s' for '%s'", udi, udi);
00246                 return;
00247             }
00248         } else {
00249             pv->destroy(pv);
00250             pv = NULL;
00251             //MSG_ERR("Couldn't add Volume (%s) to Manager", udi);
00252         }
00253     }
00254     
00255     return;
00256 }
00257 
00258 
00264 void mntd_volume_manager_remove_volume(PVOLUMEMANAGER pvm, const char *udi)
00265 {
00266     PHASHMAP phm = NULL;
00267     
00268     g_assert(pvm!=NULL);
00269     g_assert(udi!=NULL);
00270     
00271     phm = pvm->phm;
00272     if (phm!=NULL) {
00273         if (phm->contains(phm, udi)) {
00274             
00275             //MSG_DEBUG("----------==========  REMOVE VOLUME  ==========---------- (%s)", udi);
00276         
00277             phm->remove(phm, udi);
00278         }
00279     } else {
00280         //MSG_ERR("Hashmap pointer is NULL -> Cannot remove volume");
00281     }
00282     
00283     return;
00284 }
00285 
00286 
00291 void mntd_volume_manager_rescan(PVOLUMEMANAGER pvm)
00292 {
00293     int res = -1;
00294     res = system(MNTRESCAN_BIN_PATH);
00295     if (res != 0) {
00296         MSG_ERR("'" MNTRESCAN_BIN_PATH "' failed !");
00297     }
00298     return;
00299 }
00300 
00301 
00308 void mntd_volume_manager_foreach_mounted(PVOLUMEMANAGER pvm, 
00309                                                 int (*func)(void *data, void *userdata), 
00310                                                 void *userdata)
00311 {
00312     PHASHMAP phm = NULL;
00313     
00314     g_assert(pvm!=NULL);
00315     g_assert(func!=NULL);
00316     g_assert((pvm->phm)!=NULL);
00317     phm = pvm->phm;
00318     
00319     phm->foreach(phm, func, userdata);
00320     
00321     return;
00322 }
00323 
00324 
00331 char *mntd_volume_manager_get_mntpnt(PVOLUMEMANAGER pvm, const char *udi)
00332 {
00333     PHASHMAP phm = NULL;
00334     PVOLUME pv = NULL;
00335     void *data = NULL;
00336     char *mntpnt = NULL;
00337     
00338     g_assert(pvm!=NULL);
00339     g_assert(udi!=NULL);
00340     g_assert((pvm->phm)!=NULL);
00341     phm = pvm->phm;
00342     
00343     if (phm->get(phm, udi, &data) == 0) {
00344         pv = (PVOLUME) data;
00345         if (pv!=NULL) {
00346             // check for mntd handled mntpnt
00347             if (pv->is_in_mntpath(pv)) {
00348                 mntpnt = pv->get_mntpnt(pv);
00349                 if (mntpnt!=NULL) {
00350                     return mntpnt;
00351                 }
00352             }
00353         }
00354     }
00355     
00356     return NULL;
00357 }
00358 
00359 
00366 int mntd_volume_manager_contains(PVOLUMEMANAGER pvm, const char *udi)
00367 {
00368     PHASHMAP phm = NULL;
00369     
00370     g_assert(pvm!=NULL);
00371     g_assert(udi!=NULL);
00372     g_assert((pvm->phm)!=NULL);
00373     phm = pvm->phm;
00374     
00375     return phm->contains(phm, udi);
00376 }
00377 
00378 
00386 int mntd_volume_manager_remount(PVOLUMEMANAGER pvm, const char *udi, int flag)
00387 {
00388     PHASHMAP phm = NULL;
00389     PVOLUME pv = NULL;
00390     void *data = NULL;
00391     int res = -1;
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         pv = (PVOLUME) data;
00400         if (pv!=NULL) {
00401             // check for mntd handled mntpnt
00402             if (pv->is_in_mntpath(pv)) {
00403                 res = pv->remount(pv, flag);
00404             }
00405         }
00406     }
00407     
00408     return res;
00409 }
00410 
00411 

Generated on Wed Mar 30 13:43:27 2005 for Mntd by  doxygen 1.3.9.1