2012-04-25 20:13:17 +04:00
|
|
|
/*
|
|
|
|
grive: an GPL program to sync a local directory with Google Drive
|
|
|
|
Copyright (C) 2012 Wan Wai Ho
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2012-04-26 19:40:38 +04:00
|
|
|
as published by the Free Software Foundation version 2
|
2012-04-26 08:21:40 +04:00
|
|
|
of the License.
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "OS.hh"
|
2012-05-01 13:44:17 +04:00
|
|
|
|
2012-04-27 21:52:02 +04:00
|
|
|
#include "DateTime.hh"
|
2012-05-13 11:27:58 +04:00
|
|
|
#include "Exception.hh"
|
2012-04-27 21:52:02 +04:00
|
|
|
|
2012-05-14 19:32:22 +04:00
|
|
|
// boost headers
|
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
#include <boost/exception/errinfo_api_function.hpp>
|
|
|
|
#include <boost/exception/errinfo_at_line.hpp>
|
|
|
|
#include <boost/exception/errinfo_errno.hpp>
|
|
|
|
#include <boost/exception/errinfo_file_name.hpp>
|
|
|
|
#include <boost/exception/errinfo_file_open_mode.hpp>
|
|
|
|
#include <boost/exception/info.hpp>
|
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
// OS specific headers
|
2012-05-13 11:27:58 +04:00
|
|
|
#include <errno.h>
|
2012-04-27 06:49:33 +04:00
|
|
|
#include <sys/stat.h>
|
2012-04-27 21:52:02 +04:00
|
|
|
#include <sys/time.h>
|
2012-04-27 06:49:33 +04:00
|
|
|
#include <sys/types.h>
|
2012-04-26 19:40:38 +04:00
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
namespace gr { namespace os {
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2017-03-19 12:48:01 +03:00
|
|
|
void Stat( const fs::path& filename, DateTime *t, off_t *size, FileType *ft )
|
2012-05-01 13:44:17 +04:00
|
|
|
{
|
2017-03-19 12:48:01 +03:00
|
|
|
Stat( filename.string(), t, size, ft ) ;
|
2012-05-01 13:44:17 +04:00
|
|
|
}
|
|
|
|
|
2017-03-19 12:48:01 +03:00
|
|
|
void Stat( const std::string& filename, DateTime *t, off64_t *size, FileType *ft )
|
2012-04-27 21:52:02 +04:00
|
|
|
{
|
|
|
|
struct stat s = {} ;
|
|
|
|
if ( ::stat( filename.c_str(), &s ) != 0 )
|
2012-05-13 11:27:58 +04:00
|
|
|
{
|
2012-05-14 19:32:22 +04:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
Error()
|
|
|
|
<< boost::errinfo_api_function("stat")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(filename)
|
|
|
|
) ;
|
2012-05-13 11:27:58 +04:00
|
|
|
}
|
2012-04-27 21:52:02 +04:00
|
|
|
|
2017-03-19 12:48:01 +03:00
|
|
|
if ( t )
|
2016-01-01 02:27:21 +03:00
|
|
|
{
|
2012-04-29 06:20:53 +04:00
|
|
|
#if defined __APPLE__ && defined __DARWIN_64_BIT_INO_T
|
2016-01-01 02:27:21 +03:00
|
|
|
*t = DateTime( s.st_ctimespec.tv_sec, s.st_ctimespec.tv_nsec ) ;
|
2012-04-29 02:12:23 +04:00
|
|
|
#else
|
2016-01-01 02:27:21 +03:00
|
|
|
*t = DateTime( s.st_ctim.tv_sec, s.st_ctim.tv_nsec);
|
2012-04-29 02:12:23 +04:00
|
|
|
#endif
|
2016-01-01 02:27:21 +03:00
|
|
|
}
|
2016-09-28 01:19:13 +03:00
|
|
|
if ( size )
|
2016-01-01 02:27:21 +03:00
|
|
|
*size = s.st_size;
|
2017-03-19 12:48:01 +03:00
|
|
|
if ( ft )
|
|
|
|
*ft = S_ISDIR( s.st_mode ) ? FT_DIR : ( S_ISREG( s.st_mode ) ? FT_FILE : FT_UNKNOWN ) ;
|
2012-04-27 21:52:02 +04:00
|
|
|
}
|
|
|
|
|
2012-05-16 21:20:02 +04:00
|
|
|
void SetFileTime( const fs::path& filename, const DateTime& t )
|
2012-05-01 13:44:17 +04:00
|
|
|
{
|
2012-05-16 21:20:02 +04:00
|
|
|
return SetFileTime( filename.string(), t ) ;
|
2012-05-01 13:44:17 +04:00
|
|
|
}
|
|
|
|
|
2012-04-27 21:52:02 +04:00
|
|
|
void SetFileTime( const std::string& filename, const DateTime& t )
|
|
|
|
{
|
|
|
|
struct timeval tvp[2] = { t.Tv(), t.Tv() } ;
|
|
|
|
if ( ::utimes( filename.c_str(), tvp ) != 0 )
|
2012-05-24 14:56:48 +04:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
Error()
|
|
|
|
<< boost::errinfo_api_function("utimes")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(filename)
|
|
|
|
) ;
|
2012-04-27 21:52:02 +04:00
|
|
|
}
|
|
|
|
|
2012-07-19 20:50:40 +04:00
|
|
|
void Sleep( unsigned int sec )
|
|
|
|
{
|
|
|
|
struct timespec ts = { sec, 0 } ;
|
|
|
|
|
|
|
|
int result = 0 ;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
struct timespec rem ;
|
|
|
|
nanosleep( &ts, &rem ) ;
|
|
|
|
ts = rem ;
|
|
|
|
} while ( result == -1 && errno == EINTR ) ;
|
|
|
|
}
|
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
} } // end of namespaces
|