A temporary directory is used to store files for a limited time. You use temporary files to store data for a limited period of time. Temporary directories and temporary files are automatically deleted by the operating system after the Gambas programme ends.
For example, if you archive your scans in TIFF files to convert them later to PNG or JPG format, it is a good idea to copy a selection of TIFF files into a temporary directory and then convert them. Finally, exit your Gambas conversion programme, then the selected TIFF files in the temporary directory and also the temporary directory are automatically deleted.
Temporary files are needed if, for example, you want to use a Bash script in a Gambas programme and the script script.sh is located in the project folder in a data directory. If you call the script at runtime after starting the executable file *.gambas, you will end up with an error message because you do not have permission to execute the script. The reason is that all files in the Gambas archive *.gambas can only be read! A way out with the use of a temporary file that you can read and execute is briefly outlined below:
Public sScriptPath As String Public sTempScriptPath As String Public sStore As String Public Sub Form_Open() sScriptPath = "./data/test_script.sh" ' Set path to the script (original) sTempScriptPath = Temp(File.BaseName(sScriptPath)) ' Set path to a temporary file Copy sScriptPath To sTempScriptPath ' Copy original script to temporary file Chmod sTempScriptPath To "r-xr-xr-x" ' Set execution rights for the copy ' Chmod sTempScriptPath To "..x..x..x" ' Alternative End Public Sub btnRunScript_Click() Shell sTempScriptPath To sStore ' Script copy in a shell instruction … ' Execute with the required rights ... End
The temporary file /tmp/gambas.1000/<PID>/test_script.tmp - which was worked with - is automatically deleted after the programme ends.
File name = Temp$( [ Prefix ] ) File name = Temp( [ Prefix ] )
The Temp() and Temp$() functions return the path for a temporary file or directory. The path has the following form:
/tmp/gambas.<UserID>/<ProcessID>/<Prefix>.tmp
If no prefix is given, it is replaced by an integer which is automatically incremented with each further call of this function. In this way, the returned path name is always unique. This function returns only one path. You can then use this path to temporarily create, for example, a file, a directory, a Unix socket or a symlink.
Important to know:
Example:
Dim sTempDir As String = Temp$() Mkdir sTempDir ' Unpacks an archive into the created temporary directory. Exec ["tar" "-zxf", "<path_to_archiv>/archive.tar.gz", "-C", sTempDir] Wait ' If the instruction EXEC [...] generates an error, you will be informed like this: If Process.LastValue Then Print Error.Code ...
With this source code section
' Gambas class file Public Sub btnStart_Click() Dim sRuntimeDir, sTempDir As String txaResults.Clear() txaResults.Insert(gb.Lf) ' 1. Call of the function Temp$() sTempDir = Temp$() txaResults.Insert("File-Path 1 = " & sTempDir & gb.Lf) File.Save(sTempDir, "1") ' 2. Call of the function Temp() - alternative syntax sTempDir = Temp() txaResults.Insert("File-Path 2 = " & sTempDir & gb.Lf) ' 2. Calling the function with alternative syntax File.Save(sTempDir, "2") ' 3. Call of the function Temp(...) with default of a freely defined file name 'test' sTempDir = Temp$("test") txaResults.Insert("File-Path 3 = " & sTempDir & gb.Lf) File.Save(sTempDir, "3") ' 4. Creation of the file with the path /tmp/gambas.1000/<PID>/link.tmp and the content "gambas-book.net" sTempDir = Temp$("content") txaResults.Insert("File-Path 4 = " & sTempDir & gb.Lf) File.Save(sTempDir, "gambas-book.net") ' Datei txaResults.Insert("File-Content = " & File.Load(sTempDir) & gb.Lf) ' 5. Creation of a file in a special temporary directory sRuntimeDir = Desktop.RuntimeDir &/ "time.txt" txaResults.Insert("File-Path 5 = " & sRuntimeDir & gb.Lf) File.Save(sRuntimeDir, Date.ToUnixTime(Now())) ' File txaResults.Insert("Unix Timestamp = " & File.Load(sRuntimeDir) & gb.Lf) txaResults.Insert("Date from Timestamp = " & Date.FromUnixTime(File.Load(sRuntimeDir)) & gb.Lf) ' 6. Creation of an empty text file in a temporary directory sTempDir = File.Dir(Temp()) &/ "gambas-book" If Exist(sTempDir) Then DeleteDirRecursively(sTempDir) Rmdir sTempDir Endif ' If Exist(sTempDir) Then Shell.RmDir(sTempDir) ' Effective alternative! Shell.MkDir(sTempDir) File.Save(sTempDir &/ "values.log", "") txaResults.Insert("File-Path 6 = " & sTempDir &/ "values.log" & gb.Lf) End Public Sub DeleteDirRecursively(sDir As String) Dim sFile, sDirectory As String For Each sFile In RDir(sDir, "*") If Not IsDir(sFile) Then ' Print "File-Path: ", sDir &/ sFile Kill sDir &/ sFile Endif Next For Each sDirectory In RDir(sDir, "*", gb.Directory) ' Print "Folder: ", sDirectory Rmdir sDirectory Next End
results in these outputs:
Another launch results in new temporary files as well as changed contents:
The special feature in the source code is the deletion of the temporary directory with the alternative approaches via Shell.RmDir(sTempDir) and the procedure DeleteDirRecursively(sDir As String).
The /run directory is a virtual temporary file system (https://en.wikipedia.org/wiki/Tmpfs). It exists in the main memory and is automatically emptied when the computer is restarted. The environment variable $XDG_RUNTIME_DIR defines a good place for your runtime files. It is the directory /run/user/<user-id> if the environment variable $XDG_RUNTIME_DIR is set to the default value. It seems that /run/user/<user-id> rivals the /tmp directory. Obviously /run seems to be more modern, as indicated by the description on https://unix.stackexchange.com/questions/13972/what-is-this-new-run-filesystem/13983#13983. It's certainly a good plan if each user has their own (temporary) directory under /run/user/<user-id> from the start.
$ echo $XDG_RUNTIME_DIR /run/user/1000
Although Gambas currently only supports /tmp with Temp$() and generates its “gambas.<user-id>” itself, you also have access to the (base) directory /run/user/1000. You get access as a user via the value of the property Desktop.RuntimeDir (gb.desktop). This property (data type string) returns the base directory in which user-specific, but not necessarily runtime-required data and other stream objects such as Unix sockets or named pipes are to be stored. The user must be the only one with Unix access mode 0700 who has full access to the /run/user directory - which can be quickly checked:
hans@mint-183 ~ $ cd /run/user hans@mint-183 /run/user $ ls -l in total 0 drwx------ 7 hans hans 180 Aug 24 10:37 1000
See chapter '6.1 Paths in Gambas' for further instructions.