11 #include <WINNT/afsreg.h>
15 #ifdef AFS_FREELANCE_CLIENT
16 #include "cm_freelance.h"
19 extern void afsi_log(char *pattern, ...);
21 int cm_noLocalMountPoints;
23 int cm_fakeDirCallback=0;
24 int cm_fakeGettingCallback=0;
25 cm_localMountPoint_t* cm_localMountPoints;
26 osi_mutex_t cm_Freelance_Lock;
27 int cm_localMountPointChangeFlag = 0;
28 int cm_freelanceEnabled = 1;
29 time_t FakeFreelanceModTime = 0x3b49f6e2;
31 static int freelance_ShutdownFlag = 0;
32 static HANDLE hFreelanceChangeEvent = 0;
33 static HANDLE hFreelanceSymlinkChangeEvent = 0;
35 void cm_InitFakeRootDir();
37 void cm_FreelanceChangeNotifier(void * parmp) {
40 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
41 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
44 &hkFreelance) == ERROR_SUCCESS) {
46 hFreelanceChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
47 if (hFreelanceChangeEvent == NULL) {
48 RegCloseKey(hkFreelance);
54 /* check hFreelanceChangeEvent to see if it is set.
55 * if so, call cm_noteLocalMountPointChange()
57 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
58 FALSE, /* bWatchSubtree */
59 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
60 hFreelanceChangeEvent, /* hEvent */
61 TRUE /* fAsynchronous */
63 RegCloseKey(hkFreelance);
64 CloseHandle(hFreelanceChangeEvent);
65 hFreelanceChangeEvent = 0;
69 if (WaitForSingleObject(hFreelanceChangeEvent, INFINITE) == WAIT_OBJECT_0)
71 if (freelance_ShutdownFlag == 1) {
72 RegCloseKey(hkFreelance);
73 CloseHandle(hFreelanceChangeEvent);
74 hFreelanceChangeEvent = 0;
77 cm_noteLocalMountPointChange();
82 void cm_FreelanceSymlinkChangeNotifier(void * parmp) {
85 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
86 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
89 &hkFreelance) == ERROR_SUCCESS) {
91 hFreelanceSymlinkChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
92 if (hFreelanceSymlinkChangeEvent == NULL) {
93 RegCloseKey(hkFreelance);
99 /* check hFreelanceSymlinkChangeEvent to see if it is set.
100 * if so, call cm_noteLocalMountPointSymlinkChange()
102 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
103 FALSE, /* bWatchSubtree */
104 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
105 hFreelanceSymlinkChangeEvent, /* hEvent */
106 TRUE /* fAsynchronous */
107 ) != ERROR_SUCCESS) {
108 RegCloseKey(hkFreelance);
109 CloseHandle(hFreelanceSymlinkChangeEvent);
110 hFreelanceSymlinkChangeEvent = 0;
114 if (WaitForSingleObject(hFreelanceSymlinkChangeEvent, INFINITE) == WAIT_OBJECT_0)
116 if (freelance_ShutdownFlag == 1) {
117 RegCloseKey(hkFreelance);
118 CloseHandle(hFreelanceSymlinkChangeEvent);
119 hFreelanceSymlinkChangeEvent = 0;
122 cm_noteLocalMountPointChange();
128 cm_FreelanceShutdown(void)
130 freelance_ShutdownFlag = 1;
131 if (hFreelanceChangeEvent != 0)
132 thrd_SetEvent(hFreelanceChangeEvent);
133 if (hFreelanceSymlinkChangeEvent != 0)
134 thrd_SetEvent(hFreelanceSymlinkChangeEvent);
137 void cm_InitFreelance() {
141 lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock");
143 // make sure we sync the data version to the cached root scache_t
144 if (cm_data.rootSCachep && cm_data.rootSCachep->fid.cell == AFS_FAKE_ROOT_CELL_ID)
145 cm_data.fakeDirVersion = cm_data.rootSCachep->dataVersion;
147 // yj: first we make a call to cm_initLocalMountPoints
148 // to read all the local mount points from the registry
149 cm_InitLocalMountPoints();
151 // then we make a call to InitFakeRootDir to create
152 // a fake root directory based on the local mount points
153 cm_InitFakeRootDir();
154 // --- end of yj code
156 /* Start the registry monitor */
157 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceChangeNotifier,
158 NULL, 0, &lpid, "cm_FreelanceChangeNotifier");
159 osi_assertx(phandle != NULL, "cm_FreelanceChangeNotifier thread create failure");
160 thrd_CloseHandle(phandle);
162 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
163 NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
164 osi_assertx(phandle != NULL, "cm_FreelanceSymlinkChangeNotifier thread create failure");
165 thrd_CloseHandle(phandle);
168 /* yj: Initialization of the fake root directory */
169 /* to be called while holding freelance lock unless during init. */
170 void cm_InitFakeRootDir() {
175 // allocate space for the fake info
176 cm_dirHeader_t fakeDirHeader;
177 cm_dirEntry_t fakeEntry;
178 cm_pageHeader_t fakePageHeader;
180 // i'm going to calculate how much space is needed for
181 // this fake root directory. we have these rules:
182 // 1. there are cm_noLocalMountPoints number of entries
183 // 2. each page is CM_DIR_PAGESIZE in size
184 // 3. the first 13 chunks of the first page are used for
186 // 4. the first chunk of all subsequent pages are used
187 // for page header stuff
188 // 5. a max of CM_DIR_EPP entries are allowed per page
189 // 6. each entry takes 1 or more chunks, depending on
190 // the size of the mount point string, as determined
192 // 7. each chunk is CM_DIR_CHUNKSIZE bytes
194 int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
195 int curChunk = 13; // chunks 0 - 12 are used for header stuff
196 // of the first page in the directory
199 int curDirEntryInPage = 0;
203 /* Reserve 2 directory chunks for "." and ".." */
206 while (curDirEntry<cm_noLocalMountPoints) {
207 sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
208 if ((curChunk + sizeOfCurEntry >= CPP) ||
209 (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
211 curDirEntryInPage = 0;
214 curChunk += sizeOfCurEntry;
219 dirSize = (curPage+1) * CM_DIR_PAGESIZE;
220 cm_FakeRootDir = malloc(dirSize);
221 cm_fakeDirSize = dirSize;
223 // yj: when we get here, we've figured out how much memory we need and
224 // allocated the appropriate space for it. we now prceed to fill
225 // it up with entries.
228 curDirEntryInPage = 0;
231 // fields in the directory entry that are unused.
233 fakeEntry.length = 0;
235 fakeEntry.fid.unique = htonl(1);
237 // the first page is special, it uses fakeDirHeader instead of fakePageHeader
238 // we fill up the page with dirEntries that belong there and we make changes
239 // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
240 // filling up the dirEntries in this page, we copy the fakeDirHeader into
241 // the top of the page.
243 // init the freeBitmap array
245 fakeDirHeader.header.freeBitmap[i]=0;
247 fakeDirHeader.header.freeBitmap[0] = 0xff;
248 fakeDirHeader.header.freeBitmap[1] = 0x7f;
251 // we start counting at 13 because the 0th to 12th chunks are used for header
254 // stick the first 2 entries "." and ".." in
255 fakeEntry.fid.unique = htonl(1);
256 fakeEntry.fid.vnode = htonl(1);
257 strcpy(fakeEntry.name, ".");
258 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
259 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
260 curChunk++; curDirEntryInPage++;
261 strcpy(fakeEntry.name, "..");
262 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
263 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
264 curChunk++; curDirEntryInPage++;
266 // keep putting stuff into page 0 if
267 // 1. we're not done with all entries
268 // 2. we have less than CM_DIR_EPP entries in page 0
269 // 3. we're not out of chunks in page 0
271 while( (curDirEntry<cm_noLocalMountPoints) &&
272 (curDirEntryInPage < CM_DIR_EPP) &&
273 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
276 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
277 fakeEntry.fid.vnode = htonl(curDirEntry + 2);
278 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
280 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
281 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
284 for (i=0; i<noChunks; i++) {
285 t1 = (curChunk + i) / 8;
286 t2 = curChunk + i - (t1*8);
287 fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
292 // when we get here, we're done with filling in the entries for page 0
293 // copy in the header info
295 memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
299 // ok, page 0's done. Move on to the next page.
300 while (curDirEntry<cm_noLocalMountPoints) {
302 curChunk = 1; // the zeroth chunk is reserved for page header
303 curDirEntryInPage = 0;
304 for (i=0; i<8; i++) {
305 fakePageHeader.freeBitmap[i]=0;
307 fakePageHeader.freeCount = 0;
308 fakePageHeader.pgcount = 0;
309 fakePageHeader.tag = htons(1234);
311 // while we're on the same page...
312 while ( (curDirEntry<cm_noLocalMountPoints) &&
313 (curDirEntryInPage < CM_DIR_EPP) &&
314 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
316 // add an entry to this page
318 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
319 fakeEntry.fid.vnode=htonl(curDirEntry+2);
320 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
321 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
322 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
325 for (i=0; i<noChunks; i++) {
326 t1 = (curChunk + i) / 8;
327 t2 = curChunk + i - (t1*8);
328 fakePageHeader.freeBitmap[t1] |= (1 << t2);
332 memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
337 // we know the fakeDir is setup properly, so we claim that we have callback
338 osi_Log0(afsd_logp,"cm_InitFakeRootDir fakeDirCallback=1");
339 cm_fakeDirCallback=1;
341 // when we get here, we've set up everything! done!
344 int cm_FakeRootFid(cm_fid_t *fidp)
347 AFS_FAKE_ROOT_CELL_ID, /* root cell */
348 AFS_FAKE_ROOT_VOL_ID, /* root.afs ? */
353 /* called directly from ioctl */
354 /* called while not holding freelance lock */
355 int cm_noteLocalMountPointChange(void) {
356 lock_ObtainMutex(&cm_Freelance_Lock);
357 cm_data.fakeDirVersion++;
358 cm_localMountPointChangeFlag = 1;
359 lock_ReleaseMutex(&cm_Freelance_Lock);
363 int cm_getLocalMountPointChange() {
364 return cm_localMountPointChangeFlag;
367 int cm_clearLocalMountPointChange() {
368 cm_localMountPointChangeFlag = 0;
372 int cm_reInitLocalMountPoints() {
375 cm_scache_t *scp, **lscpp, *tscp;
377 osi_Log0(afsd_logp,"----- freelance reinitialization starts ----- ");
379 // first we invalidate all the SCPs that were created
380 // for the local mount points
382 osi_Log0(afsd_logp,"Invalidating local mount point scp... ");
384 cm_SetFid(&aFid, AFS_FAKE_ROOT_CELL_ID, AFS_FAKE_ROOT_VOL_ID, 1, 2);
386 lock_ObtainWrite(&cm_scacheLock);
387 lock_ObtainMutex(&cm_Freelance_Lock); /* always scache then freelance lock */
388 for (i=0; i<cm_noLocalMountPoints; i++) {
389 hash = CM_SCACHE_HASH(&aFid);
390 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
391 if (scp->fid.volume == aFid.volume &&
392 scp->fid.vnode == aFid.vnode &&
393 scp->fid.unique == aFid.unique
396 // mark the scp to be reused
397 cm_HoldSCacheNoLock(scp);
398 lock_ReleaseWrite(&cm_scacheLock);
399 lock_ObtainMutex(&scp->mx);
400 cm_DiscardSCache(scp);
401 lock_ReleaseMutex(&scp->mx);
402 cm_CallbackNotifyChange(scp);
403 lock_ObtainWrite(&cm_scacheLock);
404 cm_ReleaseSCacheNoLock(scp);
406 // take the scp out of the hash
407 for (lscpp = &cm_data.scacheHashTablep[hash], tscp = cm_data.scacheHashTablep[hash];
409 lscpp = &tscp->nextp, tscp = tscp->nextp) {
412 lock_ObtainMutex(&scp->mx);
413 scp->flags &= ~CM_SCACHEFLAG_INHASH;
414 lock_ReleaseMutex(&scp->mx);
420 cm_SetFid(&aFid, AFS_FAKE_ROOT_CELL_ID, AFS_FAKE_ROOT_VOL_ID, aFid.vnode + 1, 2);
422 lock_ReleaseWrite(&cm_scacheLock);
423 osi_Log0(afsd_logp,"\tall old scp cleared!");
425 // we must free the memory that was allocated in the prev
426 // cm_InitLocalMountPoints call
427 osi_Log0(afsd_logp,"Removing old localmountpoints... ");
428 free(cm_localMountPoints);
429 osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
431 // now re-init the localmountpoints
432 osi_Log0(afsd_logp,"Creating new localmountpoints... ");
433 cm_InitLocalMountPoints();
434 osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
436 // now we have to free the memory allocated in cm_initfakerootdir
437 osi_Log0(afsd_logp,"Removing old fakedir... ");
438 free(cm_FakeRootDir);
439 osi_Log0(afsd_logp,"\t\told fakedir removed!");
441 // then we re-create that dir
442 osi_Log0(afsd_logp,"Creating new fakedir... ");
443 cm_InitFakeRootDir();
444 osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
446 lock_ReleaseMutex(&cm_Freelance_Lock);
448 osi_Log0(afsd_logp,"----- freelance reinit complete -----");
453 // yj: open up the registry and read all the local mount
454 // points that are stored there. Part of the initialization
455 // process for the freelance client.
456 /* to be called while holding freelance lock unless during init. */
457 long cm_InitLocalMountPoints() {
462 cm_localMountPoint_t* aLocalMountPoint;
465 char rootCellName[256];
466 HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
467 DWORD dwType, dwSize;
468 DWORD dwMountPoints = 0;
470 DWORD dwSymlinks = 0;
471 FILETIME ftLastWriteTime;
473 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
474 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
476 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
477 &hkFreelance) == ERROR_SUCCESS) {
479 RegQueryInfoKey( hkFreelance,
482 NULL, /* lpReserved */
483 NULL, /* lpcSubKeys */
484 NULL, /* lpcMaxSubKeyLen */
485 NULL, /* lpcMaxClassLen */
486 &dwMountPoints, /* lpcValues */
487 NULL, /* lpcMaxValueNameLen */
488 NULL, /* lpcMaxValueLen */
489 NULL, /* lpcbSecurityDescriptor */
490 &ftLastWriteTime /* lpftLastWriteTime */
493 smb_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
495 if ( dwMountPoints == 0 ) {
496 rootCellName[0] = '.';
497 code = cm_GetRootCellName(&rootCellName[1]);
499 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
500 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
501 cm_FreelanceAddMount(".root", &rootCellName[1], "root.afs.", 1, NULL);
506 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
507 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
510 REG_OPTION_NON_VOLATILE,
511 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
513 &hkFreelanceSymlinks,
514 NULL) == ERROR_SUCCESS) {
516 RegQueryInfoKey( hkFreelanceSymlinks,
519 NULL, /* lpReserved */
520 NULL, /* lpcSubKeys */
521 NULL, /* lpcMaxSubKeyLen */
522 NULL, /* lpcMaxClassLen */
523 &dwSymlinks, /* lpcValues */
524 NULL, /* lpcMaxValueNameLen */
525 NULL, /* lpcMaxValueLen */
526 NULL, /* lpcbSecurityDescriptor */
527 NULL /* lpftLastWriteTime */
531 // get the number of entries there are from the first line
533 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
535 // create space to store the local mount points
536 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
537 aLocalMountPoint = cm_localMountPoints;
539 // now we read n lines and parse them into local mount points
540 // where n is the number of local mount points there are, as
542 // Each line in the ini file represents 1 local mount point and
543 // is in the format xxx#yyy:zzz, where xxx is the directory
544 // entry name, yyy is the cell name and zzz is the volume name.
545 // #yyy:zzz together make up the mount point.
546 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
547 TCHAR szValueName[16];
548 DWORD dwValueSize = 16;
549 dwSize = sizeof(line);
550 if (RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
551 &dwType, line, &dwSize))
553 afsi_log("RegEnumValue(hkFreelance) failed");
554 cm_noLocalMountPoints--;
558 afsi_log("Mountpoint[%d] = %s",dwIndex, line);
560 /* find the trailing dot; null terminate after it */
561 t2 = strrchr(line, '.');
565 for ( t=line;*t;t++ ) {
566 if ( !isprint(*t) ) {
567 afsi_log("error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
568 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
569 cm_noLocalMountPoints--;
574 // line is not empty, so let's parse it
575 t = strchr(line, '#');
577 t = strchr(line, '%');
578 // make sure that there is a '#' or '%' separator in the line
580 afsi_log("error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
581 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
582 cm_noLocalMountPoints--;
586 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
587 aLocalMountPoint->namep=malloc(t-line+1);
588 strncpy(aLocalMountPoint->namep, line, t-line);
589 aLocalMountPoint->namep[t-line] = '\0';
591 /* copy the mount point string */
592 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
593 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
594 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
596 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
597 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
598 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
603 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
604 TCHAR szValueName[16];
605 DWORD dwValueSize = 16;
606 dwSize = sizeof(line);
607 if (RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
608 &dwType, line, &dwSize))
610 afsi_log("RegEnumValue(hkFreelanceSymlinks) failed");
611 cm_noLocalMountPoints--;
615 afsi_log("Symlink[%d] = %s",dwIndex, line);
617 /* find the trailing dot; null terminate after it */
618 t2 = strrchr(line, '.');
622 for ( t=line;*t;t++ ) {
623 if ( !isprint(*t) ) {
624 afsi_log("error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
625 fprintf(stderr, "error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
626 cm_noLocalMountPoints--;
631 // line is not empty, so let's parse it
632 t = strchr(line, ':');
634 // make sure that there is a ':' separator in the line
636 afsi_log("error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
637 fprintf(stderr, "error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
638 cm_noLocalMountPoints--;
642 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
643 aLocalMountPoint->namep=malloc(t-line+1);
644 strncpy(aLocalMountPoint->namep, line, t-line);
645 aLocalMountPoint->namep[t-line] = '\0';
647 /* copy the symlink string */
648 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
649 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
650 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
652 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
653 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
654 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
659 if ( hkFreelanceSymlinks )
660 RegCloseKey( hkFreelanceSymlinks );
661 RegCloseKey(hkFreelance);
665 /* What follows is the old code to read freelance mount points
666 * out of a text file modified to copy the data into the registry
668 cm_GetConfigDir(hdir, sizeof(hdir));
669 strcat(hdir, AFS_FREELANCE_INI);
670 // open the ini file for reading
671 fp = fopen(hdir, "r");
673 /* look in the Windows directory where we used to store the file */
674 GetWindowsDirectory(hdir, sizeof(hdir));
676 strcat(hdir, AFS_FREELANCE_INI);
677 fp = fopen(hdir, "r");
680 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
681 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
684 REG_OPTION_NON_VOLATILE,
692 RegCloseKey(hkFreelance);
693 rootCellName[0] = '.';
694 code = cm_GetRootCellName(&rootCellName[1]);
696 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
697 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
698 cm_FreelanceAddMount(".root", &rootCellName[1], "root.afs.", 1, NULL);
703 // we successfully opened the file
704 osi_Log0(afsd_logp,"opened afs_freelance.ini");
706 // now we read the first line to see how many entries
708 fgets(line, sizeof(line), fp);
710 // if the line is empty at any point when we're reading
711 // we're screwed. report error and return.
713 afsi_log("error occurred while reading afs_freelance.ini");
714 fprintf(stderr, "error occurred while reading afs_freelance.ini");
718 // get the number of entries there are from the first line
720 cm_noLocalMountPoints = atoi(line);
722 if (cm_noLocalMountPoints > 0) {
723 // create space to store the local mount points
724 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
725 aLocalMountPoint = cm_localMountPoints;
728 // now we read n lines and parse them into local mount points
729 // where n is the number of local mount points there are, as
731 // Each line in the ini file represents 1 local mount point and
732 // is in the format xxx#yyy:zzz, where xxx is the directory
733 // entry name, yyy is the cell name and zzz is the volume name.
734 // #yyy:zzz together make up the mount point.
735 for (i=0; i<cm_noLocalMountPoints; i++) {
736 fgets(line, sizeof(line), fp);
737 // check that the line is not empty
739 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
740 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
744 /* find the trailing dot; null terminate after it */
745 t2 = strrchr(line, '.');
751 /* we are migrating to the registry */
752 sprintf(szIndex,"%d",dwIndex++);
754 dwSize = (DWORD)strlen(line) + 1;
755 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
758 // line is not empty, so let's parse it
759 t = strchr(line, '#');
761 t = strchr(line, '%');
762 // make sure that there is a '#' or '%' separator in the line
764 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
765 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
768 aLocalMountPoint->namep=malloc(t-line+1);
769 memcpy(aLocalMountPoint->namep, line, t-line);
770 *(aLocalMountPoint->namep + (t-line)) = 0;
772 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
773 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
774 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
776 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
777 aLocalMountPoint->namep,
778 aLocalMountPoint->mountPointStringp);
784 RegCloseKey(hkFreelance);
790 int cm_getNoLocalMountPoints() {
791 return cm_noLocalMountPoints;
794 long cm_FreelanceMountPointExists(char * filename, int prefix_ok)
800 HKEY hkFreelance = 0;
801 DWORD dwType, dwSize;
805 lock_ObtainMutex(&cm_Freelance_Lock);
807 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
808 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
810 KEY_READ|KEY_QUERY_VALUE,
811 &hkFreelance) == ERROR_SUCCESS)
813 RegQueryInfoKey( hkFreelance,
816 NULL, /* lpReserved */
817 NULL, /* lpcSubKeys */
818 NULL, /* lpcMaxSubKeyLen */
819 NULL, /* lpcMaxClassLen */
820 &dwMountPoints, /* lpcValues */
821 NULL, /* lpcMaxValueNameLen */
822 NULL, /* lpcMaxValueLen */
823 NULL, /* lpcbSecurityDescriptor */
824 NULL /* lpftLastWriteTime */
827 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
828 TCHAR szValueName[16];
829 DWORD dwValueSize = 16;
830 dwSize = sizeof(line);
831 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
832 &dwType, line, &dwSize);
834 cp=strchr(line, '#');
836 cp=strchr(line, '%');
837 memcpy(shortname, line, cp-line);
838 shortname[cp-line]=0;
840 if (!strcmp(shortname, filename)) {
845 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
846 TCHAR szValueName[16];
847 DWORD dwValueSize = 16;
848 dwSize = sizeof(line);
849 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
850 &dwType, line, &dwSize);
852 cp=strchr(line, '#');
854 cp=strchr(line, '%');
855 memcpy(shortname, line, cp-line);
856 shortname[cp-line]=0;
858 if (!stricmp(shortname, filename)) {
863 if (prefix_ok && strlen(shortname) - strlen(filename) == 1 && !strncmp(shortname, filename, strlen(filename))) {
868 RegCloseKey(hkFreelance);
871 lock_ReleaseMutex(&cm_Freelance_Lock);
876 long cm_FreelanceSymlinkExists(char * filename, int prefix_ok)
882 HKEY hkFreelance = 0;
883 DWORD dwType, dwSize;
887 lock_ObtainMutex(&cm_Freelance_Lock);
889 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
890 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
892 KEY_READ|KEY_QUERY_VALUE,
893 &hkFreelance) == ERROR_SUCCESS)
895 RegQueryInfoKey( hkFreelance,
898 NULL, /* lpReserved */
899 NULL, /* lpcSubKeys */
900 NULL, /* lpcMaxSubKeyLen */
901 NULL, /* lpcMaxClassLen */
902 &dwSymlinks, /* lpcValues */
903 NULL, /* lpcMaxValueNameLen */
904 NULL, /* lpcMaxValueLen */
905 NULL, /* lpcbSecurityDescriptor */
906 NULL /* lpftLastWriteTime */
909 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
910 TCHAR szValueName[16];
911 DWORD dwValueSize = 16;
912 dwSize = sizeof(line);
913 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
914 &dwType, line, &dwSize);
916 cp=strchr(line, ':');
917 memcpy(shortname, line, cp-line);
918 shortname[cp-line]=0;
920 if (!strcmp(shortname, filename)) {
925 if (prefix_ok && strlen(shortname) - strlen(filename) == 1 && !strncmp(shortname, filename, strlen(filename))) {
930 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
931 TCHAR szValueName[16];
932 DWORD dwValueSize = 16;
933 dwSize = sizeof(line);
934 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
935 &dwType, line, &dwSize);
937 cp=strchr(line, ':');
938 memcpy(shortname, line, cp-line);
939 shortname[cp-line]=0;
941 if (!stricmp(shortname, filename)) {
946 RegCloseKey(hkFreelance);
949 lock_ReleaseMutex(&cm_Freelance_Lock);
954 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
962 HKEY hkFreelance = 0;
963 DWORD dwType, dwSize;
967 /* before adding, verify the cell name; if it is not a valid cell,
968 don't add the mount point.
969 allow partial matches as a means of poor man's alias. */
970 /* major performance issue? */
971 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
972 osi_LogSaveString(afsd_logp,filename),
973 osi_LogSaveString(afsd_logp,cellname),
974 osi_LogSaveString(afsd_logp,volume),
977 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
980 if (cellname[0] == '.') {
981 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
984 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
988 if ( cm_FreelanceMountPointExists(filename, 0) ||
989 cm_FreelanceSymlinkExists(filename, 0) )
992 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
993 osi_LogSaveString(afsd_logp,cellname));
995 lock_ObtainMutex(&cm_Freelance_Lock);
997 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
998 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1000 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1001 &hkFreelance) == ERROR_SUCCESS) {
1003 RegQueryInfoKey( hkFreelance,
1005 NULL, /* lpcClass */
1006 NULL, /* lpReserved */
1007 NULL, /* lpcSubKeys */
1008 NULL, /* lpcMaxSubKeyLen */
1009 NULL, /* lpcMaxClassLen */
1010 &dwMountPoints, /* lpcValues */
1011 NULL, /* lpcMaxValueNameLen */
1012 NULL, /* lpcMaxValueLen */
1013 NULL, /* lpcbSecurityDescriptor */
1014 NULL /* lpftLastWriteTime */
1018 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
1020 sprintf(line, "%s#%s:%s", filename, fullname, volume);
1022 /* If we are adding a new value, there must be an unused name
1023 * within the range 0 to dwMountPoints
1025 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
1029 dwSize = sizeof(szMount);
1030 sprintf(szIndex, "%d", dwIndex);
1031 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
1032 /* found an unused value */
1034 dwSize = (DWORD)strlen(line) + 1;
1035 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1038 int len = (int)strlen(filename);
1039 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
1040 (szMount[len] == '%' || szMount[len] == '#')) {
1041 /* Replace the existing value */
1043 dwSize = (DWORD)strlen(line) + 1;
1044 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1049 RegCloseKey(hkFreelance);
1052 cm_GetConfigDir(hfile, sizeof(hfile));
1053 strcat(hfile, AFS_FREELANCE_INI);
1054 fp = fopen(hfile, "r+");
1056 return CM_ERROR_INVAL;
1057 fgets(line, sizeof(line), fp);
1060 fseek(fp, 0, SEEK_SET);
1061 fprintf(fp, "%d", n);
1062 fseek(fp, 0, SEEK_END);
1064 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1066 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1069 lock_ReleaseMutex(&cm_Freelance_Lock);
1071 /* cm_reInitLocalMountPoints(); */
1073 cm_SetFid(fidp, fidp->cell, fidp->volume, cm_noLocalMountPoints + 1, 1);
1074 cm_noteLocalMountPointChange();
1078 long cm_FreelanceRemoveMount(char *toremove)
1083 char shortname[200];
1084 char hfile[260], hfile2[260];
1087 HKEY hkFreelance = 0;
1088 DWORD dwType, dwSize;
1089 DWORD dwMountPoints;
1092 lock_ObtainMutex(&cm_Freelance_Lock);
1094 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1095 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1097 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1098 &hkFreelance) == ERROR_SUCCESS) {
1100 RegQueryInfoKey( hkFreelance,
1102 NULL, /* lpcClass */
1103 NULL, /* lpReserved */
1104 NULL, /* lpcSubKeys */
1105 NULL, /* lpcMaxSubKeyLen */
1106 NULL, /* lpcMaxClassLen */
1107 &dwMountPoints, /* lpcValues */
1108 NULL, /* lpcMaxValueNameLen */
1109 NULL, /* lpcMaxValueLen */
1110 NULL, /* lpcbSecurityDescriptor */
1111 NULL /* lpftLastWriteTime */
1114 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1115 TCHAR szValueName[16];
1116 DWORD dwValueSize = 16;
1117 dwSize = sizeof(line);
1118 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1119 &dwType, line, &dwSize);
1121 cp=strchr(line, '#');
1123 cp=strchr(line, '%');
1124 memcpy(shortname, line, cp-line);
1125 shortname[cp-line]=0;
1127 if (!strcmp(shortname, toremove)) {
1128 RegDeleteValue( hkFreelance, szValueName );
1132 RegCloseKey(hkFreelance);
1135 cm_GetConfigDir(hfile, sizeof(hfile));
1136 strcat(hfile, AFS_FREELANCE_INI);
1137 strcpy(hfile2, hfile);
1138 strcat(hfile2, "2");
1139 fp1=fopen(hfile, "r+");
1141 return CM_ERROR_INVAL;
1142 fp2=fopen(hfile2, "w+");
1145 return CM_ERROR_INVAL;
1148 fgets(line, sizeof(line), fp1);
1150 fprintf(fp2, "%d\n", n-1);
1152 for (i=0; i<n; i++) {
1153 fgets(line, sizeof(line), fp1);
1154 cp=strchr(line, '#');
1156 cp=strchr(line, '%');
1157 memcpy(shortname, line, cp-line);
1158 shortname[cp-line]=0;
1160 if (strcmp(shortname, toremove)==0) {
1171 return CM_ERROR_NOSUCHFILE;
1174 rename(hfile2, hfile);
1177 lock_ReleaseMutex(&cm_Freelance_Lock);
1178 cm_noteLocalMountPointChange();
1182 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1187 HKEY hkFreelanceSymlinks = 0;
1188 DWORD dwType, dwSize;
1192 /* before adding, verify the filename. If it is already in use, either as
1193 * as mount point or a cellname, do not permit the creation of the symlink.
1195 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1196 osi_LogSaveString(afsd_logp,filename),
1197 osi_LogSaveString(afsd_logp,destination));
1199 if ( filename[0] == '\0' || destination[0] == '\0' )
1200 return CM_ERROR_INVAL;
1203 if (filename[0] == '.') {
1204 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1205 if (stricmp(&filename[1],fullname) == 0)
1206 return CM_ERROR_EXISTS;
1208 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1209 if (stricmp(filename,fullname) == 0)
1210 return CM_ERROR_EXISTS;
1213 if ( cm_FreelanceMountPointExists(filename, 0) ||
1214 cm_FreelanceSymlinkExists(filename, 0) )
1215 return CM_ERROR_EXISTS;
1217 lock_ObtainMutex(&cm_Freelance_Lock);
1219 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1220 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1223 REG_OPTION_NON_VOLATILE,
1224 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1226 &hkFreelanceSymlinks,
1227 NULL) == ERROR_SUCCESS) {
1229 RegQueryInfoKey( hkFreelanceSymlinks,
1231 NULL, /* lpcClass */
1232 NULL, /* lpReserved */
1233 NULL, /* lpcSubKeys */
1234 NULL, /* lpcMaxSubKeyLen */
1235 NULL, /* lpcMaxClassLen */
1236 &dwSymlinks, /* lpcValues */
1237 NULL, /* lpcMaxValueNameLen */
1238 NULL, /* lpcMaxValueLen */
1239 NULL, /* lpcbSecurityDescriptor */
1240 NULL /* lpftLastWriteTime */
1243 sprintf(line, "%s:%s.", filename, destination);
1245 /* If we are adding a new value, there must be an unused name
1246 * within the range 0 to dwSymlinks
1248 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1252 dwSize = sizeof(szLink);
1253 sprintf(szIndex, "%d", dwIndex);
1254 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1255 /* found an unused value */
1257 dwSize = (DWORD)strlen(line) + 1;
1258 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1261 int len = (int)strlen(filename);
1262 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1263 /* Replace the existing value */
1265 dwSize = (DWORD)strlen(line) + 1;
1266 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1271 RegCloseKey(hkFreelanceSymlinks);
1273 lock_ReleaseMutex(&cm_Freelance_Lock);
1275 /* cm_reInitLocalMountPoints(); */
1277 cm_SetFid(fidp, fidp->cell, fidp->volume, cm_noLocalMountPoints + 1, 1);
1278 cm_noteLocalMountPointChange();
1282 long cm_FreelanceRemoveSymlink(char *toremove)
1286 char shortname[200];
1288 HKEY hkFreelanceSymlinks = 0;
1289 DWORD dwType, dwSize;
1293 lock_ObtainMutex(&cm_Freelance_Lock);
1295 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1296 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1298 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1299 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1301 RegQueryInfoKey( hkFreelanceSymlinks,
1303 NULL, /* lpcClass */
1304 NULL, /* lpReserved */
1305 NULL, /* lpcSubKeys */
1306 NULL, /* lpcMaxSubKeyLen */
1307 NULL, /* lpcMaxClassLen */
1308 &dwSymlinks, /* lpcValues */
1309 NULL, /* lpcMaxValueNameLen */
1310 NULL, /* lpcMaxValueLen */
1311 NULL, /* lpcbSecurityDescriptor */
1312 NULL /* lpftLastWriteTime */
1315 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1316 TCHAR szValueName[16];
1317 DWORD dwValueSize = 16;
1318 dwSize = sizeof(line);
1319 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1320 &dwType, line, &dwSize);
1322 cp=strchr(line, ':');
1323 memcpy(shortname, line, cp-line);
1324 shortname[cp-line]=0;
1326 if (!strcmp(shortname, toremove)) {
1327 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1331 RegCloseKey(hkFreelanceSymlinks);
1334 lock_ReleaseMutex(&cm_Freelance_Lock);
1335 cm_noteLocalMountPointChange();
1338 #endif /* AFS_FREELANCE_CLIENT */