User Tools

Site Tools


k6:k6.7:start

6.7 Instructions CHOWN, CHMOD and CHGRP

Selected properties of the Stat class can be used to read information about the owner, group or permissions of a particular file or directory:

Dim sFilePath As String = User.Home &/ "nwm.xml"

Print "User  = " & Stat(sFilePath).User
Print "Group = " & Stat(sFilePath).Group
Print "Permissions = " & Stat(sFilePath).Auth

Results:

User  = hans
Group = hans
Permissions = rwSrw-r--

Since Gambas 3.1, the three instructions CHOWN, CHGRP and CHMOD are available to change the owner, group or rights of a file. However, this is only possible if you have the appropriate rights as a user.

6.7.1 CHOWN

The CHOWN (change owner) instruction changes the owner of a file or directory:

CHOWN Path TO User
  • Path is the file or directory path,
  • User is the name of the new owner.

6.7.1.1 CHGRP

The CHGRP (change group) instruction changes the group membership of a file or directory:

CHGRP Path TO Group
  • Path is the file or directory path,
  • Group is the name of the new group.

6.7.1.2 CHMOD

Note that up to (stable) Gambas version 3.11.3, the CHMOD instruction is sometimes incorrect when setting the sticky bit. A correction option is given in the presented project. The error was eliminated with commit d44b4bd7f in the development version.

The CHMOD (change mode) instruction changes the permissions (mode) of a file or directory:

CHMOD Path TO Mode
  • Path is the file or directory path,
  • Mode is a string (permission string) describing the new mode.

At http://gambaswiki.org/wiki/cat/mode or https://wiki.ubuntuusers.de/rights you can read the mode syntax. Furthermore, any omitted or unknown character - such as a dot in the following 2nd case - is ignored in the mode string.

In this way, you can change selected file permissions. In the first case, all permissions are reset. In the second case, only the “Execute” right is set for the user, all others are retained:

CHMOD sScriptPath To "r-x------"		' 1. Case
CHMOD sScriptPath To "..x......"		' 2. Case

6.7.1.3 Syntax File Permissions

The file modes are described by a nine-character (rights) string that follows the same scheme as the shell command 'ls':

PositionCharacterDescription
1-The file owner cannot read it.
1rThe file owner can read it.
2-The file owner cannot write it.
2wThe file owner can write it.
3-The file owner cannot execute it.
3xThe file owner can execute it.
3SThe file owner cannot execute it and the bit 'setuid' is set.
3sThe file owner can execute it and the bit 'setuid' is set.
4-The file group cannot read it.
4rThe file group can read it.
5-The file group cannot write it.
5wThe file group can write it.
6-The file group cannot execute it.
6xThe file group can execute it.
6SThe file group cannot execute it and the bit 'setgid' is set.
6sThe file group can execute it and the bit 'setgid' is set.
7-Other users cannot read the file.
7rOther users can read the file.
8-Other users cannot write to the file.
8wOther users can write to the file.
9-Other users cannot execute the file.
9xOther users can execute the file.
9TOther users cannot execute the file and the 'sticky' bit is set.
9tOther users can execute the file and the 'sticky' bit is set.

Table 6.7.1.3.1 : Syntax file permissions

6.7.2 Digression: file timings

The reading of time information (all times in UTC) of a file is achieved via the properties Stat.LastAccess (atime) or Stat.Change (ctime) or Stat.Modified (mtime, synonym: Stat.Time).

Changing the timestamp of a file is obviously not currently provided for in Gambas. However, this can be done quite easily with an Exec instruction:

' Gambas class file
 
Public sFilePath As String = Temp("date_time")
 
Public Sub _new()
  File.Save(sFilePath, "Empty ...")
End
 
Public Sub btnSetTime_Click()
 
  Dim aExecCommand As String[]
  Dim sDateTimeTZ, sDate, sTime, sTimeZone As String
 
  sDate = "2018-07-31"
  sTime = "18:17:34"
  sTimeZone = "+0000" ' UTC: "+0200" for Germany
  sDateTimeTZ = sDate & " " & sTime & " " & sTimeZone
 
  aExecCommand = ["touch", "-d", sDateTimeTZ, sFilePath]
  Exec aExecCommand
 
End
 
Public Sub btnGetTime_Click()
 
  txbDateTime.Text = Subst("FileDateTime = &1 &2", Stat(sFilePath).Time, "(UTC)")
  Print Stat(sFilePath).LastModified
 
End

B1
Figure 6.7.2.1: Changing the timestamp of a file

6.7.3 CreationTime (crtime)

One problem is reading out the time that represents the time at which the file was created (CreationTime or crtime). There are several reasons for this:

  • The time crtime is only stored on Ext4 systems.
  • Strictly speaking, the time crtime is the timestamp of the inode in the file system and not of the file.
  • With sudo tune2fs -l $(df . –output=source | grep ^/) | grep “Inode size:” the value 'Inode size' can be determined and must be greater than 256, because otherwise the additional information about crtime cannot be stored in the file.
  • Many editors always store changed files as a new file and overwrite the original CreationTime (crtime). OpenLibre is a welcome exception in this respect.
  • When copying a file, the copy receives as creation time crtime the time when copying.

At https://tecadmin.net/file-creation-time-linux/ you will find more information and the specification of two command lines to determine the value for the CreationTime of a file in a console.

Here is the result after entering the above two command lines for a file after copying from a BackUp hard disk:

Inode: 3672254   Type: regular    Mode:  0644   Flags: 0x80000
Generation: 1948083613    Version: 0x00000000:00000001
User:  1000   Group:  1000   Size: 72674
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 144
Fragment:  Address: 0    Number: 0    Size: 0
 ctime: 0x5a9fd64f:454b4df4 -- Wed Mar  7 13:08:47 2018
 atime: 0x5b618f64:831a72b8 -- Wed Aug  1 12:45:56 2018
 mtime: 0x599b1a34:5ea40700 -- Mon Aug 21 19:36:52 2017
crtime: 0x5a9fd64f:454b4df4 -- Wed Mar  7 13:08:47 2018
Size of extra inode fields: 32
EXTENTS:
(0-17):14719769-14719786

What value the knowledge of the CreationTime (crtime) has for you, you have to decide yourself, because the ModifyTime (mtime) is far ahead of the CreationTime in the above case!

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k6/k6.7/start.txt · Last modified: 16.01.2022 (external edit)

Page Tools