NCBI C++ Toolkit Cross Reference

C++/src/util/file_obsolete.cpp


  1 /*  $Id: file_obsolete.cpp 112032 2007-10-10 19:03:47Z ivanovp $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Author:  Anatoliy Kuznetsov
 27  *
 28  * File Description:
 29  *   Remove old files from the specified directory
 30  *
 31  */
 32 
 33 #include <ncbi_pch.hpp>
 34 #include <util/file_obsolete.hpp>
 35 #include <corelib/ncbitime.hpp>
 36 #include <corelib/ncbifile.hpp>
 37 #include <util/error_codes.hpp>
 38 
 39 
 40 #define NCBI_USE_ERRCODE_X   Util_File
 41 
 42 
 43 BEGIN_NCBI_SCOPE
 44 
 45 
 46 CFileObsolete::CFileObsolete(const string& path)
 47     : m_Path(path)
 48 {
 49 }
 50 
 51 
 52 CFileObsolete::~CFileObsolete()
 53 {
 54 }
 55 
 56 
 57 bool CFileObsolete::OnRemove(const string& /* filename */)
 58 {
 59     return true;
 60 }
 61 
 62 
 63 void CFileObsolete::Remove(const string&  mask, 
 64                            unsigned int   age,
 65                            ETimeMode      tmode)
 66 {
 67     CDir dir(m_Path);
 68     if (!dir.Exists()) {
 69         ERR_POST_X(1, Info << "Directory is not found or access denied:" << m_Path);
 70         return;
 71     }
 72 
 73     CTime current(CTime::eCurrent);
 74     time_t cutoff_time = current.GetTimeT();
 75 
 76     if (cutoff_time < (time_t) age) {
 77         cutoff_time = 0;
 78     } else {
 79         cutoff_time -= age;
 80     }
 81 
 82     CDir::TEntries  content(dir.GetEntries(mask));
 83     ITERATE(CDir::TEntries, it, content) {
 84 
 85         if (!(*it)->IsFile()) {
 86             continue;
 87         }
 88 
 89         CTime modification;
 90         CTime creation;
 91         CTime access;
 92 
 93         bool res = 
 94             (*it)->GetTime(&modification, &access, &creation);
 95 
 96         if (!res) {
 97             continue;
 98         }
 99 
100         time_t check_time = 0;
101 
102         switch (tmode) {
103         case eLastModification:
104             check_time = modification.GetTimeT();
105             break;
106         case eLastAccess:
107             check_time = access.GetTimeT();
108             break;
109         default:
110             _ASSERT(0);
111             continue;
112         } // switch
113 
114         if (check_time < cutoff_time) {  // Remove file
115             (*it)->Remove();
116         }
117 
118     } // ITERATE
119 }
120 
121 
122 END_NCBI_SCOPE
123 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.