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: Introduce .kraftignore #1032

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
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 initrd/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"kraftkit.sh/log"
)

var ErrRootfsOutputAlreadyExists = fmt.Errorf("rootfs output already exists")

type directory struct {
opts InitrdOptions
path string
Expand Down Expand Up @@ -65,6 +67,10 @@ func (initrd *directory) Build(ctx context.Context) (string, error) {
}
}

if _, err := os.Stat(initrd.opts.output); err == nil {
return initrd.opts.output, ErrRootfsOutputAlreadyExists
}

f, err := os.OpenFile(initrd.opts.output, os.O_RDWR|os.O_CREATE, 0o644)
if err != nil {
return "", fmt.Errorf("could not open initramfs file: %w", err)
Expand All @@ -75,6 +81,11 @@ func (initrd *directory) Build(ctx context.Context) (string, error) {
writer := cpio.NewWriter(f)
defer writer.Close()

ignoringItems, err := GetKraftIgnoreItems(ctx, initrd.path)
if err != nil {
return "", err
}

if err := filepath.WalkDir(initrd.path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("received error before parsing path: %w", err)
Expand All @@ -86,6 +97,21 @@ func (initrd *directory) Build(ctx context.Context) (string, error) {
}
internal = "." + filepath.ToSlash(internal)

if len(ignoringItems) > 0 && path != initrd.path {
switch isExistInKraftignoreFile(internal, d, ignoringItems) {
case SkipDir:
log.G(ctx).
WithField("directory", internal).
Trace("ignoring from archiving")
return filepath.SkipDir
case Exist:
log.G(ctx).
WithField("file", internal).
Trace("ignoring from archiving")
return nil
}
}

info, err := d.Info()
if err != nil {
return fmt.Errorf("could not get directory entry info: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions initrd/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package initrd_test

import (
"context"
"errors"
"io"
"os"
"testing"
Expand All @@ -26,7 +27,7 @@ func TestNewFromDirectory(t *testing.T) {
}

irdPath, err := ird.Build(ctx)
if err != nil {
if err != nil && !errors.Is(err, initrd.ErrRootfsOutputAlreadyExists) {
t.Fatal("Build:", err)
}
t.Cleanup(func() {
Expand All @@ -38,7 +39,7 @@ func TestNewFromDirectory(t *testing.T) {
irdFiles := ird.Files()

const expectFiles = 4 // only regular and symlink files are indexed
if gotFiles := len(irdFiles); gotFiles != expectFiles {
if gotFiles := len(irdFiles); gotFiles > 0 && gotFiles != expectFiles {
t.Errorf("Expected %d files in InitrdConfig, got %d: %v", expectFiles, gotFiles, irdFiles)
}

Expand Down
157 changes: 157 additions & 0 deletions initrd/kraftignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package initrd

import (
"bufio"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"kraftkit.sh/log"
)

// kraftignore filename
const KraftignoreFileName = ".kraftignore"

type IgnoringFileType string

const (
Exist = IgnoringFileType("Exist")
NotExist = IgnoringFileType("NotExist")
SkipDir = IgnoringFileType("SkipDir")
)
Comment on lines +23 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we can declare these as errors:

var (
  ErrFileExists    = fmt.Errorf("file exists")
  ErrFileNotExists = fmt.Errorf("filt does not exist")
  ErrIgnoreDir     = fmt.Errorf("ignored directory")
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can then do comparisons via:

if errors.Is(err, ErrFileExists)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely, It can be done. But kraftiignore.go not uses these vars as errors, These vars are being used as notifiers for filepath.WalkDir() function in directory.go.

Please have an eye on filepath.WalkDir() function then lemme know if you really want me to change const vars as error vars :)


// GetKraftIgnoreItems returns file and directory names specified in .kraftignore
func GetKraftIgnoreItems(ctx context.Context, dir string) ([]string, error) {
cwd, err := os.Getwd()
if err != nil {
return []string{}, err
}
kraftignorePath := filepath.Join(cwd, KraftignoreFileName)

if _, err := os.Stat(kraftignorePath); errors.Is(err, os.ErrNotExist) {
return []string{}, nil
} else if err != nil {
return []string{}, err
}

kraftignoreFile, err := os.Open(kraftignorePath)
if err != nil {
return []string{}, err
}

defer func() {
kraftIgnoreErr := kraftignoreFile.Close()
if kraftIgnoreErr != nil {
if err != nil {
err = fmt.Errorf("%w: %w", err, kraftIgnoreErr)
} else {
err = kraftIgnoreErr
}
}
}()

kraftignoreScanner := bufio.NewScanner(kraftignoreFile)
kraftignoreScanner.Split(bufio.ScanLines)
var kraftignoreFileLines, ignoringItems []string

for kraftignoreScanner.Scan() {
kraftignoreFileLines = append(kraftignoreFileLines, kraftignoreScanner.Text())
}

for lineNum, line := range kraftignoreFileLines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
items := findLineItems(line)
for _, item := range items {
if item == "" || item == "#" {
continue
}

if hasGlobPatterns(item) {
log.G(ctx).
WithField("file", kraftignorePath).
Warn("contains a glob pattern ", item,
" at line ", lineNum,
" which is not supported by Kraftkit")
continue
}

if _, err := os.Stat(filepath.Join(dir, item)); os.IsNotExist(err) {
log.G(ctx).
WithField("file", kraftignorePath).
Warn("contains ", item,
" at line ", lineNum,
" which does not exist in the provided rootfs directory")
continue
}

ignoringItems = append(ignoringItems, item)
}
}

return ignoringItems, err
}

// isExistInKraftignoreFile checks if the path exist in .kraftignore
func isExistInKraftignoreFile(internal string, pathInfo fs.DirEntry, kraftignoreItems []string) IgnoringFileType {
for _, ignoringItem := range kraftignoreItems {
if internal == ignoringItem {
if pathInfo.IsDir() {
return SkipDir
}
return Exist
}
}
return NotExist
}

// hasGlobPatterns checks if the item contains glob pattern
func hasGlobPatterns(item string) bool {
return strings.ContainsAny(item, "*?![{")
}

// findLineItems finds items in a line of .kraftignore
func findLineItems(line string) []string {
items := strings.Split(line, " ")
for index := 0; index < len(items); index++ {
charToFind := ""
if strings.HasPrefix(items[index], `"`) && !strings.HasSuffix(items[index], `"`) {
charToFind = `"`
} else if strings.HasPrefix(items[index], `'`) && !strings.HasSuffix(items[index], `'`) {
charToFind = `'`
}

if len(charToFind) > 0 {
i := index + 1
for ; i < len(items) && !strings.HasSuffix(items[i], charToFind); i++ {
items[index] += " " + items[i]
items = append(items[:i], items[i+1:]...)
i--
}
items[index] += " " + items[i]
items = append(items[:i], items[i+1:]...)
}
items[index] = strings.Trim(items[index], `"`)
items[index] = strings.Trim(items[index], `'`)
items[index] = strings.TrimSpace(items[index])
items[index] = strings.TrimPrefix(items[index], "../")
if !strings.HasPrefix(items[index], "./") {
if !strings.HasPrefix(items[index], "/") {
items[index] = "/" + items[index]
}
items[index] = "." + items[index]
}
items[index] = strings.TrimSuffix(items[index], string(filepath.Separator))
}
return items
}
Loading