xref: /aosp_15_r20/external/spdx-tools/builder/builder2v2/build_file.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package builder2v2
4
5import (
6	"fmt"
7	"path/filepath"
8
9	"github.com/spdx/tools-golang/spdx/common"
10	"github.com/spdx/tools-golang/spdx/v2_2"
11	"github.com/spdx/tools-golang/utils"
12)
13
14// BuildFileSection2_2 creates an SPDX File (version 2.2), returning that
15// file or error if any is encountered. Arguments:
16//   - filePath: path to file, relative to prefix
17//   - prefix: relative directory for filePath
18//   - fileNumber: integer index (unique within package) to use in identifier
19func BuildFileSection2_2(filePath string, prefix string, fileNumber int) (*v2_2.File, error) {
20	// build the full file path
21	p := filepath.Join(prefix, filePath)
22
23	// make sure we can get the file and its hashes
24	ssha1, ssha256, smd5, err := utils.GetHashesForFilePath(p)
25	if err != nil {
26		return nil, err
27	}
28
29	// build the identifier
30	i := fmt.Sprintf("File%d", fileNumber)
31
32	// now build the File section
33	f := &v2_2.File{
34		FileName:           filePath,
35		FileSPDXIdentifier: common.ElementID(i),
36		Checksums: []common.Checksum{
37			{
38				Algorithm: common.SHA1,
39				Value:     ssha1,
40			},
41			{
42				Algorithm: common.SHA256,
43				Value:     ssha256,
44			},
45			{
46				Algorithm: common.MD5,
47				Value:     smd5,
48			},
49		},
50		LicenseConcluded:   "NOASSERTION",
51		LicenseInfoInFiles: []string{"NOASSERTION"},
52		FileCopyrightText:  "NOASSERTION",
53	}
54
55	return f, nil
56}
57