summaryrefslogtreecommitdiff
path: root/support/lintex/cxx/cleandir.cxx
blob: 799f702c3501f57ac974ab26ff2c034a499e79cb (plain)
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
//     Author: Maurizio Loreti, aka MLO or (HAM) I3NOO
//     Work:   University of Padova - Department of Physics
//             Via F. Marzolo, 8 - 35131 PADOVA - Italy
//     Phone:  +39 (049) 827-7216   FAX: +39 (049) 827-7102
//     EMail:  loreti@pd.infn.it
//     WWW:    http://www.pd.infn.it/~loreti/mlo.html
//
// -------------------------------------------------------------------
//
//     $Id: cleandir.cxx,v 1.11 2002/09/25 12:19:41 loreti Exp $
//
// -------------------------------------------------------------------

#include <algorithm>
#include <iterator>
#include <vector>
#include <cstring>
#include "ltx.hh"               // Includes: functional, iostream, string
#include "cleandir.hh"          // Includes: string
#include "cleanup.hh"           // Includes: string
#include "file.hh"              // Includes: list, map, string, utility, ctime

extern "C" {
  #include <dirent.h>
  #include <sys/stat.h>
  #include <sys/types.h>
}

using std::cerr;
using std::cout;
using std::strcmp;
using std::string;

// Local variables

namespace {
  // In "re" are defined the extensions of the files relevant for
  // LaTeX; ".tex" is missing, being handled separately.  THIS ARRAY
  // MUST BE SORTED IN ASCENDING ORDER.

  const char * re[] = {
    ".aux", ".dvi", ".idx", ".ilg", ".ind",
    ".lof", ".log", ".lot", ".pdf", ".ps",
    ".toc"
  };
  const size_t nRE = sizeof(re) / sizeof(re[0]);
  std::vector<string> texExts(re, re + nRE);

  const string tex(".tex");
  const string dot(".");

  const size_t filename_max = 256;
}

// Local functions (declarations)

namespace {
  void check_file(const string &, const time_t, currDir &);
}

// Code

void scan_dir(
  const string & name
) {
  // Scans the directory "name", building the related instantiation of
  // the class "currDir" containing all the informations for the
  // relevant files; then calls "clean_dir" to perform the actual
  // cleanup.  If the "-r" options has been specified, recurses over
  // all the directories under the current one.

#if defined(DEBUG)
  static bool firstTime(true);

  if (firstTime) {
    cout << "--------------------Relevant extensions ("
         << nRE << ")\n";
    copy(texExts.begin(), texExts.end(),
         std::ostream_iterator<string>(cout, " "));
    cout << std::endl;
    firstTime = false;
  }

  cout << "--------------------scan_dir called for \""
       << name << "\"\n";
#endif // DEBUG

  DIR * pDir;

  if ((pDir = opendir( name.c_str() )) != 0) {

    string fullName(name);
    if (*(fullName.rbegin()) != '/') fullName.append("/");

    currDir             thisDir(fullName);
    std::list<string>   subDirs;
    struct dirent     * pDe;

    // Reads every file: skips null inodes (already deleted
    // files), and the two special files "." and ".." .

    while ((pDe = readdir(pDir)) != 0) {
      if (pDe->d_ino == 0) continue;

#if defined(DEBUG)
      cout << "Next file: " << pDe->d_name << " - ";
#endif // DEBUG

      if (strcmp(pDe->d_name, ".")  == 0) {
#if defined(DEBUG)
        cout << "skipped\n";
#endif // DEBUG
        continue;
      }

      if (strcmp(pDe->d_name, "..") == 0) {
#if defined(DEBUG)
        cout << "skipped\n";
#endif // DEBUG
        continue;
      }

      // Gets the file related informations with stat(2) (we need file
      // type and modification time).  If the call to "stat" fails,
      // the file is not considered.

      char tName[filename_max];
      std::strcpy(tName, fullName.c_str());
      std::strcat(tName, pDe->d_name);

      struct stat sStat;

      if (stat(tName, &sStat) != 0) {
#if defined(DEBUG)
        cout << "got error from stat()\n";
#else
        cerr << ltx::progname << ": error calling stat("
             << tName << ")\n";
#endif // DEBUG

      } else {
        if (S_ISDIR(sStat.st_mode) != 0) {
#if defined(DEBUG)
          cout << "is a directory\n";
#endif // DEBUG

          // If needed, push the subdirectory names in the dedicated
          // list, for future recursion; plain files are handled by
          // the local procedure check_file().

          if (ltx::recurse) subDirs.push_back(tName);

        } else {
          check_file(pDe->d_name, sStat.st_mtime, thisDir);
        }
      }
    }

    // Looks if some cleanup has to be performed

    clean_files(thisDir);

    if (ltx::recurse) {
      for_each(subDirs.begin(), subDirs.end(), std::ptr_fun(scan_dir));
    }

    closedir(pDir);
  } else {
    cerr << ltx::progname << ": \"" << name
         << "\" could not be opened (or is not a directory)\n";
  }
}

namespace {
  void check_file(
    const string & name,
    const time_t   mTime,
    currDir      & CDir
  ) {
    // - If the file "name" matches the trailing string identifying
    //   backup editor files, is removed;
    // - if it matches a relevant extension, is inserted in the
    //   "currDir" instance.

    string::size_type where;

    if (ltx::lTrailEd > 0) {
      if ((where = name.rfind(ltx::trailEd)) != string::npos) {
        if ((where + ltx::lTrailEd) == name.size()) {
  #if defined(DEBUG)
          cout << "matches the default editor extension\n";
  #endif // DEBUG
          nuke(CDir.getName(), name);
          return;
        }
      }
    }

    // Breaks the file name in "basename" and "extension"; and checks
    // the latter against the known ones (".tex" files are handled
    // separately).

    if ((where = name.find_last_of(dot)) != string::npos) {
      string       extension = name.substr(where);
      string       basename  = name.substr(0, where);
      fileFamily & fF        = CDir.getFileFamily(basename);

      if (extension == tex) {
        fF.addExtension(mTime, 0);
  #if defined(DEBUG)
        cout << "inserted\n";
  #endif // DEBUG

      } else if (binary_search(texExts.begin(), texExts.end(), extension)) {
        fF.addExtension(mTime, &extension);
  #if defined(DEBUG)
        cout << "extension " << extension << " - inserted\n";
      } else {
        cout << "extension not relevant\n";
  #endif // DEBUG

      }

  #if defined(DEBUG)
    } else {
      cout << "no extension\n";
  #endif // DEBUG
    }
  }
}