summaryrefslogtreecommitdiff
path: root/main.ha
blob: 28d8658e5c0301da151900245d0610a5fe94f4af (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use fmt;
use fs;
use getopt;
use io;
use os;
use strings;

// Version 0.5
// TODO: do something about existing directories/files

// Also does the same exact thing as `cp -lr` except cp is less likely to have
// strange bugs in it.

// Global Vars
let verbose:  bool = false;
let test_run: bool = false;

export fn main() void = {
	const help: [_]getopt::help = [
		"Hard Link Copy for directories.",
		('d', "destination", "The destination which will be hard link copied to.\n  If it does not exist it will be created."),
		('s', "source", "The source directory which will be hard link copied from. Must be a directory."),
		('t', "Test-run. This indicates that nothing should actually occur but look like it will. Useful with verbose."),
		('v', "Toggle verbose - quiet by default unless error."),
	];
	const cmd = getopt::parse(os::args, help...);
	defer getopt::finish(&cmd);

	let destination: str  = "";
	let source:      str  = "";
	for (let i = 0z; i < len(cmd.opts); i += 1) {
		const opt = cmd.opts[i];
		switch (opt.0) {
		case 'd' =>
			destination = opt.1;
		case 's' =>
			source = opt.1;
		case 't' =>
			test_run = !test_run;
		case 'v' =>
			verbose = !verbose;
		};
	};

	if (len(destination) > 0 && len(source) > 0) {
		if (destination == source) {
			fmt::fatal("destination cannot be the source.");
		};
	} else {
		usage(help);
	};

	if (! fs::exists(os::cwd, source) ) {
		fmt::fatal("source doesn't exist.");
	};

	const source_fh = itorator(source);
	const source_resolve = strings::dup(fs::resolve(os::cwd, source));
	const dest_resolve = strings::dup(fs::resolve(os::cwd, destination));

	if (! fs::exists(os::cwd, destination) ) {
		const mode = fs::mode::USER_RWX + fs::mode::GROUP_RX + fs::mode::OTHER_RX;
		match (os::mkdir(dest_resolve, mode)) {
		case void =>
			yield;
		case let err: fs::error =>
			fmt::fatalf("Cannot mkdir {}: {}", dest_resolve, fs::strerror(err));
		};
	};

	needful(source_fh, source_resolve, dest_resolve);
	os::exit(0);
};

fn itorator(source: str) *fs::iterator = {
	const source_fh = match (os::iter( fs::resolve(os::cwd, source) )) {
	case let f: *fs::iterator =>
		yield f;
	case let err: fs::error =>
		fmt::fatalf("Unable to open {}: {}",
			source, fs::strerror(err));
	};
	return source_fh;
};

fn needful(source_fh: *fs::iterator, source: str, destination: str) void = {
	for (true) {
		const file = match (fs::next(source_fh)) {
		case let f: fs::dirent =>
			yield f;
		case void =>
			break;
		};

		if (file.name == "." || file.name == "..") {
			continue;
		};


		const source_concat =
			strings::dup(fs::resolve(os::cwd, strings::concat(source, "/", file.name)));
		defer source_concat;

		const dest_concat =
			strings::dup(fs::resolve(os::cwd, strings::concat(destination, "/", file.name)));
		defer dest_concat;

		//fmt::println(source_concat)!;
		//fmt::println(dest_concat)!;
		//fmt::println(fs::mode_str(file.ftype))!;

		if (fs::isdir(file.ftype)) {
			if (verbose) {
				fmt::printfln("{} is a directory", source_concat)!;
			};
			if (! fs::exists(os::cwd, dest_concat) ) {
				if (! test_run) {
					// Ideally would just copy file.ftype
					// but file.ftype is either d--------- or
					// ----------. Dunno why.
					const mode =
						fs::mode::USER_RWX + fs::mode::GROUP_RX + fs::mode::OTHER_RX;
					match (os::mkdir(dest_concat, mode)) {
					case void =>
						yield;
					case let err: fs::error =>
						fmt::errorfln("Cannot mkdir {}: {}",
							dest_concat, fs::strerror(err))!;
					};
				};
				if (verbose) {
					fmt::printfln("{} making dir.", dest_concat)!;
				};
			};

			const dir_fh = itorator(source_concat);
			needful(dir_fh, fs::resolve(os::cwd, source_concat), dest_concat);
		} else {
			if (! test_run) {
				match (fs::link(os::cwd, source_concat, dest_concat)) {
				case void =>
					yield;
				case let err: fs::error =>
					fmt::errorfln("Cannot hardlink {}: {}",
						dest_concat, fs::strerror(err))!;
				};
			};
			if (verbose) {
				fmt::printfln("{} hard linked.", dest_concat)!;
			};
		};
	};
};

@noreturn fn usage(help: []getopt::help) void = {
	getopt::printusage(os::stderr, os::args[0], help);
	os::exit(1);
};