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 an ini file
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_assert(phandle != NULL);
160 thrd_CloseHandle(phandle);
162 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
163 NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
164 osi_assert(phandle != NULL);
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)
346 fidp->cell = AFS_FAKE_ROOT_CELL_ID; /* root cell */
347 fidp->volume = 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 aFid.cell = AFS_FAKE_ROOT_CELL_ID;
385 aFid.volume=AFS_FAKE_ROOT_VOL_ID;
389 lock_ObtainWrite(&cm_scacheLock);
390 lock_ObtainMutex(&cm_Freelance_Lock); /* always scache then freelance lock */
391 for (i=0; i<cm_noLocalMountPoints; i++) {
392 hash = CM_SCACHE_HASH(&aFid);
393 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
394 if (scp->fid.volume == aFid.volume &&
395 scp->fid.vnode == aFid.vnode &&
396 scp->fid.unique == aFid.unique
399 // mark the scp to be reused
400 cm_HoldSCacheNoLock(scp);
401 lock_ReleaseWrite(&cm_scacheLock);
402 lock_ObtainMutex(&scp->mx);
403 cm_DiscardSCache(scp);
404 lock_ReleaseMutex(&scp->mx);
405 cm_CallbackNotifyChange(scp);
406 lock_ObtainWrite(&cm_scacheLock);
407 cm_ReleaseSCacheNoLock(scp);
409 // take the scp out of the hash
410 for (lscpp = &cm_data.hashTablep[hash], tscp = cm_data.hashTablep[hash];
412 lscpp = &tscp->nextp, tscp = tscp->nextp) {
415 lock_ObtainMutex(&scp->mx);
416 scp->flags &= ~CM_SCACHEFLAG_INHASH;
417 lock_ReleaseMutex(&scp->mx);
423 aFid.vnode = aFid.vnode + 1;
425 lock_ReleaseWrite(&cm_scacheLock);
426 osi_Log0(afsd_logp,"\tall old scp cleared!");
428 // we must free the memory that was allocated in the prev
429 // cm_InitLocalMountPoints call
430 osi_Log0(afsd_logp,"Removing old localmountpoints... ");
431 free(cm_localMountPoints);
432 osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
434 // now re-init the localmountpoints
435 osi_Log0(afsd_logp,"Creating new localmountpoints... ");
436 cm_InitLocalMountPoints();
437 osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
439 // now we have to free the memory allocated in cm_initfakerootdir
440 osi_Log0(afsd_logp,"Removing old fakedir... ");
441 free(cm_FakeRootDir);
442 osi_Log0(afsd_logp,"\t\told fakedir removed!");
444 // then we re-create that dir
445 osi_Log0(afsd_logp,"Creating new fakedir... ");
446 cm_InitFakeRootDir();
447 osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
449 lock_ReleaseMutex(&cm_Freelance_Lock);
451 osi_Log0(afsd_logp,"----- freelance reinit complete -----");
456 // yj: open up the ini file and read all the local mount
457 // points that are stored there. Part of the initialization
458 // process for the freelance client.
459 /* to be called while holding freelance lock unless during init. */
460 long cm_InitLocalMountPoints() {
465 cm_localMountPoint_t* aLocalMountPoint;
468 char rootCellName[256];
469 HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
470 DWORD dwType, dwSize;
471 DWORD dwMountPoints = 0;
473 DWORD dwSymlinks = 0;
474 FILETIME ftLastWriteTime;
476 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
477 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
479 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
480 &hkFreelance) == ERROR_SUCCESS) {
482 RegQueryInfoKey( hkFreelance,
485 NULL, /* lpReserved */
486 NULL, /* lpcSubKeys */
487 NULL, /* lpcMaxSubKeyLen */
488 NULL, /* lpcMaxClassLen */
489 &dwMountPoints, /* lpcValues */
490 NULL, /* lpcMaxValueNameLen */
491 NULL, /* lpcMaxValueLen */
492 NULL, /* lpcbSecurityDescriptor */
493 &ftLastWriteTime /* lpftLastWriteTime */
496 smb_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
498 if ( dwMountPoints == 0 ) {
499 rootCellName[0] = '.';
500 code = cm_GetRootCellName(&rootCellName[1]);
502 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
503 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
508 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
509 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
512 REG_OPTION_NON_VOLATILE,
513 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
515 &hkFreelanceSymlinks,
516 NULL) == ERROR_SUCCESS) {
518 RegQueryInfoKey( hkFreelanceSymlinks,
521 NULL, /* lpReserved */
522 NULL, /* lpcSubKeys */
523 NULL, /* lpcMaxSubKeyLen */
524 NULL, /* lpcMaxClassLen */
525 &dwSymlinks, /* lpcValues */
526 NULL, /* lpcMaxValueNameLen */
527 NULL, /* lpcMaxValueLen */
528 NULL, /* lpcbSecurityDescriptor */
529 NULL /* lpftLastWriteTime */
533 // get the number of entries there are from the first line
535 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
537 // create space to store the local mount points
538 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
539 aLocalMountPoint = cm_localMountPoints;
541 // now we read n lines and parse them into local mount points
542 // where n is the number of local mount points there are, as
544 // Each line in the ini file represents 1 local mount point and
545 // is in the format xxx#yyy:zzz, where xxx is the directory
546 // entry name, yyy is the cell name and zzz is the volume name.
547 // #yyy:zzz together make up the mount point.
548 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
549 TCHAR szValueName[16];
550 DWORD dwValueSize = 16;
551 dwSize = sizeof(line);
552 if (RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
553 &dwType, line, &dwSize))
555 afsi_log("RegEnumValue(hkFreelance) failed");
556 cm_noLocalMountPoints--;
560 afsi_log("Mountpoint[%d] = %s",dwIndex, line);
562 /* find the trailing dot; null terminate after it */
563 t2 = strrchr(line, '.');
567 for ( t=line;*t;t++ ) {
568 if ( !isprint(*t) ) {
569 afsi_log("error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
570 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
571 cm_noLocalMountPoints--;
576 // line is not empty, so let's parse it
577 t = strchr(line, '#');
579 t = strchr(line, '%');
580 // make sure that there is a '#' or '%' separator in the line
582 afsi_log("error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
583 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
584 cm_noLocalMountPoints--;
588 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
589 aLocalMountPoint->namep=malloc(t-line+1);
590 strncpy(aLocalMountPoint->namep, line, t-line);
591 aLocalMountPoint->namep[t-line] = '\0';
593 /* copy the mount point string */
594 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
595 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
596 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
598 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
599 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
600 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
605 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
606 TCHAR szValueName[16];
607 DWORD dwValueSize = 16;
608 dwSize = sizeof(line);
609 if (RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
610 &dwType, line, &dwSize))
612 afsi_log("RegEnumValue(hkFreelanceSymlinks) failed");
613 cm_noLocalMountPoints--;
617 afsi_log("Symlink[%d] = %s",dwIndex, line);
619 /* find the trailing dot; null terminate after it */
620 t2 = strrchr(line, '.');
624 for ( t=line;*t;t++ ) {
625 if ( !isprint(*t) ) {
626 afsi_log("error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
627 fprintf(stderr, "error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
628 cm_noLocalMountPoints--;
633 // line is not empty, so let's parse it
634 t = strchr(line, ':');
636 // make sure that there is a ':' separator in the line
638 afsi_log("error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
639 fprintf(stderr, "error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
640 cm_noLocalMountPoints--;
644 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
645 aLocalMountPoint->namep=malloc(t-line+1);
646 strncpy(aLocalMountPoint->namep, line, t-line);
647 aLocalMountPoint->namep[t-line] = '\0';
649 /* copy the symlink string */
650 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
651 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
652 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
654 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
655 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
656 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
661 if ( hkFreelanceSymlinks )
662 RegCloseKey( hkFreelanceSymlinks );
663 RegCloseKey(hkFreelance);
667 /* What follows is the old code to read freelance mount points
668 * out of a text file modified to copy the data into the registry
670 cm_GetConfigDir(hdir);
671 strcat(hdir, AFS_FREELANCE_INI);
672 // open the ini file for reading
673 fp = fopen(hdir, "r");
675 /* look in the Windows directory where we used to store the file */
676 GetWindowsDirectory(hdir, sizeof(hdir));
678 strcat(hdir, AFS_FREELANCE_INI);
679 fp = fopen(hdir, "r");
682 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
683 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
686 REG_OPTION_NON_VOLATILE,
694 RegCloseKey(hkFreelance);
695 rootCellName[0] = '.';
696 code = cm_GetRootCellName(&rootCellName[1]);
698 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
699 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
704 // we successfully opened the file
705 osi_Log0(afsd_logp,"opened afs_freelance.ini");
707 // now we read the first line to see how many entries
709 fgets(line, sizeof(line), fp);
711 // if the line is empty at any point when we're reading
712 // we're screwed. report error and return.
714 afsi_log("error occurred while reading afs_freelance.ini");
715 fprintf(stderr, "error occurred while reading afs_freelance.ini");
719 // get the number of entries there are from the first line
721 cm_noLocalMountPoints = atoi(line);
723 if (cm_noLocalMountPoints > 0) {
724 // create space to store the local mount points
725 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
726 aLocalMountPoint = cm_localMountPoints;
729 // now we read n lines and parse them into local mount points
730 // where n is the number of local mount points there are, as
732 // Each line in the ini file represents 1 local mount point and
733 // is in the format xxx#yyy:zzz, where xxx is the directory
734 // entry name, yyy is the cell name and zzz is the volume name.
735 // #yyy:zzz together make up the mount point.
736 for (i=0; i<cm_noLocalMountPoints; i++) {
737 fgets(line, sizeof(line), fp);
738 // check that the line is not empty
740 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
741 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
745 /* find the trailing dot; null terminate after it */
746 t2 = strrchr(line, '.');
752 /* we are migrating to the registry */
753 sprintf(szIndex,"%d",dwIndex++);
755 dwSize = (DWORD)strlen(line) + 1;
756 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
759 // line is not empty, so let's parse it
760 t = strchr(line, '#');
762 t = strchr(line, '%');
763 // make sure that there is a '#' or '%' separator in the line
765 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
766 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
769 aLocalMountPoint->namep=malloc(t-line+1);
770 memcpy(aLocalMountPoint->namep, line, t-line);
771 *(aLocalMountPoint->namep + (t-line)) = 0;
773 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
774 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
775 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
777 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
778 aLocalMountPoint->namep,
779 aLocalMountPoint->mountPointStringp);
785 RegCloseKey(hkFreelance);
791 int cm_getNoLocalMountPoints() {
792 return cm_noLocalMountPoints;
795 long cm_FreelanceMountPointExists(char * filename)
801 HKEY hkFreelance = 0;
802 DWORD dwType, dwSize;
806 lock_ObtainMutex(&cm_Freelance_Lock);
808 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
809 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
811 KEY_READ|KEY_QUERY_VALUE,
812 &hkFreelance) == ERROR_SUCCESS)
814 RegQueryInfoKey( hkFreelance,
817 NULL, /* lpReserved */
818 NULL, /* lpcSubKeys */
819 NULL, /* lpcMaxSubKeyLen */
820 NULL, /* lpcMaxClassLen */
821 &dwMountPoints, /* lpcValues */
822 NULL, /* lpcMaxValueNameLen */
823 NULL, /* lpcMaxValueLen */
824 NULL, /* lpcbSecurityDescriptor */
825 NULL /* lpftLastWriteTime */
828 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
829 TCHAR szValueName[16];
830 DWORD dwValueSize = 16;
831 dwSize = sizeof(line);
832 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
833 &dwType, line, &dwSize);
835 cp=strchr(line, '#');
837 cp=strchr(line, '%');
838 memcpy(shortname, line, cp-line);
839 shortname[cp-line]=0;
841 if (!strcmp(shortname, filename)) {
846 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
847 TCHAR szValueName[16];
848 DWORD dwValueSize = 16;
849 dwSize = sizeof(line);
850 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
851 &dwType, line, &dwSize);
853 cp=strchr(line, '#');
855 cp=strchr(line, '%');
856 memcpy(shortname, line, cp-line);
857 shortname[cp-line]=0;
859 if (!stricmp(shortname, filename)) {
864 RegCloseKey(hkFreelance);
867 lock_ReleaseMutex(&cm_Freelance_Lock);
872 long cm_FreelanceSymlinkExists(char * filename)
878 HKEY hkFreelance = 0;
879 DWORD dwType, dwSize;
883 lock_ObtainMutex(&cm_Freelance_Lock);
885 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
886 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
888 KEY_READ|KEY_QUERY_VALUE,
889 &hkFreelance) == ERROR_SUCCESS)
891 RegQueryInfoKey( hkFreelance,
894 NULL, /* lpReserved */
895 NULL, /* lpcSubKeys */
896 NULL, /* lpcMaxSubKeyLen */
897 NULL, /* lpcMaxClassLen */
898 &dwSymlinks, /* lpcValues */
899 NULL, /* lpcMaxValueNameLen */
900 NULL, /* lpcMaxValueLen */
901 NULL, /* lpcbSecurityDescriptor */
902 NULL /* lpftLastWriteTime */
905 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
906 TCHAR szValueName[16];
907 DWORD dwValueSize = 16;
908 dwSize = sizeof(line);
909 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
910 &dwType, line, &dwSize);
912 cp=strchr(line, ':');
913 memcpy(shortname, line, cp-line);
914 shortname[cp-line]=0;
916 if (!strcmp(shortname, filename)) {
921 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
922 TCHAR szValueName[16];
923 DWORD dwValueSize = 16;
924 dwSize = sizeof(line);
925 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
926 &dwType, line, &dwSize);
928 cp=strchr(line, ':');
929 memcpy(shortname, line, cp-line);
930 shortname[cp-line]=0;
932 if (!stricmp(shortname, filename)) {
937 RegCloseKey(hkFreelance);
940 lock_ReleaseMutex(&cm_Freelance_Lock);
945 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
953 HKEY hkFreelance = 0;
954 DWORD dwType, dwSize;
958 /* before adding, verify the cell name; if it is not a valid cell,
959 don't add the mount point.
960 allow partial matches as a means of poor man's alias. */
961 /* major performance issue? */
962 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
963 osi_LogSaveString(afsd_logp,filename),
964 osi_LogSaveString(afsd_logp,cellname),
965 osi_LogSaveString(afsd_logp,volume),
968 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
971 if (cellname[0] == '.') {
972 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
975 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
979 if ( cm_FreelanceMountPointExists(filename) ||
980 cm_FreelanceSymlinkExists(filename) )
983 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
984 osi_LogSaveString(afsd_logp,cellname));
986 lock_ObtainMutex(&cm_Freelance_Lock);
988 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
989 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
991 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
992 &hkFreelance) == ERROR_SUCCESS) {
994 RegQueryInfoKey( hkFreelance,
997 NULL, /* lpReserved */
998 NULL, /* lpcSubKeys */
999 NULL, /* lpcMaxSubKeyLen */
1000 NULL, /* lpcMaxClassLen */
1001 &dwMountPoints, /* lpcValues */
1002 NULL, /* lpcMaxValueNameLen */
1003 NULL, /* lpcMaxValueLen */
1004 NULL, /* lpcbSecurityDescriptor */
1005 NULL /* lpftLastWriteTime */
1009 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
1011 sprintf(line, "%s#%s:%s", filename, fullname, volume);
1013 /* If we are adding a new value, there must be an unused name
1014 * within the range 0 to dwMountPoints
1016 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
1020 dwSize = sizeof(szMount);
1021 sprintf(szIndex, "%d", dwIndex);
1022 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
1023 /* found an unused value */
1025 dwSize = (DWORD)strlen(line) + 1;
1026 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1029 int len = (int)strlen(filename);
1030 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
1031 (szMount[len] == '%' || szMount[len] == '#')) {
1032 /* Replace the existing value */
1034 dwSize = (DWORD)strlen(line) + 1;
1035 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1040 RegCloseKey(hkFreelance);
1043 cm_GetConfigDir(hfile);
1044 strcat(hfile, AFS_FREELANCE_INI);
1045 fp = fopen(hfile, "r+");
1047 return CM_ERROR_INVAL;
1048 fgets(line, sizeof(line), fp);
1051 fseek(fp, 0, SEEK_SET);
1052 fprintf(fp, "%d", n);
1053 fseek(fp, 0, SEEK_END);
1055 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1057 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1060 lock_ReleaseMutex(&cm_Freelance_Lock);
1062 /* cm_reInitLocalMountPoints(); */
1065 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1067 cm_noteLocalMountPointChange();
1071 long cm_FreelanceRemoveMount(char *toremove)
1076 char shortname[200];
1077 char hfile[120], hfile2[120];
1080 HKEY hkFreelance = 0;
1081 DWORD dwType, dwSize;
1082 DWORD dwMountPoints;
1085 lock_ObtainMutex(&cm_Freelance_Lock);
1087 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1088 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1090 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1091 &hkFreelance) == ERROR_SUCCESS) {
1093 RegQueryInfoKey( hkFreelance,
1095 NULL, /* lpcClass */
1096 NULL, /* lpReserved */
1097 NULL, /* lpcSubKeys */
1098 NULL, /* lpcMaxSubKeyLen */
1099 NULL, /* lpcMaxClassLen */
1100 &dwMountPoints, /* lpcValues */
1101 NULL, /* lpcMaxValueNameLen */
1102 NULL, /* lpcMaxValueLen */
1103 NULL, /* lpcbSecurityDescriptor */
1104 NULL /* lpftLastWriteTime */
1107 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1108 TCHAR szValueName[16];
1109 DWORD dwValueSize = 16;
1110 dwSize = sizeof(line);
1111 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1112 &dwType, line, &dwSize);
1114 cp=strchr(line, '#');
1116 cp=strchr(line, '%');
1117 memcpy(shortname, line, cp-line);
1118 shortname[cp-line]=0;
1120 if (!strcmp(shortname, toremove)) {
1121 RegDeleteValue( hkFreelance, szValueName );
1125 RegCloseKey(hkFreelance);
1128 cm_GetConfigDir(hfile);
1129 strcat(hfile, AFS_FREELANCE_INI);
1130 strcpy(hfile2, hfile);
1131 strcat(hfile2, "2");
1132 fp1=fopen(hfile, "r+");
1134 return CM_ERROR_INVAL;
1135 fp2=fopen(hfile2, "w+");
1138 return CM_ERROR_INVAL;
1141 fgets(line, sizeof(line), fp1);
1143 fprintf(fp2, "%d\n", n-1);
1145 for (i=0; i<n; i++) {
1146 fgets(line, sizeof(line), fp1);
1147 cp=strchr(line, '#');
1149 cp=strchr(line, '%');
1150 memcpy(shortname, line, cp-line);
1151 shortname[cp-line]=0;
1153 if (strcmp(shortname, toremove)==0) {
1164 return CM_ERROR_NOSUCHFILE;
1167 rename(hfile2, hfile);
1170 lock_ReleaseMutex(&cm_Freelance_Lock);
1171 cm_noteLocalMountPointChange();
1175 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1180 HKEY hkFreelanceSymlinks = 0;
1181 DWORD dwType, dwSize;
1185 /* before adding, verify the filename. If it is already in use, either as
1186 * as mount point or a cellname, do not permit the creation of the symlink.
1188 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1189 osi_LogSaveString(afsd_logp,filename),
1190 osi_LogSaveString(afsd_logp,destination));
1192 if ( filename[0] == '\0' || destination[0] == '\0' )
1196 if (filename[0] == '.') {
1197 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1198 if (stricmp(&filename[1],fullname) == 0)
1201 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1202 if (stricmp(filename,fullname) == 0)
1206 if ( cm_FreelanceMountPointExists(filename) ||
1207 cm_FreelanceSymlinkExists(filename) )
1210 lock_ObtainMutex(&cm_Freelance_Lock);
1212 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1213 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1216 REG_OPTION_NON_VOLATILE,
1217 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1219 &hkFreelanceSymlinks,
1220 NULL) == ERROR_SUCCESS) {
1222 RegQueryInfoKey( hkFreelanceSymlinks,
1224 NULL, /* lpcClass */
1225 NULL, /* lpReserved */
1226 NULL, /* lpcSubKeys */
1227 NULL, /* lpcMaxSubKeyLen */
1228 NULL, /* lpcMaxClassLen */
1229 &dwSymlinks, /* lpcValues */
1230 NULL, /* lpcMaxValueNameLen */
1231 NULL, /* lpcMaxValueLen */
1232 NULL, /* lpcbSecurityDescriptor */
1233 NULL /* lpftLastWriteTime */
1236 sprintf(line, "%s:%s.", filename, destination);
1238 /* If we are adding a new value, there must be an unused name
1239 * within the range 0 to dwSymlinks
1241 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1245 dwSize = sizeof(szLink);
1246 sprintf(szIndex, "%d", dwIndex);
1247 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1248 /* found an unused value */
1250 dwSize = (DWORD)strlen(line) + 1;
1251 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1254 int len = (int)strlen(filename);
1255 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1256 /* Replace the existing value */
1258 dwSize = (DWORD)strlen(line) + 1;
1259 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1264 RegCloseKey(hkFreelanceSymlinks);
1266 lock_ReleaseMutex(&cm_Freelance_Lock);
1268 /* cm_reInitLocalMountPoints(); */
1271 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1273 cm_noteLocalMountPointChange();
1277 long cm_FreelanceRemoveSymlink(char *toremove)
1281 char shortname[200];
1283 HKEY hkFreelanceSymlinks = 0;
1284 DWORD dwType, dwSize;
1288 lock_ObtainMutex(&cm_Freelance_Lock);
1290 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1291 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1293 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1294 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1296 RegQueryInfoKey( hkFreelanceSymlinks,
1298 NULL, /* lpcClass */
1299 NULL, /* lpReserved */
1300 NULL, /* lpcSubKeys */
1301 NULL, /* lpcMaxSubKeyLen */
1302 NULL, /* lpcMaxClassLen */
1303 &dwSymlinks, /* lpcValues */
1304 NULL, /* lpcMaxValueNameLen */
1305 NULL, /* lpcMaxValueLen */
1306 NULL, /* lpcbSecurityDescriptor */
1307 NULL /* lpftLastWriteTime */
1310 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1311 TCHAR szValueName[16];
1312 DWORD dwValueSize = 16;
1313 dwSize = sizeof(line);
1314 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1315 &dwType, line, &dwSize);
1317 cp=strchr(line, ':');
1318 memcpy(shortname, line, cp-line);
1319 shortname[cp-line]=0;
1321 if (!strcmp(shortname, toremove)) {
1322 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1326 RegCloseKey(hkFreelanceSymlinks);
1329 lock_ReleaseMutex(&cm_Freelance_Lock);
1330 cm_noteLocalMountPointChange();
1333 #endif /* AFS_FREELANCE_CLIENT */