Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelsales committed Aug 25, 2017
0 parents commit b120547
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
vendor
18 changes: 18 additions & 0 deletions .goreleaser.yml
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
25 changes: 25 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Gopkg.toml
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"
21 changes: 21 additions & 0 deletions LICENSE
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.
15 changes: 15 additions & 0 deletions README.md
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`
66 changes: 66 additions & 0 deletions cmd/main.go
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
}
61 changes: 61 additions & 0 deletions internal/file_sweeper/main.go
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
}
}
}

0 comments on commit b120547

Please sign in to comment.