filemodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,844 行 · 第 1/5 页
JAVA
2,844 行
String cwdPattern; String subPattern = null; int firstSlash = pattern.indexOf('/'); if (firstSlash < 0) cwdPattern = pattern; else { cwdPattern = pattern.substring(0, firstSlash); // strip off any extra slashes for (; firstSlash < pattern.length(); firstSlash++) { if (pattern.charAt(firstSlash) != '/') break; } subPattern = pattern.substring(firstSlash); } int fnmatchFlags = 0; if ((flags & GLOB_NOESCAPE) != 0) fnmatchFlags = FNM_NOESCAPE; boolean doBraces = (flags & GLOB_BRACE) != 0; String globRegex = globToRegex(cwdPattern, fnmatchFlags, doBraces); if (globRegex == null) return null; Pattern compiledGlobRegex; try { compiledGlobRegex = Pattern.compile(globRegex); } catch (PatternSyntaxException e) { log.log(Level.FINE, e.toString(), e); return null; } String [] list; try { list = path.list(); } catch (IOException e) { log.log(Level.FINE, e.toString(), e); return null; } for (String entry : list) { Matcher matcher = compiledGlobRegex.matcher(entry); if (matcher.matches()) { StringValue sb = env.createUnicodeBuilder(); if (prefix.length() > 0) { sb.append(prefix); if (! prefix.equals("/")) sb.append("/"); } sb.append(entry); Path entryPath = path.lookup(entry); if (entryPath != null && entryPath.isDirectory()) { if (firstSlash >= 0 && subPattern.length() > 0) { // ArrayValue.add only adds values when the argument is an // actual array boolean isNull = null == globImpl(env, subPattern, flags, entryPath, sb.toString(), result); if ((flags & GLOB_ERR) != 0 && isNull) return null; } else if ((flags & GLOB_MARK) != 0) { sb.append("/"); } } if ((firstSlash < 0 || subPattern.length() == 0) && (((flags & GLOB_ONLYDIR) == 0) || (((flags & GLOB_ONLYDIR) != 0) && (entryPath != null && entryPath.isDirectory())))) { result.put(sb); } } } return result; } /** * Matches all files with the given pattern. */ public static Value glob(Env env, String pattern, @Optional int flags) { Path path = env.getPwd(); int patternLength = pattern.length(); String prefix = ""; int braceIndex; if ((flags & GLOB_BRACE) != 0 && (braceIndex = pattern.indexOf('{')) >= 0) { if ((flags & GLOB_NOESCAPE) != 0) return globBrace(env, pattern, flags, braceIndex); int i = 0; boolean isEscaped = false; // find open bracket '{' while (i < patternLength) { char ch = pattern.charAt(i); if (ch == '\\') isEscaped = ! isEscaped; else if (ch == '{') { if (isEscaped) isEscaped = false; else break; } else isEscaped = false; i++; } if (i < patternLength) return globBrace(env, pattern, flags, i); } if (patternLength > 0 && pattern.charAt(0) == '/') { prefix = "/"; int i; // strip off any leading slashes for (i = 0; i < patternLength; i++) { if (pattern.charAt(i) != '/') break; } path = path.lookup("/"); pattern = pattern.substring(i); } else if (Path.isWindows() && patternLength > 2 && pattern.charAt(1) == ':') { prefix = pattern.substring(0, 2); String driveLetter = pattern.substring(0, 2); // X:/ - slash is required when looking up root path = path.lookup(driveLetter + '/'); pattern = pattern.substring(3); } ArrayValue result = new ArrayValueImpl(); result = globImpl(env, pattern, flags, path, prefix, result); if (result == null) return BooleanValue.FALSE; else if (result.getSize() == 0 && (flags & GLOB_NOCHECK) != 0) result.put(pattern); if ((flags & GLOB_NOSORT) == 0) result.sort(ArrayValue.ValueComparator.CMP, true, true); return result; } /* * Breaks a glob with braces into multiple globs. */ private static Value globBrace(Env env, String pattern, int flags, int braceIndex) { int patternLength = pattern.length(); boolean isEscaped = false; String prefix = pattern.substring(0, braceIndex); ArrayList<StringBuilder> basePathList = new ArrayList<StringBuilder>(); StringBuilder sb = new StringBuilder(); sb.append(prefix); isEscaped = false; int i = braceIndex + 1; // parse bracket contents while (i < patternLength) { char ch = pattern.charAt(i++); if (ch == ',') { if (isEscaped) { isEscaped = false; sb.append(','); } else { basePathList.add(sb); sb = new StringBuilder(); sb.append(prefix); } } else if (ch == '}') { if (isEscaped) { isEscaped = false; sb.append('}'); } else { basePathList.add(sb); break; } } else if (ch == '\\') { if ((flags & GLOB_NOESCAPE) != 0) sb.append('\\'); else if (isEscaped) { isEscaped = false; sb.append('\\'); sb.append('\\'); } else isEscaped = true; } else { if (isEscaped) { isEscaped = false; sb.append('\\'); } sb.append(ch); } } String suffix = ""; if (i < patternLength) suffix = pattern.substring(i); Value result = null; for (StringBuilder path : basePathList) { path.append(suffix); Value subresult = glob(env, path.toString(), flags); if (subresult.isArray() && subresult.getSize() > 0) { if (prefix.length() == 0 && suffix.length() == 0) return subresult; else { if (result == null) result = subresult; else { Iterator<Value> iter = subresult.getValueIterator(env); while (iter.hasNext()) result.put(iter.next()); } } } } if (result == null) result = new ArrayValueImpl(); return result; } /** * Returns the current working directory. * * @return the current directory */ public static String getcwd(Env env) { // for xoops on Windows // paths returned must be consistent across Quercus return env.getPwd().getPath(); // return env.getPwd().getNativePath(); } /** * Returns true if the path is a directory. * * @param path the path to check */ public static boolean is_dir(@NotNull Path path) { if (path == null) return false; return path.isDirectory(); } /** * Returns true if the path is an executable file * * @param path the path to check */ public static boolean is_executable(@NotNull Path path) { if (path == null) return false; return path.isExecutable(); } /** * Returns true if the path is a file. * * @param path the path to check */ public static boolean is_file(@NotNull Path path) { if (path == null) return false; return path.isFile(); } /** * Returns true if the path is a symbolic link * * @param path the path to check */ public static boolean is_link(Env env, @NotNull Path path) { if (path == null) return false; return path.isLink(); } /** * Returns true if the path is readable * * @param path the path to check */ public static boolean is_readable(Path path) { if (path == null) return false; return path.canRead(); } /** * Returns true for an uploaded file. * * @param path the temp name of the uploaded file */ public static boolean is_uploaded_file(Env env, @NotNull Path path) { // php/1663, php/1664 if (path == null) return false; String tail = path.getTail(); return env.getUploadDirectory().lookup(tail).canRead(); } /** * Returns true if the path is writable * * @param path the path to check */ public static boolean is_writable(Path path) { if (path == null) return false; return path.canWrite(); } /** * Returns true if the path is writable * * @param path the path to check */ public static boolean is_writeable(Path path) { if (path == null) return false; return is_writable(path); } /** * Creates a hard link */ public boolean link(Env env, Path source, Path destination) { try { return destination.createLink(source, true); } catch (Exception e) { env.warning(e); return false; } } public static long linkinfo(Env env, Path path) { // XXX: Hack to trigger lstat() in JNI code if (path.isLink()) return path.getDevice(); else return 0; } /** * Returns file statistics */ public static Value lstat(Env env, StringValue filename) { ProtocolWrapper wrapper = getProtocolWrapper(env, filename); if (wrapper != null) // XXX flags? return wrapper.url_stat(env, filename, LongValue.create(StreamModule.STREAM_URL_STAT_LINK)); Path path = env.getPwd().lookup(filename.toString()); // XXX: Hack to trigger lstat() in JNI code path.isLink(); return statImpl(env, path); } /** * Makes the directory * * @param path the directory to make */ public static boolean mkdir(Env env, StringValue dirname, @Optional int mode, @Optional boolean recursive, @Optional Value context) { ProtocolWrapper wrapper = getProtocolWrapper(env, dirname); if (wrapper != null) // XXX options? return wrapper.mkdir(env, dirname, LongValue.create(mode), LongValue.ZERO); Path path = env.getPwd().lookup(dirname.toString()); try { if (recursive) return path.mkdirs(); else return path.mkdir(); } catch (IOException e) { log.log(Level.FINE, e.toString(), e); return false; } } /** * Moves the uploaded file. * * @param path the temp name of the uploaded file * @param dst the destination path */ public static boolean move_uploaded_file(Env env, @NotNull Path src, @NotNull Path dst) { // php/1665, php/1666 if (src == null) return false; if (dst == null) return false; String tail = src.getTail(); src = env.getUploadDirectory().lookup(tail); try { if (src.canRead()) { src.renameTo(dst); return true; } else return false; } catch (IOException e) { env.warning(e); return false; } } /** * Opens a directory * * @param pathName the directory to open */ public static Value opendir(Env env, StringValue pathName, @Optional Value context) { ProtocolWrapper wrapper = getProtocolWrapper(env, pathName); if (wrapper != null) /// XXX options? return wrapper.opendir(env, pathName, LongValue.ZERO); try { Path path = env.getPwd().lookup(pathName.toString()); if (path.isDirectory()) return new DirectoryValue(env, path); else { env.warning(L.l("{0} is not a directory", path.getFullPath())); return BooleanValue.FALSE; } } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Parses the ini file. */ public static Value parse_ini_file(Env env, Path path, @Optional boolean processSections) { ReadStream is = null; try { is = path.openRead(); is.setEncoding(env.getScriptEncoding()); return parseIni(env, is, processSections); } catch (IOException e) { env.warning(e); return BooleanValue.FALSE; } finally { if (is != null) is.close(); } } private static ArrayValue parseIni(Env env,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?