Thread overview
Phobos function to check if files are identical?
Mar 13, 2017
XavierAP
Mar 13, 2017
H. S. Teoh
Mar 13, 2017
XavierAP
Mar 14, 2017
XavierAP
Mar 14, 2017
Andrea Fontana
Mar 14, 2017
XavierAP
Mar 14, 2017
flamencofantasy
Mar 14, 2017
XavierAP
Mar 13, 2017
H. S. Teoh
March 13, 2017
It's not easy to do by hand of course, but I was wondering if there was one simple function taking two file names and just returning a bool or something like that. I haven't found it in std.file.

If such a function doesn't exist in Phobos but there's a good implementation in some other library, I'm interested to know. Although this time it's for a unit test so I'd rather implement it in two lines than add a dependency.

And otherwise to write it by hand, how do you think is the best way? And in terms of performance? By chunks in case of a binary comparison? And what about the case of a text comparison?

Thanks
March 13, 2017
On Mon, Mar 13, 2017 at 04:50:49PM +0000, XavierAP via Digitalmars-d-learn wrote:
> It's not easy to do by hand of course, but I was wondering if there was one simple function taking two file names and just returning a bool or something like that. I haven't found it in std.file.

Why it is not easy to do by hand?  All you have to do is open the two files, then iterate over their data and compare.  Of course, you'd want to chunk them up to minimize I/O roundtrips, but it'd be something along the lines of:

	bool isEqual(string filename1, string filename2) {
		import std.algorithm.comparison : equal;
		import std.range : zip;
		import std.stdio : File, chunks;

		auto f1 = File(filename1);
		auto f2 = File(filename2);

		size_t blockSize = 4096; // or something similar

		return f1.chunks(blockSize).equal(f2.chunks(blockSize));
	}


> If such a function doesn't exist in Phobos but there's a good implementation in some other library, I'm interested to know. Although this time it's for a unit test so I'd rather implement it in two lines than add a dependency.
> 
> And otherwise to write it by hand, how do you think is the best way? And in terms of performance? By chunks in case of a binary comparison? And what about the case of a text comparison?
[...]

Binary comparison is easy. Just read the files by fixed-sized chunks and compare them.

Text comparison is a can of worms. What kind of comparison do you have in mind? Case-sensitive or insensitive? Is it ASCII or Unicode? What kind of Unicode encoding is involved?  Are the files expected to be in one of the normalization forms, or is it free-for-all?  Do you expect grapheme equivalence or just character equivalence?  Depending on the answer to these questions, text comparison can range from trivial to hair-raisingly complex.

But the fundamental ideas remain the same: read a stream of characters / graphemes from each file in tandem, preferably buffered, and compare them using some given comparison function.


P.S. I just realized that std.stdio.chunks() doesn't return a range.
Bah. File an enhancement request. I might even submit a PR for it. ;-)


T

-- 
There are four kinds of lies: lies, damn lies, and statistics.
March 13, 2017
P.P.S.  It's not overly hard to write an alternative version of std.stdio.chunks that returns a real range. Something like this should do:

	// Warning: untested code
	auto realChunks(File f, size_t blockSize)
	{
		static struct Result
		{
			private File f;
			private ubyte[] buffer;
			bool empty = true;
			ubyte[] front;

			this(File _f, size_t blockSize)
			{
				f = _f;
				buffer.length = blockSize;
				empty = false;
				popFront();
			}
			void popFront()
			{
				front = f.rawRead(buffer);
				if (front.length == 0)
					empty = true;
			}
		}
		return Result(f, blockSize);
	}


T

-- 
A program should be written to model the concepts of the task it performs rather than the physical world or a process because this maximizes the potential for it to be applied to tasks that are conceptually similar and, more important, to tasks that have not yet been conceived. -- Michael B. Allen
March 13, 2017
On Monday, 13 March 2017 at 17:47:09 UTC, H. S. Teoh wrote:
>
> Why it is not easy to do by hand?

Sorry typo, I had intended to type "I know it is easy"
March 14, 2017
On Monday, 13 March 2017 at 17:47:09 UTC, H. S. Teoh wrote:
>
> Binary comparison is easy. Just read the files by fixed-sized chunks and compare them.

Follow up question... What is the best @safe way? Since File.byChunk() is @system. Just out of curiosity, I would rather use it and flag my code @trusted, although I guess there could be concurrency issues I have to take into account anyway... anything else?
March 14, 2017
On Monday, 13 March 2017 at 17:47:09 UTC, H. S. Teoh wrote:
> 	bool isEqual(string filename1, string filename2) {
> 		import std.algorithm.comparison : equal;
> 		import std.range : zip;
> 		import std.stdio : File, chunks;
>
> 		auto f1 = File(filename1);
> 		auto f2 = File(filename2);
>
> 		size_t blockSize = 4096; // or something similar
>
> 		return f1.chunks(blockSize).equal(f2.chunks(blockSize));
> 	}

First I would check if the files have different size or if they are the same file (same path, symlink, etc).


March 14, 2017
On Tuesday, 14 March 2017 at 08:12:16 UTC, Andrea Fontana wrote:
>
> First I would check if the files have different size or if they are the same file (same path, symlink, etc).

Good idea. Good reason to have it in std.file. There might also be platform dependent shortcuts?
March 14, 2017
On Tuesday, 14 March 2017 at 08:31:20 UTC, XavierAP wrote:
> On Tuesday, 14 March 2017 at 08:12:16 UTC, Andrea Fontana wrote:
>>
>> First I would check if the files have different size or if they are the same file (same path, symlink, etc).
>
> Good idea. Good reason to have it in std.file. There might also be platform dependent shortcuts?


  import std.mmfile;

  auto f1 = new MmFile("file1");
  auto f2 = new MmFile("file2");

  return f1[] == f2[];

March 14, 2017
On Tuesday, 14 March 2017 at 18:26:52 UTC, flamencofantasy wrote:
>
>   import std.mmfile;
>
>   auto f1 = new MmFile("file1");
>   auto f2 = new MmFile("file2");
>
>   return f1[] == f2[];

Nice! I don't have experience with memory-mapped files. What are the pros and cons?