您的位置:首页 > 其它

如何在snap设计中选择文件目录作为临时存储

2017-02-04 11:37 686 查看
在我们的应用设计中,我们通过会选择一些临时的文件目录来存储我们的文件,比如在Linux中的tmp文件目录.那么在我们的snap设计中,我们应该利用哪个文件目录来存储我们的文件呢?答案是我们可以选择XDG_RUNTIME_DIR,当然这也依赖于开发者自己的选择.

我们先来看一下我的一个做好的例程:

https://github.com/liu-xiao-guo/helloworld-fifo

它的snapcraft.yaml文件如下:

name: hello
version: "1.0"
summary: The 'hello-world' of snaps
description: |
This is a simple snap example that includes a few interesting binaries
to demonstrate snaps and their confinement.
* hello-world.env  - dump the env of commands run inside app sandbox
* hello-world.evil - show how snappy sandboxes binaries
* hello-world.sh   - enter interactive shell that runs in app sandbox
* hello-world      - simply output text
grade: stable
confinement: strict
type: app  #it can be gadget or framework
icon: icon.png

apps:
fifo:
command: bin/fifo
env:
command: bin/env
evil:
command: bin/evil
sh:
command: bin/sh
hello-world:
command: bin/echo
createfile:
command: bin/createfile
createfiletohome:
command: bin/createfiletohome
writetocommon:
command: bin/writetocommon

parts:
hello:
plugin: dump
source: .


在这里,我们设计了一个叫做fifo的应用.它的脚本具体如下:

#!/bin/bash

echo "Going to make a directory at: $XDG_RUNTIME_DIR"
mkdir -p $XDG_RUNTIME_DIR

echo "Create a file at the location..."
cd $XDG_RUNTIME_DIR
pwd
touch thisfile

if [ $? == 0 ]; then
echo "The file is successfully created!"
else
echo "The file is not successfully created!"
fi


我首先创建一个目录,并在目录中创建一个文件.显示如下:

liuxg@liuxg:~$ hello.fifo
Going to make a directory at: /run/user/1000/snap.hello
Create a file at the location...
/run/user/1000/snap.hello
The file is successfully created!


显然这个应用的运行是没有任何的permission的问题的.它是完全可以访问并进行读写的位置.这个位置可以被我们的应用程序用来进行FIFO的操作.

我们实际上也可以运行我在应用中的env这个应用来展示所有的环境变量:

liuxg@liuxg:~$ hello.env | grep XDG_RUNTIME_DIR
XDG_RUNTIME_DIR=/run/user/1000/snap.hello


当然,我们也可以使用/tmp目录来作为临时存储文件目录.这个目录对于每个snap应用来说都是独特的,也就是每个应用有一个自己的独立的tmp目录.但是我们我们都可以按照/tmp的方式去访问.这个文件的位置可以在我们的桌面电脑的/tmp目录下找到。它的文件目录有点像/tmp/snap.1000_snap.hello.fifo_5BpMiB/tmp。

我们可以使用如下的代码来检验这个:

fifo

#!/bin/bash

echo "Going to make a directory at: $XDG_RUNTIME_DIR"
mkdir -p $XDG_RUNTIME_DIR

echo "Create a file at the location..."
cd $XDG_RUNTIME_DIR
pwd
touch thisfile

if [ $? == 0 ]; then
echo "The file is successfully created!"
else
echo "The file is not successfully created!"
fi

cd /tmp
pwd
echo "Haha" > test.txt

if [ $? == 0 ]; then
echo "The test.txt file is successfully created!"
else
echo "The test.txt file is not successfully created!"
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