From 87d96059cc6880d2a3d3842fadeca4f8d6a72b63 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 21 May 2010 22:05:50 +0000 Subject: [PATCH] forgot to commit this git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2969 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/util/rsdiscspace.cc | 123 ++++++++++++++++++++++++++ libretroshare/src/util/rsdiscspace.h | 57 ++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 libretroshare/src/util/rsdiscspace.cc create mode 100644 libretroshare/src/util/rsdiscspace.h diff --git a/libretroshare/src/util/rsdiscspace.cc b/libretroshare/src/util/rsdiscspace.cc new file mode 100644 index 000000000..84ea78b51 --- /dev/null +++ b/libretroshare/src/util/rsdiscspace.cc @@ -0,0 +1,123 @@ +/* + * libretroshare/src/util: rsdiscspace.cc + * + * Universal Networking Header for RetroShare. + * + * Copyright 2010-2010 by Cyril Soler. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "csoler@users.sourceforge.net". + * + */ + +#include +#include +#include +#include +#include "rsdiscspace.h" +#include +#include + +#define DELAY_BETWEEN_CHECKS 2 + +/* + * #define DEBUG_RSDISCSPACE + */ + +time_t RsDiscSpace::_last_check[3] = { 0,0,0 } ; +uint32_t RsDiscSpace::_size_limit_mb = 100 ; +uint32_t RsDiscSpace::_current_size[3] = { 10000,10000,10000 } ; +bool RsDiscSpace::_last_res[3] = { true,true,true }; +RsMutex RsDiscSpace::_mtx ; + +bool RsDiscSpace::checkForDiscSpace(RsDiscSpace::DiscLocation loc) +{ + RsStackMutex m(_mtx) ; // Locked + + time_t now = time(NULL) ; + + if(_last_check[loc]+DELAY_BETWEEN_CHECKS < now) + { + struct statvfs v ; + int res = 1; + +#ifdef DEBUG_RSDISCSPACE + std::cerr << "Size determination:" << std::endl ; +#endif + switch(loc) + { + case RS_DOWNLOAD_DIRECTORY: res = statvfs(rsFiles->getDownloadDirectory().c_str(),&v) ; +#ifdef DEBUG_RSDISCSPACE + std::cerr << " path = " << rsFiles->getDownloadDirectory() << std::endl ; +#endif + break ; + + case RS_PARTIALS_DIRECTORY: res = statvfs(rsFiles->getPartialsDirectory().c_str(),&v) ; +#ifdef DEBUG_RSDISCSPACE + std::cerr << " path = " << rsFiles->getPartialsDirectory() << std::endl ; +#endif + break ; + + case RS_CONFIG_DIRECTORY: res = statvfs(RsInit::RsConfigDirectory().c_str(),&v) ; +#ifdef DEBUG_RSDISCSPACE + std::cerr << " path = " << RsInit::RsConfigDirectory() << std::endl ; +#endif + break ; + } + + if(res) + { + std::cerr << "Determination of free disc space failed ! Be careful !" << std::endl ; + return true ; + } + _last_check[loc] = now ; + + // Now compute the size in megabytes + // + _current_size[loc] = v.f_bavail * v.f_bsize / (1024*1024) ; // on purpose integer division + + bool new_result = (_current_size[loc] > _size_limit_mb) ; +#ifdef DEBUG_RSDISCSPACE + std::cerr << " f_bavail = " << v.f_bavail << std::endl ; + std::cerr << " f_bsize = " << v.f_bsize << std::endl ; + std::cerr << " free MBs = " << _current_size[loc] << std::endl ; +#endif + } + + bool res = _current_size[loc] > _size_limit_mb ; + + if(_last_res[loc] && !res) + rsicontrol->getNotify().notifyDiskFull(loc,_size_limit_mb) ; + + _last_res[loc] = res ; + + return res ; +} + +void RsDiscSpace::setFreeSpaceLimit(uint32_t size_in_mb) +{ + RsStackMutex m(_mtx) ; // Locked + + _size_limit_mb = size_in_mb ; +} + +uint32_t RsDiscSpace::freeSpaceLimit() +{ + RsStackMutex m(_mtx) ; // Locked + + return _size_limit_mb ; +} + diff --git a/libretroshare/src/util/rsdiscspace.h b/libretroshare/src/util/rsdiscspace.h new file mode 100644 index 000000000..aac914b60 --- /dev/null +++ b/libretroshare/src/util/rsdiscspace.h @@ -0,0 +1,57 @@ +/* + * libretroshare/src/util: rsdiscspace.h + * + * Universal Networking Header for RetroShare. + * + * Copyright 2010-2010 by Cyril Soler. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "csoler@users.sourceforge.net". + * + */ + +#pragma once + +#include +#include +#include + +class RsDiscSpace +{ + public: + typedef uint32_t DiscLocation ; + + // Returns false is the disc space is lower than the given size limit, true otherwise. + // When the size limit is reached, this class calls notify to warn the user (possibly + // with a popup window). + // + static bool checkForDiscSpace(DiscLocation loc) ; + + // Allow the user to specify his own size limit. Should not be too low, especially not 0 MB ;-) + // 10MB to 100MB are sensible values. + // + static void setFreeSpaceLimit(uint32_t mega_bytes) ; + static uint32_t freeSpaceLimit() ; + + private: + static RsMutex _mtx ; + + static time_t _last_check[3] ; + static uint32_t _size_limit_mb ; + static uint32_t _current_size[3] ; + static bool _last_res[3] ; +}; +