Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ count below and mark it as done in this README.md. Thanks!
GNU coreutils. They are not 100% compatiable. If you encounter different behaviors,
compare against the true GNU coreutils version on the Linux-based tests first.

## Completed (76/109) - 69% done!
## Completed (79/109) - 72% done!

| Done | Cmd | Descripton | Windows |
| :-----: | --------- | ------------------------------------------------ | ------- |
Expand All @@ -70,7 +70,7 @@ compare against the true GNU coreutils version on the Linux-based tests first.
| ✓ | comm | Compare two sorted files line by line | ✓ |
| | coreutils | Multi-call program | ✓ |
| ✓ | cp | Copy files and directories | ✓ |
| | csplit | Split a file into context-determined pieces | ✓ |
| ✓ | csplit | Split a file into context-determined pieces | ✓ |
| ✓ | cut | Print selected parts of lines | ✓ |
| | date | Print or set system date and time | ✓ |
| | dd | Convert and copy a file | ✓ |
Expand All @@ -93,7 +93,7 @@ compare against the true GNU coreutils version on the Linux-based tests first.
| ✓ | hostname | Print or set system name | |
| ✓ | id | Print user identity | |
| | install | Copy files and set attributes | |
| | join | Join lines on a common field | ✓ |
| ✓ | join | Join lines on a common field | ✓ |
| | kill | Send a signal to processes | ✓ |
| ✓ | link | Make a hard link via the link syscall | ✓ |
| ✓ | ln | Make links between files | ✓ |
Expand Down
55 changes: 55 additions & 0 deletions common/progress.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// A progress bar for cp and mv's -progress, drawn on standard error so it does not
// interfere with the tool's own output on standard output. advcpmv draws on stderr
// too.
module common

import os

struct ProgressBar {
total int
width int
mut:
current int
}

pub fn new_progress_bar(total int) ProgressBar {
return ProgressBar{
total: total
width: 40
}
}

pub fn (mut b ProgressBar) update(n int) {
b.current += n
pct := if b.total > 0 { b.current * 100 / b.total } else { 100 }
mut filled := if b.total > 0 { b.current * b.width / b.total } else { b.width }
if filled > b.width {
filled = b.width
}
mut bar := ''
for i in 0 .. b.width {
bar += if i < filled { '=' } else { ' ' }
}
// Carriage return returns to the start of the line, so the next update
// overwrites this one rather than scrolling.
eprint('\r[${bar}] ${pct}%')
}

pub fn (mut b ProgressBar) finish() {
eprint('\n')
}

// dir_size totals the bytes under a path, walking directories. This is what the
// bar needs before it starts, and it is why -progress is slower than a plain
// copy: the whole tree is stat'd before the first byte moves.
pub fn dir_size(path string) int {
if !os.is_dir(path) {
return os.file_size(path)
}
mut total := 0
entries := os.ls(path) or { [] }
for entry in entries {
total += dir_size(os.join_path(path, entry))
}
return total
}
18 changes: 16 additions & 2 deletions src/cp/CpCommand.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import common

