Golang program to make a file read-only


We can use chmod function and syscall package in Golang to make a file read-only file. To convert a file into read-only files,the chmod function set the file’s right to 044. The os.Chmod function is then used to modify the file's mode. In syscall package of Golang we are going to use os.stat function to obtain information about the file.

Method 1: Using chmod function

This program employs the Chmod function to modify a file's permissions using the os package.

Syntax

os.Chmod

In Go, this function belongs to the os package. The main goal of this function is to change the permission of a file or directory. It takes two arguments , one is the filename whose permission is to be changed and the second argument is the new permission to be set.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Specify the file's location where you wish it to be read-only.

  • Step 3 − Change the file's permissions to 0444 using the Chmod function in the os package. This makes the file readable but not writable.

  • Step 4 − Verify for any mistakes that might have occurred when updating the permissions.

  • Step 5 − Print an error message and stop the program if there is one or Print a success message if there are no mistakes.

Example

In this example, we will use chmod function to make a file read-only.

package main
import (
   "fmt"
   "os"
)

func main() {
   // change the file path to your file's location
   file_name := "/path/to/your/file"

   // change the file permissions to read-only
   errs := os.Chmod(file_name, 0444)
   if errs != nil {
      fmt.Println("Error making file read-only:", errs)
      return
   }
   fmt.Println("File made read-only successfully!")
}

Output

File made read-only successfully!

Method 2: Using syscall package

In this method, bitwise operations are used to eliminate the write permission bits for the user, group, and other permissions in order to determine the new mode. The resulting mode will let anyone read the file, but not edit it.

Syntax

os.Stat

In Go programming language, the os package contains the function stat which is used to to obtain the information about a file's size, permission etc. It takes only one argument i.e. the filename whose information is to be generated.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) os and syscall package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − In the first step, specify the file's path to make it read-only.

  • Step 3 − To learn more about the file, use the os.Stat function.

  • Step 4 − Examine the file information for any possible errors that may have occurred during the retrieval process.

  • Step 5 − If there is a mistake, print a message and stop the application.

  • Step 6 − Remove the user, group, and other permissions' write permission bits from the file's mode using bitwise operations.

  • Step 7 − The file's permissions can be changed using the os.Chmod function to the new mode determined in step 6

  • Step 8 − Check for any possible issues that may have occurred during the permissions change process.

  • Step 9 − If there is a mistake, print a failure message and stop the application.

  • Step 10 − Print a message of success if there are no mistakes.

Example

In this example, we will specifically use syscall package to execute the program.

package main
import (
   "os"
   "syscall"
   "fmt"
)

func main() {
   // change the file path to your file's location
   filename := "/path/to/your/file"

   // get the existing file information
   fileInfo, errs := os.Stat(filename)
   if errs != nil {
      println("Error getting file information:", errs)
      return
   }

   // change the file mode to read-only
   errs = os.Chmod(filename, fileInfo.Mode()&^(os.FileMode(syscall.S_IWUSR)|os.FileMode(syscall.S_IWGRP)|os.FileMode(syscall.S_IWOTH)))
   if errs != nil {
      fmt.Println("Error making file read-only:", errs)
      return
   }
   fmt.Println("File made read-only successfully!")
}

Output

File made read-only successfully!

Conclusion

We executed the program of making a file read-only using two examples. In the first example we used chmod function and in the second example we used syscall package to execute the program.

Updated on: 21-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements