Lua - Checking if File is Readonly



It is sometime good to know if a file is readonly before performing an operation on a file like writing or appending and then handle the situation gracefully. To check if file is readonly, we can check the io.open() status.

Syntax - io.open()

f = io.open(filename, [mode])

Where−

  • filename− path of the file along with file name to be opened.

  • mode− optional flag like r for read, w for write, a for append and so on.

f represents the file handle returned by io.open() method. In case of successful open operation, f is not nil. Following examples show the use cases of checking a file is readonly or not.

Example - Checking if writable file is readonly

We're making a check on example.txt which is present in the current directory and is both readable and writeable.

main.lua

-- check if file is readonly
-- returns true if file is readonly
-- false if both readble and writable
-- otherwise nil is returned
function readonly(filename)

   local isReadable = true
   local isWritable = true
   -- Opens a file in read mode
   f = io.open(filename, "r")

   -- if file is not readable, f will be nil
   if not f then
      isReadable = false
   else
      -- close the file  
      f:close()
   end

   -- Opens a file in write mode
   f = io.open(filename, "w")

   -- if file is not writable, f will be nil
   if not f then
      isWritable = false
   else
      -- close the file  
      f:close()
   end

   if isReadable and isWritable then
      return false
   elseif isReadable then
      return true
   else
      return nil
   end
end

-- check if file is readonly
if readonly("example.txt") then
   print("example.txt is readonly.")
elseif readonly("example.txt") == false then
   print("example.txt is not readonly.")
else
   print("example.txt is not readable.")
end

Output

When we run the above program, we will get the following output−

example.txt is not readonly.

Example - Checking if file is readonly

We're making a check on example1.txt which is present in the current directory and is readonly and not writeable.

main.lua

-- check if file is readonly
-- returns true if file is readonly
-- false if both readble and writable
-- otherwise nil is returned
function readonly(filename)

   local isReadable = true
   local isWritable = true
   -- Opens a file in read mode
   f = io.open(filename, "r")

   -- if file is not readable, f will be nil
   if not f then
      isReadable = false
   else
      -- close the file
      f:close()
   end

   -- Opens a file in write mode
   f = io.open(filename, "w")

   -- if file is not writable, f will be nil
   if not f then
      isWritable = false
   else
      -- close the file
      f:close()
   end

   if isReadable and isWritable then
      return false
   elseif isReadable then
      return true
   else
      return nil
   end
end

-- check if file is readonly
if readonly("example1.txt") then
   print("example1.txt is readonly.")
elseif readonly("example1.txt") == false then
   print("example1.txt is not readonly.")
else
   print("example1.txt is not readable.")
end

Output

When we run the above program, we will get the following output−

example1.txt is readonly.
Advertisements