Skip to content

Commit

Permalink
Merge pull request #216 from rusq/fsadapter-ret
Browse files Browse the repository at this point in the history
Unfuck the API
  • Loading branch information
rusq authored Apr 29, 2023
2 parents 023f1cd + 172b59b commit 74520fa
Show file tree
Hide file tree
Showing 27 changed files with 964 additions and 20 deletions.
2 changes: 1 addition & 1 deletion downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/slack-go/slack"
"golang.org/x/time/rate"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/network"
"github.com/rusq/slackdump/v2/logger"
)
Expand Down
2 changes: 1 addition & 1 deletion downloader/downloader_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/downloader"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/slack-go/slack"
"golang.org/x/time/rate"
)
Expand Down
2 changes: 1 addition & 1 deletion downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/time/rate"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/fixtures"
"github.com/rusq/slackdump/v2/internal/mocks/mock_downloader"
)
Expand Down
2 changes: 1 addition & 1 deletion export/expfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package export
import (
"github.com/slack-go/slack"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/structures/files/dl"
"github.com/rusq/slackdump/v2/logger"
)
Expand Down
2 changes: 1 addition & 1 deletion export/expfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/logger"
"github.com/slack-go/slack"
)
Expand Down
2 changes: 1 addition & 1 deletion export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"runtime/trace"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/slack-go/slack"

"github.com/rusq/slackdump/v2"
Expand Down
2 changes: 1 addition & 1 deletion export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"time"

gomock "github.com/golang/mock/gomock"
"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/fixtures"
"github.com/rusq/slackdump/v2/internal/mocks/mock_dl"
"github.com/rusq/slackdump/v2/internal/mocks/mock_fsadapter"
Expand Down
2 changes: 1 addition & 1 deletion export/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"
"strings"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/types"

Expand Down
2 changes: 1 addition & 1 deletion export/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package export
import (
"testing"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/slack-go/slack"
)

Expand Down
29 changes: 29 additions & 0 deletions fsadapter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2023, rusq
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 changes: 18 additions & 0 deletions fsadapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# fsadapter - File System adapter

[![Go Reference](https://pkg.go.dev/badge/github.com/rusq/fsadapter.svg)](https://pkg.go.dev/github.com/rusq/fsadapter)

fsadapter is a wrapper for writing to directory or a ZIP file.

There are currently 2 adapters:

- Directory
- ZIP

Each adapter exposes the following methods:

- Create(string) (io.WriteCloser, error)
- WriteFile(name string, data []byte, perm os.FileMode) error
- Close() error

It is meant to be a drop-in replacement for os.* functions for [Slackdump](https://github.com/rusq/slackdump).
94 changes: 94 additions & 0 deletions fsadapter/directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package fsadapter

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

var _ FS = Directory{}

type Directory struct {
dir string
}

// NewDirectory returns a new Directory filesystem adapter for a given
// directory.
func NewDirectory(dir string) Directory {
return Directory{dir: dir}
}

func (d Directory) String() string {
return "<directory: " + d.dir + ">"
}

// Create creates a new file in the directory.
func (fs Directory) Create(fpath string) (io.WriteCloser, error) {
node := filepath.Join(fs.dir, fpath)
if err := fs.ensureSubdir(node); err != nil {
return nil, fmt.Errorf("failed to create %s: %w", node, err)
}
nodeDir := filepath.Dir(node)
if err := mkdirAll(nodeDir); err != nil {
return nil, err
}
return os.Create(node)
}

// ErrIllegalDir is returned, if the file path reference is outside of the
// working directory.
var ErrIllegalDir = errors.New("illegal file path reference outside of working directory")

// ensureSubdir ensures that the node is a subdirectory of
// the fs.dir, and returns ErrIllegalDir, if not. This ensures
// that caller won't be able to do anything naughty.
func (fs Directory) ensureSubdir(node string) error {
if rel, err := filepath.Rel(fs.dir, node); err != nil {
return err
} else if strings.HasPrefix(rel, "..") {
return ErrIllegalDir
}

return nil
}

// mkdirAll creates a directory "name", if the directory exists, it does
// nothing.
func mkdirAll(name string) error {
if name == "" {
return errors.New("empty directory")
}

fi, err := os.Stat(name)
if err == nil && fi.IsDir() {
// exists and is a directory
return nil
}

if err := os.MkdirAll(name, 0755); err != nil {
return err
}
return nil
}

// WriteFile writes data to a file named by name. If the file does not exist,
// WriteFile creates it with permissions perm (before umask); otherwise
// WriteFile truncates it before writing.
func (fs Directory) WriteFile(name string, data []byte, perm os.FileMode) error {
node := filepath.Join(fs.dir, name)
if err := fs.ensureSubdir(node); err != nil {
return fmt.Errorf("WriteFile: %w", err)
}
if err := mkdirAll(filepath.Dir(node)); err != nil {
return err
}
return os.WriteFile(node, data, perm)
}

// Close is a noop for Directory.
func (fs Directory) Close() error {
return nil
}
Loading

0 comments on commit 74520fa

Please sign in to comment.