-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
12 changed files
with
307 additions
and
69 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
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
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
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,92 @@ | ||
package info_snap | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/proofrock/snapkup/util" | ||
) | ||
|
||
const sql1 = `WITH | ||
consts AS (SELECT ? AS snap), | ||
data AS ( | ||
SELECT 1 AS key, COUNT(1) AS val FROM ITEMS WHERE SNAP = (SELECT snap FROM consts) AND IS_DIR = 0 | ||
UNION ALL SELECT 2 AS key, COUNT(1) AS val FROM ITEMS WHERE SNAP = (SELECT snap FROM consts) AND IS_DIR = 1 | ||
UNION ALL SELECT 3 AS key, SUM(b.SIZE) AS val FROM ITEMS i, BLOBS b WHERE i.HASH = b.HASH AND SNAP = (SELECT snap FROM consts) AND IS_DIR = 0 | ||
UNION ALL SELECT 4 AS key, SUM(BLOB_SIZE) AS val FROM BLOBS WHERE HASH IN (SELECT HASH FROM ITEMS WHERE SNAP = (SELECT snap FROM consts) AND IS_DIR = 0) | ||
UNION ALL SELECT 5 AS key, SUM(BLOB_SIZE) AS val FROM BLOBS | ||
UNION ALL SELECT 6 AS key, SUM(BLOB_SIZE) AS val FROM BLOBS WHERE HASH NOT IN (SELECT HASH FROM ITEMS WHERE SNAP != (SELECT snap FROM consts) AND IS_DIR = 0) | ||
) | ||
SELECT val FROM data ORDER BY key ASC` | ||
|
||
var titles = [6]string{ | ||
"Files", | ||
"Directories", | ||
"Size", | ||
"Stored size", | ||
"Tot. stored (all snaps)", | ||
"Free when deleted", | ||
} | ||
|
||
var isInByte = [6]bool{false, false, true, true, true, true} | ||
|
||
const suffixes = "KMGTPE" | ||
|
||
const unit = 1024 | ||
|
||
func fmtBytes(b int64) string { | ||
if b < unit { | ||
return fmt.Sprintf("%d b", b) | ||
} | ||
div, exp := int64(unit), 0 | ||
for n := b / unit; n >= unit; n /= unit { | ||
div *= unit | ||
exp++ | ||
} | ||
return fmt.Sprintf("%.1f %cb", | ||
float64(b)/float64(div), suffixes[exp]) | ||
} | ||
|
||
func InfoSnap(bkpDir string, snap int) error { | ||
maxLen := 0 | ||
for _, title := range titles { | ||
if len(title) > maxLen { | ||
maxLen = len(title) | ||
} | ||
} | ||
|
||
dbPath, errComposingDbPath := util.DbFile(bkpDir) | ||
if errComposingDbPath != nil { | ||
return errComposingDbPath | ||
} | ||
|
||
db, errOpeningDb := sql.Open("sqlite3", dbPath) | ||
if errOpeningDb != nil { | ||
return errOpeningDb | ||
} | ||
defer db.Close() | ||
|
||
rows, errQuerying := db.Query(sql1, snap) | ||
if errQuerying != nil { | ||
return errQuerying | ||
} | ||
defer rows.Close() | ||
i := 0 | ||
for rows.Next() { | ||
var val int64 | ||
if errScanning := rows.Scan(&val); errScanning != nil { | ||
return errScanning | ||
} | ||
if isInByte[i] { | ||
fmt.Printf(fmt.Sprintf("%%-%ds: %%s\n", maxLen), titles[i], fmtBytes(val)) | ||
} else { | ||
fmt.Printf(fmt.Sprintf("%%-%ds: %%d\n", maxLen), titles[i], val) | ||
} | ||
i++ | ||
} | ||
if errClosingQry := rows.Err(); errClosingQry != nil { | ||
return errClosingQry | ||
} | ||
|
||
return nil | ||
} |
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
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,28 @@ | ||
package labelsnap | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/proofrock/snapkup/util" | ||
) | ||
|
||
func LabelSnap(bkpDir string, snap int, label string) error { | ||
dbPath, errComposingDbPath := util.DbFile(bkpDir) | ||
if errComposingDbPath != nil { | ||
return errComposingDbPath | ||
} | ||
|
||
db, errOpeningDb := sql.Open("sqlite3", dbPath) | ||
if errOpeningDb != nil { | ||
return errOpeningDb | ||
} | ||
defer db.Close() | ||
|
||
if _, errExecing := db.Exec("UPDATE SNAPS SET LABEL = ? WHERE ID = ?", label, snap); errExecing != nil { | ||
return errExecing | ||
} | ||
|
||
println("Ok.") | ||
|
||
return nil | ||
} |
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,40 @@ | ||
package list_snap | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/proofrock/snapkup/util" | ||
) | ||
|
||
const sql1 = "SELECT PATH FROM ITEMS WHERE SNAP = ? ORDER BY PATH ASC" | ||
|
||
func ListSnap(bkpDir string, snap int) error { | ||
dbPath, errComposingDbPath := util.DbFile(bkpDir) | ||
if errComposingDbPath != nil { | ||
return errComposingDbPath | ||
} | ||
|
||
db, errOpeningDb := sql.Open("sqlite3", dbPath) | ||
if errOpeningDb != nil { | ||
return errOpeningDb | ||
} | ||
defer db.Close() | ||
|
||
rows, errQuerying := db.Query(sql1, snap) | ||
if errQuerying != nil { | ||
return errQuerying | ||
} | ||
defer rows.Close() | ||
for rows.Next() { | ||
var path string | ||
if errScanning := rows.Scan(&path); errScanning != nil { | ||
return errScanning | ||
} | ||
println(path) | ||
} | ||
if errClosingQry := rows.Err(); errClosingQry != nil { | ||
return errClosingQry | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.