mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-22 23:21:11 +02:00
Actual API is implemented in the `init`, as it has access to root filesystem. `osd` proxies API back to `init` with some tricks to support grpc streaming. Given some absolute path, `init` produces and streams back .tar.gz archive with filesystem contents. `osctl cp` works in two modes. First mode streams data to stdout, so that we can do e.g.: `osctl cp /etc - | tar tz`. Second mode extracts archive to specified location, dropping ownership info and adjusting permissions a bit. Timestamps are not preserved. If full dump with owner/permisisons is required, it's better to stream data to `tar xz`, for quick and dirty look into filesystem contents under unprivileged user it's easier to use in-place extraction. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
102 lines
1.9 KiB
Go
102 lines
1.9 KiB
Go
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package archiver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
)
|
|
|
|
// FileItem is unit of work for archive
|
|
type FileItem struct {
|
|
FullPath string
|
|
RelPath string
|
|
FileInfo os.FileInfo
|
|
Link string
|
|
}
|
|
|
|
// Walker provides a channel of file info/paths for archival
|
|
//
|
|
//nolint: gocyclo
|
|
func Walker(ctx context.Context, rootPath string) (<-chan FileItem, <-chan error, error) {
|
|
_, err := os.Stat(rootPath)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
ch := make(chan FileItem)
|
|
errCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
defer close(ch)
|
|
|
|
multiErr := &multierror.Error{}
|
|
|
|
defer func() {
|
|
errCh <- multiErr.ErrorOrNil()
|
|
}()
|
|
|
|
err := filepath.Walk(rootPath, func(path string, fileInfo os.FileInfo, walkErr error) error {
|
|
if walkErr != nil {
|
|
multiErr = multierror.Append(multiErr, walkErr)
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
relPath string
|
|
err error
|
|
)
|
|
|
|
if path == rootPath {
|
|
if fileInfo.IsDir() {
|
|
// skip containing directory
|
|
return nil
|
|
}
|
|
|
|
// only one file
|
|
relPath = filepath.Base(path)
|
|
} else {
|
|
relPath, err = filepath.Rel(rootPath, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
item := FileItem{
|
|
FullPath: path,
|
|
RelPath: relPath,
|
|
FileInfo: fileInfo,
|
|
}
|
|
|
|
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
item.Link, err = os.Readlink(path)
|
|
if err != nil {
|
|
multiErr = multierror.Append(multiErr, fmt.Errorf("error reading symlink %q: %s", path, err))
|
|
return nil
|
|
}
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case ch <- item:
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
multiErr = multierror.Append(multiErr, err)
|
|
}
|
|
|
|
}()
|
|
|
|
return ch, errCh, nil
|
|
}
|