Boost logo

Boost :

From: Anthony Williams (anthony.williamsNOSPAM_at_[hidden])
Date: 2003-05-23 07:41:24


Vladimir Prus <ghost_at_[hidden]> writes:

> Anthony Williams wrote:
>
> > Vladimir Prus <ghost_at_[hidden]> writes:
> >> > Shame about the "NT symlinks not supported yet" errors...
> >>
> >> Hmm... I hope someone more knowledgeble about NT will implement them
> >> someday. I even don't known is there's command line tool to create them.
> >
> > Within an NTFS partition you can create hard links for files. You can also
> > link directories within or across local drives, so c:/cdrom points to the
> > contents of the CDROM drive, and c:/boost_current points to
> > c:/libs/boost_1_30_0.
> >
> > I can provide code for doing the former; www.sysinternals.com provide a
> > tool (with source code) for doing the latter (junction).
>
> I've looked at the latter now, and would be very interesting in code for the
> former --- since hard links for files more closely match Unix links
> semantics.

The code below creates a file "c:\target", and then creates a link to it
called "c:\link".

The important things to note are:

* The link filename is UNICODE

* The link filename must be normalized, as if by calling
  GetFullPathNameW. This means that directory separators have to be
  backslashes not forward slashes, and .. and . components have been removed

Anthony

-- 
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.
 
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
int main()
{
    char const targetName[]="c:\\target";
    // create the target
    FILE * target=fopen(targetName,"w");
    fprintf(target,"hello world\n");
    fclose(target);
    // open the target with backup semantics
    HANDLE linkHandle=CreateFile(targetName,GENERIC_WRITE,0,NULL,OPEN_EXISTING,
                                 FILE_FLAG_BACKUP_SEMANTICS,NULL);
    if(linkHandle==INVALID_HANDLE_VALUE)
    {
        printf("Error %08X creating file\n",GetLastError());
        return 0;
    }
    wchar_t const linkName[]=L"c:\\link";
    
    // prepare the link info
    WIN32_STREAM_ID streamInfo;
    streamInfo.dwStreamId=BACKUP_LINK;
    streamInfo.dwStreamAttributes=0;
    streamInfo.Size.LowPart=wcslen(linkName)*2+2;
    streamInfo.Size.HighPart=0;
    streamInfo.dwStreamNameSize=0;
    
    // write the info
    DWORD bytesWritten=0;
    LPVOID context=0;
    
    if(!BackupWrite(linkHandle,(unsigned char*)&streamInfo,
                    offsetof(WIN32_STREAM_ID,cStreamName),
                    &bytesWritten,FALSE,FALSE,&context))
    {
        CloseHandle(linkHandle);
        printf("Error %08X creating link header\n",GetLastError());
        return 0;
    }
    if(!BackupWrite(linkHandle,(unsigned char*)linkName,
                    wcslen(linkName)*2+2,
                    &bytesWritten,FALSE,FALSE,&context))
    {
        CloseHandle(linkHandle);
        printf("Error %08X creating link filename\n",GetLastError());
        return 0;
    }
    
    // end the backup write
    if(!BackupWrite(linkHandle,0,0,&bytesWritten,TRUE,FALSE,&context))
    {
        printf("Error %08X closing link\n",GetLastError());
        CloseHandle(linkHandle);
        return 0;
    }
   
    CloseHandle(linkHandle);
}

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk