Skip to content

Commit

Permalink
Fix #136 - Skip links, sockets, fifos and etc
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif committed Mar 19, 2017
1 parent e8a8801 commit a0aff5b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
28 changes: 18 additions & 10 deletions libgrive/src/base/Resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ void Resource::FromLocal( Val& state )
if ( !IsRoot() )
{
fs::path path = Path() ;
bool is_dir;
FileType ft ;
try
{
os::Stat( path, &m_ctime, (off64_t*)&m_size, &is_dir ) ;
os::Stat( path, &m_ctime, (off64_t*)&m_size, &ft ) ;
}
catch ( os::Error &e )
{
Expand All @@ -276,22 +276,30 @@ void Resource::FromLocal( Val& state )
m_kind = "bad";
return;
}
if ( ft == FT_UNKNOWN )
{
// Skip sockets/FIFOs/etc
Log( "File %1% is not a regular file or directory; skipping file", path.string(), log::warning );
m_state = sync;
m_kind = "bad";
return;
}

m_name = path.filename().string() ;
m_kind = is_dir ? "folder" : "file";
m_kind = ft == FT_DIR ? "folder" : "file";
m_local_exists = true;

bool is_changed;
if ( state.Has( "ctime" ) && (u64_t) m_ctime.Sec() <= state["ctime"].U64() &&
( is_dir || state.Has( "md5" ) ) )
( ft == FT_DIR || state.Has( "md5" ) ) )
{
if ( !is_dir )
if ( ft != FT_DIR )
m_md5 = state["md5"];
is_changed = false;
}
else
{
if ( !is_dir )
if ( ft != FT_DIR )
{
// File is changed locally. TODO: Detect conflicts
is_changed = ( state.Has( "size" ) && m_size != state["size"].U64() ) ||
Expand Down Expand Up @@ -703,13 +711,13 @@ void Resource::SetIndex( bool re_stat )
assert( m_parent && m_parent->m_json != NULL );
if ( !m_json )
m_json = &((*m_parent->m_json)["tree"]).Item( Name() );
bool is_dir;
FileType ft;
if ( re_stat )
os::Stat( Path(), &m_ctime, NULL, &is_dir );
os::Stat( Path(), &m_ctime, NULL, &ft );
else
is_dir = IsFolder();
ft = IsFolder() ? FT_DIR : FT_FILE;
m_json->Set( "ctime", Val( m_ctime.Sec() ) );
if ( !is_dir )
if ( ft != FT_DIR )
{
m_json->Set( "md5", Val( m_md5 ) );
m_json->Set( "size", Val( m_size ) );
Expand Down
12 changes: 6 additions & 6 deletions libgrive/src/util/OS.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@

namespace gr { namespace os {

void Stat( const fs::path& filename, DateTime *t, off_t *size, bool *is_dir )
void Stat( const fs::path& filename, DateTime *t, off_t *size, FileType *ft )
{
Stat( filename.string(), t, size, is_dir ) ;
Stat( filename.string(), t, size, ft ) ;
}

void Stat( const std::string& filename, DateTime *t, off64_t *size, bool *is_dir )
void Stat( const std::string& filename, DateTime *t, off64_t *size, FileType *ft )
{
struct stat s = {} ;
if ( ::stat( filename.c_str(), &s ) != 0 )
Expand All @@ -57,7 +57,7 @@ void Stat( const std::string& filename, DateTime *t, off64_t *size, bool *is_dir
) ;
}

if (t)
if ( t )
{
#if defined __APPLE__ && defined __DARWIN_64_BIT_INO_T
*t = DateTime( s.st_ctimespec.tv_sec, s.st_ctimespec.tv_nsec ) ;
Expand All @@ -67,8 +67,8 @@ void Stat( const std::string& filename, DateTime *t, off64_t *size, bool *is_dir
}
if ( size )
*size = s.st_size;
if ( is_dir )
*is_dir = S_ISDIR( s.st_mode ) ? true : false;
if ( ft )
*ft = S_ISDIR( s.st_mode ) ? FT_DIR : ( S_ISREG( s.st_mode ) ? FT_FILE : FT_UNKNOWN ) ;
}

void SetFileTime( const fs::path& filename, const DateTime& t )
Expand Down
6 changes: 4 additions & 2 deletions libgrive/src/util/OS.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ namespace gr {
class DateTime ;
class Path ;

enum FileType { FT_FILE = 1, FT_DIR = 2, FT_UNKNOWN = 3 } ;

namespace os
{
struct Error : virtual Exception {} ;

void Stat( const std::string& filename, DateTime *t, off64_t *size, bool *is_dir ) ;
void Stat( const fs::path& filename, DateTime *t, off64_t *size, bool *is_dir ) ;
void Stat( const std::string& filename, DateTime *t, off64_t *size, FileType *ft ) ;
void Stat( const fs::path& filename, DateTime *t, off64_t *size, FileType *ft ) ;

void SetFileTime( const std::string& filename, const DateTime& t ) ;
void SetFileTime( const fs::path& filename, const DateTime& t ) ;
Expand Down

0 comments on commit a0aff5b

Please sign in to comment.