Create a file which can be renamed while in use - c++

I have a application which has two sub modules. Also their is custom Log class written to log module activities.Requirement I am working on is each module should create log file with same name and write log into that. To explain it better consider initial run in which module1 is wrting logs in app.log now when another session of application starts with module2 it should also create app.log and start writing. but before that old app.log should get rename to something app.log.1.
Issue I am facing when log file is open with one module function fails to rename. I am working in C++ on window 7. To create a file I am using -
std::ofstream s_ofs.open("app.log", std::ios::out | std::ios::app);

Windows does not allow this. When you open a file, for writing or for reading, it's locked and you can't do operations such as rename or delete while the file is open.
You might want to reconsider your design. Either so that each submodule have its own uniquely named log file. Or use a logging module that can receive logging input from multiple sources and multiplex those into a single file.

You can achieve this by synchronizing access to the Log class object. An approach could be as follows:
Application creates the Log class object on startup Create a
Synchronization object (say Mutex) which protects access to the
Logging
Have the Log method accept a flag which will differentiate
between accesses from two different modules
Module1 gains access and starts logging
When module2 wants to write, the Logger will detect that it has a Log request from another module, and will close the file and then rename it and create another log file with the same name

Related

log4cplus - flush file before writing in it

My need is quite simple:
with log4cplus, I'd like to be able to write a log in a log file and to flush the log file everytime before I write in it. This way, while I run my application, I will only have one single line in my log file.
I've tryed the append=False property, but it only flush the log file at startup.
I could do it by hand in C++ but I don't want to write C++ code as the product is already in prod.
Any idea ?
Thanks,
Use the ImmediateFlush property to force the file appender to flush after each event.

Checking a user's file exists, from a daemon

I have a daemon application, which is run via launchd and runs as its own user.
Using Qt, I have come across an issue with QFileInfo::exists. If the file resides in a user's folder, that file is inaccessible and exists() returns false.
The daemon needs to know if a given file path is a path to a file (if it exists), even if it is in a folder belonging to another user, though it need not access the file.
Obviously, trying to open the file in this instance will fail and the documentation for stat states:
all directories listed in the path name leading to the file must be searchable
So the function 'stat' is also out, as is access.
As the daemon has been developed with Qt, creating a separate XPC helper app is also going to be a problem, unless someone can advise how to do this, without XCode!
For security, I really don't want to have to run the daemon as root, so is there any way for the daemon to correctly check if the file exists, perhaps by adding it to a specific group, or using a function from C, C++ or Objective-C?
Note that being a member of the "Administrators" group also fails to allow access to the file.

GetFileAttributes treat normal file as hidden file during frequent rename action

I am facing some issues with this function GetFileAttributes().
I am using C++ to get windows OS notifications and process them. For a particular case of frequent rename action on a single file, some times we are getting FILE_ATTRIBUTE_HIDDEN for those file. I am using Windows 7 Profession Service Pack 1.
Assume I have a pdf file say test.pdf. We are monitoring a directory say (D:\Test) using automatic directory monitoring in windows os. I am renaming the file (D:\Test\test.pdf) frequently as test1.pdf, test12.pdf, test123.pdf and so on.
I got rename notifications for each of the above actions. We used to check the file attributes for each notifications and if it is hidden file, we won't process further. While checking the file attributes, some times it shows as FILE_ATTRIBUTE_HIDDEN for the file. Is there any known issue with GetFileAttributes() ?
Is there any other thing I could try instead of GetFileAttributes ?

File open problem with many process in C

Iam working in c++ .i have an problem while run an application ,which have my dll within it ,My dll code is suitable to application (needed process).i wrote a log file (xml file) throughout application using fopen within all function(dll source) ,here i receive exception like "cannot access the file ,due to using by another process." .please help me ,how can manage a file ,where can use only one process at a time...
Unless you are using a different file for each process that uses your DLL then the problem is that you have the potential for multiple processes trying to access the same resource.
You should do one of the following:
Change your code so that it uses a
separate file for each calling
process.
Change it so that it uses
semaphores, mutexes or critcial
sections and wait states to control
access to the file.
Or rewrite your DLL so that it runs
as a process in its own right and
directly controls data passed to it
to place into the file.

Intercept windows open file

I'm trying to make a small program that could intercept the open process of a file.
The purpose is when an user double-click on a file in a given folder, windows would inform to the software, then it process that petition and return windows the data of the file.
Maybe there would be another solution like monitoring Open messages and force Windows to wait while the program prepare the contents of the file.
One application of this concept, could be to manage desencryption of a file in a transparent way to the user.
In this context, the encrypted file would be on the disk and when the user open it ( with double-click on it or with some application such as notepad ), the background process would intercept that open event, desencrypt the file and give the contents of that file to the asking application.
It's a little bit strange concept, it could be like "Man In The Middle" network concept, but with files instead of network packets.
Thanks for reading.
The best way to do it to cover all cases of opening from any program would be via a file system filter driver. This may be too complex for your needs though.
You can use the trick that Process Explorer uses to replace itself with task manager. Basically create a key like this:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\taskmgr.exe
Where you replace 'taskmgr.exe' with the name of the process to intercept. Then add a string value called 'Debugger' that has the path to your executable. E.g:
Debugger -> "C:\windows\system32\notepad.exe"
Every a process is run that matches the image name your process will actually be called as a debugger for that process with the path to the actual process as an argument.
You could use code injection and API redirection. You'd start your target process and then inject a DLL which hooks the windows API functions that you want to intercept. You then get called when the target process thinks it's calling OpenFile() or whatever and you can do what you like before passing the call on to the real API.
Google for "IAT hooking".
Windows has an option to encrypt files on the disk (file->properties->advanced->encrypt) and this option is completely transparent to the applications.
Maybe to encrypt decrypt file portions of a disk you should consider softwares like criptainer?
There is this software as well http://www.truecrypt.org/downloads (free and open source) but I haven't tried it.
Developing a custom solution sounds very difficult.