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