使用C ++锁定/阻止在Linux上编辑源文件

时间:2021-12-10 15:10:37

How can I programmatically lock/unlock, or otherwise prevent/enable editing, a source file on Linux using C++.

如何使用C ++以编程方式锁定/解锁或以其他方式阻止/启用编辑Linux上的源文件。

I want to be able to lock source file so that if I open it in an editor it will not allow me to save back to the same source file.

我希望能够锁定源文件,这样如果我在编辑器中打开它,它将不允许我保存回相同的源文件。

I am thinking of maybe changing the permissions to read-only (and change it back to read-write later): how do I do that from C++?

我想可能将权限更改为只读(并在以后将其更改为读写):我如何从C ++中执行此操作?

3 个解决方案

#1


5  

Try man fchmod:

试试man fchmod:

NAME
       chmod, fchmod - change permissions of a file

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>

       int chmod(const char *path, mode_t mode);
       int fchmod(int fildes, mode_t mode);

#2


1  

Why aren't you using a source code management tool like CVS or Subversion? CVS does nice locking (so does Subversion). More importantly, you have the history of changes. Better still (with CVS anyway) you have to make the step of doing a "checkout" to make the file writeable.

你为什么不使用像CVS或Subversion这样的源代码管理工具? CVS做得很好(Subversion也是如此)。更重要的是,您有变化的历史。更好的是(无论如何使用CVS)你必须做一个“结账”步骤,使文件可写。

#3


1  

Yes, it is a bit hard to tell what you are looking for

是的,有点难以分辨你在寻找什么

  • Security against other users editing you files -> use "chmod, fchmod"

    针对编辑文件的其他用户的安全性 - >使用“chmod,fchmod”

  • Security against you yourself accidentally messing with your source files -> you should really change your thinking and use a source control tool. Like Subversion (SVN) or even better Mercurial.

    对你自己的安全意外地弄乱你的源文件 - >你应该真正改变你的想法并使用源代码控制工具。像Subversion(SVN)甚至更好的Mercurial。

#1


5  

Try man fchmod:

试试man fchmod:

NAME
       chmod, fchmod - change permissions of a file

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>

       int chmod(const char *path, mode_t mode);
       int fchmod(int fildes, mode_t mode);

#2


1  

Why aren't you using a source code management tool like CVS or Subversion? CVS does nice locking (so does Subversion). More importantly, you have the history of changes. Better still (with CVS anyway) you have to make the step of doing a "checkout" to make the file writeable.

你为什么不使用像CVS或Subversion这样的源代码管理工具? CVS做得很好(Subversion也是如此)。更重要的是,您有变化的历史。更好的是(无论如何使用CVS)你必须做一个“结账”步骤,使文件可写。

#3


1  

Yes, it is a bit hard to tell what you are looking for

是的,有点难以分辨你在寻找什么

  • Security against other users editing you files -> use "chmod, fchmod"

    针对编辑文件的其他用户的安全性 - >使用“chmod,fchmod”

  • Security against you yourself accidentally messing with your source files -> you should really change your thinking and use a source control tool. Like Subversion (SVN) or even better Mercurial.

    对你自己的安全意外地弄乱你的源文件 - >你应该真正改变你的想法并使用源代码控制工具。像Subversion(SVN)甚至更好的Mercurial。