~snappy-dev/snappy/15.04

« back to all changes in this revision

Viewing changes to pkg/snapfs/pkg.go

  • Committer: Snappy Tarmac
  • Author(s): John R. Lenton, Michael Vogt, Leo Arias, Launchpad Translations on behalf of snappy-dev, Federico Gimenez, Ricardo Mendoza
  • Date: 2015-10-13 06:03:57 UTC
  • mfrom: (711.1.54 snappy)
  • Revision ID: snappy_tarmac-20151013060357-rf1cz6y7190uzzlr
Merge trunk fixes. by snappy-dev approved by chipaca

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: Go; indent-tabs-mode: t -*-
 
2
 
 
3
/*
 
4
 * Copyright (C) 2014-2015 Canonical Ltd
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 3 as
 
8
 * published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 */
 
19
 
 
20
package snapfs
 
21
 
 
22
import (
 
23
        "fmt"
 
24
        "io/ioutil"
 
25
        "os"
 
26
        "os/exec"
 
27
        "path/filepath"
 
28
        "strings"
 
29
 
 
30
        "launchpad.net/snappy/helpers"
 
31
)
 
32
 
 
33
// Snap is the squashfs based snap
 
34
type Snap struct {
 
35
        path string
 
36
}
 
37
 
 
38
// Name returns the Name of the backing file
 
39
func (s *Snap) Name() string {
 
40
        return filepath.Base(s.path)
 
41
}
 
42
 
 
43
// New returns a new Snapfs snap
 
44
func New(path string) *Snap {
 
45
        return &Snap{path: path}
 
46
}
 
47
 
 
48
// Close is not doing anything for snapfs - COMPAT
 
49
func (s *Snap) Close() error {
 
50
        return nil
 
51
}
 
52
 
 
53
// ControlMember extracts from meta/ - COMPAT
 
54
func (s *Snap) ControlMember(controlMember string) ([]byte, error) {
 
55
        return s.ReadFile(filepath.Join("DEBIAN", controlMember))
 
56
}
 
57
 
 
58
// MetaMember extracts from meta/ - COMPAT
 
59
func (s *Snap) MetaMember(metaMember string) ([]byte, error) {
 
60
        return s.ReadFile(filepath.Join("meta", metaMember))
 
61
}
 
62
 
 
63
// ExtractHashes does notthing for snapfs snaps - COMAPT
 
64
func (s *Snap) ExtractHashes(dir string) error {
 
65
        return nil
 
66
}
 
67
 
 
68
// UnpackWithDropPrivs unpacks the meta and puts stuff in place - COMAPT
 
69
func (s *Snap) UnpackWithDropPrivs(instDir, rootdir string) error {
 
70
        // FIXME: actually drop privs
 
71
        return s.Unpack("*", instDir)
 
72
}
 
73
 
 
74
// UnpackMeta unpacks just the meta/* directory of the given snap
 
75
func (s *Snap) UnpackMeta(dst string) error {
 
76
        if err := s.Unpack("meta/*", dst); err != nil {
 
77
                return err
 
78
        }
 
79
 
 
80
        return s.Unpack(".click/*", dst)
 
81
}
 
82
 
 
83
var runCommand = func(args ...string) error {
 
84
        cmd := exec.Command(args[0], args[1:]...)
 
85
        if output, err := cmd.CombinedOutput(); err != nil {
 
86
                return fmt.Errorf("cmd: %q failed: %v (%q)", strings.Join(args, " "), err, output)
 
87
        }
 
88
 
 
89
        return nil
 
90
}
 
91
 
 
92
// Unpack unpacks the src (which may be a glob into the given target dir
 
93
func (s *Snap) Unpack(src, dstDir string) error {
 
94
        return runCommand("unsquashfs", "-f", "-i", "-d", dstDir, s.path, src)
 
95
}
 
96
 
 
97
// ReadFile returns the content of a single file inside a snapfs snap
 
98
func (s *Snap) ReadFile(path string) (content []byte, err error) {
 
99
        tmpdir, err := ioutil.TempDir("", "read-file")
 
100
        if err != nil {
 
101
                return nil, err
 
102
        }
 
103
        defer os.RemoveAll(tmpdir)
 
104
 
 
105
        unpackDir := filepath.Join(tmpdir, "unpack")
 
106
        if err := runCommand("unsquashfs", "-i", "-d", unpackDir, s.path, path); err != nil {
 
107
                return nil, err
 
108
        }
 
109
 
 
110
        return ioutil.ReadFile(filepath.Join(unpackDir, path))
 
111
}
 
112
 
 
113
// CopyBlob copies the snap to a new place
 
114
func (s *Snap) CopyBlob(targetFile string) error {
 
115
        // FIXME: helpers.CopyFile() has no preserve attribute flag yet
 
116
        return runCommand("cp", "-a", s.path, targetFile)
 
117
}
 
118
 
 
119
// Verify verifies the snap
 
120
func (s *Snap) Verify(unauthOk bool) error {
 
121
        // FIXME: there is no verification yet for snapfs packages, this
 
122
        //        will be done via assertions later for now we rely on
 
123
        //        the https security
 
124
        return nil
 
125
}
 
126
 
 
127
// Build builds the snap
 
128
func (s *Snap) Build(buildDir string) error {
 
129
        fullSnapPath, err := filepath.Abs(s.path)
 
130
        if err != nil {
 
131
                return err
 
132
        }
 
133
 
 
134
        return helpers.ChDir(buildDir, func() error {
 
135
                return runCommand(
 
136
                        "mksquashfs",
 
137
                        ".", fullSnapPath,
 
138
                        "-all-root",
 
139
                        "-noappend",
 
140
                        "-comp", "xz")
 
141
        })
 
142
}