github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/daemon/api_snap_file.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 daemon
    21  
    22  import (
    23  	"net/http"
    24  
    25  	"github.com/snapcore/snapd/overlord/auth"
    26  	"github.com/snapcore/snapd/overlord/snapstate"
    27  	"github.com/snapcore/snapd/overlord/state"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  var snapFileCmd = &Command{
    32  	Path:     "/v2/snaps/{name}/file",
    33  	UserOK:   true,
    34  	PolkitOK: "io.snapcraft.snapd.manage",
    35  	GET:      getSnapFile,
    36  }
    37  
    38  func getSnapFile(c *Command, r *http.Request, user *auth.UserState) Response {
    39  	vars := muxVars(r)
    40  	name := vars["name"]
    41  
    42  	st := c.d.overlord.State()
    43  	st.Lock()
    44  	defer st.Unlock()
    45  
    46  	var snapst snapstate.SnapState
    47  	var info *snap.Info
    48  	err := snapstate.Get(st, name, &snapst)
    49  	if err == nil {
    50  		info, err = snapst.CurrentInfo()
    51  	}
    52  	switch err {
    53  	case nil:
    54  		// ok
    55  	case state.ErrNoState:
    56  		return SnapNotFound(name, err)
    57  	default:
    58  		return InternalError("cannot download file for snap %q: %v", name, err)
    59  	}
    60  	if !snapst.Active {
    61  		return BadRequest("cannot download file of inactive snap %q", name)
    62  	}
    63  	if snapst.TryMode {
    64  		return BadRequest("cannot download file for try-mode snap %q", name)
    65  	}
    66  
    67  	return fileResponse(info.MountFile())
    68  }