PDA

View Full Version : On deployment, the program does not find text file dependencies



ayanda83
30th January 2017, 08:08
Hi there guys, I've made this program that reads and writes text files. When running the program from the IDE it finds the files without any problems however when I deploy the program and then try to access the same files I get the error message "Access denied". What could be causing this?

anda_skoa
30th January 2017, 10:52
Maybe you are trying to write to a file at its installation location instead of at a user writable location.

Or you are using relative paths and haven't set the working directory correctly.

Cheers,
_

ayanda83
30th January 2017, 12:03
1231712318Thank you for your reply anda_skoa. YES, the files are in the same Dir is the executable, and the reason for that is that the program needs them to function properly. Users are not suppose to open or even see these files. I have attached a snap shot of where my executable is and the files are located inside the include folder. The code below shows how I reference the files.
QFile Sourced_RFQ_File(QCoreApplication::applicationDirP ath().append("/Include/Program_Files/RFQ_Files/Sourced_RFQs.txt"));
if(!Sourced_RFQ_File.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox msgBox_2;
msgBox_2.setText("Sourced RFQs file could not open reading...");
msgBox_2.exec();
return;
}
QTextStream outStream(&Sourced_RFQ_File);

Lesiok
30th January 2017, 12:35
What does it mean "Users are not suppose to open or even see These files." ? How do you control it ?

ayanda83
30th January 2017, 13:31
I mean these .txt files are system dependencies (i.e. the system doesn't function properly without them). They are not meant to be accessed by users, hence I put them in the installation folder.

Lesiok
30th January 2017, 14:00
So you are using the system access protection mechanisms. The program has such permissions as the user. The program can not write to the file because user do not have permission. For the test run the program as an administrator.

ayanda83
30th January 2017, 14:17
Thank you for your reply. Do you have any suggestions for my problem?

Lesiok
30th January 2017, 14:37
Use encrypted QSettings.

d_stranz
30th January 2017, 19:37
Thank you for your reply. Do you have any suggestions for my problem?

If this is Windows, you need to use a user-writable location for these files. Windows 7+ does not allow the user to create files in the installation directory; if the file is already there, -sometimes- Windows will let a program modify the file (i.e. write to it) without admin permission, but this can depend on the access rights for the user. If the user is working in an organization with a centrally-controlled IT policy, often users will have no permission to modify anything in the install folder.

There are at least two ways around this:

1 - Your installer can create any needed files at install time. I do this for my app's license key file - the installer copies an empty file to the install directory. Installations usually run with elevated permission, so can create files in the app directory. Of course, if the organization doesn't allow the user to write to this location during normal runtime, this won't work.

2 - Use QStandardPaths to identify a location that -can- be used at runtime. If the files need to be shared by all users, then QStandardPaths::AppDataLocation or QStandardPaths::AppConfigLocation (Qt >= 5.5) would be the first choice. Otherwise, a user-specific location could be used: QStandardPaths::ConfigLocation, QStandardPaths::DocumentsLocation, or QStandardPaths::HomeLocation.

In our case, the program license key is written by the user at runtime, not during installation. So, we use a series of fallbacks when we try to write the license key file. We first try to write it to the empty file we install. If that fails, we try to create a new app folder and file in AppData. If that fails, we try the user's config directory, and so forth. Typically our apps are installed on a single user machine, so writing to the user-specific directory as a last resort usually doesn't cause a problem. When we need to check the license at runtime, we follow the same fallback procedure, looking for a valid license file in each of the locations in turn. We wrap the whole thing in a CheckLicense class so the apps don't have to worry about the details.

And as Lesiok says, if you don't want users to touch the data, use encryption.

ayanda83
31st January 2017, 14:18
Thank you for your reply d_stranz, very informative. You helped solve my problem, I really do appreciate it.

anda_skoa
1st February 2017, 10:15
YES, the files are in the same Dir is the executable

Assuming one can write to an installation location is a problematic Windows developer trait.
Any operating system worth that term will have file access permission of some sort, which usually do not permit write access to installation locations.

Hence why there are easily accessible user writable locations, such as QDir::home() or various locations offered through QStandardPaths.



and the reason for that is that the program needs them to function properly

Then you need to create them when needed, e.g. by creating them programmatically, copying them from an installation location, or extracting them from a Qt resource location.



Users are not suppose to open or even see these files

That's going to be very difficult for systems that allow users file system access, e.g. through a file manager.



The code below shows how I reference the files

For reading this is trivial, just put it into a Qt resource. Those are not real files on the file system and thus not visible to the user.

But files you need to write to will be in some place visible to the user, unless the system hides files, e..g. on some mobile platforms.

Cheers,
_