18 #ifdef AFS_FREELANCE_CLIENT
19 #include "cm_freelance.h"
22 extern void afsi_log(char *pattern, ...);
24 int cm_noLocalMountPoints;
26 int cm_fakeDirCallback=0;
27 int cm_fakeGettingCallback=0;
28 int cm_fakeDirVersion = 0x8;
29 cm_localMountPoint_t* cm_localMountPoints;
30 osi_mutex_t cm_Freelance_Lock;
31 int cm_localMountPointChangeFlag = 0;
32 int cm_freelanceEnabled = 0;
33 time_t FakeFreelanceModTime = 0x3b49f6e2;
35 void cm_InitFakeRootDir();
38 void cm_FreelanceChangeNotifier(void * parmp) {
39 HANDLE hFreelanceChangeEvent = 0;
42 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
43 "SOFTWARE\\OpenAFS\\Client\\Freelance",
46 &hkFreelance) == ERROR_SUCCESS) {
48 hFreelanceChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
49 if (hFreelanceChangeEvent == NULL) {
50 RegCloseKey(hkFreelance);
56 /* check hFreelanceChangeEvent to see if it is set.
57 * if so, call cm_noteLocalMountPointChange()
59 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
60 FALSE, /* bWatchSubtree */
61 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
62 hFreelanceChangeEvent, /* hEvent */
63 TRUE /* fAsynchronous */
65 RegCloseKey(hkFreelance);
66 CloseHandle(hFreelanceChangeEvent);
70 if (WaitForSingleObject(hFreelanceChangeEvent, INFINITE) == WAIT_OBJECT_0)
72 cm_noteLocalMountPointChange();
77 void cm_FreelanceSymlinkChangeNotifier(void * parmp) {
78 HANDLE hFreelanceSymlinkChangeEvent = 0;
81 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
82 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
85 &hkFreelance) == ERROR_SUCCESS) {
87 hFreelanceSymlinkChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
88 if (hFreelanceSymlinkChangeEvent == NULL) {
89 RegCloseKey(hkFreelance);
95 /* check hFreelanceSymlinkChangeEvent to see if it is set.
96 * if so, call cm_noteLocalMountPointSymlinkChange()
98 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
99 FALSE, /* bWatchSubtree */
100 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
101 hFreelanceSymlinkChangeEvent, /* hEvent */
102 TRUE /* fAsynchronous */
103 ) != ERROR_SUCCESS) {
104 RegCloseKey(hkFreelance);
105 CloseHandle(hFreelanceSymlinkChangeEvent);
109 if (WaitForSingleObject(hFreelanceSymlinkChangeEvent, INFINITE) == WAIT_OBJECT_0)
111 cm_noteLocalMountPointChange();
117 void cm_InitFreelance() {
123 lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock");
125 // yj: first we make a call to cm_initLocalMountPoints
126 // to read all the local mount points from an ini file
127 cm_InitLocalMountPoints();
129 // then we make a call to InitFakeRootDir to create
130 // a fake root directory based on the local mount points
131 cm_InitFakeRootDir();
132 // --- end of yj code
135 /* Start the registry monitor */
136 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceChangeNotifier,
137 NULL, 0, &lpid, "cm_FreelanceChangeNotifier");
138 osi_assert(phandle != NULL);
139 thrd_CloseHandle(phandle);
141 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
142 NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
143 osi_assert(phandle != NULL);
144 thrd_CloseHandle(phandle);
148 /* yj: Initialization of the fake root directory */
149 /* to be called while holding freelance lock unless during init. */
150 void cm_InitFakeRootDir() {
155 // allocate space for the fake info
156 cm_dirHeader_t fakeDirHeader;
157 cm_dirEntry_t fakeEntry;
158 cm_pageHeader_t fakePageHeader;
160 // i'm going to calculate how much space is needed for
161 // this fake root directory. we have these rules:
162 // 1. there are cm_noLocalMountPoints number of entries
163 // 2. each page is CM_DIR_PAGESIZE in size
164 // 3. the first 13 chunks of the first page are used for
166 // 4. the first chunk of all subsequent pages are used
167 // for page header stuff
168 // 5. a max of CM_DIR_EPP entries are allowed per page
169 // 6. each entry takes 1 or more chunks, depending on
170 // the size of the mount point string, as determined
172 // 7. each chunk is CM_DIR_CHUNKSIZE bytes
174 int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
175 int curChunk = 13; // chunks 0 - 12 are used for header stuff
176 // of the first page in the directory
179 int curDirEntryInPage = 0;
183 /* Reserve 2 directory chunks for "." and ".." */
186 while (curDirEntry!=cm_noLocalMountPoints) {
187 sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
188 if ((curChunk + sizeOfCurEntry >= CPP) ||
189 (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
191 curDirEntryInPage = 0;
194 curChunk += sizeOfCurEntry;
199 dirSize = (curPage+1) * CM_DIR_PAGESIZE;
200 cm_FakeRootDir = malloc(dirSize);
201 cm_fakeDirSize = dirSize;
203 // yj: when we get here, we've figured out how much memory we need and
204 // allocated the appropriate space for it. we now prceed to fill
205 // it up with entries.
208 curDirEntryInPage = 0;
211 // fields in the directory entry that are unused.
213 fakeEntry.length = 0;
215 fakeEntry.fid.unique = htonl(1);
217 // the first page is special, it uses fakeDirHeader instead of fakePageHeader
218 // we fill up the page with dirEntries that belong there and we make changes
219 // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
220 // filling up the dirEntries in this page, we copy the fakeDirHeader into
221 // the top of the page.
223 // init the freeBitmap array
225 fakeDirHeader.header.freeBitmap[i]=0;
227 fakeDirHeader.header.freeBitmap[0] = 0xff;
228 fakeDirHeader.header.freeBitmap[1] = 0x7f;
231 // we start counting at 13 because the 0th to 12th chunks are used for header
234 // stick the first 2 entries "." and ".." in
235 fakeEntry.fid.unique = htonl(1);
236 fakeEntry.fid.vnode = htonl(1);
237 strcpy(fakeEntry.name, ".");
238 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
239 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
240 curChunk++; curDirEntryInPage++;
241 strcpy(fakeEntry.name, "..");
242 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
243 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
244 curChunk++; curDirEntryInPage++;
246 // keep putting stuff into page 0 if
247 // 1. we're not done with all entries
248 // 2. we have less than CM_DIR_EPP entries in page 0
249 // 3. we're not out of chunks in page 0
251 while( (curDirEntry!=cm_noLocalMountPoints) &&
252 (curDirEntryInPage < CM_DIR_EPP) &&
253 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
256 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
257 fakeEntry.fid.vnode = htonl(curDirEntry + 2);
258 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
260 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
261 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
264 for (i=0; i<noChunks; i++) {
265 t1 = (curChunk + i) / 8;
266 t2 = curChunk + i - (t1*8);
267 fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
272 // when we get here, we're done with filling in the entries for page 0
273 // copy in the header info
275 memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
279 // ok, page 0's done. Move on to the next page.
280 while (curDirEntry!=cm_noLocalMountPoints) {
282 curChunk = 1; // the zeroth chunk is reserved for page header
283 curDirEntryInPage = 0;
284 for (i=0; i<8; i++) {
285 fakePageHeader.freeBitmap[i]=0;
287 fakePageHeader.freeCount = 0;
288 fakePageHeader.pgcount = 0;
289 fakePageHeader.tag = htons(1234);
291 // while we're on the same page...
292 while ( (curDirEntry!=cm_noLocalMountPoints) &&
293 (curDirEntryInPage < CM_DIR_EPP) &&
294 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
296 // add an entry to this page
298 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
299 fakeEntry.fid.vnode=htonl(curDirEntry+2);
300 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
301 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
302 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
305 for (i=0; i<noChunks; i++) {
306 t1 = (curChunk + i) / 8;
307 t2 = curChunk + i - (t1*8);
308 fakePageHeader.freeBitmap[t1] |= (1 << t2);
312 memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
317 // we know the fakeDir is setup properly, so we claim that we have callback
318 osi_Log0(afsd_logp,"cm_InitFakeRootDir fakeDirCallback=1");
319 cm_fakeDirCallback=1;
321 // when we get here, we've set up everything! done!
324 int cm_FakeRootFid(cm_fid_t *fidp)
326 fidp->cell = AFS_FAKE_ROOT_CELL_ID; /* root cell */
327 fidp->volume = AFS_FAKE_ROOT_VOL_ID; /* root.afs ? */
333 /* called directly from ioctl */
334 /* called while not holding freelance lock */
335 int cm_noteLocalMountPointChange() {
336 lock_ObtainMutex(&cm_Freelance_Lock);
338 cm_localMountPointChangeFlag = 1;
339 lock_ReleaseMutex(&cm_Freelance_Lock);
343 int cm_getLocalMountPointChange() {
344 return cm_localMountPointChangeFlag;
347 int cm_clearLocalMountPointChange() {
348 cm_localMountPointChangeFlag = 0;
352 int cm_reInitLocalMountPoints() {
355 cm_scache_t *scp, **lscpp, *tscp;
357 osi_Log0(afsd_logp,"----- freelance reinitialization starts ----- ");
359 // first we invalidate all the SCPs that were created
360 // for the local mount points
362 osi_Log0(afsd_logp,"Invalidating local mount point scp... ");
364 aFid.cell = AFS_FAKE_ROOT_CELL_ID;
365 aFid.volume=AFS_FAKE_ROOT_VOL_ID;
369 lock_ObtainWrite(&cm_scacheLock);
370 lock_ObtainMutex(&cm_Freelance_Lock); /* always scache then freelance lock */
371 for (i=0; i<cm_noLocalMountPoints; i++) {
372 hash = CM_SCACHE_HASH(&aFid);
373 for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
374 if (scp->fid.volume == aFid.volume &&
375 scp->fid.vnode == aFid.vnode &&
376 scp->fid.unique == aFid.unique
379 // mark the scp to be reused
380 cm_HoldSCacheNoLock(scp);
381 lock_ReleaseWrite(&cm_scacheLock);
382 lock_ObtainMutex(&scp->mx);
383 cm_DiscardSCache(scp);
384 lock_ReleaseMutex(&scp->mx);
385 cm_CallbackNotifyChange(scp);
386 lock_ObtainWrite(&cm_scacheLock);
387 cm_ReleaseSCacheNoLock(scp);
389 // take the scp out of the hash
390 lscpp = &cm_hashTablep[hash];
391 for (tscp=*lscpp; tscp; lscpp = &tscp->nextp, tscp = *lscpp) {
392 if (tscp == scp) break;
395 scp->flags &= ~CM_SCACHEFLAG_INHASH;
398 aFid.vnode = aFid.vnode + 1;
400 lock_ReleaseWrite(&cm_scacheLock);
401 osi_Log0(afsd_logp,"\tall old scp cleared!");
403 // we must free the memory that was allocated in the prev
404 // cm_InitLocalMountPoints call
405 osi_Log0(afsd_logp,"Removing old localmountpoints... ");
406 free(cm_localMountPoints);
407 osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
409 // now re-init the localmountpoints
410 osi_Log0(afsd_logp,"Creating new localmountpoints... ");
411 cm_InitLocalMountPoints();
412 osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
414 // now we have to free the memory allocated in cm_initfakerootdir
415 osi_Log0(afsd_logp,"Removing old fakedir... ");
416 free(cm_FakeRootDir);
417 osi_Log0(afsd_logp,"\t\told fakedir removed!");
419 // then we re-create that dir
420 osi_Log0(afsd_logp,"Creating new fakedir... ");
421 cm_InitFakeRootDir();
422 osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
424 lock_ReleaseMutex(&cm_Freelance_Lock);
426 osi_Log0(afsd_logp,"----- freelance reinit complete -----");
431 // yj: open up the ini file and read all the local mount
432 // points that are stored there. Part of the initialization
433 // process for the freelance client.
434 /* to be called while holding freelance lock unless during init. */
435 long cm_InitLocalMountPoints() {
440 cm_localMountPoint_t* aLocalMountPoint;
443 char rootCellName[256];
445 HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
446 DWORD dwType, dwSize;
447 DWORD dwMountPoints = 0;
449 DWORD dwSymlinks = 0;
450 FILETIME ftLastWriteTime;
454 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
455 "SOFTWARE\\OpenAFS\\Client\\Freelance",
457 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
458 &hkFreelance) == ERROR_SUCCESS) {
460 RegQueryInfoKey( hkFreelance,
463 NULL, /* lpReserved */
464 NULL, /* lpcSubKeys */
465 NULL, /* lpcMaxSubKeyLen */
466 NULL, /* lpcMaxClassLen */
467 &dwMountPoints, /* lpcValues */
468 NULL, /* lpcMaxValueNameLen */
469 NULL, /* lpcMaxValueLen */
470 NULL, /* lpcbSecurityDescriptor */
471 &ftLastWriteTime /* lpftLastWriteTime */
474 smb_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
476 if ( dwMountPoints == 0 ) {
477 sprintf(line,"%s#%s:root.cell.\n",rootCellName,rootCellName);
479 dwSize = strlen(line) + 1;
480 RegSetValueEx( hkFreelance, "0", 0, dwType, line, dwSize);
481 sprintf(line,".%s%%%s:root.cell.\n",rootCellName,rootCellName);
482 dwSize = strlen(line) + 1;
483 RegSetValueEx( hkFreelance, "1", 0, dwType, line, dwSize);
487 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
488 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
491 REG_OPTION_NON_VOLATILE,
492 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
494 &hkFreelanceSymlinks,
495 NULL) == ERROR_SUCCESS) {
497 RegQueryInfoKey( hkFreelanceSymlinks,
500 NULL, /* lpReserved */
501 NULL, /* lpcSubKeys */
502 NULL, /* lpcMaxSubKeyLen */
503 NULL, /* lpcMaxClassLen */
504 &dwSymlinks, /* lpcValues */
505 NULL, /* lpcMaxValueNameLen */
506 NULL, /* lpcMaxValueLen */
507 NULL, /* lpcbSecurityDescriptor */
508 NULL /* lpftLastWriteTime */
512 // get the number of entries there are from the first line
514 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
516 // create space to store the local mount points
517 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
518 aLocalMountPoint = cm_localMountPoints;
520 // now we read n lines and parse them into local mount points
521 // where n is the number of local mount points there are, as
523 // Each line in the ini file represents 1 local mount point and
524 // is in the format xxx#yyy:zzz, where xxx is the directory
525 // entry name, yyy is the cell name and zzz is the volume name.
526 // #yyy:zzz together make up the mount point.
527 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
528 TCHAR szValueName[16];
529 DWORD dwValueSize = 16;
530 dwSize = sizeof(line);
531 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
532 &dwType, line, &dwSize);
534 /* find the trailing dot; null terminate after it */
535 t2 = strrchr(line, '.');
539 // line is not empty, so let's parse it
540 t = strchr(line, '#');
542 t = strchr(line, '%');
543 // make sure that there is a '#' or '%' separator in the line
545 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, dwIndex);
546 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", dwIndex);
547 cm_noLocalMountPoints--;
551 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
552 aLocalMountPoint->namep=malloc(t-line+1);
553 strncpy(aLocalMountPoint->namep, line, t-line);
554 aLocalMountPoint->namep[t-line] = '\0';
556 /* copy the mount point string */
557 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
558 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
559 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
561 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
562 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
563 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
568 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
569 TCHAR szValueName[16];
570 DWORD dwValueSize = 16;
571 dwSize = sizeof(line);
572 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
573 &dwType, line, &dwSize);
575 /* find the trailing dot; null terminate after it */
576 t2 = strrchr(line, '.');
580 // line is not empty, so let's parse it
581 t = strchr(line, ':');
583 // make sure that there is a ':' separator in the line
585 afsi_log("error occurred while parsing symlink entry: no ':' separator in line %d", dwIndex);
586 fprintf(stderr, "error occurred while parsing symlink entry: no ':' separator in line %d", dwIndex);
587 cm_noLocalMountPoints--;
591 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
592 aLocalMountPoint->namep=malloc(t-line+1);
593 strncpy(aLocalMountPoint->namep, line, t-line);
594 aLocalMountPoint->namep[t-line] = '\0';
596 /* copy the symlink string */
597 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
598 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
599 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
601 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
602 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
603 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
608 if ( hkFreelanceSymlinks )
609 RegCloseKey( hkFreelanceSymlinks );
610 RegCloseKey(hkFreelance);
615 /* What follows is the old code to read freelance mount points
616 * out of a text file modified to copy the data into the registry
618 cm_GetConfigDir(hdir);
619 strcat(hdir, AFS_FREELANCE_INI);
620 // open the ini file for reading
621 fp = fopen(hdir, "r");
623 /* look in the Windows directory where we used to store the file */
624 GetWindowsDirectory(hdir, sizeof(hdir));
626 strcat(hdir, AFS_FREELANCE_INI);
627 fp = fopen(hdir, "r");
631 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
632 "SOFTWARE\\OpenAFS\\Client\\Freelance",
635 REG_OPTION_NON_VOLATILE,
645 RegCloseKey(hkFreelance);
647 rootCellName[0] = '.';
648 code = cm_GetRootCellName(&rootCellName[1]);
650 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell", 0, NULL);
651 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell", 1, NULL);
656 // we successfully opened the file
657 osi_Log0(afsd_logp,"opened afs_freelance.ini");
659 // now we read the first line to see how many entries
661 fgets(line, sizeof(line), fp);
663 // if the line is empty at any point when we're reading
664 // we're screwed. report error and return.
666 afsi_log("error occurred while reading afs_freelance.ini");
667 fprintf(stderr, "error occurred while reading afs_freelance.ini");
671 // get the number of entries there are from the first line
673 cm_noLocalMountPoints = atoi(line);
675 if (cm_noLocalMountPoints > 0) {
676 // create space to store the local mount points
677 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
678 aLocalMountPoint = cm_localMountPoints;
681 // now we read n lines and parse them into local mount points
682 // where n is the number of local mount points there are, as
684 // Each line in the ini file represents 1 local mount point and
685 // is in the format xxx#yyy:zzz, where xxx is the directory
686 // entry name, yyy is the cell name and zzz is the volume name.
687 // #yyy:zzz together make up the mount point.
688 for (i=0; i<cm_noLocalMountPoints; i++) {
689 fgets(line, sizeof(line), fp);
690 // check that the line is not empty
692 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
693 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
697 /* find the trailing dot; null terminate after it */
698 t2 = strrchr(line, '.');
705 /* we are migrating to the registry */
706 sprintf(szIndex,"%d",dwIndex++);
708 dwSize = strlen(line) + 1;
709 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
713 // line is not empty, so let's parse it
714 t = strchr(line, '#');
716 t = strchr(line, '%');
717 // make sure that there is a '#' or '%' separator in the line
719 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
720 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
723 aLocalMountPoint->namep=malloc(t-line+1);
724 memcpy(aLocalMountPoint->namep, line, t-line);
725 *(aLocalMountPoint->namep + (t-line)) = 0;
727 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
728 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
729 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
731 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
732 aLocalMountPoint->namep,
733 aLocalMountPoint->mountPointStringp);
740 RegCloseKey(hkFreelance);
747 int cm_getNoLocalMountPoints() {
748 return cm_noLocalMountPoints;
752 long cm_FreelanceMountPointExists(char * filename)
758 HKEY hkFreelance = 0;
759 DWORD dwType, dwSize;
763 lock_ObtainMutex(&cm_Freelance_Lock);
765 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
766 "SOFTWARE\\OpenAFS\\Client\\Freelance",
768 KEY_READ|KEY_QUERY_VALUE,
769 &hkFreelance) == ERROR_SUCCESS)
771 RegQueryInfoKey( hkFreelance,
774 NULL, /* lpReserved */
775 NULL, /* lpcSubKeys */
776 NULL, /* lpcMaxSubKeyLen */
777 NULL, /* lpcMaxClassLen */
778 &dwMountPoints, /* lpcValues */
779 NULL, /* lpcMaxValueNameLen */
780 NULL, /* lpcMaxValueLen */
781 NULL, /* lpcbSecurityDescriptor */
782 NULL /* lpftLastWriteTime */
785 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
786 TCHAR szValueName[16];
787 DWORD dwValueSize = 16;
788 dwSize = sizeof(line);
789 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
790 &dwType, line, &dwSize);
792 cp=strchr(line, '#');
794 cp=strchr(line, '%');
795 memcpy(shortname, line, cp-line);
796 shortname[cp-line]=0;
798 if (!strcmp(shortname, filename)) {
803 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
804 TCHAR szValueName[16];
805 DWORD dwValueSize = 16;
806 dwSize = sizeof(line);
807 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
808 &dwType, line, &dwSize);
810 cp=strchr(line, '#');
812 cp=strchr(line, '%');
813 memcpy(shortname, line, cp-line);
814 shortname[cp-line]=0;
816 if (!stricmp(shortname, filename)) {
821 RegCloseKey(hkFreelance);
824 lock_ReleaseMutex(&cm_Freelance_Lock);
829 long cm_FreelanceSymlinkExists(char * filename)
835 HKEY hkFreelance = 0;
836 DWORD dwType, dwSize;
840 lock_ObtainMutex(&cm_Freelance_Lock);
842 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
843 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
845 KEY_READ|KEY_QUERY_VALUE,
846 &hkFreelance) == ERROR_SUCCESS)
848 RegQueryInfoKey( hkFreelance,
851 NULL, /* lpReserved */
852 NULL, /* lpcSubKeys */
853 NULL, /* lpcMaxSubKeyLen */
854 NULL, /* lpcMaxClassLen */
855 &dwSymlinks, /* lpcValues */
856 NULL, /* lpcMaxValueNameLen */
857 NULL, /* lpcMaxValueLen */
858 NULL, /* lpcbSecurityDescriptor */
859 NULL /* lpftLastWriteTime */
862 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
863 TCHAR szValueName[16];
864 DWORD dwValueSize = 16;
865 dwSize = sizeof(line);
866 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
867 &dwType, line, &dwSize);
869 cp=strchr(line, ':');
870 memcpy(shortname, line, cp-line);
871 shortname[cp-line]=0;
873 if (!strcmp(shortname, filename)) {
878 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
879 TCHAR szValueName[16];
880 DWORD dwValueSize = 16;
881 dwSize = sizeof(line);
882 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
883 &dwType, line, &dwSize);
885 cp=strchr(line, ':');
886 memcpy(shortname, line, cp-line);
887 shortname[cp-line]=0;
889 if (!stricmp(shortname, filename)) {
894 RegCloseKey(hkFreelance);
897 lock_ReleaseMutex(&cm_Freelance_Lock);
903 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
912 HKEY hkFreelance = 0;
913 DWORD dwType, dwSize;
918 /* before adding, verify the cell name; if it is not a valid cell,
919 don't add the mount point.
920 allow partial matches as a means of poor man's alias. */
921 /* major performance issue? */
922 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
923 osi_LogSaveString(afsd_logp,filename),
924 osi_LogSaveString(afsd_logp,cellname),
925 osi_LogSaveString(afsd_logp,volume),
928 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
931 if (cellname[0] == '.') {
932 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
935 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
940 if ( cm_FreelanceMountPointExists(filename) ||
941 cm_FreelanceSymlinkExists(filename) )
945 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
946 osi_LogSaveString(afsd_logp,cellname));
948 lock_ObtainMutex(&cm_Freelance_Lock);
951 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
952 "SOFTWARE\\OpenAFS\\Client\\Freelance",
954 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
955 &hkFreelance) == ERROR_SUCCESS) {
957 RegQueryInfoKey( hkFreelance,
960 NULL, /* lpReserved */
961 NULL, /* lpcSubKeys */
962 NULL, /* lpcMaxSubKeyLen */
963 NULL, /* lpcMaxClassLen */
964 &dwMountPoints, /* lpcValues */
965 NULL, /* lpcMaxValueNameLen */
966 NULL, /* lpcMaxValueLen */
967 NULL, /* lpcbSecurityDescriptor */
968 NULL /* lpftLastWriteTime */
972 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
974 sprintf(line, "%s#%s:%s", filename, fullname, volume);
976 /* If we are adding a new value, there must be an unused name
977 * within the range 0 to dwMountPoints
979 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
983 dwSize = sizeof(szMount);
984 sprintf(szIndex, "%d", dwIndex);
985 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
986 /* found an unused value */
988 dwSize = strlen(line) + 1;
989 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
992 int len = strlen(filename);
993 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
994 (szMount[len] == '%' || szMount[len] == '#')) {
995 /* Replace the existing value */
997 dwSize = strlen(line) + 1;
998 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1003 RegCloseKey(hkFreelance);
1007 cm_GetConfigDir(hfile);
1008 strcat(hfile, AFS_FREELANCE_INI);
1009 fp = fopen(hfile, "r+");
1011 return CM_ERROR_INVAL;
1012 fgets(line, sizeof(line), fp);
1015 fseek(fp, 0, SEEK_SET);
1016 fprintf(fp, "%d", n);
1017 fseek(fp, 0, SEEK_END);
1019 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1021 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1024 lock_ReleaseMutex(&cm_Freelance_Lock);
1026 /* cm_reInitLocalMountPoints(); */
1029 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1031 cm_noteLocalMountPointChange();
1035 long cm_FreelanceRemoveMount(char *toremove)
1040 char shortname[200];
1041 char hfile[120], hfile2[120];
1045 HKEY hkFreelance = 0;
1046 DWORD dwType, dwSize;
1047 DWORD dwMountPoints;
1051 lock_ObtainMutex(&cm_Freelance_Lock);
1054 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1055 "SOFTWARE\\OpenAFS\\Client\\Freelance",
1057 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1058 &hkFreelance) == ERROR_SUCCESS) {
1060 RegQueryInfoKey( hkFreelance,
1062 NULL, /* lpcClass */
1063 NULL, /* lpReserved */
1064 NULL, /* lpcSubKeys */
1065 NULL, /* lpcMaxSubKeyLen */
1066 NULL, /* lpcMaxClassLen */
1067 &dwMountPoints, /* lpcValues */
1068 NULL, /* lpcMaxValueNameLen */
1069 NULL, /* lpcMaxValueLen */
1070 NULL, /* lpcbSecurityDescriptor */
1071 NULL /* lpftLastWriteTime */
1074 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1075 TCHAR szValueName[16];
1076 DWORD dwValueSize = 16;
1077 dwSize = sizeof(line);
1078 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1079 &dwType, line, &dwSize);
1081 cp=strchr(line, '#');
1083 cp=strchr(line, '%');
1084 memcpy(shortname, line, cp-line);
1085 shortname[cp-line]=0;
1087 if (!strcmp(shortname, toremove)) {
1088 RegDeleteValue( hkFreelance, szValueName );
1092 RegCloseKey(hkFreelance);
1096 cm_GetConfigDir(hfile);
1097 strcat(hfile, AFS_FREELANCE_INI);
1098 strcpy(hfile2, hfile);
1099 strcat(hfile2, "2");
1100 fp1=fopen(hfile, "r+");
1102 return CM_ERROR_INVAL;
1103 fp2=fopen(hfile2, "w+");
1106 return CM_ERROR_INVAL;
1109 fgets(line, sizeof(line), fp1);
1111 fprintf(fp2, "%d\n", n-1);
1113 for (i=0; i<n; i++) {
1114 fgets(line, sizeof(line), fp1);
1115 cp=strchr(line, '#');
1117 cp=strchr(line, '%');
1118 memcpy(shortname, line, cp-line);
1119 shortname[cp-line]=0;
1121 if (strcmp(shortname, toremove)==0) {
1132 return CM_ERROR_NOSUCHFILE;
1135 rename(hfile2, hfile);
1138 lock_ReleaseMutex(&cm_Freelance_Lock);
1139 cm_noteLocalMountPointChange();
1143 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1152 HKEY hkFreelanceSymlinks = 0;
1153 DWORD dwType, dwSize;
1158 /* before adding, verify the filename. If it is already in use, either as
1159 * as mount point or a cellname, do not permit the creation of the symlink.
1161 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1162 osi_LogSaveString(afsd_logp,filename),
1163 osi_LogSaveString(afsd_logp,destination));
1165 if ( filename[0] == '\0' || destination[0] == '\0' )
1169 if (filename[0] == '.') {
1170 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1171 if (stricmp(&filename[1],fullname) == 0)
1174 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1175 if (stricmp(filename,fullname) == 0)
1180 if ( cm_FreelanceMountPointExists(filename) ||
1181 cm_FreelanceSymlinkExists(filename) )
1185 lock_ObtainMutex(&cm_Freelance_Lock);
1188 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1189 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
1192 REG_OPTION_NON_VOLATILE,
1193 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1195 &hkFreelanceSymlinks,
1196 NULL) == ERROR_SUCCESS) {
1198 RegQueryInfoKey( hkFreelanceSymlinks,
1200 NULL, /* lpcClass */
1201 NULL, /* lpReserved */
1202 NULL, /* lpcSubKeys */
1203 NULL, /* lpcMaxSubKeyLen */
1204 NULL, /* lpcMaxClassLen */
1205 &dwSymlinks, /* lpcValues */
1206 NULL, /* lpcMaxValueNameLen */
1207 NULL, /* lpcMaxValueLen */
1208 NULL, /* lpcbSecurityDescriptor */
1209 NULL /* lpftLastWriteTime */
1212 sprintf(line, "%s:%s.", filename, destination);
1214 /* If we are adding a new value, there must be an unused name
1215 * within the range 0 to dwSymlinks
1217 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1221 dwSize = sizeof(szLink);
1222 sprintf(szIndex, "%d", dwIndex);
1223 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1224 /* found an unused value */
1226 dwSize = strlen(line) + 1;
1227 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1230 int len = strlen(filename);
1231 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1232 /* Replace the existing value */
1234 dwSize = strlen(line) + 1;
1235 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1240 RegCloseKey(hkFreelanceSymlinks);
1243 lock_ReleaseMutex(&cm_Freelance_Lock);
1245 /* cm_reInitLocalMountPoints(); */
1248 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1250 cm_noteLocalMountPointChange();
1254 long cm_FreelanceRemoveSymlink(char *toremove)
1259 char shortname[200];
1260 char hfile[120], hfile2[120];
1264 HKEY hkFreelanceSymlinks = 0;
1265 DWORD dwType, dwSize;
1270 lock_ObtainMutex(&cm_Freelance_Lock);
1273 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1274 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
1276 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1277 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1279 RegQueryInfoKey( hkFreelanceSymlinks,
1281 NULL, /* lpcClass */
1282 NULL, /* lpReserved */
1283 NULL, /* lpcSubKeys */
1284 NULL, /* lpcMaxSubKeyLen */
1285 NULL, /* lpcMaxClassLen */
1286 &dwSymlinks, /* lpcValues */
1287 NULL, /* lpcMaxValueNameLen */
1288 NULL, /* lpcMaxValueLen */
1289 NULL, /* lpcbSecurityDescriptor */
1290 NULL /* lpftLastWriteTime */
1293 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1294 TCHAR szValueName[16];
1295 DWORD dwValueSize = 16;
1296 dwSize = sizeof(line);
1297 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1298 &dwType, line, &dwSize);
1300 cp=strchr(line, ':');
1301 memcpy(shortname, line, cp-line);
1302 shortname[cp-line]=0;
1304 if (!strcmp(shortname, toremove)) {
1305 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1309 RegCloseKey(hkFreelanceSymlinks);
1313 lock_ReleaseMutex(&cm_Freelance_Lock);
1314 cm_noteLocalMountPointChange();
1317 #endif /* AFS_FREELANCE_CLIENT */