17 #include <WINNT/afsreg.h>
19 #ifdef AFS_FREELANCE_CLIENT
20 #include "cm_freelance.h"
23 extern void afsi_log(char *pattern, ...);
25 int cm_noLocalMountPoints;
27 int cm_fakeDirCallback=0;
28 int cm_fakeGettingCallback=0;
29 cm_localMountPoint_t* cm_localMountPoints;
30 osi_mutex_t cm_Freelance_Lock;
31 int cm_localMountPointChangeFlag = 0;
32 int cm_freelanceEnabled = 0;
33 time_t FakeFreelanceModTime = 0x3b49f6e2;
35 static int freelance_ShutdownFlag = 0;
37 static HANDLE hFreelanceChangeEvent = 0;
38 static HANDLE hFreelanceSymlinkChangeEvent = 0;
41 void cm_InitFakeRootDir();
44 void cm_FreelanceChangeNotifier(void * parmp) {
47 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
48 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
51 &hkFreelance) == ERROR_SUCCESS) {
53 hFreelanceChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
54 if (hFreelanceChangeEvent == NULL) {
55 RegCloseKey(hkFreelance);
61 /* check hFreelanceChangeEvent to see if it is set.
62 * if so, call cm_noteLocalMountPointChange()
64 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
65 FALSE, /* bWatchSubtree */
66 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
67 hFreelanceChangeEvent, /* hEvent */
68 TRUE /* fAsynchronous */
70 RegCloseKey(hkFreelance);
71 CloseHandle(hFreelanceChangeEvent);
72 hFreelanceChangeEvent = 0;
76 if (WaitForSingleObject(hFreelanceChangeEvent, INFINITE) == WAIT_OBJECT_0)
78 if (freelance_ShutdownFlag == 1) {
79 RegCloseKey(hkFreelance);
80 CloseHandle(hFreelanceChangeEvent);
81 hFreelanceChangeEvent = 0;
84 cm_noteLocalMountPointChange();
89 void cm_FreelanceSymlinkChangeNotifier(void * parmp) {
92 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
93 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
96 &hkFreelance) == ERROR_SUCCESS) {
98 hFreelanceSymlinkChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
99 if (hFreelanceSymlinkChangeEvent == NULL) {
100 RegCloseKey(hkFreelance);
106 /* check hFreelanceSymlinkChangeEvent to see if it is set.
107 * if so, call cm_noteLocalMountPointSymlinkChange()
109 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
110 FALSE, /* bWatchSubtree */
111 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
112 hFreelanceSymlinkChangeEvent, /* hEvent */
113 TRUE /* fAsynchronous */
114 ) != ERROR_SUCCESS) {
115 RegCloseKey(hkFreelance);
116 CloseHandle(hFreelanceSymlinkChangeEvent);
117 hFreelanceSymlinkChangeEvent = 0;
121 if (WaitForSingleObject(hFreelanceSymlinkChangeEvent, INFINITE) == WAIT_OBJECT_0)
123 if (freelance_ShutdownFlag == 1) {
124 RegCloseKey(hkFreelance);
125 CloseHandle(hFreelanceSymlinkChangeEvent);
126 hFreelanceSymlinkChangeEvent = 0;
129 cm_noteLocalMountPointChange();
136 cm_FreelanceShutdown(void)
138 freelance_ShutdownFlag = 1;
140 if (hFreelanceChangeEvent != 0)
141 thrd_SetEvent(hFreelanceChangeEvent);
142 if (hFreelanceSymlinkChangeEvent != 0)
143 thrd_SetEvent(hFreelanceSymlinkChangeEvent);
147 void cm_InitFreelance() {
153 lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock");
155 // make sure we sync the data version to the cached root scache_t
156 if (cm_data.rootSCachep && cm_data.rootSCachep->fid.cell == AFS_FAKE_ROOT_CELL_ID)
157 cm_data.fakeDirVersion = cm_data.rootSCachep->dataVersion;
159 // yj: first we make a call to cm_initLocalMountPoints
160 // to read all the local mount points from an ini file
161 cm_InitLocalMountPoints();
163 // then we make a call to InitFakeRootDir to create
164 // a fake root directory based on the local mount points
165 cm_InitFakeRootDir();
166 // --- end of yj code
169 /* Start the registry monitor */
170 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceChangeNotifier,
171 NULL, 0, &lpid, "cm_FreelanceChangeNotifier");
172 osi_assert(phandle != NULL);
173 thrd_CloseHandle(phandle);
175 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
176 NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
177 osi_assert(phandle != NULL);
178 thrd_CloseHandle(phandle);
182 /* yj: Initialization of the fake root directory */
183 /* to be called while holding freelance lock unless during init. */
184 void cm_InitFakeRootDir() {
189 // allocate space for the fake info
190 cm_dirHeader_t fakeDirHeader;
191 cm_dirEntry_t fakeEntry;
192 cm_pageHeader_t fakePageHeader;
194 // i'm going to calculate how much space is needed for
195 // this fake root directory. we have these rules:
196 // 1. there are cm_noLocalMountPoints number of entries
197 // 2. each page is CM_DIR_PAGESIZE in size
198 // 3. the first 13 chunks of the first page are used for
200 // 4. the first chunk of all subsequent pages are used
201 // for page header stuff
202 // 5. a max of CM_DIR_EPP entries are allowed per page
203 // 6. each entry takes 1 or more chunks, depending on
204 // the size of the mount point string, as determined
206 // 7. each chunk is CM_DIR_CHUNKSIZE bytes
208 int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
209 int curChunk = 13; // chunks 0 - 12 are used for header stuff
210 // of the first page in the directory
213 int curDirEntryInPage = 0;
217 /* Reserve 2 directory chunks for "." and ".." */
220 while (curDirEntry!=cm_noLocalMountPoints) {
221 sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
222 if ((curChunk + sizeOfCurEntry >= CPP) ||
223 (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
225 curDirEntryInPage = 0;
228 curChunk += sizeOfCurEntry;
233 dirSize = (curPage+1) * CM_DIR_PAGESIZE;
234 cm_FakeRootDir = malloc(dirSize);
235 cm_fakeDirSize = dirSize;
237 // yj: when we get here, we've figured out how much memory we need and
238 // allocated the appropriate space for it. we now prceed to fill
239 // it up with entries.
242 curDirEntryInPage = 0;
245 // fields in the directory entry that are unused.
247 fakeEntry.length = 0;
249 fakeEntry.fid.unique = htonl(1);
251 // the first page is special, it uses fakeDirHeader instead of fakePageHeader
252 // we fill up the page with dirEntries that belong there and we make changes
253 // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
254 // filling up the dirEntries in this page, we copy the fakeDirHeader into
255 // the top of the page.
257 // init the freeBitmap array
259 fakeDirHeader.header.freeBitmap[i]=0;
261 fakeDirHeader.header.freeBitmap[0] = 0xff;
262 fakeDirHeader.header.freeBitmap[1] = 0x7f;
265 // we start counting at 13 because the 0th to 12th chunks are used for header
268 // stick the first 2 entries "." and ".." in
269 fakeEntry.fid.unique = htonl(1);
270 fakeEntry.fid.vnode = htonl(1);
271 strcpy(fakeEntry.name, ".");
272 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
273 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
274 curChunk++; curDirEntryInPage++;
275 strcpy(fakeEntry.name, "..");
276 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
277 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
278 curChunk++; curDirEntryInPage++;
280 // keep putting stuff into page 0 if
281 // 1. we're not done with all entries
282 // 2. we have less than CM_DIR_EPP entries in page 0
283 // 3. we're not out of chunks in page 0
285 while( (curDirEntry!=cm_noLocalMountPoints) &&
286 (curDirEntryInPage < CM_DIR_EPP) &&
287 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
290 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
291 fakeEntry.fid.vnode = htonl(curDirEntry + 2);
292 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
294 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
295 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
298 for (i=0; i<noChunks; i++) {
299 t1 = (curChunk + i) / 8;
300 t2 = curChunk + i - (t1*8);
301 fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
306 // when we get here, we're done with filling in the entries for page 0
307 // copy in the header info
309 memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
313 // ok, page 0's done. Move on to the next page.
314 while (curDirEntry!=cm_noLocalMountPoints) {
316 curChunk = 1; // the zeroth chunk is reserved for page header
317 curDirEntryInPage = 0;
318 for (i=0; i<8; i++) {
319 fakePageHeader.freeBitmap[i]=0;
321 fakePageHeader.freeCount = 0;
322 fakePageHeader.pgcount = 0;
323 fakePageHeader.tag = htons(1234);
325 // while we're on the same page...
326 while ( (curDirEntry!=cm_noLocalMountPoints) &&
327 (curDirEntryInPage < CM_DIR_EPP) &&
328 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
330 // add an entry to this page
332 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
333 fakeEntry.fid.vnode=htonl(curDirEntry+2);
334 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
335 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
336 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
339 for (i=0; i<noChunks; i++) {
340 t1 = (curChunk + i) / 8;
341 t2 = curChunk + i - (t1*8);
342 fakePageHeader.freeBitmap[t1] |= (1 << t2);
346 memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
351 // we know the fakeDir is setup properly, so we claim that we have callback
352 osi_Log0(afsd_logp,"cm_InitFakeRootDir fakeDirCallback=1");
353 cm_fakeDirCallback=1;
355 // when we get here, we've set up everything! done!
358 int cm_FakeRootFid(cm_fid_t *fidp)
360 fidp->cell = AFS_FAKE_ROOT_CELL_ID; /* root cell */
361 fidp->volume = AFS_FAKE_ROOT_VOL_ID; /* root.afs ? */
367 /* called directly from ioctl */
368 /* called while not holding freelance lock */
369 int cm_noteLocalMountPointChange(void) {
370 lock_ObtainMutex(&cm_Freelance_Lock);
371 cm_data.fakeDirVersion++;
372 cm_localMountPointChangeFlag = 1;
373 lock_ReleaseMutex(&cm_Freelance_Lock);
377 int cm_getLocalMountPointChange() {
378 return cm_localMountPointChangeFlag;
381 int cm_clearLocalMountPointChange() {
382 cm_localMountPointChangeFlag = 0;
386 int cm_reInitLocalMountPoints() {
389 cm_scache_t *scp, **lscpp, *tscp;
391 osi_Log0(afsd_logp,"----- freelance reinitialization starts ----- ");
393 // first we invalidate all the SCPs that were created
394 // for the local mount points
396 osi_Log0(afsd_logp,"Invalidating local mount point scp... ");
398 aFid.cell = AFS_FAKE_ROOT_CELL_ID;
399 aFid.volume=AFS_FAKE_ROOT_VOL_ID;
403 lock_ObtainWrite(&cm_scacheLock);
404 lock_ObtainMutex(&cm_Freelance_Lock); /* always scache then freelance lock */
405 for (i=0; i<cm_noLocalMountPoints; i++) {
406 hash = CM_SCACHE_HASH(&aFid);
407 for (scp=cm_data.hashTablep[hash]; scp; scp=scp->nextp) {
408 if (scp->fid.volume == aFid.volume &&
409 scp->fid.vnode == aFid.vnode &&
410 scp->fid.unique == aFid.unique
413 // mark the scp to be reused
414 cm_HoldSCacheNoLock(scp);
415 lock_ReleaseWrite(&cm_scacheLock);
416 lock_ObtainMutex(&scp->mx);
417 cm_DiscardSCache(scp);
418 lock_ReleaseMutex(&scp->mx);
419 cm_CallbackNotifyChange(scp);
420 lock_ObtainWrite(&cm_scacheLock);
421 cm_ReleaseSCacheNoLock(scp);
423 // take the scp out of the hash
424 for (lscpp = &cm_data.hashTablep[hash], tscp = cm_data.hashTablep[hash];
426 lscpp = &tscp->nextp, tscp = tscp->nextp) {
429 scp->flags &= ~CM_SCACHEFLAG_INHASH;
435 aFid.vnode = aFid.vnode + 1;
437 lock_ReleaseWrite(&cm_scacheLock);
438 osi_Log0(afsd_logp,"\tall old scp cleared!");
440 // we must free the memory that was allocated in the prev
441 // cm_InitLocalMountPoints call
442 osi_Log0(afsd_logp,"Removing old localmountpoints... ");
443 free(cm_localMountPoints);
444 osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
446 // now re-init the localmountpoints
447 osi_Log0(afsd_logp,"Creating new localmountpoints... ");
448 cm_InitLocalMountPoints();
449 osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
451 // now we have to free the memory allocated in cm_initfakerootdir
452 osi_Log0(afsd_logp,"Removing old fakedir... ");
453 free(cm_FakeRootDir);
454 osi_Log0(afsd_logp,"\t\told fakedir removed!");
456 // then we re-create that dir
457 osi_Log0(afsd_logp,"Creating new fakedir... ");
458 cm_InitFakeRootDir();
459 osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
461 lock_ReleaseMutex(&cm_Freelance_Lock);
463 osi_Log0(afsd_logp,"----- freelance reinit complete -----");
468 // yj: open up the ini file and read all the local mount
469 // points that are stored there. Part of the initialization
470 // process for the freelance client.
471 /* to be called while holding freelance lock unless during init. */
472 long cm_InitLocalMountPoints() {
477 cm_localMountPoint_t* aLocalMountPoint;
480 char rootCellName[256];
482 HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
483 DWORD dwType, dwSize;
484 DWORD dwMountPoints = 0;
486 DWORD dwSymlinks = 0;
487 FILETIME ftLastWriteTime;
491 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
492 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
494 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
495 &hkFreelance) == ERROR_SUCCESS) {
497 RegQueryInfoKey( hkFreelance,
500 NULL, /* lpReserved */
501 NULL, /* lpcSubKeys */
502 NULL, /* lpcMaxSubKeyLen */
503 NULL, /* lpcMaxClassLen */
504 &dwMountPoints, /* lpcValues */
505 NULL, /* lpcMaxValueNameLen */
506 NULL, /* lpcMaxValueLen */
507 NULL, /* lpcbSecurityDescriptor */
508 &ftLastWriteTime /* lpftLastWriteTime */
511 smb_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
513 if ( dwMountPoints == 0 ) {
514 rootCellName[0] = '.';
515 code = cm_GetRootCellName(&rootCellName[1]);
517 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
518 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
523 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
524 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
527 REG_OPTION_NON_VOLATILE,
528 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
530 &hkFreelanceSymlinks,
531 NULL) == ERROR_SUCCESS) {
533 RegQueryInfoKey( hkFreelanceSymlinks,
536 NULL, /* lpReserved */
537 NULL, /* lpcSubKeys */
538 NULL, /* lpcMaxSubKeyLen */
539 NULL, /* lpcMaxClassLen */
540 &dwSymlinks, /* lpcValues */
541 NULL, /* lpcMaxValueNameLen */
542 NULL, /* lpcMaxValueLen */
543 NULL, /* lpcbSecurityDescriptor */
544 NULL /* lpftLastWriteTime */
548 // get the number of entries there are from the first line
550 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
552 // create space to store the local mount points
553 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
554 aLocalMountPoint = cm_localMountPoints;
556 // now we read n lines and parse them into local mount points
557 // where n is the number of local mount points there are, as
559 // Each line in the ini file represents 1 local mount point and
560 // is in the format xxx#yyy:zzz, where xxx is the directory
561 // entry name, yyy is the cell name and zzz is the volume name.
562 // #yyy:zzz together make up the mount point.
563 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
564 TCHAR szValueName[16];
565 DWORD dwValueSize = 16;
566 dwSize = sizeof(line);
567 if (RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
568 &dwType, line, &dwSize))
570 afsi_log("RegEnumValue(hkFreelance) failed");
571 cm_noLocalMountPoints--;
575 afsi_log("Mountpoint[%d] = %s",dwIndex, line);
577 /* find the trailing dot; null terminate after it */
578 t2 = strrchr(line, '.');
582 for ( t=line;*t;t++ ) {
583 if ( !isprint(*t) ) {
584 afsi_log("error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
585 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
586 cm_noLocalMountPoints--;
591 // line is not empty, so let's parse it
592 t = strchr(line, '#');
594 t = strchr(line, '%');
595 // make sure that there is a '#' or '%' separator in the line
597 afsi_log("error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
598 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
599 cm_noLocalMountPoints--;
603 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
604 aLocalMountPoint->namep=malloc(t-line+1);
605 strncpy(aLocalMountPoint->namep, line, t-line);
606 aLocalMountPoint->namep[t-line] = '\0';
608 /* copy the mount point string */
609 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
610 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
611 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
613 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
614 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
615 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
620 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
621 TCHAR szValueName[16];
622 DWORD dwValueSize = 16;
623 dwSize = sizeof(line);
624 if (RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
625 &dwType, line, &dwSize))
627 afsi_log("RegEnumValue(hkFreelanceSymlinks) failed");
628 cm_noLocalMountPoints--;
632 afsi_log("Symlink[%d] = %s",dwIndex, line);
634 /* find the trailing dot; null terminate after it */
635 t2 = strrchr(line, '.');
639 for ( t=line;*t;t++ ) {
640 if ( !isprint(*t) ) {
641 afsi_log("error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
642 fprintf(stderr, "error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
643 cm_noLocalMountPoints--;
648 // line is not empty, so let's parse it
649 t = strchr(line, ':');
651 // make sure that there is a ':' separator in the line
653 afsi_log("error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
654 fprintf(stderr, "error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
655 cm_noLocalMountPoints--;
659 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
660 aLocalMountPoint->namep=malloc(t-line+1);
661 strncpy(aLocalMountPoint->namep, line, t-line);
662 aLocalMountPoint->namep[t-line] = '\0';
664 /* copy the symlink string */
665 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
666 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
667 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
669 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
670 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
671 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
676 if ( hkFreelanceSymlinks )
677 RegCloseKey( hkFreelanceSymlinks );
678 RegCloseKey(hkFreelance);
683 /* What follows is the old code to read freelance mount points
684 * out of a text file modified to copy the data into the registry
686 cm_GetConfigDir(hdir);
687 strcat(hdir, AFS_FREELANCE_INI);
688 // open the ini file for reading
689 fp = fopen(hdir, "r");
691 /* look in the Windows directory where we used to store the file */
692 GetWindowsDirectory(hdir, sizeof(hdir));
694 strcat(hdir, AFS_FREELANCE_INI);
695 fp = fopen(hdir, "r");
699 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
700 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
703 REG_OPTION_NON_VOLATILE,
713 RegCloseKey(hkFreelance);
715 rootCellName[0] = '.';
716 code = cm_GetRootCellName(&rootCellName[1]);
718 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
719 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
724 // we successfully opened the file
725 osi_Log0(afsd_logp,"opened afs_freelance.ini");
727 // now we read the first line to see how many entries
729 fgets(line, sizeof(line), fp);
731 // if the line is empty at any point when we're reading
732 // we're screwed. report error and return.
734 afsi_log("error occurred while reading afs_freelance.ini");
735 fprintf(stderr, "error occurred while reading afs_freelance.ini");
739 // get the number of entries there are from the first line
741 cm_noLocalMountPoints = atoi(line);
743 if (cm_noLocalMountPoints > 0) {
744 // create space to store the local mount points
745 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
746 aLocalMountPoint = cm_localMountPoints;
749 // now we read n lines and parse them into local mount points
750 // where n is the number of local mount points there are, as
752 // Each line in the ini file represents 1 local mount point and
753 // is in the format xxx#yyy:zzz, where xxx is the directory
754 // entry name, yyy is the cell name and zzz is the volume name.
755 // #yyy:zzz together make up the mount point.
756 for (i=0; i<cm_noLocalMountPoints; i++) {
757 fgets(line, sizeof(line), fp);
758 // check that the line is not empty
760 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
761 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
765 /* find the trailing dot; null terminate after it */
766 t2 = strrchr(line, '.');
773 /* we are migrating to the registry */
774 sprintf(szIndex,"%d",dwIndex++);
776 dwSize = strlen(line) + 1;
777 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
781 // line is not empty, so let's parse it
782 t = strchr(line, '#');
784 t = strchr(line, '%');
785 // make sure that there is a '#' or '%' separator in the line
787 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
788 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
791 aLocalMountPoint->namep=malloc(t-line+1);
792 memcpy(aLocalMountPoint->namep, line, t-line);
793 *(aLocalMountPoint->namep + (t-line)) = 0;
795 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
796 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
797 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
799 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
800 aLocalMountPoint->namep,
801 aLocalMountPoint->mountPointStringp);
808 RegCloseKey(hkFreelance);
815 int cm_getNoLocalMountPoints() {
816 return cm_noLocalMountPoints;
820 long cm_FreelanceMountPointExists(char * filename)
826 HKEY hkFreelance = 0;
827 DWORD dwType, dwSize;
831 lock_ObtainMutex(&cm_Freelance_Lock);
833 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
834 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
836 KEY_READ|KEY_QUERY_VALUE,
837 &hkFreelance) == ERROR_SUCCESS)
839 RegQueryInfoKey( hkFreelance,
842 NULL, /* lpReserved */
843 NULL, /* lpcSubKeys */
844 NULL, /* lpcMaxSubKeyLen */
845 NULL, /* lpcMaxClassLen */
846 &dwMountPoints, /* lpcValues */
847 NULL, /* lpcMaxValueNameLen */
848 NULL, /* lpcMaxValueLen */
849 NULL, /* lpcbSecurityDescriptor */
850 NULL /* lpftLastWriteTime */
853 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
854 TCHAR szValueName[16];
855 DWORD dwValueSize = 16;
856 dwSize = sizeof(line);
857 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
858 &dwType, line, &dwSize);
860 cp=strchr(line, '#');
862 cp=strchr(line, '%');
863 memcpy(shortname, line, cp-line);
864 shortname[cp-line]=0;
866 if (!strcmp(shortname, filename)) {
871 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
872 TCHAR szValueName[16];
873 DWORD dwValueSize = 16;
874 dwSize = sizeof(line);
875 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
876 &dwType, line, &dwSize);
878 cp=strchr(line, '#');
880 cp=strchr(line, '%');
881 memcpy(shortname, line, cp-line);
882 shortname[cp-line]=0;
884 if (!stricmp(shortname, filename)) {
889 RegCloseKey(hkFreelance);
892 lock_ReleaseMutex(&cm_Freelance_Lock);
897 long cm_FreelanceSymlinkExists(char * filename)
903 HKEY hkFreelance = 0;
904 DWORD dwType, dwSize;
908 lock_ObtainMutex(&cm_Freelance_Lock);
910 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
911 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
913 KEY_READ|KEY_QUERY_VALUE,
914 &hkFreelance) == ERROR_SUCCESS)
916 RegQueryInfoKey( hkFreelance,
919 NULL, /* lpReserved */
920 NULL, /* lpcSubKeys */
921 NULL, /* lpcMaxSubKeyLen */
922 NULL, /* lpcMaxClassLen */
923 &dwSymlinks, /* lpcValues */
924 NULL, /* lpcMaxValueNameLen */
925 NULL, /* lpcMaxValueLen */
926 NULL, /* lpcbSecurityDescriptor */
927 NULL /* lpftLastWriteTime */
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 (!strcmp(shortname, filename)) {
946 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
947 TCHAR szValueName[16];
948 DWORD dwValueSize = 16;
949 dwSize = sizeof(line);
950 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
951 &dwType, line, &dwSize);
953 cp=strchr(line, ':');
954 memcpy(shortname, line, cp-line);
955 shortname[cp-line]=0;
957 if (!stricmp(shortname, filename)) {
962 RegCloseKey(hkFreelance);
965 lock_ReleaseMutex(&cm_Freelance_Lock);
971 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
980 HKEY hkFreelance = 0;
981 DWORD dwType, dwSize;
986 /* before adding, verify the cell name; if it is not a valid cell,
987 don't add the mount point.
988 allow partial matches as a means of poor man's alias. */
989 /* major performance issue? */
990 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
991 osi_LogSaveString(afsd_logp,filename),
992 osi_LogSaveString(afsd_logp,cellname),
993 osi_LogSaveString(afsd_logp,volume),
996 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
999 if (cellname[0] == '.') {
1000 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
1003 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
1008 if ( cm_FreelanceMountPointExists(filename) ||
1009 cm_FreelanceSymlinkExists(filename) )
1013 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
1014 osi_LogSaveString(afsd_logp,cellname));
1016 lock_ObtainMutex(&cm_Freelance_Lock);
1019 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1020 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1022 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1023 &hkFreelance) == ERROR_SUCCESS) {
1025 RegQueryInfoKey( hkFreelance,
1027 NULL, /* lpcClass */
1028 NULL, /* lpReserved */
1029 NULL, /* lpcSubKeys */
1030 NULL, /* lpcMaxSubKeyLen */
1031 NULL, /* lpcMaxClassLen */
1032 &dwMountPoints, /* lpcValues */
1033 NULL, /* lpcMaxValueNameLen */
1034 NULL, /* lpcMaxValueLen */
1035 NULL, /* lpcbSecurityDescriptor */
1036 NULL /* lpftLastWriteTime */
1040 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
1042 sprintf(line, "%s#%s:%s", filename, fullname, volume);
1044 /* If we are adding a new value, there must be an unused name
1045 * within the range 0 to dwMountPoints
1047 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
1051 dwSize = sizeof(szMount);
1052 sprintf(szIndex, "%d", dwIndex);
1053 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
1054 /* found an unused value */
1056 dwSize = strlen(line) + 1;
1057 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1060 int len = strlen(filename);
1061 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
1062 (szMount[len] == '%' || szMount[len] == '#')) {
1063 /* Replace the existing value */
1065 dwSize = strlen(line) + 1;
1066 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1071 RegCloseKey(hkFreelance);
1075 cm_GetConfigDir(hfile);
1076 strcat(hfile, AFS_FREELANCE_INI);
1077 fp = fopen(hfile, "r+");
1079 return CM_ERROR_INVAL;
1080 fgets(line, sizeof(line), fp);
1083 fseek(fp, 0, SEEK_SET);
1084 fprintf(fp, "%d", n);
1085 fseek(fp, 0, SEEK_END);
1087 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1089 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1092 lock_ReleaseMutex(&cm_Freelance_Lock);
1094 /* cm_reInitLocalMountPoints(); */
1097 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1099 cm_noteLocalMountPointChange();
1103 long cm_FreelanceRemoveMount(char *toremove)
1108 char shortname[200];
1109 char hfile[120], hfile2[120];
1113 HKEY hkFreelance = 0;
1114 DWORD dwType, dwSize;
1115 DWORD dwMountPoints;
1119 lock_ObtainMutex(&cm_Freelance_Lock);
1122 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1123 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1125 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1126 &hkFreelance) == ERROR_SUCCESS) {
1128 RegQueryInfoKey( hkFreelance,
1130 NULL, /* lpcClass */
1131 NULL, /* lpReserved */
1132 NULL, /* lpcSubKeys */
1133 NULL, /* lpcMaxSubKeyLen */
1134 NULL, /* lpcMaxClassLen */
1135 &dwMountPoints, /* lpcValues */
1136 NULL, /* lpcMaxValueNameLen */
1137 NULL, /* lpcMaxValueLen */
1138 NULL, /* lpcbSecurityDescriptor */
1139 NULL /* lpftLastWriteTime */
1142 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1143 TCHAR szValueName[16];
1144 DWORD dwValueSize = 16;
1145 dwSize = sizeof(line);
1146 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1147 &dwType, line, &dwSize);
1149 cp=strchr(line, '#');
1151 cp=strchr(line, '%');
1152 memcpy(shortname, line, cp-line);
1153 shortname[cp-line]=0;
1155 if (!strcmp(shortname, toremove)) {
1156 RegDeleteValue( hkFreelance, szValueName );
1160 RegCloseKey(hkFreelance);
1164 cm_GetConfigDir(hfile);
1165 strcat(hfile, AFS_FREELANCE_INI);
1166 strcpy(hfile2, hfile);
1167 strcat(hfile2, "2");
1168 fp1=fopen(hfile, "r+");
1170 return CM_ERROR_INVAL;
1171 fp2=fopen(hfile2, "w+");
1174 return CM_ERROR_INVAL;
1177 fgets(line, sizeof(line), fp1);
1179 fprintf(fp2, "%d\n", n-1);
1181 for (i=0; i<n; i++) {
1182 fgets(line, sizeof(line), fp1);
1183 cp=strchr(line, '#');
1185 cp=strchr(line, '%');
1186 memcpy(shortname, line, cp-line);
1187 shortname[cp-line]=0;
1189 if (strcmp(shortname, toremove)==0) {
1200 return CM_ERROR_NOSUCHFILE;
1203 rename(hfile2, hfile);
1206 lock_ReleaseMutex(&cm_Freelance_Lock);
1207 cm_noteLocalMountPointChange();
1211 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1217 HKEY hkFreelanceSymlinks = 0;
1218 DWORD dwType, dwSize;
1223 /* before adding, verify the filename. If it is already in use, either as
1224 * as mount point or a cellname, do not permit the creation of the symlink.
1226 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1227 osi_LogSaveString(afsd_logp,filename),
1228 osi_LogSaveString(afsd_logp,destination));
1230 if ( filename[0] == '\0' || destination[0] == '\0' )
1234 if (filename[0] == '.') {
1235 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1236 if (stricmp(&filename[1],fullname) == 0)
1239 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1240 if (stricmp(filename,fullname) == 0)
1245 if ( cm_FreelanceMountPointExists(filename) ||
1246 cm_FreelanceSymlinkExists(filename) )
1250 lock_ObtainMutex(&cm_Freelance_Lock);
1253 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1254 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1257 REG_OPTION_NON_VOLATILE,
1258 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1260 &hkFreelanceSymlinks,
1261 NULL) == ERROR_SUCCESS) {
1263 RegQueryInfoKey( hkFreelanceSymlinks,
1265 NULL, /* lpcClass */
1266 NULL, /* lpReserved */
1267 NULL, /* lpcSubKeys */
1268 NULL, /* lpcMaxSubKeyLen */
1269 NULL, /* lpcMaxClassLen */
1270 &dwSymlinks, /* lpcValues */
1271 NULL, /* lpcMaxValueNameLen */
1272 NULL, /* lpcMaxValueLen */
1273 NULL, /* lpcbSecurityDescriptor */
1274 NULL /* lpftLastWriteTime */
1277 sprintf(line, "%s:%s.", filename, destination);
1279 /* If we are adding a new value, there must be an unused name
1280 * within the range 0 to dwSymlinks
1282 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1286 dwSize = sizeof(szLink);
1287 sprintf(szIndex, "%d", dwIndex);
1288 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1289 /* found an unused value */
1291 dwSize = strlen(line) + 1;
1292 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1295 int len = strlen(filename);
1296 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1297 /* Replace the existing value */
1299 dwSize = strlen(line) + 1;
1300 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1305 RegCloseKey(hkFreelanceSymlinks);
1308 lock_ReleaseMutex(&cm_Freelance_Lock);
1310 /* cm_reInitLocalMountPoints(); */
1313 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1315 cm_noteLocalMountPointChange();
1319 long cm_FreelanceRemoveSymlink(char *toremove)
1323 char shortname[200];
1326 HKEY hkFreelanceSymlinks = 0;
1327 DWORD dwType, dwSize;
1332 lock_ObtainMutex(&cm_Freelance_Lock);
1335 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1336 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1338 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1339 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1341 RegQueryInfoKey( hkFreelanceSymlinks,
1343 NULL, /* lpcClass */
1344 NULL, /* lpReserved */
1345 NULL, /* lpcSubKeys */
1346 NULL, /* lpcMaxSubKeyLen */
1347 NULL, /* lpcMaxClassLen */
1348 &dwSymlinks, /* lpcValues */
1349 NULL, /* lpcMaxValueNameLen */
1350 NULL, /* lpcMaxValueLen */
1351 NULL, /* lpcbSecurityDescriptor */
1352 NULL /* lpftLastWriteTime */
1355 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1356 TCHAR szValueName[16];
1357 DWORD dwValueSize = 16;
1358 dwSize = sizeof(line);
1359 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1360 &dwType, line, &dwSize);
1362 cp=strchr(line, ':');
1363 memcpy(shortname, line, cp-line);
1364 shortname[cp-line]=0;
1366 if (!strcmp(shortname, toremove)) {
1367 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1371 RegCloseKey(hkFreelanceSymlinks);
1375 lock_ReleaseMutex(&cm_Freelance_Lock);
1376 cm_noteLocalMountPointChange();
1379 #endif /* AFS_FREELANCE_CLIENT */