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(void) {
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 rootCellName[0] = '.';
478 code = cm_GetRootCellName(&rootCellName[1]);
480 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell", 0, NULL);
481 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell", 1, NULL);
486 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
487 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
490 REG_OPTION_NON_VOLATILE,
491 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
493 &hkFreelanceSymlinks,
494 NULL) == ERROR_SUCCESS) {
496 RegQueryInfoKey( hkFreelanceSymlinks,
499 NULL, /* lpReserved */
500 NULL, /* lpcSubKeys */
501 NULL, /* lpcMaxSubKeyLen */
502 NULL, /* lpcMaxClassLen */
503 &dwSymlinks, /* lpcValues */
504 NULL, /* lpcMaxValueNameLen */
505 NULL, /* lpcMaxValueLen */
506 NULL, /* lpcbSecurityDescriptor */
507 NULL /* lpftLastWriteTime */
511 // get the number of entries there are from the first line
513 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
515 // create space to store the local mount points
516 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
517 aLocalMountPoint = cm_localMountPoints;
519 // now we read n lines and parse them into local mount points
520 // where n is the number of local mount points there are, as
522 // Each line in the ini file represents 1 local mount point and
523 // is in the format xxx#yyy:zzz, where xxx is the directory
524 // entry name, yyy is the cell name and zzz is the volume name.
525 // #yyy:zzz together make up the mount point.
526 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
527 TCHAR szValueName[16];
528 DWORD dwValueSize = 16;
529 dwSize = sizeof(line);
530 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
531 &dwType, line, &dwSize);
533 /* find the trailing dot; null terminate after it */
534 t2 = strrchr(line, '.');
538 // line is not empty, so let's parse it
539 t = strchr(line, '#');
541 t = strchr(line, '%');
542 // make sure that there is a '#' or '%' separator in the line
544 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, dwIndex);
545 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", dwIndex);
546 cm_noLocalMountPoints--;
550 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
551 aLocalMountPoint->namep=malloc(t-line+1);
552 strncpy(aLocalMountPoint->namep, line, t-line);
553 aLocalMountPoint->namep[t-line] = '\0';
555 /* copy the mount point string */
556 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
557 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
558 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
560 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
561 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
562 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
567 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
568 TCHAR szValueName[16];
569 DWORD dwValueSize = 16;
570 dwSize = sizeof(line);
571 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
572 &dwType, line, &dwSize);
574 /* find the trailing dot; null terminate after it */
575 t2 = strrchr(line, '.');
579 // line is not empty, so let's parse it
580 t = strchr(line, ':');
582 // make sure that there is a ':' separator in the line
584 afsi_log("error occurred while parsing symlink entry: no ':' separator in line %d", dwIndex);
585 fprintf(stderr, "error occurred while parsing symlink entry: no ':' separator in line %d", dwIndex);
586 cm_noLocalMountPoints--;
590 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
591 aLocalMountPoint->namep=malloc(t-line+1);
592 strncpy(aLocalMountPoint->namep, line, t-line);
593 aLocalMountPoint->namep[t-line] = '\0';
595 /* copy the symlink string */
596 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
597 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
598 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
600 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
601 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
602 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
607 if ( hkFreelanceSymlinks )
608 RegCloseKey( hkFreelanceSymlinks );
609 RegCloseKey(hkFreelance);
614 /* What follows is the old code to read freelance mount points
615 * out of a text file modified to copy the data into the registry
617 cm_GetConfigDir(hdir);
618 strcat(hdir, AFS_FREELANCE_INI);
619 // open the ini file for reading
620 fp = fopen(hdir, "r");
622 /* look in the Windows directory where we used to store the file */
623 GetWindowsDirectory(hdir, sizeof(hdir));
625 strcat(hdir, AFS_FREELANCE_INI);
626 fp = fopen(hdir, "r");
630 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
631 "SOFTWARE\\OpenAFS\\Client\\Freelance",
634 REG_OPTION_NON_VOLATILE,
644 RegCloseKey(hkFreelance);
646 rootCellName[0] = '.';
647 code = cm_GetRootCellName(&rootCellName[1]);
649 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell", 0, NULL);
650 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell", 1, NULL);
655 // we successfully opened the file
656 osi_Log0(afsd_logp,"opened afs_freelance.ini");
658 // now we read the first line to see how many entries
660 fgets(line, sizeof(line), fp);
662 // if the line is empty at any point when we're reading
663 // we're screwed. report error and return.
665 afsi_log("error occurred while reading afs_freelance.ini");
666 fprintf(stderr, "error occurred while reading afs_freelance.ini");
670 // get the number of entries there are from the first line
672 cm_noLocalMountPoints = atoi(line);
674 if (cm_noLocalMountPoints > 0) {
675 // create space to store the local mount points
676 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
677 aLocalMountPoint = cm_localMountPoints;
680 // now we read n lines and parse them into local mount points
681 // where n is the number of local mount points there are, as
683 // Each line in the ini file represents 1 local mount point and
684 // is in the format xxx#yyy:zzz, where xxx is the directory
685 // entry name, yyy is the cell name and zzz is the volume name.
686 // #yyy:zzz together make up the mount point.
687 for (i=0; i<cm_noLocalMountPoints; i++) {
688 fgets(line, sizeof(line), fp);
689 // check that the line is not empty
691 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
692 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
696 /* find the trailing dot; null terminate after it */
697 t2 = strrchr(line, '.');
704 /* we are migrating to the registry */
705 sprintf(szIndex,"%d",dwIndex++);
707 dwSize = strlen(line) + 1;
708 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
712 // line is not empty, so let's parse it
713 t = strchr(line, '#');
715 t = strchr(line, '%');
716 // make sure that there is a '#' or '%' separator in the line
718 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
719 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
722 aLocalMountPoint->namep=malloc(t-line+1);
723 memcpy(aLocalMountPoint->namep, line, t-line);
724 *(aLocalMountPoint->namep + (t-line)) = 0;
726 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
727 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
728 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
730 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
731 aLocalMountPoint->namep,
732 aLocalMountPoint->mountPointStringp);
739 RegCloseKey(hkFreelance);
746 int cm_getNoLocalMountPoints() {
747 return cm_noLocalMountPoints;
751 long cm_FreelanceMountPointExists(char * filename)
757 HKEY hkFreelance = 0;
758 DWORD dwType, dwSize;
762 lock_ObtainMutex(&cm_Freelance_Lock);
764 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
765 "SOFTWARE\\OpenAFS\\Client\\Freelance",
767 KEY_READ|KEY_QUERY_VALUE,
768 &hkFreelance) == ERROR_SUCCESS)
770 RegQueryInfoKey( hkFreelance,
773 NULL, /* lpReserved */
774 NULL, /* lpcSubKeys */
775 NULL, /* lpcMaxSubKeyLen */
776 NULL, /* lpcMaxClassLen */
777 &dwMountPoints, /* lpcValues */
778 NULL, /* lpcMaxValueNameLen */
779 NULL, /* lpcMaxValueLen */
780 NULL, /* lpcbSecurityDescriptor */
781 NULL /* lpftLastWriteTime */
784 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
785 TCHAR szValueName[16];
786 DWORD dwValueSize = 16;
787 dwSize = sizeof(line);
788 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
789 &dwType, line, &dwSize);
791 cp=strchr(line, '#');
793 cp=strchr(line, '%');
794 memcpy(shortname, line, cp-line);
795 shortname[cp-line]=0;
797 if (!strcmp(shortname, filename)) {
802 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
803 TCHAR szValueName[16];
804 DWORD dwValueSize = 16;
805 dwSize = sizeof(line);
806 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
807 &dwType, line, &dwSize);
809 cp=strchr(line, '#');
811 cp=strchr(line, '%');
812 memcpy(shortname, line, cp-line);
813 shortname[cp-line]=0;
815 if (!stricmp(shortname, filename)) {
820 RegCloseKey(hkFreelance);
823 lock_ReleaseMutex(&cm_Freelance_Lock);
828 long cm_FreelanceSymlinkExists(char * filename)
834 HKEY hkFreelance = 0;
835 DWORD dwType, dwSize;
839 lock_ObtainMutex(&cm_Freelance_Lock);
841 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
842 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
844 KEY_READ|KEY_QUERY_VALUE,
845 &hkFreelance) == ERROR_SUCCESS)
847 RegQueryInfoKey( hkFreelance,
850 NULL, /* lpReserved */
851 NULL, /* lpcSubKeys */
852 NULL, /* lpcMaxSubKeyLen */
853 NULL, /* lpcMaxClassLen */
854 &dwSymlinks, /* lpcValues */
855 NULL, /* lpcMaxValueNameLen */
856 NULL, /* lpcMaxValueLen */
857 NULL, /* lpcbSecurityDescriptor */
858 NULL /* lpftLastWriteTime */
861 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
862 TCHAR szValueName[16];
863 DWORD dwValueSize = 16;
864 dwSize = sizeof(line);
865 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
866 &dwType, line, &dwSize);
868 cp=strchr(line, ':');
869 memcpy(shortname, line, cp-line);
870 shortname[cp-line]=0;
872 if (!strcmp(shortname, filename)) {
877 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
878 TCHAR szValueName[16];
879 DWORD dwValueSize = 16;
880 dwSize = sizeof(line);
881 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
882 &dwType, line, &dwSize);
884 cp=strchr(line, ':');
885 memcpy(shortname, line, cp-line);
886 shortname[cp-line]=0;
888 if (!stricmp(shortname, filename)) {
893 RegCloseKey(hkFreelance);
896 lock_ReleaseMutex(&cm_Freelance_Lock);
902 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
911 HKEY hkFreelance = 0;
912 DWORD dwType, dwSize;
917 /* before adding, verify the cell name; if it is not a valid cell,
918 don't add the mount point.
919 allow partial matches as a means of poor man's alias. */
920 /* major performance issue? */
921 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
922 osi_LogSaveString(afsd_logp,filename),
923 osi_LogSaveString(afsd_logp,cellname),
924 osi_LogSaveString(afsd_logp,volume),
927 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
930 if (cellname[0] == '.') {
931 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
934 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
939 if ( cm_FreelanceMountPointExists(filename) ||
940 cm_FreelanceSymlinkExists(filename) )
944 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
945 osi_LogSaveString(afsd_logp,cellname));
947 lock_ObtainMutex(&cm_Freelance_Lock);
950 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
951 "SOFTWARE\\OpenAFS\\Client\\Freelance",
953 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
954 &hkFreelance) == ERROR_SUCCESS) {
956 RegQueryInfoKey( hkFreelance,
959 NULL, /* lpReserved */
960 NULL, /* lpcSubKeys */
961 NULL, /* lpcMaxSubKeyLen */
962 NULL, /* lpcMaxClassLen */
963 &dwMountPoints, /* lpcValues */
964 NULL, /* lpcMaxValueNameLen */
965 NULL, /* lpcMaxValueLen */
966 NULL, /* lpcbSecurityDescriptor */
967 NULL /* lpftLastWriteTime */
971 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
973 sprintf(line, "%s#%s:%s", filename, fullname, volume);
975 /* If we are adding a new value, there must be an unused name
976 * within the range 0 to dwMountPoints
978 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
982 dwSize = sizeof(szMount);
983 sprintf(szIndex, "%d", dwIndex);
984 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
985 /* found an unused value */
987 dwSize = strlen(line) + 1;
988 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
991 int len = strlen(filename);
992 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
993 (szMount[len] == '%' || szMount[len] == '#')) {
994 /* Replace the existing value */
996 dwSize = strlen(line) + 1;
997 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1002 RegCloseKey(hkFreelance);
1006 cm_GetConfigDir(hfile);
1007 strcat(hfile, AFS_FREELANCE_INI);
1008 fp = fopen(hfile, "r+");
1010 return CM_ERROR_INVAL;
1011 fgets(line, sizeof(line), fp);
1014 fseek(fp, 0, SEEK_SET);
1015 fprintf(fp, "%d", n);
1016 fseek(fp, 0, SEEK_END);
1018 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1020 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1023 lock_ReleaseMutex(&cm_Freelance_Lock);
1025 /* cm_reInitLocalMountPoints(); */
1028 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1030 cm_noteLocalMountPointChange();
1034 long cm_FreelanceRemoveMount(char *toremove)
1039 char shortname[200];
1040 char hfile[120], hfile2[120];
1044 HKEY hkFreelance = 0;
1045 DWORD dwType, dwSize;
1046 DWORD dwMountPoints;
1050 lock_ObtainMutex(&cm_Freelance_Lock);
1053 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1054 "SOFTWARE\\OpenAFS\\Client\\Freelance",
1056 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1057 &hkFreelance) == ERROR_SUCCESS) {
1059 RegQueryInfoKey( hkFreelance,
1061 NULL, /* lpcClass */
1062 NULL, /* lpReserved */
1063 NULL, /* lpcSubKeys */
1064 NULL, /* lpcMaxSubKeyLen */
1065 NULL, /* lpcMaxClassLen */
1066 &dwMountPoints, /* lpcValues */
1067 NULL, /* lpcMaxValueNameLen */
1068 NULL, /* lpcMaxValueLen */
1069 NULL, /* lpcbSecurityDescriptor */
1070 NULL /* lpftLastWriteTime */
1073 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1074 TCHAR szValueName[16];
1075 DWORD dwValueSize = 16;
1076 dwSize = sizeof(line);
1077 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1078 &dwType, line, &dwSize);
1080 cp=strchr(line, '#');
1082 cp=strchr(line, '%');
1083 memcpy(shortname, line, cp-line);
1084 shortname[cp-line]=0;
1086 if (!strcmp(shortname, toremove)) {
1087 RegDeleteValue( hkFreelance, szValueName );
1091 RegCloseKey(hkFreelance);
1095 cm_GetConfigDir(hfile);
1096 strcat(hfile, AFS_FREELANCE_INI);
1097 strcpy(hfile2, hfile);
1098 strcat(hfile2, "2");
1099 fp1=fopen(hfile, "r+");
1101 return CM_ERROR_INVAL;
1102 fp2=fopen(hfile2, "w+");
1105 return CM_ERROR_INVAL;
1108 fgets(line, sizeof(line), fp1);
1110 fprintf(fp2, "%d\n", n-1);
1112 for (i=0; i<n; i++) {
1113 fgets(line, sizeof(line), fp1);
1114 cp=strchr(line, '#');
1116 cp=strchr(line, '%');
1117 memcpy(shortname, line, cp-line);
1118 shortname[cp-line]=0;
1120 if (strcmp(shortname, toremove)==0) {
1131 return CM_ERROR_NOSUCHFILE;
1134 rename(hfile2, hfile);
1137 lock_ReleaseMutex(&cm_Freelance_Lock);
1138 cm_noteLocalMountPointChange();
1142 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1148 HKEY hkFreelanceSymlinks = 0;
1149 DWORD dwType, dwSize;
1154 /* before adding, verify the filename. If it is already in use, either as
1155 * as mount point or a cellname, do not permit the creation of the symlink.
1157 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1158 osi_LogSaveString(afsd_logp,filename),
1159 osi_LogSaveString(afsd_logp,destination));
1161 if ( filename[0] == '\0' || destination[0] == '\0' )
1165 if (filename[0] == '.') {
1166 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1167 if (stricmp(&filename[1],fullname) == 0)
1170 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1171 if (stricmp(filename,fullname) == 0)
1176 if ( cm_FreelanceMountPointExists(filename) ||
1177 cm_FreelanceSymlinkExists(filename) )
1181 lock_ObtainMutex(&cm_Freelance_Lock);
1184 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1185 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
1188 REG_OPTION_NON_VOLATILE,
1189 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1191 &hkFreelanceSymlinks,
1192 NULL) == ERROR_SUCCESS) {
1194 RegQueryInfoKey( hkFreelanceSymlinks,
1196 NULL, /* lpcClass */
1197 NULL, /* lpReserved */
1198 NULL, /* lpcSubKeys */
1199 NULL, /* lpcMaxSubKeyLen */
1200 NULL, /* lpcMaxClassLen */
1201 &dwSymlinks, /* lpcValues */
1202 NULL, /* lpcMaxValueNameLen */
1203 NULL, /* lpcMaxValueLen */
1204 NULL, /* lpcbSecurityDescriptor */
1205 NULL /* lpftLastWriteTime */
1208 sprintf(line, "%s:%s.", filename, destination);
1210 /* If we are adding a new value, there must be an unused name
1211 * within the range 0 to dwSymlinks
1213 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1217 dwSize = sizeof(szLink);
1218 sprintf(szIndex, "%d", dwIndex);
1219 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1220 /* found an unused value */
1222 dwSize = strlen(line) + 1;
1223 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1226 int len = strlen(filename);
1227 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1228 /* Replace the existing value */
1230 dwSize = strlen(line) + 1;
1231 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1236 RegCloseKey(hkFreelanceSymlinks);
1239 lock_ReleaseMutex(&cm_Freelance_Lock);
1241 /* cm_reInitLocalMountPoints(); */
1244 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1246 cm_noteLocalMountPointChange();
1250 long cm_FreelanceRemoveSymlink(char *toremove)
1254 char shortname[200];
1257 HKEY hkFreelanceSymlinks = 0;
1258 DWORD dwType, dwSize;
1263 lock_ObtainMutex(&cm_Freelance_Lock);
1266 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1267 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
1269 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1270 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1272 RegQueryInfoKey( hkFreelanceSymlinks,
1274 NULL, /* lpcClass */
1275 NULL, /* lpReserved */
1276 NULL, /* lpcSubKeys */
1277 NULL, /* lpcMaxSubKeyLen */
1278 NULL, /* lpcMaxClassLen */
1279 &dwSymlinks, /* lpcValues */
1280 NULL, /* lpcMaxValueNameLen */
1281 NULL, /* lpcMaxValueLen */
1282 NULL, /* lpcbSecurityDescriptor */
1283 NULL /* lpftLastWriteTime */
1286 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1287 TCHAR szValueName[16];
1288 DWORD dwValueSize = 16;
1289 dwSize = sizeof(line);
1290 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1291 &dwType, line, &dwSize);
1293 cp=strchr(line, ':');
1294 memcpy(shortname, line, cp-line);
1295 shortname[cp-line]=0;
1297 if (!strcmp(shortname, toremove)) {
1298 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1302 RegCloseKey(hkFreelanceSymlinks);
1306 lock_ReleaseMutex(&cm_Freelance_Lock);
1307 cm_noteLocalMountPointChange();
1310 #endif /* AFS_FREELANCE_CLIENT */