5 #include <boost/lexical_cast.hpp>
6 #include "Filesystem.hpp"
10 Filesystem::Filesystem( const std::string
& root
)
19 Filesystem::~Filesystem()
26 bool Filesystem::ChangeDirectory( const std::string
& cd
)
28 PathVector reqPath
= SplitPath( cd
);
29 PathVector path
= SplitProperPath( cd
);
31 if( !TryChangePath( reqPath
, path
) )
36 m_path
= MakePath( path
);
41 bool Filesystem::FileExists( const std::string
& file
)
43 return CheckFileExists( GetFilePath( file
) );
46 FILE* Filesystem::FileOpen( const std::string
& file
, Mode mode
)
48 std::string path
= m_root
+ GetFilePath( file
);
53 return fopen( path
.c_str(), "rb" );
57 return fopen( path
.c_str(), "wb" );
68 RFile
* Filesystem::FileOpenSymbian( const std::string
& file
, Mode mode
)
72 std::string path
= m_root
+ GetFilePath( file
);
74 for( unsigned int i
=0; i
<path
.size(); i
++ )
76 if( path
[i
] == '/' || path
[i
] == '\\' )
78 if( i
>0 && path
[i
-1] == '\\' )
90 TPtrC8
ptr( reinterpret_cast<const TUint8
*>( path
.c_str() ) );
98 if( f
->Open( m_rfs
, buf
, EFileRead
| EFileStream
| EFileShareReadersOnly
) != KErrNone
)
106 if( f
->Replace( m_rfs
, buf
, EFileWrite
| EFileStream
) != KErrNone
)
123 std::list
<std::string
> Filesystem::GetListing( const std::string
& path
)
125 std::list
<std::string
> ret
;
127 time_t now
= time( NULL
);
129 std::string filePath
= GetFilePath( path
);
130 if( CheckFileExists( filePath
) )
133 stat( ( m_root
+ filePath
).c_str(), &s
);
135 std::string entry
= "-rw-r--r--";
138 localtime_r( &s
.st_mtime
, &timeFile
);
141 if( now
> s
.st_mtime
&& now
- s
.st_mtime
< 60*60*24*180 )
144 strftime( dateBuf
, sizeof( dateBuf
), " %b %m %H:%M ", &timeFile
);
148 strftime( dateBuf
, sizeof( dateBuf
), " %b %m %Y ", &timeFile
);
151 entry
+= " 1 root root " + boost::lexical_cast
<std::string
>( s
.st_size
) + dateBuf
+ path
;
153 ret
.push_back( entry
);
158 PathVector reqPath
= SplitPath( path
);
159 PathVector pv
= SplitProperPath( path
);
161 if( !TryChangePath( reqPath
, pv
) )
166 std::string newPath
= m_root
+ MakePath( pv
);
169 if( !d
.Open( newPath
) )
175 localtime_r( &now
, &timeNow
);
177 // Symbian readdir() doesn't report "." and ".."
179 bool hasDotDot
= false;
184 std::string dirName
= d
.GetName();
185 stat( ( newPath
+ "/" + dirName
).c_str(), &s
);
189 if( S_ISDIR( s
.st_mode
) )
191 entry
+= "drwxr-xr-x";
195 entry
+= "-rw-r--r--";
199 localtime_r( &s
.st_mtime
, &timeFile
);
202 if( now
> s
.st_mtime
&& now
- s
.st_mtime
< 60*60*24*180 )
205 strftime( dateBuf
, sizeof( dateBuf
), " %b %m %H:%M ", &timeFile
);
209 strftime( dateBuf
, sizeof( dateBuf
), " %b %m %Y ", &timeFile
);
212 entry
+= " 1 root root " + boost::lexical_cast
<std::string
>( s
.st_size
) + dateBuf
+ dirName
;
218 else if( dirName
== ".." )
223 ret
.push_back( entry
);
230 ret
.push_front( "drwxr-xr-x 1 root root 0 Jan 1 1970 .." );
234 ret
.push_front( "drwxr-xr-x 1 root root 0 Jan 1 1970 ." );
240 bool Filesystem::Delete( const std::string
& file
)
242 std::string path
= m_root
+ GetFilePath( file
);
244 return unlink( path
.c_str() ) == 0;
247 std::string
Filesystem::MkDir( const std::string
& dir
)
249 std::string filePath
= GetFilePath( dir
);
255 std::string path
= m_root
+ filePath
;
257 if( !IO::MkDir( path
) )
265 bool Filesystem::RmDir( const std::string
& dir
)
267 std::string filePath
= GetFilePath( dir
);
273 std::string path
= m_root
+ filePath
;
275 if( !IO::RmDir( path
) )
283 std::string
Filesystem::MakePath( const PathVector
& pv
)
293 for( PathVector::const_iterator it
= pv
.begin(); it
!= pv
.end(); ++it
)
303 bool Filesystem::DirectoryExists( const std::string
& dir
)
305 std::string path
= m_root
+ dir
;
308 if( stat( path
.c_str(), &s
) == -1 )
313 return S_ISDIR( s
.st_mode
);
316 bool Filesystem::TryChangePath( const PathVector
& reqPath
, PathVector
& path
)
318 for( PathVector::const_iterator it
= reqPath
.begin(); it
!= reqPath
.end(); ++it
)
324 else if( *it
== ".." )
326 if( path
.size() == 0 )
328 // Can't go below the root directory.
329 // Or maybe it should be possible as no-op?
339 if( DirectoryExists( MakePath( path
) + "/" + *it
) )
341 path
.push_back( *it
);
353 bool Filesystem::CheckFileExists( const std::string
& file
)
355 std::string path
= m_root
+ file
;
358 if( stat( path
.c_str(), &s
) == -1 )
363 return S_ISREG( s
.st_mode
);
366 std::string
Filesystem::GetFilePath( const std::string
& file
)
368 PathVector reqPath
= SplitPath( file
);
370 if( reqPath
.size() == 0 )
375 // Remove filename from path
376 std::string fname
= reqPath
.back();
379 PathVector path
= SplitProperPath( file
);
381 if( !TryChangePath( reqPath
, path
) )
386 return MakePath( path
) + "/" + fname
;
389 PathVector
Filesystem::SplitProperPath( const std::string
& path
)
393 if( path
.length() > 0 &&
395 ( path
.length() > 1 && ( path
[0] == '~' && path
[1] == '/' ) ) ) )
397 ret
= SplitPath( "/" );
401 ret
= SplitPath( m_path
);