-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b120547
Showing
8 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
project_name: streetsweeper | ||
builds: | ||
- main: ./cmd/main.go | ||
binary: streetsweeper | ||
goos: | ||
- darwin | ||
- linux | ||
goarch: | ||
- amd64 | ||
- 386 | ||
- arm | ||
archive: | ||
format: tar.gz | ||
replacements: | ||
darwin: macOS | ||
files: | ||
- README.md | ||
- LICENSE |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[[constraint]] | ||
name = "github.com/fsnotify/fsnotify" | ||
revision = "v1.4.2" | ||
|
||
[[constraint]] | ||
name = "github.com/urfave/cli" | ||
revision = "v1.19.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Rafael Sales | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Street sweeper | ||
|
||
Street sweeper is a program that monitors a directory and removes old files whenever | ||
the storage use meets certain condition. | ||
|
||
# Usage and examples | ||
|
||
* All options: | ||
|
||
`streetsweeper --help` | ||
|
||
* When the directory `/etc/lib/motion` has used more than 4GB, | ||
delete oldest video files until it only has 3.5GB used. | ||
|
||
`streetsweeper --max-size 4000MB --target-size 3500MB /etc/lib/motion` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"github.com/fsnotify/fsnotify" | ||
//"github.com/urfave/cli" | ||
"../internal/file_sweeper" | ||
"fmt" | ||
"strings" | ||
"errors" | ||
"os" | ||
) | ||
|
||
func main() { | ||
maxSize, err := parseSize(os.Args[1]) | ||
if err != nil { log.Fatal(err) } | ||
targetSize, err := parseSize(os.Args[2]) | ||
if err != nil { log.Fatal(err) } | ||
path := os.Args[3] | ||
|
||
log.Printf("Initializing. Path: %s | Max size: %d | Target size: %d", path, maxSize, targetSize) | ||
|
||
onWrite := func() { file_sweeper.Run(path, maxSize, targetSize) } | ||
onWrite() | ||
fileWatcher(path, onWrite) | ||
} | ||
|
||
func parseSize(formattedSize string) (bytes int64, err error) { | ||
formattedSize = strings.TrimSpace(formattedSize) | ||
|
||
_, err = fmt.Sscanf(formattedSize, "%dKB", &bytes) | ||
if err == nil { return bytes * 1024, nil } | ||
|
||
_, err = fmt.Sscanf(formattedSize, "%dMB", &bytes) | ||
if err == nil { return bytes * 1024 * 1024, nil} | ||
|
||
_, err = fmt.Sscanf(formattedSize, "%dGB", &bytes) | ||
if err == nil { return bytes * 1024 * 1024 * 1024, nil } | ||
|
||
return -1, errors.New("Unknown size format: " + formattedSize) | ||
} | ||
|
||
func fileWatcher(path string, onWrite func()) { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { log.Fatal(err) } | ||
defer watcher.Close() | ||
|
||
done := make(chan bool) | ||
go func() { | ||
for { | ||
select { | ||
case event := <-watcher.Events: | ||
if event.Op == fsnotify.Write || event.Op == fsnotify.Create { | ||
log.Printf("Write detected: %s", event.Name) | ||
onWrite() | ||
} | ||
case err := <-watcher.Errors: | ||
log.Println("Error: %s", err) | ||
} | ||
} | ||
}() | ||
|
||
err = watcher.Add(path) | ||
if err != nil { log.Fatal(err) } | ||
<-done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package file_sweeper | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"log" | ||
"sort" | ||
) | ||
|
||
func Run(path string, maxSize int64, targetSize int64) { | ||
currentSize, _ := dirSize(path) | ||
log.Printf("Path: %s | Current size: %d", path, currentSize) | ||
|
||
if currentSize > maxSize { | ||
log.Printf( "Path: %s | Over max size - Current size: %d | Max size: %d", path, currentSize, maxSize) | ||
deleteOldFiles(path, currentSize, targetSize) | ||
} | ||
} | ||
|
||
func dirSize(path string) (int64, error) { | ||
var size int64 | ||
os.Open(path) | ||
err := filepath.Walk(path, func(a string, info os.FileInfo, err error) error { | ||
if !info.IsDir() { | ||
size += info.Size() | ||
} | ||
return err | ||
}) | ||
return size, err | ||
} | ||
|
||
func deleteOldFiles(path string, currentSize int64, targetSize int64) { | ||
var sizeDeleted int64 | ||
|
||
dir, err := os.Open(path) | ||
if err != nil { log.Fatal(err) } | ||
|
||
entries, err := dir.Readdir(-1) | ||
if err != nil { log.Fatal(err) } | ||
|
||
sort.SliceStable(entries, func(i, j int) bool { | ||
return entries[i].ModTime().Before(entries[j].ModTime()) | ||
}) | ||
|
||
for _, entry := range entries { | ||
if !entry.IsDir() { | ||
filePath := filepath.Join(path, entry.Name()) | ||
modifiedAt := entry.ModTime().UTC().String() | ||
size := entry.Size() | ||
|
||
log.Printf( "Deleting: %s | Modified at: %s | Size: %d", filePath, modifiedAt, size) | ||
os.Remove(filePath) | ||
sizeDeleted += size | ||
} | ||
|
||
if currentSize - sizeDeleted < targetSize { | ||
log.Printf( "Deleted a total of %d", sizeDeleted) | ||
return | ||
} | ||
} | ||
} |