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