Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve FS and speed up file opening #134

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cmd/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ func (lrfs *localRemoteFS) write(name string, src fs.File) error {
}
}
case remotePath:
if !stat.IsDir() {
return lrfs.cfs.WriteFile(p.path, src)
if stat.IsDir() {
return lrfs.cfs.MkdirAll(p.path, stat.Mode())
}
buf, err := io.ReadAll(src)
if err != nil {
return err
}
return lrfs.cfs.WriteFile(p.path, buf, stat.Mode())
default:
return fmt.Errorf("invalid path type")
}
Expand Down Expand Up @@ -259,6 +264,9 @@ func fsRemove(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if isRecursive {
return lsfs.RemoveAll(args[0])
}
return lsfs.Remove(args[0])
}

Expand Down Expand Up @@ -318,6 +326,9 @@ func fsTree(cmd *cobra.Command, args []string) error {
return err
}
err = fs.WalkDir(lsfs, args[0], func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
fmt.Println(path)
return nil
})
Expand Down Expand Up @@ -355,6 +366,7 @@ func printDir(f fs.ReadDirFile) error {
func init() {
fsCopyCmd.Flags().BoolVarP(&isRecursive, "recursive", "r", false, "copy directories recursively")
fsMoveCmd.Flags().BoolVarP(&isRecursive, "recursive", "r", false, "move directories recursively")
fsRemoveCmd.Flags().BoolVarP(&isRecursive, "recursive", "r", false, "remove files recursively")

FSCmd.AddCommand(fsCatCmd)
FSCmd.AddCommand(fsCopyCmd)
Expand Down
3 changes: 2 additions & 1 deletion cmd/post_news.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/charmbracelet/charm/server"
"github.com/charmbracelet/charm/server/config"
"github.com/charmbracelet/keygen"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +22,7 @@ var (
Short: "Post news to the self-hosted Charm server.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := server.DefaultConfig()
cfg := config.DefaultConfig()
if serverDataDir != "" {
cfg.DataDir = serverDataDir
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/charmbracelet/charm/server"
"github.com/charmbracelet/charm/server/config"
"github.com/charmbracelet/keygen"
"github.com/spf13/cobra"
)
Expand All @@ -30,7 +31,7 @@ var (
Long: paragraph("Start the SSH and HTTP servers needed to power a SQLite-backed Charm Cloud."),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := server.DefaultConfig()
cfg := config.DefaultConfig()
if serverHTTPPort != 0 {
cfg.HTTPPort = serverHTTPPort
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/serve_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"path/filepath"

"github.com/charmbracelet/charm/server"
"github.com/charmbracelet/charm/server/config"
"github.com/charmbracelet/charm/server/db/sqlite"
"github.com/charmbracelet/charm/server/db/sqlite/migration"
"github.com/spf13/cobra"
Expand All @@ -24,7 +24,7 @@ var ServeMigrationCmd = &cobra.Command{
Long: paragraph("Run the server migration tool to migrate the database."),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := server.DefaultConfig()
cfg := config.DefaultConfig()
dp := filepath.Join(cfg.DataDir, "db", sqlite.DbName)
_, err := os.Stat(dp)
if err != nil {
Expand Down
72 changes: 56 additions & 16 deletions crypt/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,49 @@ func NewCrypt() (*Crypt, error) {
// NewDecryptedReader creates a new Reader that will read from and decrypt the
// passed in io.Reader of encrypted data.
func (cr *Crypt) NewDecryptedReader(r io.Reader) (*DecryptedReader, error) {
dr, _, err := cr.NewDecryptedReaderWithMetadata(r)
return dr, err
}

// NewDecryptedReaderWithMetadata creates a new Reader that will read from and decrypt the
// passed in io.Reader of encrypted data and its metadata.
func (cr *Crypt) NewDecryptedReaderWithMetadata(r io.Reader) (*DecryptedReader, []byte, error) {
var sdr io.Reader
var md []byte
dr := &DecryptedReader{}
for _, k := range cr.keys {
id, err := sasquatch.NewScryptIdentity(k.Key)
if err != nil {
return nil, err
return nil, nil, err
}
sdr, err = sasquatch.Decrypt(r, id)
sdr, md, err = sasquatch.DecryptWithMetadata(r, id)
if err == nil {
break
}
}
if sdr == nil {
return nil, ErrIncorrectEncryptKeys
return nil, nil, ErrIncorrectEncryptKeys
}
dr.r = sdr
return dr, nil
return dr, md, nil
}

// NewEncryptedWriter creates a new Writer that encrypts all data and writes
// the encrypted data to the supplied io.Writer.
func (cr *Crypt) NewEncryptedWriter(w io.Writer) (*EncryptedWriter, error) {
return cr.NewEncryptedWriterWithMetadata(w, nil)
}

// NewEncryptedWriterWithMetadata creates a new Writer that encrypts all data
// and writes the encrypted data along with their metadata to the supplied
// io.Writer.
func (cr *Crypt) NewEncryptedWriterWithMetadata(w io.Writer, metadata []byte) (*EncryptedWriter, error) {
ew := &EncryptedWriter{}
rec, err := sasquatch.NewScryptRecipient(cr.keys[0].Key)
if err != nil {
return ew, err
}
sew, err := sasquatch.Encrypt(w, rec)
sew, err := sasquatch.EncryptWithMetadata(w, metadata, rec)
if err != nil {
return ew, err
}
Expand All @@ -94,6 +109,18 @@ func (cr *Crypt) Keys() []*charm.EncryptKey {
return cr.keys
}

// Encrypt encrypts data.
func (cr *Crypt) Encrypt(b []byte) ([]byte, error) {
if b == nil {
return nil, nil
}
ct, err := siv.Encrypt(nil, []byte(cr.keys[0].Key[:32]), b, nil)
if err != nil {
return nil, err
}
return ct, nil
}

// EncryptLookupField will deterministically encrypt a string and the same
// encrypted value every time this string is encrypted with the same
// EncryptKey. This is useful if you need to look up an encrypted value without
Expand All @@ -103,11 +130,30 @@ func (cr *Crypt) EncryptLookupField(field string) (string, error) {
if field == "" {
return "", nil
}
ct, err := siv.Encrypt(nil, []byte(cr.keys[0].Key[:32]), []byte(field), nil)
b, err := cr.Encrypt([]byte(field))
if err != nil {
return "", err
}
return hex.EncodeToString(ct), nil
return hex.EncodeToString(b), nil
}

// Decrypt decrypts data encrypted with Encrypt.
func (cr *Crypt) Decrypt(b []byte) ([]byte, error) {
if b == nil {
return nil, nil
}
var err error
var pt []byte
for _, k := range cr.keys {
pt, err = siv.Decrypt([]byte(k.Key[:32]), b, nil)
if err == nil {
break
}
}
if len(pt) == 0 {
return nil, ErrIncorrectEncryptKeys
}
return pt, nil
}

// DecryptLookupField decrypts a string encrypted with EncryptLookupField.
Expand All @@ -119,15 +165,9 @@ func (cr *Crypt) DecryptLookupField(field string) (string, error) {
if err != nil {
return "", err
}
var pt []byte
for _, k := range cr.keys {
pt, err = siv.Decrypt([]byte(k.Key[:32]), ct, nil)
if err == nil {
break
}
}
if len(pt) == 0 {
return "", ErrIncorrectEncryptKeys
pt, err := cr.Decrypt(ct)
if err != nil {
return "", err
}
return string(pt), nil
}
Expand Down
Loading