I’m writing a source control application. Now I want to copy the files from one machine to another machine over the network using BSD sockets. I also want to NOT follow symbolic links, but to copy the link itself to the other machine. I am a liitle bit stuck on this one.
One option I thought about was using the readlink() command, sending the data to the other side of the machine and writing the received contents to a file. I don’t even know if that’s going to work, because I don’t know wether different operating systems (Ubuntu and Mac OS X in my case) have the same file structure in symbolic links. And I don’t even know if writing those received contents to a file will automatically make it a symbolic link. Honestly I hardly doubt that. Maybe the open() command with the O_SYMLINK flag will work to create a symbolic link and write to it?
An other option I thought about was to let the other machine know we are handling a link and make that link on that machine with symlink(), but here I have two problems. First of all, how do I get the absolute filename the symbolic link points to on the file system. And a second problem is that I don’t know if calling symlink() with a non-existing file will work?
I’d appreciate it if someone could explain this,
ief2
You should use readlink on the source system, and symlink on the destination system. symlinks don’t really have a “file structure in symbolic links”; the symlink just contains a file path (either absolute or relative).
You should copy the symlink as-is, ie. not attempt to adjust it to the target system. It is perfectly fine to have symlinks where the target file does not exist (they are called “broken symlinks”). Users will have to set links that make sense (which might be absolute, but typically will be relative); if they don’t, things fail in a predictable way.