1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "File.h"
#include "Debug.h"
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStringList>
#if defined(Q_OS_WIN)
#include <windows.h>
#endif
#include <algorithm>
#include <cmath>
//_____________________________________________________________________
bool File::isAbsolute( void ) const
{ return QFileInfo( *this ).isAbsolute(); }
//_____________________________________________________________________
bool File::create( void ) const
{ return QFile( *this ).open( QIODevice::WriteOnly ); }
//_____________________________________________________________________
bool File::createDirectory( const File& constPath ) const
{
if( exists() && !isDirectory() ) return false;
if( !QDir( *this ).mkpath( constPath ) ) return false;
return true;
}
//_____________________________________________________________________
time_t File::created( void ) const
{
if( !exists() ) return -1;
return QFileInfo( *this ).created().toTime_t();
}
//_____________________________________________________________________
time_t File::lastModified( void ) const
{
if( !exists() ) return -1;
return QFileInfo( *this ).lastModified().toTime_t();
}
//_____________________________________________________________________
time_t File::lastAccessed( void ) const
{
if( !exists() ) return -1;
return QFileInfo( *this ).lastRead().toTime_t();
}
//_____________________________________________________________________
unsigned int File::userId( void ) const
{
if( !exists() ) return 0;
return QFileInfo( *this ).ownerId();
}
//_____________________________________________________________________
QString File::userName( void ) const
{
if( !exists() ) return QString();
QString out( QFileInfo( *this ).owner() );
if( out.isNull() || out.isEmpty() ) out = QString::number( QFileInfo( *this ).ownerId() );
return out;
}
//_____________________________________________________________________
unsigned int File::groupId( void ) const
{
if( !exists() ) return 0;
return QFileInfo( *this ).groupId();
}
//_____________________________________________________________________
QString File::groupName( void ) const
{
if( !exists() ) return QString();
return QFileInfo( *this ).group();
}
//_____________________________________________________________________
QFile::Permissions File::permissions( void ) const
{ return QFileInfo( *this ).permissions(); }
//_____________________________________________________________________
QString File::permissionsString( const QFile::Permissions& mode ) const
{
QString out;
QTextStream what( &out );
// link, directory or regular file
if( QFileInfo( *this ).isSymLink() ) what << "l";
else if( QFileInfo( *this ).isDir() ) what << "d";
else what << "-";
// user permissions
what << ( ( mode & QFile::ReadUser ) ? "r":"-" );
what << ( ( mode & QFile::WriteUser ) ? "w":"-" );
what << ( ( mode & QFile::ExeUser ) ? "x":"-" );
// group permissions
what << ( ( mode & QFile::ReadGroup ) ? "r":"-" );
what << ( ( mode & QFile::WriteGroup ) ? "w":"-" );
what << ( ( mode & QFile::ExeGroup ) ? "x":"-" );
// other permissions
what << ( ( mode & QFile::ReadOther ) ? "r":"-" );
what << ( ( mode & QFile::WriteOther ) ? "w":"-" );
what << ( ( mode & QFile::ExeOther ) ? "x":"-" );
return out;
}
//_____________________________________________________________________
qint64 File::fileSize( void ) const
{
if( !exists() ) return 0;
return QFileInfo( *this ).size();
}
//_____________________________________________________________________
QString File::sizeString( qint64 sizeInt )
{
if( sizeInt < 0 ) return QString();
if( sizeInt == 0 ) return "0";
qreal size = sizeInt;
static const QString unit[] =
{
QObject::tr( "B" ),
QObject::tr( "KiB" ),
QObject::tr( "MiB" ),
QObject::tr( "GiB" ),
QObject::tr( "TiB" )
};
int power = log( size )/log( 1024 );
QString out;
if( !power ) out.sprintf( "%.0f", size );
else {
if( power > 4 ) power = 4;
size /= pow( static_cast<double>(1024), static_cast<double>(power) );
out.sprintf( "%.1f", size );
}
return QString( "%1 %2").arg( out ).arg( unit[power] );
}
//_____________________________________________________________________
QString File::rawSizeString( qint64 sizeInt )
{
if( sizeInt < 0 ) return QString();
if( sizeInt == 0 ) return "0";
// format size to have space characters every three digits
QString out = QString::number( sizeInt );
int length( out.length() );
for( int i = 1; i < length; i++ )
{ if( !(i%3) ) out.insert( out.size() - (i + i/3 - 1), ' ' ); }
return out;
}
//_____________________________________________________________________
bool File::exists( void ) const
{ return QFileInfo( *this ).exists(); }
//_____________________________________________________________________
bool File::isWritable( void ) const
{ return isEmpty() || QFile( *this ).open( QIODevice::ReadWrite ); }
//_____________________________________________________________________
bool File::isDirectory( void ) const
{ return !isEmpty() && QFileInfo( *this ).isDir(); }
//_____________________________________________________________________
bool File::isBrokenLink( void ) const
{ return isLink() && !File( QFileInfo( *this ).symLinkTarget() ).exists(); }
//_____________________________________________________________________
bool File::isLink( void ) const
{ return !isEmpty() && QFileInfo( *this ).isSymLink(); }
//_____________________________________________________________________
bool File::isHidden( void ) const
{ return !isEmpty() && QFileInfo( *this ).isHidden(); }
//_____________________________________________________________________
void File::setHidden( void ) const
{
#if defined(Q_OS_WIN)
if( !isEmpty() ) SetFileAttributes( toLatin1(), FILE_ATTRIBUTE_HIDDEN );
#endif
}
//_____________________________________________________________________
File File::version( void ) const
{
int version=0;
QString expand( File::expand() );
File out( expand );
while( out.exists() )
{
out.clear();
QTextStream(&out) << expand << "_" << version;
version++;
}
return out;
}
//_____________________________________________________________________
File File::backup( void ) const
{
// check filename is valid and file exists
if( !exists() ) return File();
QString expand( File::expand() );
QString backup( expand+"~" );
// open this file
QFile in( expand );
in.copy( backup );
return backup;
}
//_____________________________________________________________________
bool File::diff(const File& file ) const
{
// no file exists
if( !( exists() || file.exists() ) ) return false;
// one of the file does not exists and the other does
if( !( exists() && file.exists() ) ) return true;
QFile first( *this );
QFile second( file );
bool first_open( first.open( QIODevice::ReadOnly ) );
bool second_open( second.open( QIODevice::ReadOnly ) );
// no file exists
if( !( first_open || second_open ) ) return false;
// one of the file does not exists and the other does
if( !( first_open && second_open ) ) return true;
return( first.readAll() != second.readAll() );
}
//_____________________________________________________________________
bool File::isEqual( const File& other ) const
{ return expand() == other.expand(); }
//_____________________________________________________________________
File File::readLink( void ) const
{ return File( QFileInfo( *this ).canonicalFilePath() ); }
//_____________________________________________________________________
bool File::remove( void ) const
{
// check if file exists and remove
// if it does not exists, do nothing and returns true (file was removed already)
if( isLink() || !isDirectory() )
{
if( isBrokenLink() || exists() ) return QFile( *this ).remove();
else return true;
} else {
QDir dir( *this );
dir.cdUp();
return dir.rmdir( *this );
}
}
//_____________________________________________________________________
bool File::removeRecursive( void ) const
{
if( isLink() || !isDirectory() ) return remove();
// list content of directory
QDir dir( *this );
foreach( const QString& value, dir.entryList( QDir::AllEntries|QDir::Hidden|QDir::System ) )
{
// skip "." and ".."
if( value == "." || value == ".." ) continue;
File file = File( value ).addPath( *this );
if( file.isLink() || !file.isDirectory() )
{
if( !file.remove() ) return false;
} else if( !file.removeRecursive() ) return false;
}
dir.cdUp();
dir.rmdir( *this );
return true;
}
//_____________________________________________________________________
bool File::rename( const File& newFile ) const
{
// check if file exists and rename
// return false otherwise
if( exists() ) return QFile( *this ).rename( newFile );
else return false;
}
//_____________________________________________________________________
bool File::copy( const File& newFile, bool force ) const
{
// check existence
if( !exists() ) return false;
// check destination existance
if( newFile.exists() && !( force && newFile.removeRecursive() ) ) return false;
// check file type
if( isLink() )
{
// get source and make a new link
return QFile( QFile( *this ).symLinkTarget() ).link( newFile );
} else if( isDirectory() ) {
// create directory
if( !newFile.createDirectory() ) return false;
// list files, and copy, recursively
const File::List files( listFiles( ShowHiddenFiles ) );
foreach( const File& file, files )
{ if( !file.copy( file.localName().addPath( newFile ) ) ) return false; }
return true;
} else {
// copy plain file
return QFile( *this ).copy( newFile );
}
}
//____________________________________________
File File::addPath( const File& path, bool use_absolute ) const
{
// returns 0 if either path nor file are given
if( isEmpty() && path.isEmpty() ) return File();
// return path if path is given but not the file
if( isEmpty() ) return path;
// if file is absolute, keep unchanged
if( isAbsolute() ) return *this;
// returns file if it is relative but path is not given
if( !path.size() ) return QString("./")+(*this);
QFileInfo info;
info.setFile( QDir( path ), *this );
return use_absolute ? info.absoluteFilePath(): info.filePath();
}
//_____________________________________________________________________
File File::expand( void ) const
{
if( isEmpty() ) return File();
File out( QFileInfo(*this).absoluteFilePath() );
// remove trailing slash, except for root
if( out.size() > 1 && out[out.size()-1] == '/' ) out.resize( out.size()-1 );
return out;
}
//_____________________________________________________________________
File File::path( bool use_absolute ) const
{
if( isEmpty() ) return File();
if( use_absolute ) return QFileInfo(*this).absolutePath();
else return QFileInfo(*this).path();
}
//_____________________________________________________________________
File File::localName( void ) const
{ return isEmpty() ? File(): File( QFileInfo(*this).fileName() ); }
//_____________________________________________________________________
File File::canonicalName( void ) const
{
if( isEmpty() ) return File();
QString canonicalName( QFileInfo(*this).canonicalFilePath() );
return canonicalName.isEmpty() ? File():File( canonicalName );
}
//_____________________________________________________________________
File File::extension( void ) const
{
// check file name
if( isEmpty() ) return File();
// loop over characters
QString local( localName() );
int dotpos = local.lastIndexOf(".");
return File( dotpos < 0 ? "" : local.mid( dotpos+1 ) );
}
//_____________________________________________________________________
File File::truncatedName( void ) const
{
// check file name
if( isEmpty() ) return File();
// loop over characters
int dotpos = lastIndexOf(".");
int slashpos = lastIndexOf( "/" );
if( dotpos < 0 ) return *this;
if( slashpos < 0 ) return File( dotpos >= 0 ? left(dotpos):"");
if( slashpos < dotpos ) return left(dotpos);
return *this;
}
//_____________________________________________________________________
File::List File::listFiles( ListFlags flags ) const
{
List out;
File fullname( expand() );
if( !fullname.isDirectory() || (fullname.isLink() && !(flags&FollowLinks) ) ) return out;
if( !fullname.endsWith( "/" ) ) fullname += "/";
// open directory
QDir::Filters filter = QDir::AllEntries|QDir::System;
#if QT_VERSION >= 0x040800
filter |= QDir::NoDotDot;
#endif
if( flags & ShowHiddenFiles ) filter |= QDir::Hidden;
const QDir dir( fullname );
foreach( const QString& value, dir.entryList( filter ) )
{
if( value == "." || value == ".." ) continue;
QFileInfo fileInfo;
fileInfo.setFile( dir, value );
const File file( fileInfo.absoluteFilePath() );
out << file;
// list subdirectory if recursive
if( flags & Recursive && file.isDirectory() )
{
// in case directory is a link
// make sure it is not already in the list
// to avoid recursivity
if( file.isLink() && std::find_if( out.begin(), out.end(), SameLinkFTor( file ) ) != out.end() ) continue;
// list subdirectory
out << file.listFiles( flags );
}
}
return out;
}
//_____________________________________________________________________
File File::find( const File& file, bool caseSensitive ) const
{
if( !( exists() && isDirectory() ) ) return File();
// check local files
File local;
if( ( local = File( file ).addPath( *this ) ).exists() ) return local;
// get subdirectories
File fullname( *this );
if( !fullname.endsWith( "/" ) ) fullname += "/";
const QDir dir( fullname );
// filter
QDir::Filters filter = QDir::Dirs;
#if QT_VERSION >= 0x040800
filter |= QDir::NoDotDot;
#endif
foreach( const QString& value, dir.entryList( filter ) )
{
if( value == "." || value == ".." ) continue;
QFileInfo fileInfo;
fileInfo.setFile( dir, value );
const File local( fileInfo.absoluteFilePath() );
File found( local.find( file, caseSensitive ) );
if( !found.isEmpty() ) return found;
}
return File();
}
|