Skip to content

Commit

Permalink
Merge pull request #31 from GreenmaskIO/delete_command_fix
Browse files Browse the repository at this point in the history
Fixed delete operation
  • Loading branch information
wwoytenko authored Mar 22, 2024
2 parents 8bdd5a5 + b4699e7 commit f0242a9
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 100 deletions.
21 changes: 6 additions & 15 deletions cmd/greenmask/cmd/delete_backup/delete_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package delete_backup
import (
"context"
"fmt"
"slices"

"github.com/greenmaskio/greenmask/internal/storages"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -59,24 +60,14 @@ func deleteDump(dumpId string) error {
log.Fatal().Err(err).Msg("")
}

var found bool
var backupDirStorage storages.Storager
for _, b := range dirs {
if dumpId == b.Dirname() {
found = true
backupDirStorage = b
}
}

if !found {
if !slices.ContainsFunc(dirs, func(sst storages.Storager) bool {
return dumpId == sst.Dirname()
}) {
return fmt.Errorf("dump with id %s was not found", dumpId)
}
files, _, err := backupDirStorage.ListDir(ctx)

for _, f := range files {
if err = backupDirStorage.Delete(ctx, f); err != nil {
return fmt.Errorf("storage error: %s", err)
}
if err = st.DeleteAll(ctx, dumpId); err != nil {
return fmt.Errorf("storage error: %s", err)
}

return nil
Expand Down
19 changes: 19 additions & 0 deletions internal/storages/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ func (d *Storage) Delete(ctx context.Context, filePaths ...string) error {
return nil
}

func (d *Storage) DeleteAll(ctx context.Context, pathPrefix string) error {
fileInfo, err := os.Stat(path.Join(d.cwd, pathPrefix))
if err != nil {
return err
}
if fileInfo.IsDir() {
err = os.RemoveAll(path.Join(d.cwd, pathPrefix))
if err != nil {
return fmt.Errorf(`error deliting directory %s: %w`, pathPrefix, err)
}
} else {
err = os.Remove(path.Join(d.cwd, pathPrefix))
if err != nil {
return fmt.Errorf(`error deliting file %s: %w`, pathPrefix, err)
}
}
return nil
}

func (d *Storage) Exists(ctx context.Context, fileName string) (bool, error) {
_, err := os.Stat(path.Join(d.cwd, fileName))
if err != nil {
Expand Down
27 changes: 24 additions & 3 deletions internal/storages/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func NewStorage(ctx context.Context, cfg *Config, logLevel string) (*Storage, er
)

return &Storage{
prefix: path.Join(cfg.Bucket, cfg.Prefix) + "/",
prefix: fixPrefix(path.Join(cfg.Bucket, cfg.Prefix)),
session: ses,
config: cfg,
service: service,
Expand All @@ -175,7 +175,7 @@ func (s *Storage) ListDir(ctx context.Context) (files []string, dirs []storages.
session: s.session,
service: s.service,
uploader: s.uploader,
prefix: *prefix.Prefix,
prefix: fixPrefix(*prefix.Prefix),
},
)
}
Expand Down Expand Up @@ -272,10 +272,24 @@ func (s *Storage) Delete(ctx context.Context, filePaths ...string) error {
return nil
}

func (s *Storage) DeleteAll(ctx context.Context, pathPrefix string) error {
pathPrefix = fixPrefix(pathPrefix)
ss := s.SubStorage(pathPrefix, true)
filesList, err := storages.Walk(ctx, ss, "")
if err != nil {
return fmt.Errorf("error walking through storage: %w", err)
}

if err = ss.Delete(ctx, filesList...); err != nil {
return fmt.Errorf("error deleting files: %w", err)
}
return nil
}

func (s *Storage) SubStorage(subPath string, relative bool) storages.Storager {
prefix := subPath
if relative {
prefix = path.Join(s.prefix, prefix)
prefix = fixPrefix(path.Join(s.prefix, prefix))
}
return &Storage{
config: s.config,
Expand Down Expand Up @@ -303,3 +317,10 @@ func (s *Storage) Exists(ctx context.Context, fileName string) (bool, error) {
}
return true, nil
}

func fixPrefix(prefix string) string {
if prefix != "" && prefix[len(prefix)-1] != '/' {
prefix = prefix + "/"
}
return prefix
}
2 changes: 2 additions & 0 deletions internal/storages/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Storager interface {
PutObject(ctx context.Context, filePath string, body io.Reader) error
// Delete - delete list of objects by the provided paths
Delete(ctx context.Context, filePaths ...string) error
// DeleteAll - delete all objects by the provided path prefix
DeleteAll(ctx context.Context, pathPrefix string) error
// Exists - check object existence
Exists(ctx context.Context, fileName string) (bool, error)
// SubStorage - get new Storage instance with the samo config but change current cwd via subPath
Expand Down
31 changes: 31 additions & 0 deletions internal/storages/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package storages

import (
"context"
"fmt"
"path"
)

func Walk(ctx context.Context, st Storager, parent string) (res []string, err error) {
var files []string
files, dirs, err := st.ListDir(ctx)
if err != nil {
return nil, fmt.Errorf("error listing directory: %w", err)
}
for _, f := range files {
res = append(res, path.Join(parent, f))
}
if len(dirs) > 0 {
for _, d := range dirs {
subFiles, err := Walk(ctx, d, d.Dirname())
if err != nil {
return nil, fmt.Errorf("error walking through directory: %w", err)
}
for _, f := range subFiles {
res = append(res, path.Join(parent, f))
}
}
}

return
}
12 changes: 0 additions & 12 deletions tests/demodb/dump.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package toc
package greenmask

import (
"flag"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package toc
package greenmask

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package toc
package greenmask

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package toc
package greenmask

import (
"errors"
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/storages/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"context"
"io"
"path"
"slices"

"github.com/greenmaskio/greenmask/internal/storages"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -115,4 +117,48 @@ func (suite *S3StorageSuite) TestS3Ops() {
suite.Require().NotContains(files, "test_to_del.txt")
})

suite.Run("delete_all", func() {
buf := bytes.NewBuffer([]byte("1234567890"))
err := suite.st.PutObject(context.Background(), "/test_to_del.txt", buf)
suite.Require().NoError(err)

buf = bytes.NewBuffer([]byte("1234567890"))
err = suite.st.PutObject(context.Background(), "/dir1/test_to_del2.txt", buf)
suite.Require().NoError(err)

buf = bytes.NewBuffer([]byte("1234567890"))
err = suite.st.PutObject(context.Background(), "/dir1/subdir2/test_to_del3.txt", buf)
suite.Require().NoError(err)

files, dirs, err := suite.st.ListDir(context.Background())
suite.Require().NoError(err)
suite.Require().Contains(files, "test_to_del.txt")
idx := slices.IndexFunc(dirs, func(s storages.Storager) bool {
return s.Dirname() == "dir1"
})
suite.Require().True(idx != -1)

files, dirs, err = dirs[idx].ListDir(context.Background())
suite.Require().NoError(err)
suite.Require().Contains(files, "test_to_del2.txt")
suite.Require().Len(dirs, 1)
idx = slices.IndexFunc(dirs, func(s storages.Storager) bool {
return s.Dirname() == "subdir2"
})
suite.Require().True(idx != -1)

files, dirs, err = dirs[idx].ListDir(context.Background())
suite.Require().NoError(err)
suite.Require().Contains(files, "test_to_del3.txt")
suite.Require().Empty(dirs)

err = suite.st.DeleteAll(context.Background(), "/")
suite.Require().NoError(err)

files, dirs, err = suite.st.ListDir(context.Background())
suite.Require().NoError(err)
suite.Require().NotContains(files, "test_to_del.txt")
suite.Require().Empty(dirs)
})

}
66 changes: 0 additions & 66 deletions tests/migration/test.sql

This file was deleted.

0 comments on commit f0242a9

Please sign in to comment.