3

I am working on Ubuntu 12.04 LTS and 10.04 LTS platforms and I am having a problem with QDir::mkPath. I am trying to create a path using mkPath, lets say /john/play/games and then copy a file game.htm to the /home/john/play. After I copy the file I set the permissions on the file to rwxr - - r - -. So that 3rd party applications can read the file game.htm. But the directories /john, /john/play and /john/play/games are created with permissions drwxr - x - - - due to which other applications and other users are not able to read the file game.htm.

I have also observed in a different application that the same QDir::mkpath creates the dir with drwxr - xr - x . I want to understand with what permissions does QDir::mkpath creates the directories and how to control or set the permissions.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
sky
  • 115
  • 2
  • 10
  • 1
    Your question is actually off-topic, because it's UNIX related, not programming-related. You need to read up on [how file and directory permissions work](http://content.hccfl.edu/pollock/aunix1/filepermissions.htm) in UNIX. Also, you shouldn't place globally visible files in the user's directory, place them in `/usr/share/` or whichever place is right. – sashoalm Oct 26 '12 at 07:17
  • 1
    @satuon, I am more concerned about the way qt's QDir::mkpath works and how to make it work the right way. And the path i am talking was to just give an example. – sky Oct 26 '12 at 09:25
  • `QDir::mkpath` works by calling the OS provided API. It works the same way as calling `mkdir -p` from the shell. It depends entirely on the OS. Did you read the link I gave you? – sashoalm Oct 26 '12 at 09:32
  • @satuon, Read through the whole doc. Learned a lot but nothing that will help me here with QDir::mkpath. Not sure what you were pointing to in that doc. Probably my question misled you. In short, how do I use QDir::mkpath so that all the dirs created by QDir::mkpath have r-x permissions for group and others? If QDir::mkpath cannot do it then is there any workaround to achieve this. Currently I iterate through all the dirs in the path, create a QFile on each and then use QFile::setPermissions() to set the permissions, which seems a bit silly. – sky Oct 29 '12 at 10:22
  • You cannot do it with `QDir::mkpath`, because it needs to be cross-platform. Think about it. It needs to work the same on Windows, too, where you don't have the execute permission at all, and also on Symbian. It can only do the least common denominator, so to speak. So it always uses the default permissions for the given OS. The only way to tweak them would be to use UNIX-specific APIs, which you can do, by the way. – sashoalm Oct 29 '12 at 10:36

1 Answers1

2

The environment is messed up for letting the mkdir routine create readable directories, as sky pointed out in the comments perfectly.

You could, however, use the umask() OS function to set the umask to something else before calling QDir::mkpath. You might have to #ifdef it out on windows though, though I suspect the umask() function may have been implemented in the mingw environment being used by qtcreator on wnidows. So it may work anyway.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69