Enumerating a files directory content:
Many flags can be provided as the second argument. They are defined in /pliant/language/stream/listmode.pli
Getting informations about a single file:
var FileInfo info := file_query "file:/some_directory/some_subdirectory/some_file" standard
Here are a few helper functions for 'FileInfo' data type. They are defined in /pliant/language/stream/filebase1.pli:
'path' method returns the path contained in the file name, that is the directory the file is in:
console info:path eol
'name_without_path' returns the name of the file, without the path:
console info:name_without_path eol
'stripped_name' returns the name of the file, without the path, and without the file extension:
console info:stripped_name eol
'extension' returns the file extension. The result string is either empty, or starting with a dot sign.
console info:extension eol
'is_link' returns a boolean specifying if the file is a soft link in facts:
'link' returns the soft link value:
console info:link eol
'is_directory' returns a boolean specifying if the file is a directory in facts:
'is_file' returns a boolean specifying if the file is a real file as opposed to a directory, a link, or a device:
'is_device' returns a boolean specifying if the file is a device in facts:
Creating, copying, moving and deleting files
This is implemented in /pliant/admin/file.pli module.
Creates the specified directory if it does not already exists. If some subdirectory must be created to enable the creation of the specified directory, it will be done automatically.
var ExtendedStatus s := file_delete "file:/tmp/test"
Deletes the specified file or directory. A non empty directory cannot be deleted using 'file_delete'.
var ExtendedStatus s := file_tree_delete "file:/tmp/foo/"
Deletes the specified directory including all the files and directories it might contain.
var ExtendedStatus s := file_link "file:/pliant/binary/pliant_debug1.exe" "file:/bin/pliant"
Creates a Unix soft link. The first parameter is the existing file, and the second the name of the softlink to create. See 'file_move' bellow for the thrird optional parameter.
var ExtendedStatus s := file_clone "file:/tmp/test" "file:/tmp/another"
Creates a Unix hard link. Please notice that Linux does not support directories hard links.
var ExtendedStatus s := file_copy "file:/tmp/test" "file:/tmp/another" extended
Copy the file content. The first parameter is the source, the second is the target.
Here are some of the optional flags:
file_copy "file:/tmp/test" "file:/tmp/another" extended+lazy
Copy only if the destination file does not exist or has not the same size of date and time as the source one.
file_copy "file:/tmp/test" "file:/tmp/another" extended+bidirectional
If the target file exists, and is newer than the source file, then the target file will be copied to the source file.
var ExtendedStatus s := file_tree_copy "file:/tmp/foo/" "file:/backup/foo/" extended
Copy all the directory and all files and subdirectories it contains.
file_tree_copy "file:/tmp/foo/" "file:/backup/foo/" extended+delete
If some file is in the target directory, but not in the source directory, then it will be removed.
file_tree_copy "file:/tmp/foo/" "file:/backup/foo/" extended+linktransparent
'linktransparent' attributes says that if there is a soft link in the source tree, it will not be copied as as a soft link in the target tree. It's content will rather be copied.
var ExtendedStatus s := file_move "file:/tmp/test" "file:/tmp/newname"
Moves some file or directory. Pliant will first try to use the operating system 'move' instruction, because it's more efficient. If it fails, Pliant will silently try to do copy then delete.
'file_link' 'file_clone' and 'file_move' accept a third 'force' optional parameter. If it's value is true and the target file already exists, it will be overwritten:
file_move "file:/tmp/test" "file:/tmp/newname" true
Deletes all empty directories in the specified directory and it's subdirectories.
file_extract "file:/tmp/foo.tgz" "file:/tmp/foo/"
Unpack a Unix or Windows standard tarball file to the specified directory. Supported tarball extensions are '.tgz', '.tar.gz', '.tar.bz2', '.tar' (under Unix only) and '.zip' (all plateforms).
There is no specific tool for creating tarballs. Please use 'execute' instruction.
Changing files attributes
The main instruction for changing files attributes is 'file_configure':
var ExtendedStatus s := file_configure "file:/tmp/test" "datetime "+string:(date 2009 1 1 0 0 0 0)
There are several attributes you can change through 'file_configure'. The main one is the file date that we have just seen. The file owner and access rights can be changed using 'file_configure' instruction, but using 'file_rights' instruction described bellow is probably easier. You might also want to use 'file_configure' operation to set files details such as Linux EXT3 journaling flag, but then you should just review 'configure' method in /pliant/language/stream/native.pli module to see all possibilities.
file_rights "file:/tmp/test" undefined undefined 6*8^2+4*8+4 0
Changes Unix access rights of the specified file.
file_tree_rights "file:/tmp/foo/" undefined undefined 6*8^2+4*8+4 0 7*8^2+7*8+7 0
Same, but changes access rights of all files and directories in the specified path.
The name of a tempory file shall be obtained through calling 'file_temporary' function:
var Str temp := file_temporary
Editing ascii files
Scripting and automation often requires to apply a few changes to an ASCII configuration file.
The 'AsciiFile' data type is a superset of 'Array:Str', so the following methods are available:
The following extra methods are also available:
af insert 3 "my line"
Inserts a new line. The lines with index ranging from 3 to the end are shifted. Once again, the first line has index 0, not 1.
af remove 3
Removes the line with index 3. The lines with index ranging from 4 to the end are shifted.
af get "CC" "="
Gets the value after the '=' sign in the line that looks like:
CC = anything
af set "CC" "=" "CC = gcc272"
Changes the line.
af load "file:/tmp/test"
Guess what it does.
A complete sample could be (extracted from module /pliant/linux/kernel/build_kernel.pli):
(var AsciiFile makefile) load "file:/usr/src/linux/Makefile"
The main difference between 'set' and 'fuzzy_set' is that 'fuzzy_set' will accept a line that as something in front of the requested indentifier.
makefile set "SMP" "=" "SMP = 1"
would change line
SMP = 0
SMP = 1
but would not change
# SMP = 1
On the other hand,
makefile fuzzy_set "SMP" "=" "SMP = 1"
would change both.
In the following example:
(var AsciiFile f) load "target:/etc/login.defs"
the second argument is "", so the line:
will be changed to
'set' and 'fuzzy_set' do change only the first occurrence.
Lastly 'replace' can be used to replace any occurrence of the first provided parameter with the second.
f replace "clic" "click"