Update main.zig
This commit is contained in:
58
main.zig
58
main.zig
@@ -1,17 +1,20 @@
|
||||
// using zig-0.15.2
|
||||
const std = @import("std");
|
||||
const sha256 = std.crypto.hash.sha2.Sha256;
|
||||
const stdout = std.fs.File.stdout();
|
||||
const stderr = std.fs.File.stderr();
|
||||
|
||||
fn err_print(comptime fmt: []const u8, args: anytype) void {
|
||||
var writer = stderr.writer(&.{});
|
||||
var buf: [1024 * 16]u8 = undefined;
|
||||
var writer = stderr.writer(&buf);
|
||||
writer.interface.print(fmt, args) catch unreachable;
|
||||
writer.interface.flush() catch unreachable;
|
||||
}
|
||||
|
||||
fn out_print(comptime fmt: []const u8, args: anytype) void {
|
||||
var writer = stdout.writer(&.{});
|
||||
var buf: [1024 * 16]u8 = undefined;
|
||||
var writer = stdout.writer(&buf);
|
||||
writer.interface.print(fmt, args) catch unreachable;
|
||||
writer.interface.flush() catch unreachable;
|
||||
}
|
||||
|
||||
const File = struct {
|
||||
@@ -23,19 +26,27 @@ const File = struct {
|
||||
|
||||
var hashcount: usize = 0;
|
||||
|
||||
fn hashFile(f: std.fs.File, allocator: std.mem.Allocator) !u256 {
|
||||
hashcount += 1;
|
||||
var buf: [1024 * 32]u8 = undefined;
|
||||
var reader = f.reader(&buf);
|
||||
const file_contents = try reader.interface.allocRemaining(allocator, .unlimited);
|
||||
defer allocator.free(file_contents);
|
||||
var hash_buff: [sha256.digest_length]u8 = undefined;
|
||||
sha256.hash(file_contents, &hash_buff, .{});
|
||||
return std.mem.bytesToValue(u256, &hash_buff);
|
||||
}
|
||||
fn cmp_file(path1: []const u8, path2: []const u8) !bool {
|
||||
var buf1: [1024 * 16]u8 = undefined;
|
||||
var buf2: [1024 * 16]u8 = undefined;
|
||||
const file1 = try std.fs.openFileAbsolute(path1, .{});
|
||||
defer file1.close();
|
||||
var f1reader = file1.reader(&buf1);
|
||||
const file2 = try std.fs.openFileAbsolute(path2, .{});
|
||||
var f2reader = file2.reader(&buf2);
|
||||
defer file2.close();
|
||||
|
||||
fn cmp_fn(_: u8, lhs: File, rhs: File) bool {
|
||||
return lhs.size < rhs.size;
|
||||
while (true) {
|
||||
var contents1: [1024 * 16]u8 = undefined;
|
||||
var contents2: [1024 * 16]u8 = undefined;
|
||||
const n1 = try f1reader.interface.readSliceShort(&contents1);
|
||||
const n2 = try f2reader.interface.readSliceShort(&contents2);
|
||||
if (n1 != n2) return false;
|
||||
if (!std.mem.eql(u8, contents1[0..n1], contents2[0..n2])) return false;
|
||||
if (n1 < contents1.len) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
@@ -68,11 +79,6 @@ pub fn main() !void {
|
||||
}
|
||||
}
|
||||
|
||||
std.mem.sort(File, list.items, @as(u8, 0), cmp_fn);
|
||||
for (list.items) |*item| {
|
||||
err_print("file: {any}\n", .{item.size});
|
||||
}
|
||||
|
||||
for (list.items, 0..) |*item, idx| {
|
||||
if (item.solved) continue;
|
||||
var duplicates: std.ArrayList(usize) = .empty;
|
||||
@@ -82,17 +88,7 @@ pub fn main() !void {
|
||||
if (std.mem.eql(u8, item.path, item2.path)) continue;
|
||||
|
||||
if (item.size == item2.size) {
|
||||
if (item.hash == null) {
|
||||
const file = try std.fs.openFileAbsolute(item.path, .{});
|
||||
defer file.close();
|
||||
item.hash = try hashFile(file, allocator);
|
||||
}
|
||||
if (item2.hash == null) {
|
||||
const file = try std.fs.openFileAbsolute(item2.path, .{});
|
||||
defer file.close();
|
||||
item2.hash = try hashFile(file, allocator);
|
||||
}
|
||||
if (item.hash.? == item2.hash.?) {
|
||||
if (try cmp_file(item.path, item2.path)) {
|
||||
if (duplicates.items.len == 0) {
|
||||
try duplicates.append(allocator, idx);
|
||||
item.solved = true;
|
||||
|
||||
Reference in New Issue
Block a user