diff --git a/src/system/fsmounter.cpp b/src/system/fsmounter.cpp index 2f920f10c..d10b8216d 100644 --- a/src/system/fsmounter.cpp +++ b/src/system/fsmounter.cpp @@ -54,12 +54,12 @@ pthread_cond_t g_cond; pthread_t g_mnt; int g_mntstatus; -void *mount_thread(void* cmd) +void *mount_thread(void *cmd) { int ret; - ret=system((const char *) cmd); + ret = system((const char *) cmd); pthread_mutex_lock(&g_mut); - g_mntstatus=ret; + g_mntstatus = ret; pthread_cond_broadcast(&g_cond); pthread_mutex_unlock(&g_mut); pthread_exit(NULL); @@ -69,18 +69,18 @@ CFSMounter::CFSMounter() { } -bool in_proc_filesystems(const char * const fsname) +bool in_proc_filesystems(const char *const fsname) { std::string s; std::string t; std::ifstream in("/proc/filesystems", std::ifstream::in); t = fsname; - + while (in >> s) { if (s == t) - { + { in.close(); return true; } @@ -123,7 +123,7 @@ bool remove_modules(const CFSMounter::FSType fstype) CFSMounter::FS_Support CFSMounter::fsSupported(const CFSMounter::FSType fstype, const bool keep_modules) { - const char * fsname = NULL; + const char *fsname = NULL; if (fstype == CFSMounter::NFS) fsname = "nfs"; @@ -161,18 +161,19 @@ bool CFSMounter::isMounted(const std::string &local_dir) std::ifstream in; if (local_dir.empty()) return false; - + #ifdef PATH_MAX char mount_point[PATH_MAX]; #else char mount_point[4096]; #endif - if (realpath(local_dir.c_str(), mount_point) == NULL) { - printf("[CFSMounter] could not resolve dir: %s: %s\n",local_dir.c_str(), strerror(errno)); + if (realpath(local_dir.c_str(), mount_point) == NULL) + { + printf("[CFSMounter] could not resolve dir: %s: %s\n", local_dir.c_str(), strerror(errno)); return false; } in.open("/proc/mounts", std::ifstream::in); - while(in.good()) + while (in.good()) { MountInfo mi; in >> mi.device >> mi.mountPoint >> mi.type; @@ -181,8 +182,8 @@ bool CFSMounter::isMounted(const std::string &local_dir) if (mi.type == "tmpfs") continue; - if (strcmp(mi.mountPoint.c_str(),mount_point) == 0) - { + if (strcmp(mi.mountPoint.c_str(), mount_point) == 0) + { return true; } in.ignore(std::numeric_limits::max(), '\n'); @@ -197,7 +198,7 @@ CFSMounter::MountRes CFSMounter::mount(const std::string &ip, const std::string std::string cmd; pthread_mutex_init(&g_mut, NULL); pthread_cond_init(&g_cond, NULL); - g_mntstatus=-1; + g_mntstatus = -1; FS_Support sup = fsSupported(fstype, true); /* keep modules if necessary */ @@ -211,39 +212,39 @@ CFSMounter::MountRes CFSMounter::mount(const std::string &ip, const std::string CFileHelpers fh; fh.createDir(local_dir.c_str(), 0755); - + if (isMounted(local_dir)) { printf("[CFSMounter] FS mount error %s already mounted\n", local_dir.c_str()); return MRES_FS_ALREADY_MOUNTED; } - if(options1.empty()) + if (options1.empty()) { options1 = options2; options2 = ""; } - - if(options1.empty() && options2.empty()) + + if (options1.empty() && options2.empty()) { - if(fstype == NFS) + if (fstype == NFS) { options1 = "soft"; //NI options2 = "nolock"; //NI } - else if(fstype == CIFS) + else if (fstype == CIFS) { options1 = "ro"; options2 = ""; } - else if(fstype == LUFS) + else if (fstype == LUFS) { options1 = ""; options2 = ""; } } - - if(fstype == NFS) + + if (fstype == NFS) { cmd = "mount -t nfs "; cmd += ip; @@ -254,7 +255,7 @@ CFSMounter::MountRes CFSMounter::mount(const std::string &ip, const std::string cmd += " -o "; cmd += options1; } - else if(fstype == CIFS) + else if (fstype == CIFS) { cmd = "mount -t cifs //"; cmd += ip; @@ -288,15 +289,15 @@ CFSMounter::MountRes CFSMounter::mount(const std::string &ip, const std::string cmd += ','; cmd += options1; } - - if (options2[0] !='\0') + + if (options2[0] != '\0') { cmd += ','; cmd += options2; } - + pthread_create(&g_mnt, 0, mount_thread, (void *) cmd.c_str()); - + struct timespec timeout; int retcode; @@ -304,13 +305,14 @@ CFSMounter::MountRes CFSMounter::mount(const std::string &ip, const std::string timeout.tv_sec = time(NULL) + 5; timeout.tv_nsec = 0; retcode = pthread_cond_timedwait(&g_cond, &g_mut, &timeout); - if (retcode == ETIMEDOUT) - { // timeout occurred + if (retcode == ETIMEDOUT) + { + // timeout occurred pthread_cancel(g_mnt); } pthread_mutex_unlock(&g_mut); pthread_join(g_mnt, NULL); - if ( g_mntstatus != 0 ) + if (g_mntstatus != 0) { printf("[CFSMounter] FS mount error: \"%s\"\n", cmd.c_str()); return (retcode == ETIMEDOUT) ? MRES_TIMEOUT : MRES_UNKNOWN; @@ -321,21 +323,21 @@ CFSMounter::MountRes CFSMounter::mount(const std::string &ip, const std::string bool CFSMounter::automount() { - bool res = true; - for(int i = 0; i < NETWORK_NFS_NR_OF_ENTRIES; i++) + bool res = true; + for (int i = 0; i < NETWORK_NFS_NR_OF_ENTRIES; i++) { - if(g_settings.network_nfs[i].automount) + if (g_settings.network_nfs[i].automount) { res = (MRES_OK == mount(g_settings.network_nfs[i].ip, g_settings.network_nfs[i].dir, g_settings.network_nfs[i].local_dir, - (FSType) g_settings.network_nfs[i].type, g_settings.network_nfs[i].username, - g_settings.network_nfs[i].password, g_settings.network_nfs[i].mount_options1, - g_settings.network_nfs[i].mount_options2)) && res; + (FSType) g_settings.network_nfs[i].type, g_settings.network_nfs[i].username, + g_settings.network_nfs[i].password, g_settings.network_nfs[i].mount_options1, + g_settings.network_nfs[i].mount_options2)) && res; } } return res; } -CFSMounter::UMountRes CFSMounter::umount(const char * const dir) +CFSMounter::UMountRes CFSMounter::umount(const char *const dir) { UMountRes res = UMRES_OK; if (dir != NULL) @@ -349,16 +351,16 @@ CFSMounter::UMountRes CFSMounter::umount(const char * const dir) { MountInfo mi; std::ifstream in("/proc/mounts", std::ifstream::in); - while(in.good()) + while (in.good()) { in >> mi.device >> mi.mountPoint >> mi.type; in.ignore(std::numeric_limits::max(), '\n'); - if(strcmp(mi.type.c_str(),"nfs")==0 && strcmp(mi.mountPoint.c_str(),"/")==0) + if (strcmp(mi.type.c_str(), "nfs") == 0 && strcmp(mi.mountPoint.c_str(), "/") == 0) { - if (umount2(mi.mountPoint.c_str(),MNT_FORCE) != 0) + if (umount2(mi.mountPoint.c_str(), MNT_FORCE) != 0) { - printf("[CFSMounter] Error umounting %s\n",mi.device.c_str()); + printf("[CFSMounter] Error umounting %s\n", mi.device.c_str()); res = UMRES_ERR; } } @@ -369,22 +371,20 @@ CFSMounter::UMountRes CFSMounter::umount(const char * const dir) return res; } -void CFSMounter::getMountedFS(MountInfos& info) +void CFSMounter::getMountedFS(MountInfos &info) { std::ifstream in("/proc/mounts", std::ifstream::in); - while(in.good()) + while (in.good()) { MountInfo mi; in >> mi.device >> mi.mountPoint >> mi.type; in.ignore(std::numeric_limits::max(), '\n'); - if (mi.type == "nfs" || - mi.type == "cifs" || - mi.type == "lufs") + if (mi.type == "nfs" || mi.type == "cifs" || mi.type == "lufs") { info.push_back(mi); printf("[CFSMounter] mounted fs: dev: %s, mp: %s, type: %s\n", - mi.device.c_str(),mi.mountPoint.c_str(),mi.type.c_str()); + mi.device.c_str(), mi.mountPoint.c_str(), mi.type.c_str()); } } } diff --git a/src/system/fsmounter.h b/src/system/fsmounter.h index 6cd6a2a9d..d5219035c 100644 --- a/src/system/fsmounter.h +++ b/src/system/fsmounter.h @@ -2,7 +2,7 @@ Neutrino-GUI - DBoxII-Project FS Mount/Umount by Zwen - + Homepage: http://dbox.cyberphoria.org/ Kommentar: @@ -39,69 +39,68 @@ class CFSMounter { -// protected - public: - - enum FS_Support - { - FS_UNSUPPORTED = 0, - FS_READY = 1, - FS_NEEDS_MODULES = 2, - FS_UNPROBED = 3 - }; - - public: + public: - enum FSType - { - NFS = 0, - CIFS = 1, - LUFS = 2 - }; - - enum MountRes - { - MRES_FS_NOT_SUPPORTED = 0, - MRES_FS_ALREADY_MOUNTED = 1, - MRES_TIMEOUT = 2, - MRES_UNKNOWN = 3, - MRES_OK = 4 - }; + enum FS_Support + { + FS_UNSUPPORTED = 0, + FS_READY = 1, + FS_NEEDS_MODULES = 2, + FS_UNPROBED = 3 + }; - enum UMountRes - { - UMRES_ERR = 0, - UMRES_OK = 1 - }; + public: - struct MountInfo - { - std::string device; - std::string mountPoint; - std::string type; - }; + enum FSType + { + NFS = 0, + CIFS = 1, + LUFS = 2 + }; - typedef std::vector MountInfos; + enum MountRes + { + MRES_FS_NOT_SUPPORTED = 0, + MRES_FS_ALREADY_MOUNTED = 1, + MRES_TIMEOUT = 2, + MRES_UNKNOWN = 3, + MRES_OK = 4 + }; - private: -/* - FS_Support m_nfs_sup; - FS_Support m_cifs_sup; - FS_Support m_lufs_sup; -*/ - public: - CFSMounter(); - static bool isMounted(const std::string &local_dir); - static CFSMounter::MountRes mount(const std::string &ip, const std::string &dir, const std::string &local_dir, - const FSType fstype, const std::string &username, const std::string &password, - std::string options1, std::string options2); - static bool automount(); - static CFSMounter::UMountRes umount(const char * const dir = NULL); - static void getMountedFS(MountInfos& fs); - static FS_Support fsSupported(const FSType fs, const bool keep_modules = false); + enum UMountRes + { + UMRES_ERR = 0, + UMRES_OK = 1 + }; + + struct MountInfo + { + std::string device; + std::string mountPoint; + std::string type; + }; + + typedef std::vector MountInfos; + + private: + /* + FS_Support m_nfs_sup; + FS_Support m_cifs_sup; + FS_Support m_lufs_sup; + */ + public: + CFSMounter(); + static bool isMounted(const std::string &local_dir); + static CFSMounter::MountRes mount(const std::string &ip, const std::string &dir, const std::string &local_dir, + const FSType fstype, const std::string &username, const std::string &password, + std::string options1, std::string options2); + static bool automount(); + static CFSMounter::UMountRes umount(const char *const dir = NULL); + static void getMountedFS(MountInfos &fs); + static FS_Support fsSupported(const FSType fs, const bool keep_modules = false); }; -bool in_proc_filesystems(const char * const fsname); +bool in_proc_filesystems(const char *const fsname); bool insert_modules(const CFSMounter::FSType fstype); bool remove_modules(const CFSMounter::FSType fstype);