github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/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  	GET:        getSnapFile,
    34  	ReadAccess: openAccess{},
    35  }
    36  
    37  func getSnapFile(c *Command, r *http.Request, user *auth.UserState) Response {
    38  	vars := muxVars(r)
    39  	name := vars["name"]
    40  
    41  	st := c.d.overlord.State()
    42  	st.Lock()
    43  	defer st.Unlock()
    44  
    45  	var snapst snapstate.SnapState
    46  	var info *snap.Info
    47  	err := snapstate.Get(st, name, &snapst)
    48  	if err == nil {
    49  		info, err = snapst.CurrentInfo()
    50  	}
    51  	switch err {
    52  	case nil:
    53  		// ok
    54  	case state.ErrNoState:
    55  		return SnapNotFound(name, err)
    56  	default:
    57  		return InternalError("cannot download file for snap %q: %v", name, err)
    58  	}
    59  	if !snapst.Active {
    60  		return BadRequest("cannot download file of inactive snap %q", name)
    61  	}
    62  	if snapst.TryMode {
    63  		return BadRequest("cannot download file for try-mode snap %q", name)
    64  	}
    65  
    66  	return fileResponse(info.MountFile())
    67  }