File Operations - Copy and Move - Delphi - delphi

I need to do a two fold operation in Delphi.
First is reading a directory and copy all of the files into a backup folder. If any of the files are currently in use as being written to and/or open, then bypass that file.
Second is to move the file to another directory. Again, if any of the files are currently in use as being written to and/or open, then bypass that file.
I have used file copy and move before, but I am unsure as to how to detect whether a file is being written to and/or open.
Thanks.

If you use Windows.pas' CopyFile(PChar(sFrom),PChar(sTo),false) it will return false when the copy fails, and GetLastError will return an error-code explaining why.

Related

Is it any way to add .map file as resource of the project when compiling?

I'd like to add the map file as resource, then extract the file every time that execute the system, to ensure that the map file is always right, and make the life of support easier. In Delphi 7 I can compile the project, with the map file as resource, but the map is wrong, because when an exception is raised the call stack is wrong. In Delphi Seattle, I can't even compile, because it tries to add the resource before generate the map. I know that I can add the file in a post-compile thask, but is there any way to do it in compiling/building time?
Sorry if I'm not very specific, that's my first question here. Thanks.
UPDATE
After I read some answers, I did some research. I was already using JEDI exception unit in my project, but I did not know the JEDI Debug expert. It does exactly what I want and more.
JEDI Debug expert convert a .map file to .jdbg file, wich is an encrypted file of map (map is just a text file). Also, the jdbg file is about 12% smaller then the map. It also has the options to insert the jdbg into binary (exe). To do that, I enabled the options:
Project -> JCL Debug expert -> Generate .jdbg files -> Enabled for this project
Project -> JCL Debug expert -> Insert jdbg data into binary -> Enabled for this project
Project -> JCL Debug expert -> Delete map files after conversion -> Enabled for this project (if you want to delete the file, of course)
To use this tool outside the IDE, with Jenkins for example, I had to build the project available in JEDI\jcl\examples\windows\debug\tools\MakeJclDbg.dpr. After build, it will generate the exe file in the bin directory of jcl. How to use:
MakeJclDbg -J -E -M map_filename
J - Create .JDBG files
E - Insert debug data into executable files
M - Delete MAP file after conversion
Executable files must be in the same directory as the MAP files. This will create the jdbg file (based in the map file), insert into the exe and delete the map. With this (and with the exception unit of JEDI), when an exception is raised, It's available to me the stack trace, the versions of all dll's used by the system, operation system info, and more, and also send all this to an email.
I realised that Embarcadero also have jdbg files of theirs bpl, so I think they use JCL tool as well.
No, the map file is generated after the program output is linked. It is impossible to incorporate a, not-yet generated, map file as a resource into the project.
You're missing the point.
The MAP files are generated as a separate file to avoid increasing the size of your executable. Trying to embed that file back into the executable as a resource simply defeats the purpose.
You haven't mentioned what debug framework you're using. But there are other ways to provide debug information, and I suggest you refer to the documentation of the debug framework you using for the specifics. I'll just offer some general concepts applicable to most of the frameworks I've tried.
If you're happy with increased EXE size and want debug information included within your executable: Don't use the map file option. Simply enable the linking option to include debug information. (And ensure your debug framework will use it.)
Most debug frameworks recommend compiling with stack frames turned on. This is very important because it makes it easier for the debug framework to deduce the call stack.
Some debugging frameworks have a feature that allows guessing missing call stack information. If enabled, you will need to manually ignore any stack entries that don't actually make sense.
Of course, don't forget that any units compiled without debug information won't have debug information to include in the final executable.

Safely write to files with conflicting names in an NSOperationQueue

This is probably a pretty basic NSOperationQueue question, but maybe it will help some other people out who are just learning this as well.
I'm trying to copy multiple .plist files from the ~/Documents directory to the ~/Library directory in an iOS application. I want to use NSOperations to copy each file to speed this up and take the import process off the main thread.
In my implementation, it's possible that two files of the same name could be copied into the same place. What I'd like to do is make sure that one of the operations changes the filename to one that doesn't exist before it writes the file. What would be the most straight-forward way to go about this?
Thanks,
-c

Can I use iphoneSetDoNotBackupFile on a folder of files rather than individual files?

Do you know if I can use this on a folder and all the files within that folder will not be backed up? (in other words a folder rather than a file) I have a lot of code written accessing PDF files within several folders located in the special path documents folder. It would be pretty easy for me to set the do not back up of each one of those 7 folders but would rather not set the do not back up on each one of the 300 files within those 7 folders.
The same PDF`s folders also exist on my web server and so they can be updated or replaced if needed from there in the event they get deleted. Do you think Apple would be OK with me leaving them in the Documents/PDFDIR folders and setting the do not back up of those (7) folders?
This question was asked in a private support context, however, I felt it was valuable to answer here for others.
Yes it is. This property is recursive. While it's not really a duplicate answer because it's about a different language there is this answer here that backs up my answer in objective-c.

Temporary file deleted

I'm working on ed (yes, the editor) source code.
The program uses a scratch file, opened with tmpfile, as a buffer.
But, whenever I run the program, lsof always report the temporary file as deleted! (and in fact it's not there). Why?
Because a file can exist on disk without having a filename associated with it, many programs will open a file and then promptly unlink it. The file contents can continue to be modified & read by open file-handles on the file, and won't actually be removed from the disk until all open file handles are closed.
(this is for *nix/POSIX platforms AFAICT; Windows handles files differently, preventing unlinking if an program has the file-handle still open, and thus reboots are often needed for upgrades to force those open file-handles to be closed so file contents can be replaced)

keep rsync from removing unfinished source files

I have two machines, speed and mass. speed has a fast Internet connection and is running a crawler which downloads a lot of files to disk. mass has a lot of disk space. I want to move the files from speed to mass after they're done downloading. Ideally, I'd just run:
$ rsync --remove-source-files speed:/var/crawldir .
but I worry that rsync will unlink a source file that hasn't finished downloading yet. (I looked at the source code and I didn't see anything protecting against this.) Any suggestions?
It seems to me the problem is transferring a file before it's complete, not that you're deleting it.
If this is Linux, it's possible for a file to be open by process A and process B can unlink the file. There's no error, but of course A is wasting its time. Therefore, the fact that rsync deletes the source file is not a problem.
The problem is rsync deletes the source file only after it's copied, and if it's still being written to disk you'll have a partial file.
How about this: Mount mass as a remote file system (NFS would work) in speed. Then just web-crawl the files directly.
How much control do you have over the download process? If you roll your own, you can have the file being downloaded go to a temp directory or have a temporary name until it's finished downloading, and then mv it to the correct name when it's done. If you're using third party software, then you don't have as much control, but you still might be able to do the temp directory thing.
Rsync can exclude files matching certain patters. Even if you can't modify it to make it download files to a temporary directory, maybe it has a convention of naming the files differently during download (for example: foo.downloading while downloading for a file named foo) and you can use this property to exclude files which are still being downloaded from being copied.
If you have control over the crawling process, or it has predictable output, the above solutions (storing in a tempfile until finished, then mv'ing to the completed-downloads place, or ignoring files with a '.downloading' kind of name) might work. If all of that is beyond your control, you can make sure that the file is not opened by any process by doing 'lsof $filename' and checking if there's a result. Clearly if no one has the file open, it's safe to move it over.

Resources