NCBI C++ ToolKit
file_obsolete.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: file_obsolete.cpp 35686 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 
44 
45 
46 CFileObsolete::CFileObsolete(const string& path)
47  : m_Path(path)
48 {
49 }
50 
51 
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 
ncbi::TMaskedQueryRegions mask
CDir –.
Definition: ncbifile.hpp:1695
void Remove(const string &mask, unsigned int age, ETimeMode tmode=eLastAccess)
Scan the target directory, find old files matching the specified mask, files deleted if permission is...
virtual bool OnRemove(const string &filename)
Reactor function called when file is about to be deleted.
CFileObsolete(const string &path)
string m_Path
Base path.
virtual ~CFileObsolete()
ETimeMode
Defines if file is deleted based on its creation time (attribute modification) or last access time.
@ eLastModification
Last modification.
@ eLastAccess
Last access time.
CTime –.
Definition: ncbitime.hpp:296
Delete old files from the specified directory.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
Definition: ncbidiag.hpp:550
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
TEntries GetEntries(const string &mask=kEmptyStr, TGetEntriesFlags flags=0) const
Get directory entries based on the specified "mask".
Definition: ncbifile.cpp:3846
virtual bool Exists(void) const
Check if directory "dirname" exists.
Definition: ncbifile.hpp:4065
list< TEntry > TEntries
Definition: ncbifile.hpp:1750
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
time_t GetTimeT(void) const
Get time in time_t format.
Definition: ncbitime.cpp:1395
@ eCurrent
Use current time. See also CCurrentTime.
Definition: ncbitime.hpp:300
Definition of all error codes used in util (xutil.lib).
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
Defines: CTimeFormat - storage class for time format.
#define _ASSERT
Modified on Fri Mar 01 10:05:22 2024 by modify_doxy.py rev. 669887