enum OverwriteMode {
force
Expand All @@ -10,6 +11,7 @@ struct CpCommand {
overwrite OverwriteMode
update bool
verbose bool
progress bool
target_directory string
no_target_directory bool
recursive bool
Expand All @@ -20,7 +22,7 @@ fn (c CpCommand) run(source string, dest string) {
eprintln(not_exist(source))
return
}
if c.verbose || c.overwrite != .force {
if c.verbose || c.progress || c.overwrite != .force {
c.copy(source, dest)
} else {
if os.is_dir(source) && !c.recursive {
Expand All @@ -43,7 +45,19 @@ fn (c CpCommand) copy(src string, dst string) {
if !c.int_yes(rdst) {
return
}
os.cp(src, rdst) or { return }
if c.progress {
// The size is collected up front so the bar has a total to work against,
// which is the cost -progress pays for showing progress at all.
mut bar := common.new_progress_bar(common.dir_size(src))
if os.is_dir(src) {
copy_dir_progress(src, rdst, mut bar)
} else {
copy_file_progress(src, rdst, mut bar)
}
bar.finish()
} else {
os.cp(src, rdst) or { return }
}
if c.verbose {
println(renamed(src, dst))
}
Expand Down
69 changes: 69 additions & 0 deletions src/cp/cp_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import common.testing
import os

const rig = testing.prepare_rig(util: 'cp')

const src_path = 'cp_src.txt'
const dst_path = 'cp_dst.txt'
const dir_src = 'cp_src_dir'
const dir_dst = 'cp_dst_dir'

fn testsuite_begin() {
os.write_file(src_path, 'the quick brown fox\n')!
os.mkdir(dir_src)!
os.write_file(os.join_path(dir_src, 'a.txt'), 'aaa\n')!
os.write_file(os.join_path(dir_src, 'b.txt'), 'bbbb\n')!
os.mkdir(os.join_path(dir_src, 'sub'))!
os.write_file(os.join_path(dir_src, 'sub', 'c.txt'), 'cc\n')!
}

fn testsuite_end() {
// The two directory tests both write into dir_dst, so the tree is taken down
// file by file. os.rmdir panics rather than reporting a non-empty directory, so
// each step ignores its own failure.
os.rm(src_path) or {}
os.rm(dst_path) or {}
for root in [dir_src, dir_dst] {
os.rm(os.join_path(root, 'sub', 'c.txt')) or {}
os.rm(os.join_path(root, 'a.txt')) or {}
os.rm(os.join_path(root, 'b.txt')) or {}
os.rmdir(os.join_path(root, 'sub')) or {}
os.rmdir(root) or {}
}
}

fn test_help_and_version() {
rig.assert_help_and_version_options_work()
}

// --progress is a long option with no short form, so it is spelled with two
// dashes. A single dash leaves it as a free argument and the copy fails.
fn test_progress_is_listed_in_help() {
res := os.execute('${rig.executable_under_test} --help')
assert res.exit_code == 0
assert res.output.contains('--progress')
}

fn test_progress_copies_a_file() {
res := os.execute('${rig.executable_under_test} --progress ${src_path} ${dst_path}')
assert res.exit_code == 0
assert os.read_bytes(dst_path)! == os.read_bytes(src_path)!
}

fn test_progress_copies_a_directory() {
res := os.execute('${rig.executable_under_test} --progress -r ${dir_src} ${dir_dst}')
assert res.exit_code == 0
assert os.read_bytes(os.join_path(dir_dst, 'a.txt'))! == 'aaa\n'.bytes()
assert os.read_bytes(os.join_path(dir_dst, 'b.txt'))! == 'bbbb\n'.bytes()
assert os.read_bytes(os.join_path(dir_dst, 'sub', 'c.txt'))! == 'cc\n'.bytes()
}

// The bar is drawn on standard error, which os.execute captures along with
// standard output. It is a sequence of carriage-terminated updates ending at
// 100%, so the last one is the one to look for.
fn test_progress_draws_a_bar() {
res := os.execute('${rig.executable_under_test} --progress -r ${dir_src} ${dir_dst}')
assert res.exit_code == 0
assert res.output.contains('100%')
assert res.output.contains('[')
}
2 changes: 2 additions & 0 deletions src/cp/helper.v
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn setup_cp_command(args []string) (CpCommand, []string, string) {
target_directory := fp.string('target-directory', `t`, '', 'target-directory')
no_target_directory := fp.bool('no-target-directory', `T`, false, 'no-target-directory')
recursive := fp.bool('recursive', `r`, false, 'recursive')
progress := fp.bool('progress', 0, false, 'show a progress bar while copying')

help := fp.bool('help', 0, false, 'display this help and exit')
version := fp.bool('version', 0, false, 'output version information and exit')
Expand Down Expand Up @@ -139,6 +140,7 @@ fn setup_cp_command(args []string) (CpCommand, []string, string) {
target_directory: target_directory
no_target_directory: no_target_directory
recursive: recursive
progress: progress
}, sources, dest
}

Expand Down
37 changes: 37 additions & 0 deletions src/cp/progress.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import common

fn copy_file_progress(src string, dst string, mut bar common.ProgressBar) {
// `in` is a keyword in V, so the handle is named src_f.
mut src_f := os.open(src) or { return }
defer {
src_f.close()
}
mut dst_f := os.create(dst) or { return }
defer {
dst_f.close()
}
mut buf := []u8{len: 64 * 1024}
for {
n := src_f.read(mut buf) or { break }
if n == 0 {
break
}
dst_f.write(buf[..n]) or { break }
bar.update(n)
}
}

fn copy_dir_progress(src string, dst string, mut bar common.ProgressBar) {
os.mkdir(dst) or {}
entries := os.ls(src) or { [] }
for entry in entries {
child_src := os.join_path(src, entry)
child_dst := os.join_path(dst, entry)
if os.is_dir(child_src) {
copy_dir_progress(child_src, child_dst, mut bar)
} else {
copy_file_progress(child_src, child_dst, mut bar)
}
}
}
16 changes: 14 additions & 2 deletions src/mv/MvCommand.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import common

enum OverwriteMode {
force
Expand All @@ -10,6 +11,7 @@ struct MvCommand {
overwrite OverwriteMode
update bool
verbose bool
progress bool
target_directory string
no_target_directory bool
}
Expand All @@ -19,7 +21,7 @@ fn (m MvCommand) run(source string, dest string) {
eprintln(not_exist(source))
return
}
if m.verbose || m.overwrite != .force {
if m.verbose || m.progress || m.overwrite != .force {
m.move(source, dest)
} else {
os.mv(source, dest) or { error_exit(name, err.msg()) }
Expand All @@ -38,7 +40,17 @@ fn (m MvCommand) move(src string, dst string) {
if !m.int_yes(rdst) {
return
}
os.mv(src, rdst) or { return }
if m.progress {
mut bar := common.new_progress_bar(common.dir_size(src))
if os.is_dir(src) {
move_dir_progress(src, rdst, mut bar)
} else {
move_file_progress(src, rdst, mut bar)
}
bar.finish()
} else {
os.mv(src, rdst) or { return }
}
if m.verbose {
println(renamed(src, dst))
}
Expand Down
2 changes: 2 additions & 0 deletions src/mv/helper.v
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn setup_mv_command(args []string) (MvCommand, []string, string) {
no_clobber := fp.bool('no-clobber', `n`, false, 'do not overwrite')
update := fp.bool('update', `u`, false, 'update')
verbose := fp.bool('verbose', `v`, false, 'print each rename')
progress := fp.bool('progress', 0, false, 'show a progress bar while moving')
target_directory := fp.string('target-directory', `t`, '', 'target-directory')
no_target_directory := fp.bool('no-target-directory', `T`, false, 'no-target-directory')

Expand Down Expand Up @@ -123,6 +124,7 @@ fn setup_mv_command(args []string) (MvCommand, []string, string) {
overwrite: overwrite
update: update
verbose: verbose
progress: progress
target_directory: target_directory
no_target_directory: no_target_directory
}, sources, dest
Expand Down
72 changes: 72 additions & 0 deletions src/mv/mv_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import common.testing
import os

const rig = testing.prepare_rig(util: 'mv')

const src_path = 'mv_src.txt'
const dst_path = 'mv_dst.txt'
const dir_src = 'mv_src_dir'
const dir_dst = 'mv_dst_dir'

fn testsuite_begin() {
os.write_file(src_path, 'the quick brown fox\n')!
os.mkdir(dir_src)!
os.write_file(os.join_path(dir_src, 'a.txt'), 'aaa\n')!
os.write_file(os.join_path(dir_src, 'b.txt'), 'bbbb\n')!
os.mkdir(os.join_path(dir_src, 'sub'))!
os.write_file(os.join_path(dir_src, 'sub', 'c.txt'), 'cc\n')!
}

fn testsuite_end() {
os.rm(src_path) or {}
os.rm(dst_path) or {}
for root in [dir_src, dir_dst] {
os.rm(os.join_path(root, 'sub', 'c.txt')) or {}
os.rm(os.join_path(root, 'a.txt')) or {}
os.rm(os.join_path(root, 'b.txt')) or {}
os.rmdir(os.join_path(root, 'sub')) or {}
os.rmdir(root) or {}
}
}

fn test_help_and_version() {
rig.assert_help_and_version_options_work()
}

fn test_progress_is_listed_in_help() {
res := os.execute('${rig.executable_under_test} --help')
assert res.exit_code == 0
assert res.output.contains('--progress')
}

fn test_progress_moves_a_file() {
res := os.execute('${rig.executable_under_test} --progress ${src_path} ${dst_path}')
assert res.exit_code == 0
assert !os.exists(src_path)
assert os.read_bytes(dst_path)! == 'the quick brown fox\n'.bytes()
}

fn test_progress_moves_a_directory() {
res := os.execute('${rig.executable_under_test} --progress ${dir_src} ${dir_dst}')
assert res.exit_code == 0
assert !os.exists(dir_src)
assert os.read_bytes(os.join_path(dir_dst, 'a.txt'))! == 'aaa\n'.bytes()
assert os.read_bytes(os.join_path(dir_dst, 'b.txt'))! == 'bbbb\n'.bytes()
assert os.read_bytes(os.join_path(dir_dst, 'sub', 'c.txt'))! == 'cc\n'.bytes()
}

// The directory tests above consume dir_src, so this one uses a source of its
// own rather than depending on the order the tests run in.
fn test_progress_draws_a_bar() {
bar_src := 'mv_bar_src'
bar_dst := 'mv_bar_dst'
os.mkdir(bar_src)!
os.write_file(os.join_path(bar_src, 'x.txt'), 'xxxx\n')!
res := os.execute('${rig.executable_under_test} --progress ${bar_src} ${bar_dst}')
assert res.exit_code == 0
assert res.output.contains('100%')
assert res.output.contains('[')
os.rm(os.join_path(bar_dst, 'x.txt')) or {}
os.rmdir(bar_dst) or {}
os.rmdir(bar_src) or {}
}
Loading