简体   繁体   English

如何以编程方式在root用户拥有的文件夹中创建文件

[英]How to create a file programmatically in a folder owned by root user

I'm developing an application in Qt. 我正在Qt中开发应用程序。 I need to create a file in the location /usr/share/applications. 我需要在/ usr / share / applications位置创建一个文件。 The following is my code 以下是我的代码

  QDir::setCurrent("/usr/share/applications");
  QFile launcherFile;
  launcherFile.setFileName("aptana.desktop");
  int status = launcherFile.open(QIODevice::WriteOnly | QIODevice::Text);
  QTextStream stream(&launcherFile);
  stream<<"Create file";
  launcherFile.close();

I'm not getting any error while debugging but neither does the file get created. 调试时没有出现任何错误,但文件也未创建。 I think it is a privilege issue because the user I'm logged in as does not have privilege to access this folder. 我认为这是一个特权问题,因为作为我登录的用户没有访问此文件夹的特权。 I have seen other applications like Synaptic Manager prompting for the administrative password before continuing towards the application. 我已经看到其他应用程序,例如Synaptic Manager,在继续进行该应用程序之前会提示输入管理密码。 Similarly is there anyway in which I can pop up the dialog box asking for the password and get the credential to create the file as root user? 同样,无论如何,我都可以弹出对话框询问密码并获得以根用户身份创建文件的凭据? What other ways are there to tackle this situation? 还有什么其他方法可以解决这种情况?

I've just found an approach to the general problem of asking for root priviledges and doing something. 我刚刚找到了一种解决一般问题的方法,该问题要求获得根特权并执行某些操作。 Dio3 calls system() and passes the password to the cmd-level. Dio3调用system()并将密码传递到cmd级别。 . I don't know about a way to 'read a file through system and make QT-API happy, but you could change the file-permissions that way... but everyone doin' this to serious files should have security nightmares :) 我不知道一种“通过系统读取文件并使QT-API满意的方法”,但是您可以通过这种方式更改文件权限...但是每个人都对严重的文件执行此操作应该引起安全方面的噩梦:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM