-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add oci-create-layer command to create filesystem changesets
Signed-off-by: Lei Jitang <leijitang@huawei.com>
- Loading branch information
1 parent
721ba58
commit 664b97f
Showing
281 changed files
with
115,420 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2016 The Linux Foundation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/opencontainers/image-tools/image" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type layerCmd struct { | ||
stdout *log.Logger | ||
stderr *log.Logger | ||
dest string | ||
} | ||
|
||
func main() { | ||
stdout := log.New(os.Stdout, "", 0) | ||
stderr := log.New(os.Stderr, "", 0) | ||
|
||
cmd := newLayerCmd(stdout, stderr) | ||
if err := cmd.Execute(); err != nil { | ||
stderr.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func newLayerCmd(stdout, stderr *log.Logger) *cobra.Command { | ||
v := &layerCmd{ | ||
stdout: stdout, | ||
stderr: stderr, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "oci-create-layer [child] [parent]", | ||
Short: "Create an OCI layer", | ||
Long: `Create an OCI layer based on the changeset between filesystems.`, | ||
Run: v.Run, | ||
} | ||
cmd.Flags().StringVar( | ||
&v.dest, "dest", "", | ||
`The dest specify a particular filename where the layer write to`, | ||
) | ||
return cmd | ||
} | ||
|
||
func (v *layerCmd) Run(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 && len(args) != 2 { | ||
v.stderr.Print("One or two filesystems are required") | ||
if err := cmd.Usage(); err != nil { | ||
v.stderr.Println(err) | ||
} | ||
os.Exit(1) | ||
} | ||
var err error | ||
if len(args) == 1 { | ||
err = image.CreateLayer(args[0], "", v.dest) | ||
} else { | ||
err = image.CreateLayer(args[0], args[1], v.dest) | ||
} | ||
if err != nil { | ||
v.stderr.Printf("create layer failed: %v", err) | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
// Copyright 2016 The Linux Foundation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package image | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/containers/storage/pkg/archive" | ||
) | ||
|
||
func CreateLayer(child, parent, dest string) error { | ||
arch, err := Diff(child, parent) | ||
if err != nil { | ||
return err | ||
} | ||
defer arch.Close() | ||
filename := fmt.Sprintf("%s.tar", filepath.Clean(child)) | ||
if dest != "" { | ||
filename = filepath.Clean(dest) | ||
} | ||
f, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
_, err = io.Copy(f, arch) | ||
return err | ||
} | ||
|
||
// Diff produces an archive of the changes between the specified | ||
// layer and its parent layer which may be "". | ||
func Diff(child, parent string) (arch archive.Archive, err error) { | ||
if parent == "" { | ||
archive, err := archive.Tar(child, archive.Uncompressed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return archive, nil | ||
} | ||
|
||
changes, err := archive.ChangesDirs(child, parent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
archive, err := archive.ExportChanges(child, changes, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return archive, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.