-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from rusq/fsadapter-ret
Unfuck the API
- Loading branch information
Showing
27 changed files
with
964 additions
and
20 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
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
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,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. |
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,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). |
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,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 | ||
} |
Oops, something went wrong.