Skip to content

Commit

Permalink
rmdir: not use 'os.RemoveAll'
Browse files Browse the repository at this point in the history
  • Loading branch information
hymkor committed Nov 18, 2014
1 parent ba7ebd4 commit 4c34899
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion commands/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "os"
import "syscall"

import "../conio"
import "../dos"
import "../interpreter"

func cmd_mkdir(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
Expand Down Expand Up @@ -49,7 +50,8 @@ func cmd_rmdir(cmd *interpreter.Interpreter) (interpreter.NextT, error) {
continue
}
if s_option {
err = os.RemoveAll(arg1)
fmt.Fprintln(cmd.Stdout)
err = dos.Truncate(arg1, cmd.Stdout)
} else {
err = syscall.Rmdir(arg1)
}
Expand Down
37 changes: 37 additions & 0 deletions dos/truncate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dos

import "fmt"
import "io"
import "os"
import "syscall"

func Truncate(folder string, out io.Writer) error {
fd, fdErr := os.Open(folder)
if fdErr != nil {
return fdErr
}
fi, fiErr := fd.Readdir(-1)
fd.Close()
if fiErr != nil {
return fiErr
}
for _, f := range fi {
if f.Name() == "." || f.Name() == ".." {
continue
}
fullpath := Join(folder, f.Name())
if f.IsDir() {
fmt.Fprintf(out, "%s\\\n", fullpath)
if err := Truncate(fullpath, out); err != nil {
return fmt.Errorf("%s: %s", fullpath, err.Error())
}
} else {
fmt.Fprintln(out, fullpath)
syscall.Unlink(fullpath)
}
}
if err := syscall.Rmdir(folder); err != nil {
return fmt.Errorf("%s: %s", folder, err.Error())
}
return nil
}

0 comments on commit 4c34899

Please sign in to comment.