ECode FileUtils::FileComparator::Compare( /* [in] */ IInterface* inlhs, /* [in] */ IInterface* inrhs, /* [out] */ Int32* result) { VALIDATE_NOT_NULL(result) IFile* lhs = IFile::Probe(inlhs); IFile* rhs = IFile::Probe(inrhs); if (lhs == NULL && rhs == NULL) { *result = 0; return NOERROR; } if (lhs != NULL && rhs == NULL) { *result = -1; return NOERROR; } else if (lhs == NULL && rhs != NULL) { *result = 1; return NOERROR; } Int64 l, r; lhs->GetLastModified(&l); rhs->GetLastModified(&r); *result = (Int32)(r - l); return NOERROR; }
Boolean FileUtils::DeleteOlderFiles( /* [in] */ IFile* dir, /* [in] */ Int32 minCount, /* [in] */ Int64 minAge) { if (minCount < 0 || minAge < 0) { // throw new IllegalArgumentException("Constraints must be positive or 0"); return FALSE; } AutoPtr<ArrayOf<IFile*> > files; dir->ListFiles((ArrayOf<IFile*>**)&files); if (files == NULL) return FALSE; // Sort with newest files first AutoPtr<IComparator> cmp = new FileComparator(); Arrays::Sort(files.Get(), cmp.Get()); AutoPtr<ISystem> system; CSystem::AcquireSingleton((ISystem**)&system); // Keep at least minCount files Boolean deleted = FALSE, bval; Int64 age, lastModified; for (Int32 i = minCount; i < files->GetLength(); i++) { IFile* file = (*files)[i]; // Keep files newer than minAge system->GetCurrentTimeMillis(&age); file->GetLastModified(&lastModified); age -= lastModified; if (age > minAge) { if (file->Delete(&bval), bval) { // Logger::D(TAG, "Deleted old file %s", file); deleted = true; } } } return deleted; }