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

来源:互联网 发布:虚拟货币网站源码 编辑:程序博客网 时间:2024/06/06 00:47

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


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

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

它的snapcraft.yaml文件如下:


name: helloversion: "1.0"summary: The 'hello-world' of snapsdescription: |    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 textgrade: stableconfinement: stricttype: app  #it can be gadget or frameworkicon: icon.pngapps: 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/writetocommonparts: hello:  plugin: dump  source: .

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

#!/bin/bashecho "Going to make a directory at: $XDG_RUNTIME_DIR"mkdir -p $XDG_RUNTIME_DIRecho "Create a file at the location..."cd $XDG_RUNTIME_DIRpwdtouch thisfileif [ $? == 0 ]; thenecho "The file is successfully created!"elseecho "The file is not successfully created!"fi

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

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

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

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

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


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

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

fifo

#!/bin/bashecho "Going to make a directory at: $XDG_RUNTIME_DIR"mkdir -p $XDG_RUNTIME_DIRecho "Create a file at the location..."cd $XDG_RUNTIME_DIRpwdtouch thisfileif [ $? == 0 ]; thenecho "The file is successfully created!"elseecho "The file is not successfully created!"ficd /tmppwdecho "Haha" > test.txtif [ $? == 0 ]; thenecho "The test.txt file is successfully created!"elseecho "The test.txt file is not successfully created!"fi


0 0
原创粉丝点击