File: Rdutil.hh

package info (click to toggle)
rdfind 1.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 516 kB
  • ctags: 227
  • sloc: cpp: 1,671; sh: 1,080; makefile: 10
file content (125 lines) | stat: -rw-r--r-- 4,411 bytes parent folder | download | duplicates (2)
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
/*
this file contains functions and templates that implement most of the
functionality in rdfind.

Author Paul Sundvall 2006
see LICENSE for details.
$Revision: 803 $
$Id: Rdutil.hh 803 2013-01-26 04:22:16Z paul $
 */
#ifndef rdutil_hh
#define rdutil_hh

#include <fstream> //for file writing
#include <algorithm>

#include "MultiAttributeCompare.hh"//for sorting on multiple attributes

#include "Fileinfo.hh"//file container

#include "algos.hh"//to find duplicates in a vector

class Rdutil {
public:
  Rdutil(std::vector<Fileinfo> &list) : 
    m_list(list) {};
  ~Rdutil(){};
  std::vector<Fileinfo> &m_list;
  //  std::vector<std::vector<Fileinfo> > &m_duplist;
  
public:
  //print file names to a file, with extra information.
  int printtofile(const std::string &filename);
  
  //mark files with a unique number
  int markitems();

  //sort list on multiple attributes.
  int sortlist(  bool (*lessthan1)(const Fileinfo&, const Fileinfo&),
		 bool (*equal1)(const Fileinfo&, const Fileinfo&),
		 bool (*lessthan2)(const Fileinfo&, const Fileinfo&)=NULL,
		 bool (*equal2)(const Fileinfo&, const Fileinfo&)=NULL,
		 bool (*lessthan3)(const Fileinfo&, const Fileinfo&)=NULL,
		 bool (*equal3)(const Fileinfo&, const Fileinfo&)=NULL,
		 bool (*lessthan4)(const Fileinfo&, const Fileinfo&)=NULL,
		 bool (*equal4)(const Fileinfo&, const Fileinfo&)=NULL);
 
  //cleans up, by removing all items that have the deleteflag set to true.
  int cleanup();

  //marks non unique elements for deletion. list must be sorted first.
  //this is good to eliminate duplicates on inode, to prevent from 
  //reading hardlinked files, or repeated input arguments to the main program.
  int marknonuniq(bool (*equal1)(const Fileinfo&, const Fileinfo&),
		  bool (*equal2)(const Fileinfo&, const Fileinfo&)=NULL,
		  bool (*equal3)(const Fileinfo&, const Fileinfo&)=NULL,
		  bool (*equal4)(const Fileinfo&, const Fileinfo&)=NULL);

  //marks uniq elements for deletion (remember, this is a duplicate finder!)
  // list must be sorted first, before calling this.
  int markuniq(bool (*equal1)(const Fileinfo&, const Fileinfo&),
	       bool (*equal2)(const Fileinfo&, const Fileinfo&)=NULL,
	       bool (*equal3)(const Fileinfo&, const Fileinfo&)=NULL,
	       bool (*equal4)(const Fileinfo&, const Fileinfo&)=NULL);

  //marks duplicates with tags, depending on their nature. 
  //shall be used when everything is done, and sorted.
  int markduplicates(bool (*equal1)(const Fileinfo&, const Fileinfo&),
		     bool (*equal2)(const Fileinfo&, const Fileinfo&)=NULL,
		     bool (*equal3)(const Fileinfo&, const Fileinfo&)=NULL,
		     bool (*equal4)(const Fileinfo&, const Fileinfo&)=NULL);
  
  //subfunction to above
  int marksingle(std::vector<Fileinfo>::iterator start,
		 std::vector<Fileinfo>::iterator stop);
  

  //removes items that rem returns true for.
  //returns the number of removed elements.
  int remove_if();

  //read some bytes. note! destroys the order of the list.
  //if lasttype is supplied, it does not reread files if they are shorter
  //than the file length. (unnecessary!). if -1, feature is turned off.
  //and file is read anyway. 
  //if there is trouble with too much disk reading, sleeping for nsecsleep
  //nanoseconds can be made between each file.
  int fillwithbytes(enum Fileinfo::readtobuffermode type,
		    enum Fileinfo::readtobuffermode lasttype=Fileinfo::NOT_DEFINED,
		    long nsecsleep=0);
  
  //make symlinks of duplicates.
  int makesymlinks(bool dryrun);

  //make hardlinks of duplicates.
  int makehardlinks(bool dryrun);

  //delete duplicates.
  int deleteduplicates(bool dryrun);

  //a little helper class 
  class adder_helper {
  public:
    adder_helper() : m_sum(0) {};
    typedef unsigned long long int sizetype_t;
    sizetype_t m_sum;
    void operator()(const Fileinfo &A) {
      m_sum+=static_cast<sizetype_t>(A.size());
    }
    sizetype_t getsize(void) const {return m_sum;}
  };

  //gets the total size, in bytes.
  //opmode=0 just add everything
  //opmode=1 only elements with m_duptype=Fileinfo::DUPTYPE_FIRST_OCCURRENCE
  unsigned long long int totalsizeinbytes(int opmode=0) const;

  //outputs a nicely formatted string "45 bytes" or "3 Gibytes"
  //where 1024 is used as base
  std::ostream& totalsize(std::ostream &out, int opmode=0);

  //outputs the saveable amount of space
  std::ostream& saveablespace(std::ostream &out);
};

#endif