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

feat(cloud): Add volume import partial checks #1586

Merged
merged 4 commits into from
Jul 11, 2024
Merged
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
26 changes: 26 additions & 0 deletions cpio/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2017 Ryan Armstrong. All rights reserved.

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

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

2. 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.

3. 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.
14 changes: 14 additions & 0 deletions cpio/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2017, Ryan Armstrong.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

/*
Package cpio providers readers and writers for CPIO archives. Currently, only
the SVR4 (New ASCII) format is supported, both with and without checksums.

This package aims to be feel like Go's archive/tar package.

See the CPIO man page: https://www.freebsd.org/cgi/man.cgi?query=cpio&sektion=5
*/
package cpio
82 changes: 82 additions & 0 deletions cpio/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2017, Ryan Armstrong.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package cpio_test

import (
"bytes"
"fmt"
"io"
"log"
"os"

"kraftkit.sh/cpio"
)

func Example() {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)

// Create a new cpio archive.
w := cpio.NewWriter(buf)

// Add some files to the archive.
files := []struct {
Name, Body string
}{
{"readme.txt", "This archive contains some text files."},
{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"todo.txt", "Get animal handling license."},
}
for _, file := range files {
hdr := &cpio.Header{
Name: file.Name,
Mode: 0o600,
Size: int64(len(file.Body)),
}
if err := w.WriteHeader(hdr); err != nil {
log.Fatalln(err)
}
if _, err := w.Write([]byte(file.Body)); err != nil {
log.Fatalln(err)
}
}

// Make sure to check the error on Close.
if err := w.Close(); err != nil {
log.Fatalln(err)
}

// Open the cpio archive for reading.
r := cpio.NewReader(buf)

// Iterate through the files in the archive.
for {
hdr, _, err := r.Next()
if err == io.EOF {
// end of cpio archive
break
}
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Contents of %s:\n", hdr.Name)
if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatalln(err)
}
fmt.Println()
}

// Output:
// Contents of readme.txt:
// This archive contains some text files.
// Contents of gopher.txt:
// Gopher names:
// George
// Geoffrey
// Gonzo
// Contents of todo.txt:
// Get animal handling license.
}
63 changes: 63 additions & 0 deletions cpio/fileinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2017, Ryan Armstrong.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package cpio

import (
"os"
"path"
"time"
)

// fileInfo implements fs.FileInfo.
type fileInfo struct {
h *Header
}

// Name returns the base name of the file.
func (fi fileInfo) Name() string {
if fi.IsDir() {
return path.Base(path.Clean(fi.h.Name))
}
return path.Base(fi.h.Name)
}

func (fi fileInfo) Size() int64 { return fi.h.Size }
func (fi fileInfo) IsDir() bool { return fi.Mode().IsDir() }
func (fi fileInfo) ModTime() time.Time { return fi.h.ModTime }
func (fi fileInfo) Sys() interface{} { return fi.h }

func (fi fileInfo) Mode() (mode os.FileMode) {
mode = os.FileMode(fi.h.Mode).Perm()
if fi.h.Mode&ModeSetuid != 0 {
mode |= os.ModeSetuid
}
if fi.h.Mode&ModeSetgid != 0 {
mode |= os.ModeSetgid
}
if fi.h.Mode&ModeSticky != 0 {
mode |= os.ModeSticky
}
m := os.FileMode(fi.h.Mode) & ModeType
if m == TypeDir {
mode |= os.ModeDir
}
if m == TypeFifo {
mode |= os.ModeNamedPipe
}
if m == TypeSymlink {
mode |= os.ModeSymlink
}
if m == TypeBlock {
mode |= os.ModeDevice
}
if m == TypeChar {
mode |= os.ModeDevice | os.ModeCharDevice
}
if m == TypeSocket {
mode |= os.ModeSocket
}
return mode
}
50 changes: 50 additions & 0 deletions cpio/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2017, Ryan Armstrong.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package cpio

import (
"encoding/binary"
"hash"
)

type digest struct {
sum uint32
}

// NewHash returns a new hash.Hash32 for computing SVR4 checksums.
func NewHash() hash.Hash32 {
return &digest{}
}

func (d *digest) Write(p []byte) (n int, err error) {
for _, b := range p {
d.sum += uint32(b & 0xFF)
}

return len(p), nil
}

func (d *digest) Sum(b []byte) []byte {
out := [4]byte{}
binary.LittleEndian.PutUint32(out[:], d.sum)
return append(b, out[:]...)
}

func (d *digest) Sum32() uint32 {
return d.sum
}

func (d *digest) Reset() {
d.sum = 0
}

func (d *digest) Size() int {
return 4
}

func (d *digest) BlockSize() int {
return 1
}
Loading
Loading