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


//this
#include "Media.H"

//c - c++
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "stdio.h"
#include "mntent.h"
#include "ctime"

using namespace std;


void Media :: UpdateList()
	{
//check if /etc/fstab or /etc/mtab was updated after last check
	struct stat st;
	if(stat("/etc/fstab", &st)!=0) return;
	if(st.st_mtime > _last_update)
		{
		_do_update();
		return;
		}
	if(stat("/etc/mtab", &st)!=0) return;
	if(st.st_mtime > _last_update)
		{
		_do_update();
		return;
		}
	}


void Media :: _do_update()
	{
//clear vectors
	_media.clear();
	_user_mountable.clear();
	_user_unmountable.clear();
//read /etc/fstab
	struct mntent * fstent;	
	FILE *fstab=setmntent("/etc/fstab", "r");
	if(ferror(fstab) !=0 ) return;
	Medium med;
	while((fstent = getmntent(fstab)) != NULL)
		{
		med.Initialize(fstent);
		_media.push_back(med);
		if(!med.IsUserMountable()) continue;
		if(med.IsMounted())
			{
			_user_unmountable.push_back(med);
			continue;
			}
		_user_mountable.push_back(med);
		}
	time(&_last_update);
	}
