provides information about a file
[x, ierr] = fileinfo(files)
a character string or a string column vector, file pathname.
an integer vector of size 13 containing information or an empty matrix if file does not exist.
If files is a string column vector, x will be a matrix of
size m x 13.
If a filename does not exist, it will return as output
information a line of size 13 with Nan
in each element of this line.
error indicator, 0, if no error has occurred.
x = fileinfo(file) returns:
The file size
The file mode (decimal value)
The user id
The group id
The device number
The date of last data modification
The date of last file status change
The date of last access
The device type (if inode device)
The blocksize for filesystem I/O (always 0 on Windows)
The number of blocks allocated (always 0 on Windows)
The inode
The number of hard links
This function is an interface to the C function stat.
Permissions are typically specified as octal numbers: dec2oct(x(2)) to convert.
Numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. Any omitted digits are assumed to be leading zeros. The first digit selects the set user ID (4) and set group ID (2) and sticky (1) attributes. The second digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.
So, to check permissions, it is necessary to use masks. Let us take an example:
In octal, x(2)=1664, so first digit corresponds to sticky attributes. The second
indicates that file owner has permission of writing and reading. It is the same
for other users in the file's group. Finally, others users has just right to read.
To apply a mask, it is simpler to look at this octal in binary.
So: x(2)= 1 110 110 100.
To check if the owner has write permission, we must take a look at the second triplet: 110
and compare it with write permission 010. So, the operation is: 110 000 000 & 010 000 000.
If result is not null (it is the case here), owner has write permission.
w = fileinfo(SCI+'/etc/scilab.start') // file permission dec2oct(w(2)) // file date getdate(w(6)) // Permissions S_IWRITE = 128; // mask write permission S_IEXEC = 64; // mask exec permission S_IREAD = 256; // mask read permission S_IFCHR = 8192; // mask directory permission // Checks write permission if ( bitand( w(2), S_IWRITE ) <> 0) then disp('WRITE PERMISSION on this file.'); else disp('NO WRITE PERMISSION on this file.'); end // Checks read permission if ( bitand( w(2), S_IREAD ) <> 0) then disp('READ PERMISSION on this file.'); else disp('NO READ PERMISSION on this file.'); end FILES = [SCI;SCIHOME;'not_exist_file';TMPDIR] [X,ERRS] = fileinfo(FILES) | ![]() | ![]() |