-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Md Sahil <mohdssahil1@gmail.com>
- Loading branch information
1 parent
f641dcb
commit 3d6884c
Showing
3 changed files
with
174 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,132 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2022, 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") | ||
) | ||
|
||
func getKraftignoreItems(ctx context.Context, initrd *directory) ([]string, error) { | ||
if initrd.opts.kraftignorePath == "" { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
initrd.opts.kraftignorePath = filepath.Join(cwd, KraftignoreFileName) | ||
} | ||
|
||
if _, err := os.Stat(initrd.opts.kraftignorePath); errors.Is(err, os.ErrNotExist) { | ||
return []string{}, nil | ||
} else if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
kraftignoreFile, err := os.Open(initrd.opts.kraftignorePath) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
defer func() { | ||
kraftIgnoreErr := kraftignoreFile.Close() | ||
if kraftIgnoreErr != nil { | ||
if err != nil { | ||
err = fmt.Errorf("%v\n%v", 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 := strings.Split(line, " ") | ||
for _, item := range items { | ||
item = strings.Trim(item, `"`) | ||
item = strings.Trim(item, `'`) | ||
item = strings.TrimSpace(item) | ||
if item == "" || item == "#" { | ||
continue | ||
} | ||
|
||
if hasGlobPatterns(item) { | ||
log.G(ctx). | ||
WithField("file", initrd.opts.kraftignorePath). | ||
Warn("contains a glob pattern ", item, | ||
" at line ", lineNum, | ||
" which is not supported by Kraftkit") | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(item, "..") { | ||
Check failure on line 93 in initrd/kraftignore.go GitHub Actions / All
|
||
item = strings.TrimPrefix(item, "..") | ||
} | ||
|
||
item = filepath.Join(item) | ||
if !strings.HasPrefix(item, "./") { | ||
if !strings.HasPrefix(item, "/") { | ||
item = "/" + item | ||
} | ||
item = "." + item | ||
} | ||
|
||
item = strings.TrimSuffix(item, string(filepath.Separator)) | ||
ignoringItems = append(ignoringItems, item) | ||
} | ||
} | ||
|
||
return ignoringItems, err | ||
} | ||
|
||
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 | ||
} | ||
|
||
func hasGlobPatterns(item string) bool { | ||
if strings.Contains(item, "*") || strings.Contains(item, "?") || | ||
strings.Contains(item, "!") || strings.Contains(item, "[") || | ||
strings.Contains(item, "{") { | ||
return true | ||
} | ||
return false | ||
} |
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