示例#1
0
CFSMounter::MountRes CFSMounter::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)
{
	std::string cmd;
	pthread_mutex_init(&g_mut, NULL);
	pthread_cond_init(&g_cond, NULL);
	g_mntstatus=-1;

	FS_Support sup = fsSupported(fstype, true); /* keep modules if necessary */

	if (sup == CFSMounter::FS_UNSUPPORTED)
	{
		printf("[CFSMounter] FS type %d not supported\n", (int) fstype);
		return MRES_FS_NOT_SUPPORTED;
	}

	printf("[CFSMounter] Mount(%d) %s:%s -> %s\n", (int) fstype, ip.c_str(), dir.c_str(), local_dir.c_str());

	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())
	{
		options1 = options2;
		options2 = "";
	}
	
	if(options1.empty() && options2.empty())
	{
		if(fstype == NFS)
		{
			options1 = "ro,soft,udp";
			options2 = "nolock,rsize=8192,wsize=8192";
		}
		else if(fstype == CIFS)
		{
			options1 = "ro";
			options2 = "";
		}
		else if(fstype == LUFS)
		{
			options1 = "";
			options2 = "";
		}
	}
	
	if(fstype == NFS)
	{
		cmd = "mount -t nfs ";
		cmd += ip;
		cmd += ':';
		cmd += dir;
		cmd += ' ';
		cmd += local_dir;
		cmd += " -o ";
		cmd += options1;
	}
	else if(fstype == CIFS)
	{
		cmd = "mount -t cifs //";
		cmd += ip;
		cmd += '/';
		cmd += dir;
		cmd += ' ';
		cmd += local_dir;
		cmd += " -o username="******",password="******",iocharset=UTF8";
		//cmd += ",unc=//"; for whats needed?
		//cmd += ip;
		//cmd += '/';
		//cmd += dir;
		//cmd += ',';
		//cmd += options1;
	}
	else
	{
		cmd = "lufsd none ";
		cmd += local_dir;
		cmd += " -o fs=ftpfs,username="******",password="******",host=";
		cmd += ip;
		cmd += ",root=/";
		cmd += dir;
		cmd += ',';
		cmd += options1;
	}
	
	if (options2[0] !='\0')
	{
		cmd += ',';
		cmd += options2;
	}
	
	pthread_create(&g_mnt, 0, mount_thread, (void *) cmd.c_str());
	
	struct timespec timeout;
	int retcode;

	pthread_mutex_lock(&g_mut);
	timeout.tv_sec = time(NULL) + 5;
	timeout.tv_nsec = 0;
	retcode = pthread_cond_timedwait(&g_cond, &g_mut, &timeout);
	if (retcode == ETIMEDOUT) 
	{  // timeout occurred
		pthread_cancel(g_mnt);
	}
	pthread_mutex_unlock(&g_mut);
	pthread_join(g_mnt, NULL);
	if ( g_mntstatus != 0 )
	{
		printf("[CFSMounter] FS mount error: \"%s\"\n", cmd.c_str());
		return (retcode == ETIMEDOUT) ? MRES_TIMEOUT : MRES_UNKNOWN;
	}
	return MRES_OK;

}
示例#2
0
CFSMounter::MountRes CFSMounter::mount(const char * const ip, const char * const dir, const char * const local_dir, 
				       const FSType fstype, const char * const username, const char * const password, 
				       char * options1, char * options2)
{
	std::stringstream cmd;
	pthread_mutex_init(&g_mut, NULL);
	pthread_cond_init(&g_cond, NULL);
	g_mntstatus=-1;

	FS_Support sup = fsSupported(fstype, true); /* keep modules if necessary */

	if (sup == CFSMounter::FS_UNSUPPORTED)
	{
		printf("[CFSMounter] FS type %d not supported\n", (int) fstype);
		return MRES_FS_NOT_SUPPORTED;
	}

	printf("[CFSMounter] Mount(%d) %s:%s -> %s\n", (int) fstype, ip, dir, local_dir);
	
	if (isMounted(local_dir))
	{
		printf("[CFSMounter] FS mount error %s already mounted\n", local_dir);
		return MRES_FS_ALREADY_MOUNTED;
	}

	if(options1[0] == '\0')
	{
		strcpy(options1,options2);
		options2[0] = '\0';
	}
	
	if((options1[0] == '\0') && (options2[0] == '\0'))
	{
		if(fstype == NFS)
		{
			strcpy(options1,"ro,soft,udp");
			strcpy(options2,"nolock,rsize=8192,wsize=8192");
		}
		else if(fstype == CIFS)
		{
			strcpy(options1,"ro");
			strcpy(options2,"");
		}
		else if(fstype == LUFS)
		{
			strcpy(options1,"");
			strcpy(options2,"");
		}
		else if(fstype == SMBFS)
		{
			strcpy(options1,"");
			strcpy(options2,"");
		}
	}
	
	if(fstype == NFS)
	{
		cmd << "mount -t nfs " << ip << ":" << dir << " " << local_dir << " -o " << options1;
	}
	else if(fstype == CIFS)
	{
		cmd << "mount -t cifs " << ip << "/" << dir << " " << local_dir << " -o username="******",password="******",unc=//" << ip << "/" << dir << "," << options1;
	}
        else if(fstype == SMBFS)
	{
		cmd << "smbmount //" << ip << "/" << dir << " " << password << " " << "-I" << " " << ip << " "
		<< "-U" << " " << username << " " << "-c \"mount " << local_dir << "\"" << " ";
	}
	else
	{
		cmd << "lufsd none " << local_dir << " -o fs=ftpfs,username="******",password="******",host=" << ip << ",root=/" << dir << "," << options1;
	}
	
	if (options2[0] !='\0')
	{
		cmd << "," << options2;
	}
	
	pthread_create(&g_mnt, 0, mount_thread, (void *) cmd.str().c_str());
	
	struct timespec timeout;
	int retcode;

	pthread_mutex_lock(&g_mut);
	timeout.tv_sec = time(NULL) + 5;
	timeout.tv_nsec = 0;
	retcode = pthread_cond_timedwait(&g_cond, &g_mut, &timeout);
	if (retcode == ETIMEDOUT) 
	{  // timeout occurred
		pthread_cancel(g_mnt);
	}
	pthread_mutex_unlock(&g_mut);

	if ( g_mntstatus != 0 )
	{
		printf("[CFSMounter] FS mount error: \"%s\"\n", cmd.str().c_str());
		return (retcode == ETIMEDOUT) ? MRES_TIMEOUT : MRES_UNKNOWN;
	}
	return MRES_OK;

}