Duplicate file descriptor with its own file offset Duplicate file descriptor with its own file offset unix unix

Duplicate file descriptor with its own file offset


So basically, what you really want is to be given a file descriptor, and basically open the same file over again, to get a separate position, sharing, mode, etc. And you want to do this on Windows (where the "file descriptor" is basically a foreign object, not something used directly by the OS or the run-time library at all.

Amazingly enough, there is a way to do that, at least with MS VC++. All but two steps of it use only the Win32 API so porting to other compilers/libraries should be fairly reasonable (I think most supply versions of those two functions). Those are for converting a Unix-style file descriptor to a native Win32 file handle, and converting a native Win32 file handle back to a Unix-style file descriptor.

  1. Convert file-descriptor to native file handle with _get_osfhandle()
  2. Get a name for the file with GetFileInformationByHandleEx(FILE_NAME_INFO)1
  3. Use CreateFile to open a new handle to that file
  4. Create a file descriptor for that handle with _open_osfhandle()

Et voilĂ , we have a new file descriptor referring to the same file, but with its own permissions, position, etc.

Toward the end of your question, you make it sound like you also want the "permissions", but that doesn't seem to make any real sense -- the permissions attach to the file itself, not to how the file is opened, so opening or reopening the file has no effect on the file's permissions. If you really want to know the, you can get it with GetFileInformationByHandle, but be aware that file permissions in Windows are quite a bit different from the (traditional) file permissions in Unix. Unix has owner/group/world permissions on all files, and most systems also have ACLs (though there's more variation in how they work). Windows either has no permissions at all (e.g., files on FAT or FAT32) or else uses ACLs (e.g., files on NTFS), but nothing that's really equivalent to the traditional owner/group/world permissions most people are accustomed to on Unix.

Perhaps you're using "permissions" to refer to whether the file was open for reading, writing, or both. Getting that is considerably uglier than any of the preceding. The problem is that most of it is in the library, not Win32, so there's probably no way to do it that will be even close to portable between compilers. With MS VC++ 9.0 SP1 (not guaranteed for any other compiler) you can do this:

#include <stdio.h>int get_perms(int fd) {    int i; FILE * base = __iob_func();    for (i=0; i<_IOB_ENTRIES; i++)         if (base[i]._file == fd)            return base[i]._flag;     // we've found our file    return 0; // file wasn't found.}

Since this involved some spelunking, I wrote a quick test to verify that it might actually work:

#ifdef TEST#include <io.h>void show_perms(int perms, char const *caption) {  printf("File opened for %s\n", caption); printf("Read permission = %d\n", (perms & _IOREAD)!=0); printf("Write permission = %d\n", (perms & _IOWRT)!=0);}int main(int argc, char **argv) {  FILE *file1, *file2; int perms1, perms2; file1=fopen(argv[1], "w"); perms1 = get_perms(_fileno(file1)); fclose(file1); file2=fopen(argv[1], "r"); perms2 = get_perms(_fileno(file2)); fclose(file2); show_perms(perms1, "writing"); show_perms(perms2, "reading"); return 0;}#endif

And the results seem to indicate success:

File opened for writingRead permission = 0Write permission = 1File opened for readingRead permission = 1Write permission = 0

You can then test that returned flag against _IOREAD, _IOWRT, and _IORW, which are defined in stdio.h. Despite my previous warnings, I should probably point out that I suspect (though I certainly can't guarantee) that this part of the library is fairly stable, so the real chances of major changes are probably fairly minimal.

In the other direction, however, there's basically no chance at all that it'll work with any other library. It could (but certainly isn't guaranteed to) work with the other compilers that use the MS library, such as Intel, MinGW or Comeau using MS VC++ as its back-end. Of those, I'd say the most likely to work would be Comeau, and the least likely MinGW (but that's only a guess; there's a good chance it won't work with any of them).

  1. Requires the redistributable Win32 FileID API Library


So, I recommend reading up on this a little more. The dup() and related functions serve to create a duplicate value in the file descriptor table pointing to the same entry in the open file table. This is intended to have the same offset. If you call open(), you will create a new entry the open file table.

It doesn't make any sense to create a duplicate of a file descriptor and that new file descriptor have a different offset in the open file table (this seems to contradict what the word "duplicate" means).

I'm not sure what your question is actually. I mean, it isn't the same thing as a duplicate. You could read:

/proc/self/fd/[descriptor]

and get the string that was used to open that file descriptor; bear in mind this may provide some pitfalls, some of which you actually noted in your observation of calling open() again.

Maybe you can explain a little more and I can try to update to help.


Why don't you just open the file a second time with open() or CreateFile() on windows? This gives you all freedom of different access rights and separate offset.

This of course has the drawback that you you can not open the file exclusively, but it solves your problem very simply.