Set Owner+Access-rights with io.open - ios

In a lua-script (for Domoticz # Raspberry) I apply the following script-segment to generate an htm-file and to put it in the designated folder.
Line02text till Line30text are variables which are dynamically filled elsewhere in the lua-script.
file = io.open("/home/pi/domoticz/scripts/lua/XXXXX.htm", "w+")
-- Opens a file named XXXXX.htm (stored under the designated sub-folder of Domoticz)
-- in append mode
-- write lines to opened file
file:write("SOF<br>")
file:write(Line02text .. "<br>")
file:write(Line03text .. "<br>")
....
file:write(Line29text .. "<br>")
file:write(Line30text .. "<br>")
file:write("EOF<br>")
file:close() -- closes the open file
All seems OK, because the htm-file appears as planned.
Next steps would be to copy the file to different folder, open in browser, etc..
But Owner of the htm-file is 'root' and Permission is 0640.
For further application Owner should be different, and Permission e.g. 777.
Trying manual change or use of chmod results in report 'Permission denied' by server.
Question:
How to set (as result of the lua-script) different Owner and other Permission for the htm-file?

Lua's target is to be as portable as possible, and ownership/permissions management is very os-specific. There's no embedded functions to handle that.
You'll need to expose some native function that will do what you need with files' permissions. Or use some already existing library for that, like maybe lua-fs: (https://github.com/clementfarabet/lua-fs-0.3)

Related

Can you require a file directly from the /after/plugin folder?

I decided to spring-clean and update my nvim config files/plugins, and thought I’d make proper use of the after/plug folder.
While setting up LSP (with mason, mason-lspconfig, and lspconfig), I wanted to move all the lsp language server settings out from after/plugin/lsp/init.lua to their own files (now in after/plugin/lsp/settings).
The problem is I don’t seem to be able to require them into the init.lua file.
Things I’ve tried to no avail:
require(‘after/plugin/lsp/settings/sumneko_lua.lua’)
require(vim.fn.stdpath("config") .. "/after/plugin/lsp/settings/sumneko_lua”)
require(vim.fn.expand('%:h').. ‘/settings/sumneko_lua’)
The attempt using expand works when I resource the file in nvim; but causes an error when starting nvim.
I understand that all the files in after/plugin are automagically sourced at startup. So if I had a file shared.lua:
local M = {}
function M.greet()
vim.notify("Hello!”)
end
return M
in the same folder as after/plugin/lsp/init.lua, how can I get access to the greet() function from init.lua?
Any pointers would be greatly appreciated.
It turned out to be quite a simple solution in the end: I simply updated the paths to be searched in init.lua
-- nvim/init.lua
-- Allow require to look in after/plugin folder
local home_dir = os.getenv("HOME”)
package.path = home_dir .. "/.config/nvim/after/plugin/?.lua;" .. package.path
And then I can require any file inside the after/plugin folder
e.g - require(‘lsp/settings/sumneko’) or require(‘lsp/shared’).greet()

How do i create a file in the C drive using lua

What I want to do is create a file that is pretty simple. But then I wanted to put it inside the C: drive but I am not really sure how...
this is what i currently have:
file = io.open("testFile.lua", "C:")
file:write("a semi-automated message")
file:close()
It is possible to create a file in the C: drive, however, in order for the file to show up, you have to run the Lua file as an administrator via Command Prompt (or some other terminal)
file = io.open("C:\\main.txt", "a") -- appends a file named main.txt
file:write("Hello world!") -- write a bunch of stuff
io.close(file) -- close the file

Lua io.write() not working

I am using a luvit Lua environment to run my lua code through my control panel. I am looking to write to a .txt file, but with the simple code that i am running, its not working.
The reason I wish to write to a .txt file is to log notices from my Discord Bot I am working on in the Discordia library.
I have a folder called MezzaBOT. In this file i have a write.lua file and also a log.txt file. I have this simple code in my write.lua file:
io.output('log.txt')
io.write('hello\n')
io.close()
I then run in my command promt with Luvit environment:
>luvit Desktop\mezzabot\write.lua
I don't get any errors but the log.txt file continues to stay empty. Am I missing a line in my code, or do i need to access log.txt differently?
edit: my new code is the following
file = io.open('log.txt')
file:write('hello', '\n')
file:close()
and it is not making a new line for each time with \n
edit B:
Ok, i found my problem, its creating a log.txt in my C:\Users\PC.
One other problem is when writing, its not making a new line with the \n. Can someone please help me?
Lua, by default, opens files in read mode. You need to explicitly open a file in write mode if you want to write to it (see manual)
file = io.open('log.txt', 'w')
file:write('hello', '\n')
file:close()
Should work :)

Fuse File System- general input/output error while accessing office files

I have written a fuse mirror file system using FUSE-JNA, Which mirror local directory.
This Mirror file system allow me to open all types of files correctly with no issue but it does not open all types of office files e.g. .docs , .xls etc. And give me be below error while opening any office file.
Note:
I thought its LibreOffice issue, so I removed it and installed OpenOffice. But get the same issue.
Secondly, the errors only pops up when I try to access an office file from my MirrorFileSystem. Office files opens correctly if accessed normally via ubuntu default file system.
So its some thing wrong with my File system.
Finally, (i don't know whether its related to the question or not but) in my mirror file system when I Right Click on a file>Properties> Permission its shows all the fields disabled, as below
This is my getatt() method:
public int getattr(final String path, final StatWrapper stat)
{
....
if (f.isFile())
{
stat.setMode(NodeType.FILE,true,true,true,true,true,true,true,true,true);
stat.size(f.length());
stat.atime(f.lastModified()/ 1000L);
stat.mtime(0);
stat.nlink(1);
stat.uid(0);
stat.gid(0);
stat.blocks((int) ((f.length() + 511L) / 512L));
return 0;
}
...
}
Please guide me how to fix general input/output error while office files?
Office files are not special. There is some other problem with your filesystem implementation, and you need to do more debugging work to find out precisely what the trigger and the cause is. It's very unlikely that the trigger truly is "the file is an office file", unless you have stuff in your filesystem code that operates differently based on the type of file it's dealing with (in which case you should look there). As a first debugging step, you could compare the sha1sum and stat output of the files from the fuse filesystem and from the root filesystem to see if they match. If they don't, adjust the filesystem code such that they do. You could also enable logging on your filesystem class and check if it's returning an I/O error code anywhere. The error message "general input/output error" makes it sound like that is the case.
As for the reason the permissions fields are disabled, that's because the file is owned by root, and you are not root so you can't change the permissions. The reason the file is owned by root is because you set stat.uid(0); and stat.gid(0); in getattr. UID 0 and GID 0 are for the root user and root group respectively. Fuse-JNA already puts the current UID and GID as default stat attributes in getattr, so if you want to use these then just don't call stat.uid(0); or stat.gid(0);.
Thanks for the answer.
I searched on web, on many websites they showed file locking as the reason e.g. https://forum.openoffice.org/en/forum/viewtopic.php?f=10&t=2020 etc
So in fuse, I implemented file lock function and simply return 0
My problem solved. Now I can open all types of office files.
But I do not know, is it perfect solution

How to prevent vim from setting current directory

Recently my vim will change current directory no matter what I do. I'm using spf13 distribution and when I am in a rails app root directory and did vi, my pwd will be correctly in app root directory. But once I open some file, any file, it will change the pwd to abosolute/path/to/myrailsapp/app/assets/stylesheets,
When I don't have let g:spf13_no_autochdir = 1 in my .vimrc, vim will change the pwd to current file directory; When I do, it will change to the stylesheet directory whenever I open a file.
I'm also using rails.vim installed. Here is the related code inside my .vimrc
if !exists('g:spf13_no_autochdir')
autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h | endif
" Always switch to the current file directory
endif
UPDATE:
What I want: the pwd always stay in absolute/path/to/myrailsapp/, no changing to the stylesheet directory automatically whenever I open a file.
Actually I just found and had a look at the plugin. I assume this is it:
https://github.com/spf13/spf13-vim/blob/3.0/.vimrc
Around line 75 you can see:
" Most prefer to automatically switch to the current file directory when
" a new buffer is opened; to prevent this behavior, add the following to
" your .vimrc.before.local file:
" let g:spf13_no_autochdir = 1
So just add that last line (without the comment-marker quote) to your .vimrc and you'll get rid of the automated directory change.
I note that neither method in my other answer would have worked, because the plugin author for whatever reason decided not to use the built-in option, and also not to put their autocmd in a group. Naughty, naughty!
I solved this according to Ben's second answer.
spf13 loads configuration files in order as follows.
.vimrc.before - spf13-vim before configuration
.vimrc.before.fork - fork before configuration
.vimrc.before.local - before user configuration
.vimrc.bundles - spf13-vim bundle configuration
.vimrc.bundles.fork - fork bundle configuration
.vimrc.bundles.local - local user bundle configuration
.vimrc - spf13-vim vim configuration
.vimrc.fork - fork vim configuration
.vimrc.local - local user configuration
if !exists('g:spf13_no_autochdir') check is done at (7), so let g:spf13_no_autochdir = 1 should be loaded before that.
I put it in .vimrc.before.local, and it works as expected.
There are two ways this could be happening.
The most likely, is that this "spf13" configuration includes set autochdir. To find out whether this is the case, start up Vim normally, and type :verbose set autochdir? and press Enter. This should tell you IF autochdir is set and WHICH FILE set it to that value.
If autochdir is set, then you only need to set up a VimEnter autocmd, or stick a file in ~/.vim/after/plugin, to turn it off again after spf13 loads.
If autochdir is NOT set, then probably an autocmd is setting your directory for you. If there is a plugin option in SPF13 to turn it off, then do that. If not, you'll need to find where in the plugin the directory is getting changed. If you're lucky, the autocmd will be in an augroup by itself, and you can then remove that autocmd with :au! GroupName. This command can be in the same places; a VimEnter autocmd, or a file in ~/.vim/after/plugin.

Resources