//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Created 2006-12-03 11:36:58 by Felix Rauscher
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//this
#include "Medium.H"

//c - c++
#include "stdio.h"
#include "sstream"
#include <stdlib.h>
using namespace std;


void Medium :: Initialize(struct mntent * mnt)
	{
//store values
	_name=mnt->mnt_fsname;
	_mountpoint=mnt->mnt_dir;
	_type=mnt->mnt_type;
	_options=mnt->mnt_opts;
//check if medium is user mountable
	if(hasmntopt(mnt, "user")!=NULL) _user_mount=true;
	else _user_mount=false;
//check if medium is mounted
	_check_mounted();
	}

bool  Medium :: Mount()
	{
	ostringstream cmd;
	cmd<<"mount "<<_mountpoint;
	system(cmd.str().c_str());
	_check_mounted();
	return _is_mounted;
	}

bool  Medium :: UnMount()
	{
	ostringstream cmd;
	cmd<<"umount "<<_mountpoint;
	system(cmd.str().c_str());
	_check_mounted();
	return !_is_mounted;
	}
	
	
void Medium :: _check_mounted()
	{
//check if medium is mounted
	FILE *mtab=setmntent("/etc/mtab", "r");
	if(ferror(mtab) !=0 ) return;
//loop on entries
	struct mntent * mtent;
	_is_mounted=false;
	while((mtent = 	getmntent(mtab)) != NULL)
		{
		if(_name==mtent->mnt_fsname)
			{
			_is_mounted=true;
			break;
			}
		}
	endmntent(mtab);
	}
