/** * Creates a directory hierarhy. */ Bool FILE_MkDir(Str dir) { Bool ok = False; StrBuf64 entry; StrBuf* sb = &entry.sb; STRBUF_InitBufXXX(&entry); if (STRBUF_Copy(sb, dir)) { while (sb->len > 0 && FILE_IsFileSeparator(STRBUF_LastChar(sb))) { STRBUF_SetLength(sb, sb->len-1); } /* check if the directory already exists */ if (FILE_IsDir(STRBUF_Text(sb)) || FILE_CreateDir(STRBUF_Text(sb))) { ok = True; } else { /* directory does not exists, walk the hierarhy */ int pos = 0; int next = 0; while ((next = FILE_FindSeparator(dir+pos)) >= 0) { STRBUF_Clear(sb); if (next == 0) { pos++; continue; } else { if (!STRBUF_CopyN(sb,dir,pos+next) || !FILE_CreateDir(STRBUF_Text(sb))) { break; } pos += next + 1; } } /* final test */ if (STRBUF_Copy(sb, dir)) { while (sb->len && FILE_IsFileSeparator(STRBUF_LastChar(sb))) { STRBUF_SetLength(sb, sb->len-1); } if (FILE_IsDir(STRBUF_Text(sb)) || FILE_CreateDir(STRBUF_Text(sb))) { ok = True; } } } } STRBUF_Destroy(sb); return ok; }
Bool DIR_ItrRemove(Iterator * itr) { Bool ok = False; DirIterator * di = CAST(itr,DirIterator,itr); DirType type = di->entry.type; size_t dirlen = STRBUF_Length(&di->dirName); if (STRBUF_Alloc(&di->dirName, dirlen+STRBUF_Length(&di->fileName)+1)) { Str fullName; Bool isDir; STRBUF_AppendChar(&di->dirName, FILE_SEPARATOR_CHAR); STRBUF_AppendBuf(&di->dirName, &di->fileName); fullName = STRBUF_Text(&di->dirName); if (type == DTypeUnknown) { isDir = FILE_IsDir(fullName); } else { isDir = BoolValue(type == DTypeDir); } if (isDir) { ok = FILE_RmDir(fullName, True); } else { ok = FILE_Delete(fullName); } STRBUF_SetLength(&di->dirName, dirlen); di->entry.dir = STRBUF_Text(&di->dirName); } return ok; }
/** * Internal function for creating a directory. */ Bool FILE_CreateDir(Str dir) { if (CreateDirectory(dir, NULL)) { return True; } else if (GetLastError() != ERROR_ALREADY_EXISTS) { return False; } return FILE_IsDir(dir); }
/** * Removes the directory. If recurse parameter is True, also removes * all files and subdirectories, otherwise it fails if directory is not * empty. */ Bool FILE_RmDir(Str dir, Bool recurse) { if (FILE_IsDir(dir)) { if (recurse) { FILE_List(dir, RmDirCB, NULL); } #ifdef _WIN32 return RemoveDirectory(dir); #else return BoolValue(Rmdir(dir) == 0); #endif /* WIN32 */ } return False; }
/** * FILE_RmDir callback. Recursively destroys the directory contents */ STATIC Bool RmDirCB(Str dir, Str fname, void * ctx) { Bool ok = False; StrBuf64 path; UNREF(ctx); STRBUF_InitBufXXX(&path); if (STRBUF_Copy(&path.sb, dir) && STRBUF_AppendChar(&path.sb, FILE_SEPARATOR_CHAR) && STRBUF_Append(&path.sb, fname)) { ok = True; if (FILE_IsDir(path.sb.s)) { FILE_RmDir(path.sb.s, True); } else { FILE_Delete(path.sb.s); } } STRBUF_Destroy(&path.sb); return ok; }
/** * Discovers all available JVMs. If no JVMs are discovered, returns NULL. * In addition to the standard directories, also looks in the additional * directories specified by the dirs array. Note that this directory list * replaces the default list ("jre","../jre") used by JVM_Find, it does not * adds new directories to the list. */ JVMSet * JVM_Find2(const Str dirs[], int n) { JVMSet * jvms = MEM_New(JVMSet); if (jvms) { memset(jvms, 0, sizeof(*jvms)); if (VECTOR_Init(&jvms->found, 0, JVM_VectorEquals, JVM_VectorFree)) { /* Look for JVMs in the Windows registry */ JVM_Discover(jvms); /* Look for JVMs in the additional directories */ if (n > 0) { int i; StrBuf sb,sb2; Char* baseDir = NULL; STRBUF_Init(&sb); STRBUF_Init(&sb2); TRACE("JNILIB: checking special directories\n"); for (i=0; i<n; i++) { Str javaHome = NULL; JvmPathType pathType = JVM_GetPathType(dirs[i]); if (pathType == JvmPathRelative) { LPTSTR filePath; TRACE1("JNILIB: relative path: %s\n",dirs[i]); if (baseDir) { STRBUF_Copy(&sb, baseDir); } else { int separator; JVM_GetModuleFileName(NULL,&sb); STRBUF_Replace(&sb, '/', '\\'); separator = STRBUF_LastIndexOf(&sb,'\\'); STRBUF_SetLength(&sb,separator+1); baseDir = STRBUF_Dup(&sb); if (!baseDir) continue; TRACE1("JNILIB: base dir: %s\n",baseDir); } STRBUF_Append(&sb, dirs[i]); STRBUF_Replace(&sb, '/', '\\'); STRBUF_Alloc(&sb2, STRBUF_Length(&sb)); sb2.len = GetFullPathName(STRBUF_Text(&sb), sb2.alloc, sb2.s, &filePath); ASSERT(sb2.len && sb2.s[0]); javaHome = STRBUF_Text(&sb2); } else if (pathType == JvmPathAbsolute) { TRACE1("JNILIB: absolute path: %s\n",dirs[i]); javaHome = dirs[i]; } else if (pathType == JvmPathSystem) { /* directory on the system drive */ TRACE1("JNILIB: system path: %s\n",dirs[i]); STRBUF_Alloc(&sb,GetSystemDirectory(NULL,0)+1); STRBUF_SetLength(&sb,GetSystemDirectory(sb.s,sb.alloc)); STRBUF_Clear(&sb2); STRBUF_AppendChar(&sb2,STRBUF_CharAt(&sb,0)); STRBUF_AppendChar(&sb2,':'); STRBUF_Append(&sb2, dirs[i]); javaHome = STRBUF_Text(&sb2); } else { TRACE1("JNILIB: invalid path: %s\n",dirs[i]); continue; } if (javaHome) { TRACE1("JNILIB: Java home: %s\n",javaHome); if (FILE_IsDir(javaHome)) { JVM* jvm = JVM_CreateDirContext(javaHome); if (jvm) { jvm->flags |= JVM_FLAG_SPECIAL; if (JVM_Add(jvms, jvm) && !jvms->specialVM) { jvms->specialVM = jvm; } } } else { TRACE1("JNILIB: no such directory: %s\n",javaHome); } } } MEM_Free(baseDir); STRBUF_Destroy(&sb); STRBUF_Destroy(&sb2); } /* Did we find anything? */ if (!VECTOR_IsEmpty(&jvms->found)) { JVM_Sort(jvms, JVM_DefaultSort); TRACE1("JNILIB: found %d JVM(s)\n",VECTOR_Size(&jvms->found)); return jvms; } TRACE("JNILIB: found no JVMs\n, sorry"); VECTOR_Destroy(&jvms->found); } MEM_Free(jvms); } return NULL; }
/** * Attempts to find java.exe and Jvm.dll path for the specified JVM and * updates javaHome and javaLib fields */ STATIC Bool JVM_Check(HKEY hJavaKey, JVM * jvm) { Bool ok = False; HKEY hVer = NULL; ULONG err; /* open the subkey for specific version */ TRACE2("JNILIB: checking %s %s\n",(jvm->flags & JVM_FLAG_JDK)?"JDK":"JRE", jvm->versionString); err = RegOpenKeyEx(hJavaKey, jvm->versionString, 0, KEY_READ, &hVer); if (err == NO_ERROR) { char s[MAX_PATH]; ULONG t = REG_SZ; ULONG n = COUNT(s); /* "JavaHome" must point to Java home directory */ err = RegQueryValueEx(hVer,JAVA_HOME,0,&t,(LPBYTE)s,&n); if (err == NO_ERROR) { s[COUNT(s)-1] = 0; if (jvm->flags & JVM_FLAG_JDK) { strncat(s, JRE_UNDER_JDK, COUNT(s)-strlen(s)); s[COUNT(s)-1] = 0; } if (FILE_IsDir(s)) { jvm->javaHome = STRING_Dup(s); /* "RuntimeLib" must point to jvm.dll unless this is a JDK */ s[0] = 0; if (!(jvm->flags & JVM_FLAG_JDK)) { n = COUNT(s); err = RegQueryValueEx(hVer,RUNTIME_LIB,0,&t,(LPBYTE)s,&n); } if ((jvm->flags & JVM_FLAG_JDK) || err == NO_ERROR) { /* If this is a JDK, we have to guess where jvm.dll is */ if (jvm->flags & JVM_FLAG_JDK) { snprintf(s,COUNT(s),"%s\\%s",jvm->javaHome,JVM_DLL_1); s[COUNT(s)-1] = 0; } if (FILE_IsFile(s)) { jvm->javaLib = STRING_Dup(s); if (jvm->javaLib) { TRACE1("JNILIB: Java runtime lib: %s\n",s); ok = True; } } else { TRACE1("JNILIB: no such file: %s\n",s); if (jvm->flags & JVM_FLAG_JDK) { snprintf(s,COUNT(s),"%s\\%s",jvm->javaHome,JVM_DLL_2); s[COUNT(s)-1] = 0; if (FILE_IsFile(s)) { jvm->javaLib = STRING_Dup(s); if (jvm->javaLib) { TRACE1("JNILIB: Java runtime lib: %s\n",s); ok = True; } } else { TRACE1("JNILIB: no such file: %s\n",s); } } } } else { TRACE3("JNILIB: can't read %s from %s\\%s registry key\n", RUNTIME_LIB, (jvm->flags & JVM_FLAG_JDK) ? JVM_REG_KEY : JRE_REG_KEY, jvm->versionString); } } else { TRACE1("JNILIB: no such directory: %s\n",s); } } else { TRACE3("JNILIB: can't read %s from the %s\\%s\n", JAVA_HOME, (jvm->flags & JVM_FLAG_JDK) ? JVM_REG_KEY : JRE_REG_KEY, jvm->versionString); } RegCloseKey(hVer); } return ok; }