Skip to content

Commit

Permalink
Add '-progress' option
Browse files Browse the repository at this point in the history
Pursuant to #30, this commit
adds a '-progress' option. If set to true, then a function pointer is
passed to 'fillwithbytes', with the result that each iteration of the
loop in that function outputs a progress update.

The progress update appears at the end of the current line and
overwrites itself with each repetition, producing an output resembling
the following:

    Now eliminating candidates based on [...]: (16/19324)
  • Loading branch information
entrity committed Aug 31, 2024
1 parent a8ab653 commit 0f8838c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
11 changes: 11 additions & 0 deletions ListProgress.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "ListProgress.hh"

#include <iostream>

void
outputMlistProgress(int completed, int total)
{
std::cout << "\033[s\033[K" // Save the cursor position & clear following text
<< "(" << completed << "/" << total << ")" << std::flush
<< "\033[u"; // Restore the cursor to the saved position
}
7 changes: 7 additions & 0 deletions ListProgress.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef list_progress_hh
#define list_progress_hh

void
outputMlistProgress(int completed, int total);

#endif
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
AUTOMAKE_OPTIONS = gnu # I would like dist-bzip2 here, but automake complains
bin_PROGRAMS = rdfind
rdfind_SOURCES = rdfind.cc Checksum.cc Dirlist.cc Fileinfo.cc Rdutil.cc \
EasyRandom.cc UndoableUnlink.cc CmdlineParser.cc
EasyRandom.cc UndoableUnlink.cc CmdlineParser.cc ListProgress.cc

#these are the test scripts to execute - I do not know how to glob here,
#feedback welcome.
Expand Down
8 changes: 7 additions & 1 deletion Rdutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,20 @@ Rdutil::saveablespace(std::ostream& out) const
int
Rdutil::fillwithbytes(enum Fileinfo::readtobuffermode type,
enum Fileinfo::readtobuffermode lasttype,
const long nsecsleep)
const long nsecsleep,
void (*debugProgress)(int, int))
{
// first sort on inode (to read efficiently from the hard drive)
sortOnDeviceAndInode();

const auto duration = std::chrono::nanoseconds{ nsecsleep };

int progress = 0;
for (auto& elem : m_list) {
if (debugProgress) {
progress += 1;
debugProgress(progress, m_list.size());
}
elem.fillwithbytes(type, lasttype);
if (nsecsleep > 0) {
std::this_thread::sleep_for(duration);
Expand Down
3 changes: 2 additions & 1 deletion Rdutil.hh
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public:
int fillwithbytes(enum Fileinfo::readtobuffermode type,
enum Fileinfo::readtobuffermode lasttype =
Fileinfo::readtobuffermode::NOT_DEFINED,
long nsecsleep = 0);
long nsecsleep = 0,
void (*debugProgress)(int, int) = NULL);

/// make symlinks of duplicates.
std::size_t makesymlinks(bool dryrun) const;
Expand Down
7 changes: 6 additions & 1 deletion rdfind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Fileinfo.hh" //file container
#include "RdfindDebug.hh" //debug macro
#include "Rdutil.hh" //to do some work
#include "ListProgress.hh"

// global variables

Expand Down Expand Up @@ -72,6 +73,7 @@ usage()
<< " -makeresultsfile (true)| false makes a results file\n"
<< " -outputname name sets the results file name to \"name\" "
"(default results.txt)\n"
<< " -progress true |(false) output progress information"
<< " -deleteduplicates true |(false) delete duplicate files\n"
<< " -sleep Xms sleep for X milliseconds between "
"file reads.\n"
Expand Down Expand Up @@ -110,6 +112,7 @@ struct Options
bool deterministic = true; // be independent of filesystem order
long nsecsleep = 0; // number of nanoseconds to sleep between each file read.
std::string resultsfile = "results.txt"; // results file name.
void (*debugProgressFuncPtr)(int, int) = NULL; // call this function for each iteration of fillwithbytes
};

Options
Expand Down Expand Up @@ -208,6 +211,8 @@ parseOptions(Parser& parser)
<< nextarg << "\" is not among them.\n";
std::exit(EXIT_FAILURE);
}
} else if (parser.try_parse_bool("-progress") && parser.get_parsed_bool()) {
o.debugProgressFuncPtr = outputMlistProgress;
} else if (parser.current_arg_is("-help") || parser.current_arg_is("-h") ||
parser.current_arg_is("--help")) {
usage();
Expand Down Expand Up @@ -381,7 +386,7 @@ main(int narg, const char* argv[])
<< it->second << ": " << std::flush;

// read bytes (destroys the sorting, for disk reading efficiency)
gswd.fillwithbytes(it[0].first, it[-1].first, o.nsecsleep);
gswd.fillwithbytes(it[0].first, it[-1].first, o.nsecsleep, o.debugProgressFuncPtr);

// remove non-duplicates
std::cout << "removed " << gswd.removeUniqSizeAndBuffer()
Expand Down

0 comments on commit 0f8838c

Please sign in to comment.