File: cat.go

package info (click to toggle)
rust-pathrs 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,912 kB
  • sloc: python: 1,138; sh: 371; ansic: 259; makefile: 151
file content (65 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: MPL-2.0
/*
 * libpathrs: safe path resolution on Linux
 * Copyright (C) 2019-2025 Aleksa Sarai <cyphar@cyphar.com>
 * Copyright (C) 2019-2025 SUSE LLC
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

// Original author of this example code:
// Maxim Zhiburt <zhiburt@gmail.com> (2020)

// File: examples/c/cat.c
//
// An example program which opens a file inside a root and outputs its contents
// using libpathrs.

package main

import (
	"errors"
	"fmt"
	"io"
	"os"

	"cyphar.com/go-pathrs"
)

func Main(args ...string) error {
	if len(args) != 2 {
		fmt.Fprintln(os.Stderr, "usage: cat <root> <unsafe-path>")
		os.Exit(1)
	}

	rootPath, unsafePath := args[0], args[1]

	root, err := pathrs.OpenRoot(rootPath)
	if err != nil {
		return fmt.Errorf("open root %q: %w", rootPath, err)
	}
	defer root.Close()

	file, err := root.Open(unsafePath)
	if err != nil {
		return fmt.Errorf("open %q: %w", unsafePath, err)
	}
	defer file.Close()

	fmt.Fprintf(os.Stderr, "== file %q (from root %q) ==\n", file.Name(), root.IntoFile().Name())

	if _, err := io.Copy(os.Stdout, file); err != nil {
		return fmt.Errorf("copy file contents to stdout: %w", err)
	}
	return nil
}

func main() {
	if err := Main(os.Args[1:]...); err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		fmt.Fprintf(os.Stderr, "Source: %v\n", errors.Unwrap(err))
		os.Exit(1)
	}
}