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;
751 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
760 HKEY hkFreelance = 0;
761 DWORD dwType, dwSize;
766 /* before adding, verify the cell name; if it is not a valid cell,
767 don't add the mount point.
768 allow partial matches as a means of poor man's alias. */
769 /* major performance issue? */
770 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
771 osi_LogSaveString(afsd_logp,filename),
772 osi_LogSaveString(afsd_logp,cellname),
773 osi_LogSaveString(afsd_logp,volume),
775 if (cellname[0] == '.') {
776 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
779 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
783 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
784 osi_LogSaveString(afsd_logp,cellname));
786 lock_ObtainMutex(&cm_Freelance_Lock);
789 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
790 "SOFTWARE\\OpenAFS\\Client\\Freelance",
792 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
793 &hkFreelance) == ERROR_SUCCESS) {
795 RegQueryInfoKey( hkFreelance,
798 NULL, /* lpReserved */
799 NULL, /* lpcSubKeys */
800 NULL, /* lpcMaxSubKeyLen */
801 NULL, /* lpcMaxClassLen */
802 &dwMountPoints, /* lpcValues */
803 NULL, /* lpcMaxValueNameLen */
804 NULL, /* lpcMaxValueLen */
805 NULL, /* lpcbSecurityDescriptor */
806 NULL /* lpftLastWriteTime */
810 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
812 sprintf(line, "%s#%s:%s", filename, fullname, volume);
814 /* If we are adding a new value, there must be an unused name
815 * within the range 0 to dwMountPoints
817 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
821 dwSize = sizeof(szMount);
822 sprintf(szIndex, "%d", dwIndex);
823 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
824 /* found an unused value */
826 dwSize = strlen(line) + 1;
827 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
830 int len = strlen(filename);
831 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
832 (szMount[len] == '%' || szMount[len] == '#')) {
833 /* Replace the existing value */
835 dwSize = strlen(line) + 1;
836 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
841 RegCloseKey(hkFreelance);
845 cm_GetConfigDir(hfile);
846 strcat(hfile, AFS_FREELANCE_INI);
847 fp = fopen(hfile, "r+");
849 return CM_ERROR_INVAL;
850 fgets(line, sizeof(line), fp);
853 fseek(fp, 0, SEEK_SET);
854 fprintf(fp, "%d", n);
855 fseek(fp, 0, SEEK_END);
857 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
859 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
862 lock_ReleaseMutex(&cm_Freelance_Lock);
864 /* cm_reInitLocalMountPoints(); */
867 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
869 cm_noteLocalMountPointChange();
873 long cm_FreelanceRemoveMount(char *toremove)
879 char hfile[120], hfile2[120];
883 HKEY hkFreelance = 0;
884 DWORD dwType, dwSize;
889 lock_ObtainMutex(&cm_Freelance_Lock);
893 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
894 "SOFTWARE\\OpenAFS\\Client\\Freelance",
896 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
897 &hkFreelance) == ERROR_SUCCESS) {
899 RegQueryInfoKey( hkFreelance,
902 NULL, /* lpReserved */
903 NULL, /* lpcSubKeys */
904 NULL, /* lpcMaxSubKeyLen */
905 NULL, /* lpcMaxClassLen */
906 &dwMountPoints, /* lpcValues */
907 NULL, /* lpcMaxValueNameLen */
908 NULL, /* lpcMaxValueLen */
909 NULL, /* lpcbSecurityDescriptor */
910 NULL /* lpftLastWriteTime */
913 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
914 TCHAR szValueName[16];
915 DWORD dwValueSize = 16;
916 dwSize = sizeof(line);
917 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
918 &dwType, line, &dwSize);
920 cp=strchr(line, '#');
922 cp=strchr(line, '%');
923 memcpy(shortname, line, cp-line);
924 shortname[cp-line]=0;
926 if (!strcmp(shortname, toremove)) {
927 RegDeleteValue( hkFreelance, szValueName );
931 RegCloseKey(hkFreelance);
935 cm_GetConfigDir(hfile);
936 strcat(hfile, AFS_FREELANCE_INI);
937 strcpy(hfile2, hfile);
939 fp1=fopen(hfile, "r+");
941 return CM_ERROR_INVAL;
942 fp2=fopen(hfile2, "w+");
945 return CM_ERROR_INVAL;
948 fgets(line, sizeof(line), fp1);
950 fprintf(fp2, "%d\n", n-1);
952 for (i=0; i<n; i++) {
953 fgets(line, sizeof(line), fp1);
954 cp=strchr(line, '#');
956 cp=strchr(line, '%');
957 memcpy(shortname, line, cp-line);
958 shortname[cp-line]=0;
960 if (strcmp(shortname, toremove)==0) {
971 return CM_ERROR_NOSUCHFILE;
974 rename(hfile2, hfile);
977 lock_ReleaseMutex(&cm_Freelance_Lock);
978 cm_noteLocalMountPointChange();
982 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
991 HKEY hkFreelanceSymlinks = 0;
992 DWORD dwType, dwSize;
997 /* before adding, verify the cell name; if it is not a valid cell,
998 don't add the mount point.
999 allow partial matches as a means of poor man's alias. */
1000 /* major performance issue? */
1001 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1002 osi_LogSaveString(afsd_logp,filename),
1003 osi_LogSaveString(afsd_logp,destination));
1005 lock_ObtainMutex(&cm_Freelance_Lock);
1008 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1009 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
1012 REG_OPTION_NON_VOLATILE,
1013 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1015 &hkFreelanceSymlinks,
1016 NULL) == ERROR_SUCCESS) {
1018 RegQueryInfoKey( hkFreelanceSymlinks,
1020 NULL, /* lpcClass */
1021 NULL, /* lpReserved */
1022 NULL, /* lpcSubKeys */
1023 NULL, /* lpcMaxSubKeyLen */
1024 NULL, /* lpcMaxClassLen */
1025 &dwSymlinks, /* lpcValues */
1026 NULL, /* lpcMaxValueNameLen */
1027 NULL, /* lpcMaxValueLen */
1028 NULL, /* lpcbSecurityDescriptor */
1029 NULL /* lpftLastWriteTime */
1032 sprintf(line, "%s:%s.", filename, destination);
1034 /* If we are adding a new value, there must be an unused name
1035 * within the range 0 to dwSymlinks
1037 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1041 dwSize = sizeof(szLink);
1042 sprintf(szIndex, "%d", dwIndex);
1043 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1044 /* found an unused value */
1046 dwSize = strlen(line) + 1;
1047 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1050 int len = strlen(filename);
1051 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1052 /* Replace the existing value */
1054 dwSize = strlen(line) + 1;
1055 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1060 RegCloseKey(hkFreelanceSymlinks);
1063 lock_ReleaseMutex(&cm_Freelance_Lock);
1065 /* cm_reInitLocalMountPoints(); */
1068 fidp->vnode = cm_noLocalMountPoints + 1; /* vnode value of last mt pt */
1070 cm_noteLocalMountPointChange();
1074 long cm_FreelanceRemoveSymlink(char *toremove)
1079 char shortname[200];
1080 char hfile[120], hfile2[120];
1084 HKEY hkFreelanceSymlinks = 0;
1085 DWORD dwType, dwSize;
1090 lock_ObtainMutex(&cm_Freelance_Lock);
1094 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1095 "SOFTWARE\\OpenAFS\\Client\\Freelance\\Symlinks",
1097 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1098 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1100 RegQueryInfoKey( hkFreelanceSymlinks,
1102 NULL, /* lpcClass */
1103 NULL, /* lpReserved */
1104 NULL, /* lpcSubKeys */
1105 NULL, /* lpcMaxSubKeyLen */
1106 NULL, /* lpcMaxClassLen */
1107 &dwSymlinks, /* lpcValues */
1108 NULL, /* lpcMaxValueNameLen */
1109 NULL, /* lpcMaxValueLen */
1110 NULL, /* lpcbSecurityDescriptor */
1111 NULL /* lpftLastWriteTime */
1114 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1115 TCHAR szValueName[16];
1116 DWORD dwValueSize = 16;
1117 dwSize = sizeof(line);
1118 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1119 &dwType, line, &dwSize);
1121 cp=strchr(line, ':');
1122 memcpy(shortname, line, cp-line);
1123 shortname[cp-line]=0;
1125 if (!strcmp(shortname, toremove)) {
1126 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1130 RegCloseKey(hkFreelanceSymlinks);
1134 lock_ReleaseMutex(&cm_Freelance_Lock);
1135 cm_noteLocalMountPointChange();
1138 #endif /* AFS_FREELANCE_CLIENT */