-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgorm.go
More file actions
89 lines (80 loc) · 1.77 KB
/
gorm.go
File metadata and controls
89 lines (80 loc) · 1.77 KB
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
// Copyright 2010 Google Inc. All Rights Reserved.
// Author: phanna@google.com (Patrick Scott)
package main
import (
"fmt"
"os"
)
var sem = make(chan int, 2000)
func deleteDirectory(result chan int, directory string) {
sem<-1
handle, err := os.Open(directory, os.O_RDONLY, 0)
var count int = 0
var childResult = make(chan int)
if err != nil {
fmt.Printf("Failed to open file %s: %s\n", directory, err)
os.Exit(1)
}
for {
names, err := handle.Readdirnames(100)
if err != nil {
fmt.Printf("Failed to list directory contents: %s\n", err)
os.Exit(1)
}
if len(names) == 0 {
break
}
for _, name := range names {
fullName := directory + "/" + name
fileInfo, err := os.Stat(fullName)
if err != nil {
fmt.Printf("Failed to stat file %s: %s\n", fullName, err)
os.Exit(1)
}
if fileInfo.IsDirectory() {
go deleteDirectory(childResult, fullName)
count++
} else {
err := os.Remove(fullName)
if err != nil {
fmt.Printf("Failed to remove file %s: %s\n", fullName, err)
os.Exit(1)
}
fmt.Printf("%s removed\n", fullName)
}
}
}
handle.Close()
<-sem
for count != 0 {
count = count - <-childResult
}
os.Remove(directory)
fmt.Printf("%s removed\n", directory)
result <- 1
}
func main() {
if len(os.Args) == 1 {
fmt.Printf("Missing directory to remove!\n")
os.Exit(1)
}
var dir string = os.Args[1]
file, err := os.Open(dir, os.O_RDONLY, 0)
if err != nil {
fmt.Printf("Error opening directory: %s\n", err)
os.Exit(1)
}
fileInfo, err := file.Stat()
if err != nil {
fmt.Printf("Error stat'ing directory: %s\n", err)
os.Exit(1)
}
file.Close()
if !fileInfo.IsDirectory() {
fmt.Printf("File is not a directory!\n", dir)
os.Exit(1)
}
var c = make(chan int)
go deleteDirectory(c, dir)
<-c
}