DEVEL15-windows-largefile-support-20060623
[openafs.git] / src / WINNT / afsd / cm_freelance.c
1 #include <afs/param.h>
2 #include <afs/stds.h>
3
4 #ifndef DJGPP
5 #include <windows.h>
6 #include <winreg.h>
7 #include <winsock2.h>
8 #else
9 #include <netdb.h>
10 #endif /* !DJGPP */
11 #include <stdlib.h>
12 #include <malloc.h>
13 #include <string.h>
14
15 #include <WINNT/afsreg.h>
16 #include "afsd.h"
17 #include <rx/rx.h>
18
19 #ifdef AFS_FREELANCE_CLIENT
20 #include "cm_freelance.h"
21 #include "stdio.h"
22
23 extern void afsi_log(char *pattern, ...);
24
25 int cm_noLocalMountPoints;
26 int cm_fakeDirSize;
27 int cm_fakeDirCallback=0;
28 int cm_fakeGettingCallback=0;
29 cm_localMountPoint_t* cm_localMountPoints;
30 osi_mutex_t cm_Freelance_Lock;
31 int cm_localMountPointChangeFlag = 0;
32 int cm_freelanceEnabled = 1;
33 time_t FakeFreelanceModTime = 0x3b49f6e2;
34
35 static int freelance_ShutdownFlag = 0;
36 #if !defined(DJGPP)
37 static HANDLE hFreelanceChangeEvent = 0;
38 static HANDLE hFreelanceSymlinkChangeEvent = 0;
39 #endif
40
41 void cm_InitFakeRootDir();
42
43 #if !defined(DJGPP)
44 void cm_FreelanceChangeNotifier(void * parmp) {
45     HKEY   hkFreelance = 0;
46
47     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
48                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
49                       0,
50                       KEY_NOTIFY,
51                       &hkFreelance) == ERROR_SUCCESS) {
52
53         hFreelanceChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
54         if (hFreelanceChangeEvent == NULL) {
55             RegCloseKey(hkFreelance);
56             return;
57         }
58     }
59
60     while ( TRUE ) {
61     /* check hFreelanceChangeEvent to see if it is set. 
62      * if so, call cm_noteLocalMountPointChange()
63      */
64         if (RegNotifyChangeKeyValue( hkFreelance,   /* hKey */
65                                      FALSE,         /* bWatchSubtree */
66                                      REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
67                                      hFreelanceChangeEvent, /* hEvent */
68                                      TRUE          /* fAsynchronous */
69                                      ) != ERROR_SUCCESS) {
70             RegCloseKey(hkFreelance);
71             CloseHandle(hFreelanceChangeEvent);
72             hFreelanceChangeEvent = 0;
73             return;
74         }
75
76         if (WaitForSingleObject(hFreelanceChangeEvent, INFINITE) == WAIT_OBJECT_0)
77         {
78             if (freelance_ShutdownFlag == 1) {     
79                 RegCloseKey(hkFreelance);          
80                 CloseHandle(hFreelanceChangeEvent);
81                 hFreelanceChangeEvent = 0;         
82                 return;                            
83             }                                      
84             cm_noteLocalMountPointChange();
85         }
86     }
87 }
88
89 void cm_FreelanceSymlinkChangeNotifier(void * parmp) {
90     HKEY   hkFreelance = 0;
91
92     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
93                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
94                       0,
95                       KEY_NOTIFY,
96                       &hkFreelance) == ERROR_SUCCESS) {
97
98         hFreelanceSymlinkChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
99         if (hFreelanceSymlinkChangeEvent == NULL) {
100             RegCloseKey(hkFreelance);
101             return;
102         }
103     }
104
105     while ( TRUE ) {
106     /* check hFreelanceSymlinkChangeEvent to see if it is set. 
107      * if so, call cm_noteLocalMountPointSymlinkChange()
108      */
109         if (RegNotifyChangeKeyValue( hkFreelance,   /* hKey */
110                                      FALSE,         /* bWatchSubtree */
111                                      REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
112                                      hFreelanceSymlinkChangeEvent, /* hEvent */
113                                      TRUE          /* fAsynchronous */
114                                      ) != ERROR_SUCCESS) {
115             RegCloseKey(hkFreelance);
116             CloseHandle(hFreelanceSymlinkChangeEvent);
117             hFreelanceSymlinkChangeEvent = 0;
118             return;
119         }
120
121         if (WaitForSingleObject(hFreelanceSymlinkChangeEvent, INFINITE) == WAIT_OBJECT_0)
122         {
123             if (freelance_ShutdownFlag == 1) {     
124                 RegCloseKey(hkFreelance);          
125                 CloseHandle(hFreelanceSymlinkChangeEvent);
126                 hFreelanceSymlinkChangeEvent = 0;         
127                 return;                            
128             }                                      
129             cm_noteLocalMountPointChange();
130         }
131     }
132 }
133 #endif
134
135 void                                          
136 cm_FreelanceShutdown(void)                    
137 {                                             
138     freelance_ShutdownFlag = 1;               
139 #if !defined(DJGPP)                           
140     if (hFreelanceChangeEvent != 0)           
141         thrd_SetEvent(hFreelanceChangeEvent); 
142     if (hFreelanceSymlinkChangeEvent != 0)           
143         thrd_SetEvent(hFreelanceSymlinkChangeEvent); 
144 #endif                                        
145 }                                             
146
147 void cm_InitFreelance() {
148 #if !defined(DJGPP)
149     thread_t phandle;
150     int lpid;
151 #endif
152
153     lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock");
154
155     // make sure we sync the data version to the cached root scache_t                  
156     if (cm_data.rootSCachep && cm_data.rootSCachep->fid.cell == AFS_FAKE_ROOT_CELL_ID) 
157         cm_data.fakeDirVersion = cm_data.rootSCachep->dataVersion;                          
158                                                                                       
159     // yj: first we make a call to cm_initLocalMountPoints
160     // to read all the local mount points from an ini file
161     cm_InitLocalMountPoints();
162
163     // then we make a call to InitFakeRootDir to create
164     // a fake root directory based on the local mount points
165     cm_InitFakeRootDir();
166     // --- end of yj code
167
168 #if !defined(DJGPP)
169     /* Start the registry monitor */
170     phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceChangeNotifier,
171                           NULL, 0, &lpid, "cm_FreelanceChangeNotifier");
172     osi_assert(phandle != NULL);
173     thrd_CloseHandle(phandle);
174
175     phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
176                           NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
177     osi_assert(phandle != NULL);
178     thrd_CloseHandle(phandle);
179 #endif
180 }
181
182 /* yj: Initialization of the fake root directory */
183 /* to be called while holding freelance lock unless during init. */
184 void cm_InitFakeRootDir() {
185     int i, t1, t2;
186     char* currentPos;
187     int noChunks;
188
189     // allocate space for the fake info
190     cm_dirHeader_t fakeDirHeader;
191     cm_dirEntry_t fakeEntry;
192     cm_pageHeader_t fakePageHeader;
193
194     // i'm going to calculate how much space is needed for
195     // this fake root directory. we have these rules:
196     // 1. there are cm_noLocalMountPoints number of entries
197     // 2. each page is CM_DIR_PAGESIZE in size
198     // 3. the first 13 chunks of the first page are used for
199     //    some header stuff
200     // 4. the first chunk of all subsequent pages are used
201     //    for page header stuff
202     // 5. a max of CM_DIR_EPP entries are allowed per page
203     // 6. each entry takes 1 or more chunks, depending on 
204     //    the size of the mount point string, as determined
205     //    by cm_NameEntries
206     // 7. each chunk is CM_DIR_CHUNKSIZE bytes
207
208     int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
209     int curChunk = 13;  // chunks 0 - 12 are used for header stuff
210                         // of the first page in the directory
211     int curPage = 0;
212     int curDirEntry = 0;
213     int curDirEntryInPage = 0;
214     int sizeOfCurEntry;
215     int dirSize;
216
217     /* Reserve 2 directory chunks for "." and ".." */
218     curChunk += 2;
219
220     while (curDirEntry<cm_noLocalMountPoints) {
221         sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
222         if ((curChunk + sizeOfCurEntry >= CPP) ||
223              (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
224             curPage++;
225             curDirEntryInPage = 0;
226             curChunk = 1;
227         }
228         curChunk += sizeOfCurEntry;
229         curDirEntry++;
230         curDirEntryInPage++;
231     }
232
233     dirSize = (curPage+1) *  CM_DIR_PAGESIZE;
234     cm_FakeRootDir = malloc(dirSize);
235     cm_fakeDirSize = dirSize;
236
237     // yj: when we get here, we've figured out how much memory we need and 
238     // allocated the appropriate space for it. we now prceed to fill
239     // it up with entries.
240     curPage = 0;
241     curDirEntry = 0;
242     curDirEntryInPage = 0;
243     curChunk = 0;
244
245     // fields in the directory entry that are unused.
246     fakeEntry.flag = 1;
247     fakeEntry.length = 0;
248     fakeEntry.next = 0;
249     fakeEntry.fid.unique = htonl(1);
250
251     // the first page is special, it uses fakeDirHeader instead of fakePageHeader
252     // we fill up the page with dirEntries that belong there and we make changes
253     // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
254     // filling up the dirEntries in this page, we copy the fakeDirHeader into 
255     // the top of the page.
256
257     // init the freeBitmap array
258     for (i=0; i<8; i++) 
259         fakeDirHeader.header.freeBitmap[i]=0;
260
261     fakeDirHeader.header.freeBitmap[0] = 0xff;
262     fakeDirHeader.header.freeBitmap[1] = 0x7f;
263
264
265     // we start counting at 13 because the 0th to 12th chunks are used for header
266     curChunk = 13;
267
268     // stick the first 2 entries "." and ".." in
269     fakeEntry.fid.unique = htonl(1);
270     fakeEntry.fid.vnode = htonl(1);
271     strcpy(fakeEntry.name, ".");
272     currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
273     memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
274     curChunk++; curDirEntryInPage++;
275     strcpy(fakeEntry.name, "..");
276     currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
277     memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
278     curChunk++; curDirEntryInPage++;
279
280     // keep putting stuff into page 0 if
281     // 1. we're not done with all entries
282     // 2. we have less than CM_DIR_EPP entries in page 0
283     // 3. we're not out of chunks in page 0
284
285     while( (curDirEntry<cm_noLocalMountPoints) && 
286            (curDirEntryInPage < CM_DIR_EPP) &&
287            (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP)) 
288     {       
289
290         noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
291         fakeEntry.fid.vnode = htonl(curDirEntry + 2);
292         currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
293
294         memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
295         strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
296         curDirEntry++;
297         curDirEntryInPage++;
298         for (i=0; i<noChunks; i++) {
299             t1 = (curChunk + i) / 8;
300             t2 = curChunk + i - (t1*8);
301             fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
302         }
303         curChunk+=noChunks;
304     }
305
306     // when we get here, we're done with filling in the entries for page 0
307     // copy in the header info
308
309     memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
310
311     curPage++;
312
313     // ok, page 0's done. Move on to the next page.
314     while (curDirEntry<cm_noLocalMountPoints) {
315         // setup a new page
316         curChunk = 1;                   // the zeroth chunk is reserved for page header
317         curDirEntryInPage = 0; 
318         for (i=0; i<8; i++) {
319             fakePageHeader.freeBitmap[i]=0;
320         }
321         fakePageHeader.freeCount = 0;
322         fakePageHeader.pgcount = 0;
323         fakePageHeader.tag = htons(1234);
324
325         // while we're on the same page...
326         while ( (curDirEntry<cm_noLocalMountPoints) &&
327                 (curDirEntryInPage < CM_DIR_EPP) &&
328                 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
329         {
330             // add an entry to this page
331
332             noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
333             fakeEntry.fid.vnode=htonl(curDirEntry+2);
334             currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
335             memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
336             strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
337             curDirEntry++;
338             curDirEntryInPage++;
339             for (i=0; i<noChunks; i++) {
340                 t1 = (curChunk + i) / 8;
341                 t2 = curChunk + i - (t1*8);
342                 fakePageHeader.freeBitmap[t1] |= (1 << t2);
343             }
344             curChunk+=noChunks;
345         }
346         memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
347
348         curPage++;
349     }
350
351     // we know the fakeDir is setup properly, so we claim that we have callback
352     osi_Log0(afsd_logp,"cm_InitFakeRootDir fakeDirCallback=1");
353     cm_fakeDirCallback=1;
354
355     // when we get here, we've set up everything! done!
356 }
357
358 int cm_FakeRootFid(cm_fid_t *fidp)
359 {
360     fidp->cell = AFS_FAKE_ROOT_CELL_ID;            /* root cell */
361     fidp->volume = AFS_FAKE_ROOT_VOL_ID;   /* root.afs ? */
362     fidp->vnode = 0x1;
363     fidp->unique = 0x1;
364     return 0;
365 }
366   
367 /* called directly from ioctl */
368 /* called while not holding freelance lock */
369 int cm_noteLocalMountPointChange(void) {
370     lock_ObtainMutex(&cm_Freelance_Lock);
371     cm_data.fakeDirVersion++;
372     cm_localMountPointChangeFlag = 1;
373     lock_ReleaseMutex(&cm_Freelance_Lock);
374     return 1;
375 }
376
377 int cm_getLocalMountPointChange() {
378     return cm_localMountPointChangeFlag;
379 }
380
381 int cm_clearLocalMountPointChange() {
382     cm_localMountPointChangeFlag = 0;
383     return 0;
384 }
385
386 int cm_reInitLocalMountPoints() {
387     cm_fid_t aFid;
388     int i, hash;
389     cm_scache_t *scp, **lscpp, *tscp;
390         
391     osi_Log0(afsd_logp,"----- freelance reinitialization starts ----- ");
392
393     // first we invalidate all the SCPs that were created
394     // for the local mount points
395
396     osi_Log0(afsd_logp,"Invalidating local mount point scp...  ");
397
398     aFid.cell = AFS_FAKE_ROOT_CELL_ID;
399     aFid.volume=AFS_FAKE_ROOT_VOL_ID;
400     aFid.unique=0x1;
401     aFid.vnode=0x2;
402
403     lock_ObtainWrite(&cm_scacheLock);
404     lock_ObtainMutex(&cm_Freelance_Lock);  /* always scache then freelance lock */
405     for (i=0; i<cm_noLocalMountPoints; i++) {
406         hash = CM_SCACHE_HASH(&aFid);
407         for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
408             if (scp->fid.volume == aFid.volume &&
409                  scp->fid.vnode == aFid.vnode &&
410                  scp->fid.unique == aFid.unique 
411                  ) {
412
413                 // mark the scp to be reused
414                 cm_HoldSCacheNoLock(scp);
415                 lock_ReleaseWrite(&cm_scacheLock);
416                 lock_ObtainMutex(&scp->mx);
417                 cm_DiscardSCache(scp);
418                 lock_ReleaseMutex(&scp->mx);
419                 cm_CallbackNotifyChange(scp);
420                 lock_ObtainWrite(&cm_scacheLock);
421                 cm_ReleaseSCacheNoLock(scp);
422
423                 // take the scp out of the hash
424                 for (lscpp = &cm_data.hashTablep[hash], tscp = cm_data.hashTablep[hash]; 
425                      tscp; 
426                      lscpp = &tscp->nextp, tscp = tscp->nextp) {
427                     if (tscp == scp) {
428                         *lscpp = scp->nextp;
429                         lock_ObtainMutex(&scp->mx);
430                         scp->flags &= ~CM_SCACHEFLAG_INHASH;
431                         lock_ReleaseMutex(&scp->mx);
432                         break;
433                     }
434                 }
435             }
436         }
437         aFid.vnode = aFid.vnode + 1;
438     }
439     lock_ReleaseWrite(&cm_scacheLock);
440     osi_Log0(afsd_logp,"\tall old scp cleared!");
441
442     // we must free the memory that was allocated in the prev
443     // cm_InitLocalMountPoints call
444     osi_Log0(afsd_logp,"Removing old localmountpoints...  ");
445     free(cm_localMountPoints);
446     osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
447
448     // now re-init the localmountpoints
449     osi_Log0(afsd_logp,"Creating new localmountpoints...  ");
450     cm_InitLocalMountPoints();
451     osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
452
453     // now we have to free the memory allocated in cm_initfakerootdir
454     osi_Log0(afsd_logp,"Removing old fakedir...  ");
455     free(cm_FakeRootDir);
456     osi_Log0(afsd_logp,"\t\told fakedir removed!");
457
458     // then we re-create that dir
459     osi_Log0(afsd_logp,"Creating new fakedir...  ");
460     cm_InitFakeRootDir();
461     osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
462
463     lock_ReleaseMutex(&cm_Freelance_Lock);
464
465     osi_Log0(afsd_logp,"----- freelance reinit complete -----");
466     return 0;
467 }
468
469
470 // yj: open up the ini file and read all the local mount 
471 // points that are stored there. Part of the initialization
472 // process for the freelance client.
473 /* to be called while holding freelance lock unless during init. */
474 long cm_InitLocalMountPoints() {
475     FILE *fp;
476     int i;
477     char line[512];
478     char*t, *t2;
479     cm_localMountPoint_t* aLocalMountPoint;
480     char hdir[120];
481     long code;
482     char rootCellName[256];
483 #if !defined(DJGPP)
484     HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
485     DWORD dwType, dwSize;
486     DWORD dwMountPoints = 0;
487     DWORD dwIndex;
488     DWORD dwSymlinks = 0;
489     FILETIME ftLastWriteTime;
490 #endif
491
492 #if !defined(DJGPP)
493     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
494                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
495                       0,
496                       KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
497                       &hkFreelance) == ERROR_SUCCESS) {
498
499         RegQueryInfoKey( hkFreelance,
500                          NULL,  /* lpClass */
501                          NULL,  /* lpcClass */
502                          NULL,  /* lpReserved */
503                          NULL,  /* lpcSubKeys */
504                          NULL,  /* lpcMaxSubKeyLen */
505                          NULL,  /* lpcMaxClassLen */
506                          &dwMountPoints, /* lpcValues */
507                          NULL,  /* lpcMaxValueNameLen */
508                          NULL,  /* lpcMaxValueLen */
509                          NULL,  /* lpcbSecurityDescriptor */
510                          &ftLastWriteTime /* lpftLastWriteTime */
511                          );
512
513         smb_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
514
515         if ( dwMountPoints == 0 ) {
516             rootCellName[0] = '.';
517             code = cm_GetRootCellName(&rootCellName[1]);
518             if (code == 0) {
519                 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
520                 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
521                 dwMountPoints = 2;
522             }
523         }
524
525         if (RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
526                           AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
527                           0,
528                           NULL,
529                           REG_OPTION_NON_VOLATILE,
530                           KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
531                           NULL,
532                           &hkFreelanceSymlinks,
533                           NULL) == ERROR_SUCCESS) {
534
535             RegQueryInfoKey( hkFreelanceSymlinks,
536                              NULL,  /* lpClass */
537                              NULL,  /* lpcClass */
538                              NULL,  /* lpReserved */
539                              NULL,  /* lpcSubKeys */
540                              NULL,  /* lpcMaxSubKeyLen */
541                              NULL,  /* lpcMaxClassLen */
542                              &dwSymlinks, /* lpcValues */
543                              NULL,  /* lpcMaxValueNameLen */
544                              NULL,  /* lpcMaxValueLen */
545                              NULL,  /* lpcbSecurityDescriptor */
546                              NULL   /* lpftLastWriteTime */
547                              );
548         }
549
550         // get the number of entries there are from the first line
551         // that we read
552         cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
553
554         // create space to store the local mount points
555         cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
556         aLocalMountPoint = cm_localMountPoints;
557
558         // now we read n lines and parse them into local mount points
559         // where n is the number of local mount points there are, as
560         // determined above.
561         // Each line in the ini file represents 1 local mount point and 
562         // is in the format xxx#yyy:zzz, where xxx is the directory
563         // entry name, yyy is the cell name and zzz is the volume name.
564         // #yyy:zzz together make up the mount point.
565         for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
566             TCHAR szValueName[16];
567             DWORD dwValueSize = 16;
568             dwSize = sizeof(line);
569             if (RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
570                           &dwType, line, &dwSize))
571             {
572                 afsi_log("RegEnumValue(hkFreelance) failed");
573                 cm_noLocalMountPoints--;
574                 continue;
575             }
576
577             afsi_log("Mountpoint[%d] = %s",dwIndex, line);
578
579             /* find the trailing dot; null terminate after it */
580             t2 = strrchr(line, '.');
581             if (t2)
582                 *(t2+1) = '\0';
583
584             for ( t=line;*t;t++ ) {
585                 if ( !isprint(*t) ) {
586                     afsi_log("error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
587                     fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
588                     cm_noLocalMountPoints--;
589                     continue;
590                 }
591             }
592
593             // line is not empty, so let's parse it
594             t = strchr(line, '#');
595             if (!t)
596                 t = strchr(line, '%');
597             // make sure that there is a '#' or '%' separator in the line
598             if (!t) {
599                 afsi_log("error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
600                 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
601                 cm_noLocalMountPoints--;
602                 continue;
603             }
604
605             aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
606             aLocalMountPoint->namep=malloc(t-line+1);
607             strncpy(aLocalMountPoint->namep, line, t-line);
608             aLocalMountPoint->namep[t-line] = '\0';
609                 
610             /* copy the mount point string */
611             aLocalMountPoint->mountPointStringp=malloc(strlen(t));
612             strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
613             aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
614     
615             osi_Log2(afsd_logp,"found mount point: name %s, string %s",
616                       osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
617                       osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
618
619             aLocalMountPoint++;
620         }
621
622         for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
623             TCHAR szValueName[16];
624             DWORD dwValueSize = 16;
625             dwSize = sizeof(line);
626             if (RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
627                               &dwType, line, &dwSize))
628             {
629                 afsi_log("RegEnumValue(hkFreelanceSymlinks) failed");
630                 cm_noLocalMountPoints--;
631                 continue;
632             }
633
634             afsi_log("Symlink[%d] = %s",dwIndex, line);
635
636             /* find the trailing dot; null terminate after it */
637             t2 = strrchr(line, '.');
638             if (t2)
639                 *(t2+1) = '\0';
640
641             for ( t=line;*t;t++ ) {
642                 if ( !isprint(*t) ) {
643                     afsi_log("error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
644                     fprintf(stderr, "error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
645                     cm_noLocalMountPoints--;
646                     continue;
647                 }
648             }
649
650             // line is not empty, so let's parse it
651             t = strchr(line, ':');
652
653             // make sure that there is a ':' separator in the line
654             if (!t) {
655                 afsi_log("error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
656                 fprintf(stderr, "error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
657                 cm_noLocalMountPoints--;
658                 continue;
659             }
660
661             aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
662             aLocalMountPoint->namep=malloc(t-line+1);
663             strncpy(aLocalMountPoint->namep, line, t-line);
664             aLocalMountPoint->namep[t-line] = '\0';
665                 
666             /* copy the symlink string */
667             aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
668             strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
669             aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
670     
671             osi_Log2(afsd_logp,"found symlink: name %s, string %s",
672                       osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
673                       osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
674
675             aLocalMountPoint++;
676         }
677
678         if ( hkFreelanceSymlinks )
679             RegCloseKey( hkFreelanceSymlinks );
680         RegCloseKey(hkFreelance);
681         return 0;
682     }
683 #endif
684
685     /* What follows is the old code to read freelance mount points 
686      * out of a text file modified to copy the data into the registry
687      */
688     cm_GetConfigDir(hdir);
689     strcat(hdir, AFS_FREELANCE_INI);
690     // open the ini file for reading
691     fp = fopen(hdir, "r");
692     if (!fp) {
693         /* look in the Windows directory where we used to store the file */
694         GetWindowsDirectory(hdir, sizeof(hdir));
695         strcat(hdir,"\\");
696         strcat(hdir, AFS_FREELANCE_INI);
697         fp = fopen(hdir, "r");
698     }
699
700 #if !defined(DJGPP)
701     RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
702                     AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
703                     0,
704                     NULL,
705                     REG_OPTION_NON_VOLATILE,
706                     KEY_READ|KEY_WRITE,
707                     NULL,
708                     &hkFreelance,
709                     NULL);
710     dwIndex = 0;
711 #endif
712
713     if (!fp) {
714 #if !defined(DJGPP)
715         RegCloseKey(hkFreelance);
716 #endif
717         rootCellName[0] = '.';
718         code = cm_GetRootCellName(&rootCellName[1]);
719         if (code == 0) {
720             cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
721             cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
722         }
723         return 0;
724     }
725
726     // we successfully opened the file
727     osi_Log0(afsd_logp,"opened afs_freelance.ini");
728         
729     // now we read the first line to see how many entries
730     // there are
731     fgets(line, sizeof(line), fp);
732
733     // if the line is empty at any point when we're reading
734     // we're screwed. report error and return.
735     if (*line==0) {
736         afsi_log("error occurred while reading afs_freelance.ini");
737         fprintf(stderr, "error occurred while reading afs_freelance.ini");
738         return -1;
739     }
740
741     // get the number of entries there are from the first line
742     // that we read
743     cm_noLocalMountPoints = atoi(line);
744
745     if (cm_noLocalMountPoints > 0) {
746         // create space to store the local mount points
747         cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
748         aLocalMountPoint = cm_localMountPoints;
749     }
750
751     // now we read n lines and parse them into local mount points
752     // where n is the number of local mount points there are, as
753     // determined above.
754     // Each line in the ini file represents 1 local mount point and 
755     // is in the format xxx#yyy:zzz, where xxx is the directory
756     // entry name, yyy is the cell name and zzz is the volume name.
757     // #yyy:zzz together make up the mount point.
758     for (i=0; i<cm_noLocalMountPoints; i++) {
759         fgets(line, sizeof(line), fp);
760         // check that the line is not empty
761         if (line[0]==0) {
762             afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
763             fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
764             return -1;
765         }
766
767         /* find the trailing dot; null terminate after it */
768         t2 = strrchr(line, '.');
769         if (t2)
770             *(t2+1) = '\0';
771
772 #if !defined(DJGPP)
773         if ( hkFreelance ) {
774             char szIndex[16];
775             /* we are migrating to the registry */
776             sprintf(szIndex,"%d",dwIndex++);
777             dwType = REG_SZ;
778             dwSize = (DWORD)strlen(line) + 1;
779             RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
780         }
781 #endif 
782
783         // line is not empty, so let's parse it
784         t = strchr(line, '#');
785         if (!t)
786             t = strchr(line, '%');
787         // make sure that there is a '#' or '%' separator in the line
788         if (!t) {
789             afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
790             fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
791             return -1;
792         }
793         aLocalMountPoint->namep=malloc(t-line+1);
794         memcpy(aLocalMountPoint->namep, line, t-line);
795         *(aLocalMountPoint->namep + (t-line)) = 0;
796
797         aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
798         memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
799         *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
800
801         osi_Log2(afsd_logp,"found mount point: name %s, string %s",
802                   aLocalMountPoint->namep,
803                   aLocalMountPoint->mountPointStringp);
804
805         aLocalMountPoint++;
806     }
807     fclose(fp);
808 #if !defined(DJGPP)
809     if ( hkFreelance ) {
810         RegCloseKey(hkFreelance);
811         DeleteFile(hdir);
812     }
813 #endif
814     return 0;
815 }
816
817 int cm_getNoLocalMountPoints() {
818     return cm_noLocalMountPoints;
819 }
820
821 #if !defined(DJGPP)
822 long cm_FreelanceMountPointExists(char * filename)
823 {
824     char* cp;
825     char line[512];
826     char shortname[200];
827     int found = 0;
828     HKEY hkFreelance = 0;
829     DWORD dwType, dwSize;
830     DWORD dwMountPoints;
831     DWORD dwIndex;
832         
833     lock_ObtainMutex(&cm_Freelance_Lock);
834
835     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
836                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
837                       0,
838                       KEY_READ|KEY_QUERY_VALUE,
839                       &hkFreelance) == ERROR_SUCCESS) 
840     {
841         RegQueryInfoKey( hkFreelance,
842                          NULL,  /* lpClass */
843                          NULL,  /* lpcClass */
844                          NULL,  /* lpReserved */
845                          NULL,  /* lpcSubKeys */
846                          NULL,  /* lpcMaxSubKeyLen */
847                          NULL,  /* lpcMaxClassLen */
848                          &dwMountPoints, /* lpcValues */
849                          NULL,  /* lpcMaxValueNameLen */
850                          NULL,  /* lpcMaxValueLen */
851                          NULL,  /* lpcbSecurityDescriptor */
852                          NULL   /* lpftLastWriteTime */
853                          );
854
855         for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
856             TCHAR szValueName[16];
857             DWORD dwValueSize = 16;
858             dwSize = sizeof(line);
859             RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
860                           &dwType, line, &dwSize);
861
862             cp=strchr(line, '#');
863             if (!cp)
864                 cp=strchr(line, '%');
865             memcpy(shortname, line, cp-line);
866             shortname[cp-line]=0;
867
868             if (!strcmp(shortname, filename)) {
869                 found = 1;
870                 break;
871             }
872         }
873         for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
874             TCHAR szValueName[16];
875             DWORD dwValueSize = 16;
876             dwSize = sizeof(line);
877             RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
878                           &dwType, line, &dwSize);
879
880             cp=strchr(line, '#');
881             if (!cp)
882                 cp=strchr(line, '%');
883             memcpy(shortname, line, cp-line);
884             shortname[cp-line]=0;
885
886             if (!stricmp(shortname, filename)) {
887                 found = 1;
888                 break;
889             }
890         }
891         RegCloseKey(hkFreelance);
892     }
893
894     lock_ReleaseMutex(&cm_Freelance_Lock);
895
896     return found;
897 }
898
899 long cm_FreelanceSymlinkExists(char * filename)
900 {
901     char* cp;
902     char line[512];
903     char shortname[200];
904     int found = 0;
905     HKEY hkFreelance = 0;
906     DWORD dwType, dwSize;
907     DWORD dwSymlinks;
908     DWORD dwIndex;
909         
910     lock_ObtainMutex(&cm_Freelance_Lock);
911
912     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
913                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
914                       0,
915                       KEY_READ|KEY_QUERY_VALUE,
916                       &hkFreelance) == ERROR_SUCCESS) 
917     {
918         RegQueryInfoKey( hkFreelance,
919                          NULL,  /* lpClass */
920                          NULL,  /* lpcClass */
921                          NULL,  /* lpReserved */
922                          NULL,  /* lpcSubKeys */
923                          NULL,  /* lpcMaxSubKeyLen */
924                          NULL,  /* lpcMaxClassLen */
925                          &dwSymlinks, /* lpcValues */
926                          NULL,  /* lpcMaxValueNameLen */
927                          NULL,  /* lpcMaxValueLen */
928                          NULL,  /* lpcbSecurityDescriptor */
929                          NULL   /* lpftLastWriteTime */
930                          );
931
932         for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
933             TCHAR szValueName[16];
934             DWORD dwValueSize = 16;
935             dwSize = sizeof(line);
936             RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
937                           &dwType, line, &dwSize);
938
939             cp=strchr(line, ':');
940             memcpy(shortname, line, cp-line);
941             shortname[cp-line]=0;
942
943             if (!strcmp(shortname, filename)) {
944                 found = 1;
945                 break;
946             }
947         }
948         for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
949             TCHAR szValueName[16];
950             DWORD dwValueSize = 16;
951             dwSize = sizeof(line);
952             RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
953                           &dwType, line, &dwSize);
954
955             cp=strchr(line, ':');
956             memcpy(shortname, line, cp-line);
957             shortname[cp-line]=0;
958
959             if (!stricmp(shortname, filename)) {
960                 found = 1;
961                 break;
962             }
963         }
964         RegCloseKey(hkFreelance);
965     }
966
967     lock_ReleaseMutex(&cm_Freelance_Lock);
968
969     return found;
970 }
971 #endif
972
973 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
974 {
975     FILE *fp;
976     char hfile[120];
977     char line[512];
978     char fullname[200];
979     int n;
980     int alias = 0;
981 #if !defined(DJGPP)
982     HKEY hkFreelance = 0;
983     DWORD dwType, dwSize;
984     DWORD dwMountPoints;
985     DWORD dwIndex;
986 #endif
987
988     /* before adding, verify the cell name; if it is not a valid cell,
989        don't add the mount point.
990        allow partial matches as a means of poor man's alias. */
991     /* major performance issue? */
992     osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
993               osi_LogSaveString(afsd_logp,filename), 
994               osi_LogSaveString(afsd_logp,cellname), 
995               osi_LogSaveString(afsd_logp,volume), 
996               rw ? "rw" : "ro");
997
998     if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
999         return -1;
1000
1001     if (cellname[0] == '.') {
1002         if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
1003             return -1;
1004     } else {
1005         if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
1006             return -1;
1007     }
1008
1009 #if !defined(DJGPP)
1010     if ( cm_FreelanceMountPointExists(filename) ||
1011          cm_FreelanceSymlinkExists(filename) )
1012         return -1;
1013 #endif
1014     
1015     osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s", 
1016               osi_LogSaveString(afsd_logp,cellname));
1017
1018     lock_ObtainMutex(&cm_Freelance_Lock);
1019
1020 #if !defined(DJGPP)
1021     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
1022                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1023                       0,
1024                       KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1025                       &hkFreelance) == ERROR_SUCCESS) {
1026
1027         RegQueryInfoKey( hkFreelance,
1028                          NULL,  /* lpClass */
1029                          NULL,  /* lpcClass */
1030                          NULL,  /* lpReserved */
1031                          NULL,  /* lpcSubKeys */
1032                          NULL,  /* lpcMaxSubKeyLen */
1033                          NULL,  /* lpcMaxClassLen */
1034                          &dwMountPoints, /* lpcValues */
1035                          NULL,  /* lpcMaxValueNameLen */
1036                          NULL,  /* lpcMaxValueLen */
1037                          NULL,  /* lpcbSecurityDescriptor */
1038                          NULL   /* lpftLastWriteTime */
1039                          );
1040
1041         if (rw)
1042             sprintf(line, "%s%%%s:%s", filename, fullname, volume);
1043         else
1044             sprintf(line, "%s#%s:%s", filename, fullname, volume);
1045
1046         /* If we are adding a new value, there must be an unused name
1047          * within the range 0 to dwMountPoints 
1048          */
1049         for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
1050             char szIndex[16];
1051             char szMount[1024];
1052
1053             dwSize = sizeof(szMount);
1054             sprintf(szIndex, "%d", dwIndex);
1055             if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
1056                 /* found an unused value */
1057                 dwType = REG_SZ;
1058                 dwSize = (DWORD)strlen(line) + 1;
1059                 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1060                 break;
1061             } else {
1062                 int len = (int)strlen(filename);
1063                 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) && 
1064                      (szMount[len] == '%' || szMount[len] == '#')) {
1065                     /* Replace the existing value */
1066                     dwType = REG_SZ;
1067                     dwSize = (DWORD)strlen(line) + 1;
1068                     RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1069                     break;
1070                 }
1071             }
1072         }
1073         RegCloseKey(hkFreelance);
1074     } else 
1075 #endif
1076     {
1077         cm_GetConfigDir(hfile);
1078         strcat(hfile, AFS_FREELANCE_INI);
1079         fp = fopen(hfile, "r+");
1080         if (!fp)
1081             return CM_ERROR_INVAL;
1082         fgets(line, sizeof(line), fp);
1083         n = atoi(line);
1084         n++;
1085         fseek(fp, 0, SEEK_SET);
1086         fprintf(fp, "%d", n);
1087         fseek(fp, 0, SEEK_END);
1088         if (rw)
1089             fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1090         else
1091             fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1092         fclose(fp);
1093     }
1094     lock_ReleaseMutex(&cm_Freelance_Lock);
1095
1096     /* cm_reInitLocalMountPoints(); */
1097     if (fidp) {
1098         fidp->unique = 1;
1099         fidp->vnode = cm_noLocalMountPoints + 1;   /* vnode value of last mt pt */
1100     }
1101     cm_noteLocalMountPointChange();
1102     return 0;
1103 }
1104
1105 long cm_FreelanceRemoveMount(char *toremove)
1106 {
1107     int i, n;
1108     char* cp;
1109     char line[512];
1110     char shortname[200];
1111     char hfile[120], hfile2[120];
1112     FILE *fp1, *fp2;
1113     int found=0;
1114 #if !defined(DJGPP)
1115     HKEY hkFreelance = 0;
1116     DWORD dwType, dwSize;
1117     DWORD dwMountPoints;
1118     DWORD dwIndex;
1119 #endif
1120
1121     lock_ObtainMutex(&cm_Freelance_Lock);
1122
1123 #if !defined(DJGPP)
1124     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
1125                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1126                       0,
1127                       KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1128                       &hkFreelance) == ERROR_SUCCESS) {
1129
1130         RegQueryInfoKey( hkFreelance,
1131                          NULL,  /* lpClass */
1132                          NULL,  /* lpcClass */
1133                          NULL,  /* lpReserved */
1134                          NULL,  /* lpcSubKeys */
1135                          NULL,  /* lpcMaxSubKeyLen */
1136                          NULL,  /* lpcMaxClassLen */
1137                          &dwMountPoints, /* lpcValues */
1138                          NULL,  /* lpcMaxValueNameLen */
1139                          NULL,  /* lpcMaxValueLen */
1140                          NULL,  /* lpcbSecurityDescriptor */
1141                          NULL   /* lpftLastWriteTime */
1142                          );
1143
1144         for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1145             TCHAR szValueName[16];
1146             DWORD dwValueSize = 16;
1147             dwSize = sizeof(line);
1148             RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1149                           &dwType, line, &dwSize);
1150
1151             cp=strchr(line, '#');
1152             if (!cp)
1153                 cp=strchr(line, '%');
1154             memcpy(shortname, line, cp-line);
1155             shortname[cp-line]=0;
1156
1157             if (!strcmp(shortname, toremove)) {
1158                 RegDeleteValue( hkFreelance, szValueName );
1159                 break;
1160             }
1161         }
1162         RegCloseKey(hkFreelance);
1163     } else 
1164 #endif
1165     {
1166         cm_GetConfigDir(hfile);
1167         strcat(hfile, AFS_FREELANCE_INI);
1168         strcpy(hfile2, hfile);
1169         strcat(hfile2, "2");
1170         fp1=fopen(hfile, "r+");
1171         if (!fp1)
1172             return CM_ERROR_INVAL;
1173         fp2=fopen(hfile2, "w+");
1174         if (!fp2) {
1175             fclose(fp1);
1176             return CM_ERROR_INVAL;
1177         }
1178
1179         fgets(line, sizeof(line), fp1);
1180         n=atoi(line);
1181         fprintf(fp2, "%d\n", n-1);
1182
1183         for (i=0; i<n; i++) {
1184             fgets(line, sizeof(line), fp1);
1185             cp=strchr(line, '#');
1186             if (!cp)
1187                 cp=strchr(line, '%');
1188             memcpy(shortname, line, cp-line);
1189             shortname[cp-line]=0;
1190
1191             if (strcmp(shortname, toremove)==0) {
1192
1193             } else {
1194                 found = 1;
1195                 fputs(line, fp2);
1196             }
1197         }
1198
1199         fclose(fp1);
1200         fclose(fp2);
1201         if (!found)
1202             return CM_ERROR_NOSUCHFILE;
1203
1204         unlink(hfile);
1205         rename(hfile2, hfile);
1206     }
1207     
1208     lock_ReleaseMutex(&cm_Freelance_Lock);
1209     cm_noteLocalMountPointChange();
1210     return 0;
1211 }
1212
1213 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1214 {
1215     char line[512];
1216     char fullname[200];
1217     int alias = 0;
1218 #if !defined(DJGPP)
1219     HKEY hkFreelanceSymlinks = 0;
1220     DWORD dwType, dwSize;
1221     DWORD dwSymlinks;
1222     DWORD dwIndex;
1223 #endif
1224
1225     /* before adding, verify the filename.  If it is already in use, either as 
1226      * as mount point or a cellname, do not permit the creation of the symlink.
1227      */
1228     osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1229               osi_LogSaveString(afsd_logp,filename), 
1230               osi_LogSaveString(afsd_logp,destination));
1231     
1232     if ( filename[0] == '\0' || destination[0] == '\0' )
1233         return -1;
1234
1235     fullname[0] = '\0';
1236     if (filename[0] == '.') {
1237         cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1238         if (stricmp(&filename[1],fullname) == 0)
1239             return -1;
1240     } else {
1241         cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1242         if (stricmp(filename,fullname) == 0)
1243             return -1;
1244     }
1245
1246 #if !defined(DJGPP)
1247     if ( cm_FreelanceMountPointExists(filename) ||
1248          cm_FreelanceSymlinkExists(filename) )
1249         return -1;
1250 #endif
1251
1252     lock_ObtainMutex(&cm_Freelance_Lock);
1253
1254 #if !defined(DJGPP)
1255     if (RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
1256                         AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1257                         0,
1258                         NULL,
1259                         REG_OPTION_NON_VOLATILE,
1260                         KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1261                         NULL,
1262                         &hkFreelanceSymlinks,
1263                         NULL) == ERROR_SUCCESS) {
1264
1265         RegQueryInfoKey( hkFreelanceSymlinks,
1266                          NULL,  /* lpClass */
1267                          NULL,  /* lpcClass */
1268                          NULL,  /* lpReserved */
1269                          NULL,  /* lpcSubKeys */
1270                          NULL,  /* lpcMaxSubKeyLen */
1271                          NULL,  /* lpcMaxClassLen */
1272                          &dwSymlinks, /* lpcValues */
1273                          NULL,  /* lpcMaxValueNameLen */
1274                          NULL,  /* lpcMaxValueLen */
1275                          NULL,  /* lpcbSecurityDescriptor */
1276                          NULL   /* lpftLastWriteTime */
1277                          );
1278
1279         sprintf(line, "%s:%s.", filename, destination);
1280
1281         /* If we are adding a new value, there must be an unused name
1282          * within the range 0 to dwSymlinks 
1283          */
1284         for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1285             char szIndex[16];
1286             char szLink[1024];
1287
1288             dwSize = sizeof(szLink);
1289             sprintf(szIndex, "%d", dwIndex);
1290             if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1291                 /* found an unused value */
1292                 dwType = REG_SZ;
1293                 dwSize = (DWORD)strlen(line) + 1;
1294                 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1295                 break;
1296             } else {
1297                 int len = (int)strlen(filename);
1298                 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1299                     /* Replace the existing value */
1300                     dwType = REG_SZ;
1301                     dwSize = (DWORD)strlen(line) + 1;
1302                     RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1303                     break;
1304                 }
1305             }
1306         }
1307         RegCloseKey(hkFreelanceSymlinks);
1308     } 
1309 #endif
1310     lock_ReleaseMutex(&cm_Freelance_Lock);
1311
1312     /* cm_reInitLocalMountPoints(); */
1313     if (fidp) {
1314         fidp->unique = 1;
1315         fidp->vnode = cm_noLocalMountPoints + 1;   /* vnode value of last mt pt */
1316     }
1317     cm_noteLocalMountPointChange();
1318     return 0;
1319 }
1320
1321 long cm_FreelanceRemoveSymlink(char *toremove)
1322 {
1323     char* cp;
1324     char line[512];
1325     char shortname[200];
1326     int found=0;
1327 #if !defined(DJGPP)
1328     HKEY hkFreelanceSymlinks = 0;
1329     DWORD dwType, dwSize;
1330     DWORD dwSymlinks;
1331     DWORD dwIndex;
1332 #endif
1333
1334     lock_ObtainMutex(&cm_Freelance_Lock);
1335
1336 #if !defined(DJGPP)
1337     if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
1338                       AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1339                       0,
1340                       KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1341                       &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1342
1343         RegQueryInfoKey( hkFreelanceSymlinks,
1344                          NULL,  /* lpClass */
1345                          NULL,  /* lpcClass */
1346                          NULL,  /* lpReserved */
1347                          NULL,  /* lpcSubKeys */
1348                          NULL,  /* lpcMaxSubKeyLen */
1349                          NULL,  /* lpcMaxClassLen */
1350                          &dwSymlinks, /* lpcValues */
1351                          NULL,  /* lpcMaxValueNameLen */
1352                          NULL,  /* lpcMaxValueLen */
1353                          NULL,  /* lpcbSecurityDescriptor */
1354                          NULL   /* lpftLastWriteTime */
1355                          );
1356
1357         for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1358             TCHAR szValueName[16];
1359             DWORD dwValueSize = 16;
1360             dwSize = sizeof(line);
1361             RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1362                           &dwType, line, &dwSize);
1363
1364             cp=strchr(line, ':');
1365             memcpy(shortname, line, cp-line);
1366             shortname[cp-line]=0;
1367
1368             if (!strcmp(shortname, toremove)) {
1369                 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1370                 break;
1371             }
1372         }
1373         RegCloseKey(hkFreelanceSymlinks);
1374     }
1375 #endif
1376     
1377     lock_ReleaseMutex(&cm_Freelance_Lock);
1378     cm_noteLocalMountPointChange();
1379     return 0;
1380 }
1381 #endif /* AFS_FREELANCE_CLIENT */