filemodule.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,844 行 · 第 1/5 页
JAVA
2,844 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.lib.file;import com.caucho.quercus.QuercusModuleException;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.env.*;import com.caucho.quercus.lib.UrlModule;import com.caucho.quercus.lib.string.StringModule;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.quercus.module.IniDefinitions;import com.caucho.quercus.module.IniDefinition;import com.caucho.util.Alarm;import com.caucho.util.L10N;import com.caucho.vfs.Path;import com.caucho.vfs.ReadStream;import com.caucho.vfs.WriteStream;import com.caucho.vfs.LockableStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;/** * Information and actions for about files */public class FileModule extends AbstractQuercusModule { private static final L10N L = new L10N(FileModule.class); private static final Logger log = Logger.getLogger(FileModule.class.getName()); public static final String DIRECTORY_SEPARATOR = "" + Path.getFileSeparatorChar(); public static final String PATH_SEPARATOR = "" + Path.getPathSeparatorChar(); public static final int UPLOAD_ERR_OK = 0; public static final int UPLOAD_ERR_INI_SIZE = 1; public static final int UPLOAD_ERR_FORM_SIZE = 2; public static final int UPLOAD_ERR_PARTIAL = 3; public static final int UPLOAD_ERR_NO_FILE = 4; public static final int UPLOAD_ERR_NO_TMP_DIR = 6; public static final int UPLOAD_ERR_CANT_WRITE = 7; public static final int UPLOAD_ERR_EXTENSION = 8; public static final int FILE_USE_INCLUDE_PATH = 1; public static final int FILE_APPEND = 8; public static final int LOCK_SH = 1; public static final int LOCK_EX = 2; public static final int LOCK_UN = 3; public static final int LOCK_NB = 4; public static final int FNM_PATHNAME = 1; public static final int FNM_NOESCAPE = 2; public static final int FNM_PERIOD = 4; public static final int FNM_CASEFOLD = 16; public static final int GLOB_MARK = 1; public static final int GLOB_NOSORT = 2; public static final int GLOB_NOCHECK = 4; public static final int GLOB_NOESCAPE = 8; public static final int GLOB_BRACE = 16; public static final int GLOB_ONLYDIR = 32; public static final int GLOB_ERR = 64; public static final int PATHINFO_DIRNAME = 1; public static final int PATHINFO_BASENAME = 2; public static final int PATHINFO_EXTENSION = 4; public static final int PATHINFO_FILENAME = 8; public static final int SEEK_SET = BinaryStream.SEEK_SET; public static final int SEEK_CUR = BinaryStream.SEEK_CUR; public static final int SEEK_END = BinaryStream.SEEK_END; private static final IniDefinitions _iniDefinitions = new IniDefinitions(); private static final HashMap<String,Value> _constMap = new HashMap<String,Value>(); /** * Returns the default quercus.ini values. */ public IniDefinitions getIniDefinitions() { return _iniDefinitions; } /** * Returns the constants defined by this module. */ public Map<String,Value> getConstMap() { return _constMap; } /** * Returns the base name of a string. */ public static Value basename(StringValue path, @Optional StringValue suffix) { int len = path.length(); if (len == 0) return path; else if (path.charAt(len - 1) == '/') len -= 1; else if (path.charAt(len - 1) == '\\') len -= 1; int p = path.lastIndexOf('/', len - 1); if (p < 0) p = path.lastIndexOf('\\', len - 1); StringValue file; if (p < 0) file = path.substring(0, len); else file = path.substring(p + 1, len); if (suffix != null && file.endsWith(suffix)) file = file.substring(0, file.length() - suffix.length()); return file; } /** * Changes the working directory * * @param path the path to change to */ public static boolean chdir(Env env, Path path) { if (path.isDirectory()) { env.setPwd(path); return true; } else { env.warning(L.l("{0} is not a directory", path.getFullPath())); return false; } } /** * Changes the working directory, forming a virtual root * * @param path the path to change to */ public static boolean chroot(Env env, Path path) { if (path.isDirectory()) { env.setPwd(path.createRoot()); return true; } else { env.warning(L.l("{0} is not a directory", path.getFullPath())); return false; } } /** * Changes the group of the file. * * @param env the PHP executing environment * @param file the file to change the group of * @param group the group id to change to */ public static boolean chgrp(Env env, Path file, Value group) { if (!file.canRead()) { env.warning(L.l("{0} cannot be read", file.getFullPath())); return false; } // quercus/160i try { // XXX: safe_mode if (group instanceof LongValue) file.changeGroup(group.toInt()); else file.changeGroup(group.toString()); return true; } catch (IOException e) { log.log(Level.FINE, e.toString(), e); return false; } } /** * Changes the permissions of the file. * * @param env the PHP executing environment * @param file the file to change the group of * @param mode the mode id to change to */ public static boolean chmod(Env env, Path file, int mode) { if (! file.canRead()) { // XXX: gallery? env.warning(L.l("{0} cannot be read", file.getFullPath())); return false; } // quercus/160j file.chmod(mode); return true; } /** * Changes the ownership of the file. * * @param env the PHP executing environment * @param file the file to change the group of * @param user the user id to change to */ public static boolean chown(Env env, Path file, Value user) { if (!file.canRead()) { env.warning(L.l("{0} cannot be read", file.getFullPath())); return false; } try { // XXX: safe_mode if (user instanceof LongValue) file.changeOwner(user.toInt()); else file.changeOwner(user.toString()); return true; } catch (IOException e) { log.log(Level.FINE, e.toString(), e); return false; } } /** * Clears the stat cache for the file * * @param env the PHP executing environment */ public static Value clearstatcache(Env env) { // quercus/160l // XXX: stubbed return NullValue.NULL; } /** * Copies a file to the destination. * * @param src the source path * @param dst the destination path */ public static boolean copy(Env env, Path src, Path dst) { // quercus/1603 try { if (! src.canRead() || ! src.isFile()) { env.warning(L.l("{0} cannot be read", src.getFullPath())); return false; } WriteStream os = dst.openWrite(); try { src.writeToStream(os); } finally { os.close(); } return true; } catch (IOException e) { log.log(Level.FINE, e.toString(), e); return false; } } /** * Opens a directory * * @param path the path to change to */ @ReturnNullAsFalse public static Directory dir(Env env, Path path) { try { if (! path.isDirectory()) { env.warning(L.l("{0} is not a directory", path.getFullPath())); return null; } return new Directory(env, path);/* DirectoryValue dir = new DirectoryValue(path); env.addClose(dir); return dir;*/ } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Returns the directory name of a string. */ public StringValue dirname(Env env, StringValue path) { int len = path.length(); if (len == 0) return env.createString("."); else if (len == 1 && path.charAt(0) == '/') return path; int p = path.lastIndexOf('/', len - 2); // php/1601 (for Windows) p = Math.max(p, path.lastIndexOf('\\', len - 2)); if (p == 0) return env.createString("/"); else if (p > 0) return path.substring(0, p); p = path.lastIndexOf('\\', len - 2); if (p == 0) return env.createString("\\"); else if (p > 0) return path.substring(0, p); return env.createString("."); } /** * Returns the free space for disk partition containing the directory * * @param directory the disk directory */ public static Value disk_free_space(Env env, Path directory) { // quercus/160m if (! directory.canRead()) { env.warning(L.l("{0} cannot be read", directory.getFullPath())); return BooleanValue.FALSE; } return new DoubleValue(directory.getDiskSpaceFree()); } /** * Returns the total space for disk partition containing the directory * * @param directory the disk directory */ public static Value disk_total_space(Env env, Path directory) { // quercus/160n if (! directory.canRead()) { env.warning(L.l("{0} cannot be read", directory.getFullPath())); return BooleanValue.FALSE; } return new DoubleValue(directory.getDiskSpaceTotal()); } /** * Returns the total space for disk partition containing the directory * * @param directory the disk directory */ public static Value diskfreespace(Env env, Path directory) { return disk_free_space(env, directory); } /** * Closes a file. */ public static boolean fclose(Env env, @NotNull BinaryStream s) { if (s == null) return false; s.close(); return true; } /** * Checks for the end of file. */ public static boolean feof(Env env, @NotNull BinaryStream binaryStream) { if (binaryStream == null) return false; return binaryStream.isEOF(); } /** * Flushes a file. */ public static boolean fflush(Env env, @NotNull BinaryOutput os) { if (os == null) return false; try { os.flush(); return true; } catch (IOException e) { return false; } } /** * Returns the next character as a byte */ public static Value fgetc(Env env, @NotNull BinaryInput is) { if (is == null) return BooleanValue.FALSE; try { // XXX: char for i18n and mode = "t" // php/1612 int ch = is.read(); if (ch >= 0) { StringValue v = env.createBinaryBuilder(1); v.append((char) ch); return v; } else return BooleanValue.FALSE; } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Parses a comma-separated-value line from a file. * * @param file the file to read * @param length the maximum line length * @param delimiter optional comma replacement * @param enclosure optional quote replacement */ public Value fgetcsv(Env env, @NotNull BinaryInput is, @Optional int length, @Optional String delimiter, @Optional String enclosure) { // php/1619 try { if (is == null) return BooleanValue.FALSE; // XXX: length is never used if (length <= 0) length = Integer.MAX_VALUE; int comma = ','; if (delimiter != null && delimiter.length() > 0) comma = delimiter.charAt(0); int quote = '"'; if (enclosure != null && enclosure.length() > 0) quote = enclosure.charAt(0); ArrayValue array = new ArrayValueImpl(); int ch; while (true) { // scan whitespace while (true) { ch = is.read(); if (ch < 0 || ch == '\n') return array; else if (ch == '\r') { is.readOptionalLinefeed(); return array; } else if (ch == ' ' || ch == '\t') continue; else break; } StringValue sb = env.createBinaryBuilder();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?