Rename code in c have a error - c

i want to move a file from a directory to anther directory with C Coding.
I search and find rename(); function , but when working it doesnt work and have a error:
my code:
#include <stdio.h>
int main() {
if(rename("/root/tmpfile.php", "/home/check-tmp.php"))
perror( NULL );
}
the code well compiled but when running this code showing this error:
Invalid cross-device link
How to move a file from a directory to anther directory without using System for fopen?
Aslo , i finded many codes and ways to do it but doesnt working all codes.
Please say me a way and make sure it will work
Thanks.

Many aspects of the behavior of `rename' are inherently platform-dependent: The rename operation might not be able to move a file from one file system to another , it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.
In other words, your system does not support rename files cross different partitions (your root partition and your home partition are different.)
So the solution is when it fails, copy the file to the destination and delete the original.

The rename call can only rename and move files within a single disk partition. The error "cross-device link" indicates that you attempted to move a file from one partition to another. (If you were on a Windows system, you can imagine if you tried to rename a file from C: to D:.)
When you use the Unix mv command to move files, it first tries a rename, but if it fails in this way, it falls back and makes a new copy of the file in the new location, then deletes the original. That's what you would have to have your code do in this situation, too.
(Copying a file is easy enough, but there are plenty of library functions out there that will do it for you, and also take care of things like preserving the last-modified time and other file attributes.)

Related

Update file across multiple folder locations?

I need something that can copy a specified file any and everywhere on my drive (or computer) where that file already exists; i.e. update a file. I tried to search this site, in case I'm not the first, and found this:
CMD command line: copy file to multiple locations at the same time
But not quite the same.
Example:
Say I have a file called CurrentList.txt, and I have copies of it all over my hard drive.  But then I change it and I want all the copies to update.  So I want to copy the newer one over all the others.  It could 'copy if newer', but generally I know it's newer, so it could also just find every instance and copy over it.
I was originally going to use some kind of .bat file that would have to iterate over every folder seeking the file in question, but my batch file programming is limited/rusty.  Then I looked to see if xcopy could do it, but I don't think so...
For how I will use it most, I generally know where those files are going to be, so it actually might be as good or better if I could specify it to (using example), "copy CurrentList.txt, overwriting all other copies wherever found in the C:\Lists folder and all subfolders".
I would really like to be able to have it in a context menu, so I could (from a file explorer) right click on a file or selected files and choose the option to distribute it.
Thanks in advance for any ideas.
Use the "replace" command...
replace CurrentList.txt C:\Lists /s

Shake: automatically deleting file after failed command

Using Shake, to create an mp3 (this is just a learning example), I use lame, and then id3v2 to tag it.
If the lame succeeds, but the id3v2 fails, then I'm left with the mp3 file in place; but of course it is "wrong". I was looking for an option to automatically delete target files if a producing command errors, but I can't find anything. I can do this manually by checking the exit code and using removeFiles, or by building in a temporary directory and moving as the last step; but this seems like a common-enough requirement (make does this by default), so I wonder if there's a function or simple technique that I'm just not seeing.
The reason Make does this by default is that if Make has a partial incomplete file on disk, it considers the task to have run successfully and be up to date, which breaks everything. In contrast, Shake records that a task ran successfully in a separate file (.shake.database), so it knows that your mp3 file isn't complete, and will rebuild it next time.
While Shake doesn't need you to delete the file, you might still want to so as to avoid confusing users. You can do that with actionOnException, something like:
let generateMp3 = do cmd "lame" ... ; cmd "id3v2" ...
let deleteMp3 = removeFile "foo.mp3"
actionOnException generateMp3 deleteMp3

How to get file name when file change is observed via watch_file

I am currently facing an issue which I don't know how to fix. I got the following Julia code:
while true
print(watch_file("test"))
end
So this should get me all the file changes in the directory named "test". At least on windows.
Now thats all well and good, and it kinda works, at least for creating a file or moving a file to that directory. This is an example of what I get:
("New Textfile.txt",Base.FileEvent(true,false,false))
But when I delete or rename that file, I don't get the filename of the file deleted or renamed.
("",Base.FileEvent(true,false,false))
Is there a different method/function I can get the filename with, even when the file is deleted or renamed? Or even better, a way that archives this and is cross-platform-compatible? Any help appreciated.
EDIT: If you could give me an alternative that supports recursive monitoring, that would be even better.
In Linux, Julia 0.4.5 and 0.4.3 watch_file returns file name always. It is a very platform-dependent feature (like in Node.js https://nodejs.org/api/fs.html#fs_caveats) and only manual polling can be truly platform-independent solution.

How do I write a java code that will export a file to a usb drive

It is exactly wnat the title says I have beenlooking for quite sometime and haven't found anything the main use would be as a auto run file to collect error reports from our offic computers
You could do this if you run the program from the USB drive itself, and declare wherever the file is stored as the "working directory" as the USB may have different IDs for different computers...things get messy.
My recommendation is to use File in Java, and Path (http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html)
A warning though is that if you copy a directory (folder), the files within that folder are actually not automatically copied...its just the way it works. (more here: http://docs.oracle.com/javase/tutorial/essential/io/copy.html)
Assuming the file is always in the same place going to the same place. For example:
Files.copy(source, destination, options);
or you can open text file and read from it for a more advanced method:
Files.copy(InputStream, path, options);
etc.

Creating a unique temporary directory from pure C in windows

I'd like to create a unique temporary directory in Windows from some C
code (not C++ or C#). I want to do this so that I can put some temp
files in the directory, and then delete them all easily when I'm done
(by removing the directory recursively).
I'm essentially looking for an equivalent of the linux
mkdtemp
function. There is a C# answer here, and responses on this
question
suggest using Boost. But since I'm using C, those solutions don't work
for me.
The best I've been able to come up with so far is to use
GetTempFileName
followed by CreateDirectory,
but the problem there is that if I ask
GetTempFileName
to create a unique file name, it will also create the file (which I
don't want, since I want to make a directory instead).
Relatedly, there's
GetTempPath,
which returns the location of the user's temp folder from environment
variables - but since I want to create my own directory that I can
safely delete later, I still need to create a directory inside any
path it would return.
It looks like if I want a unique directory to be created, I'll have to
create a temp file, get the name, delete it, and then create a
directory with the same name - which sounds very messy.
Any other ideas?
You can use what GetTempPath returns concatenated with a Guid to ensure uniqueness of the directory. You can create a Guid using UuidCreate or CoCreateGuid Function.
To delete recursively the directory, there is an example here in pure C: How to remove directory recursively? based on FindFirstFile, FindNextFile, DeleteFile and RemoveDirectory.
There is also SHFileOperation but it's more heavyweight and is based on the Windows Shell functions, and the Shell DLLs are not always wanted, especially if you're writing server code.
Use GetTempPath then CreateDirectory with a random name under it, optionally retrying if CreateDirectory fails due to it already existing. But if your name generation is good enough, the likelihood of a collision with an existing name is much smaller than the likelihood of a blackhat guessing your password or even your private key, so you might as well ignore it.
Use _tempnam tmpnam_s to create a filename that doesn't exist yet, and then use CreateDirectory to create the directory. There's technically a race condition if you do this, in that another process could potentially create a file or directory with that name in the time in between when you generate the filename and when you create the directory, but the odds of that are rather unlikely. To protect against that, you can loop until you succeed.
For recursively removing a directory tree, you can use SHFileOperation. Alternatively, you can do the directory traversal yourself with FindFirstFile/FindNextFile, DeleteFile, and RemoveDirectory.
If you want to remove the directory automatically upon exiting, register a function using atexit. This will only work for normal program termination (i.e. via the exit function or via returning from main/WinMain). This will not work for abnormal program termination (e.g. via abort, an access violation, someone else calling TerminateProcess, etc.).

Resources