コード例 #1
0
 /**
  * Set the base directory of persistent databases, unless the database is in the user home folder
  * (~).
  *
  * @param dir the new base directory
  */
 public void setBaseDir(String dir) {
   if (persistent) {
     String absDir = FileUtils.unwrap(FileUtils.toRealPath(dir));
     boolean absolute = FileUtils.isAbsolute(name);
     String n;
     String prefix = null;
     if (dir.endsWith(SysProperties.FILE_SEPARATOR)) {
       dir = dir.substring(0, dir.length() - 1);
     }
     if (absolute) {
       n = name;
     } else {
       n = FileUtils.unwrap(name);
       prefix = name.substring(0, name.length() - n.length()); // 比如nio:./test,此时prefix就是"nio:"
       n = dir + SysProperties.FILE_SEPARATOR + n;
     }
     String normalizedName = FileUtils.unwrap(FileUtils.toRealPath(n));
     if (normalizedName.equals(absDir) || !normalizedName.startsWith(absDir)) {
       // database name matches the baseDir or
       // database name is clearly outside of the baseDir
       throw DbException.get(ErrorCode.IO_EXCEPTION_1, normalizedName + " outside " + absDir);
     }
     if (absDir.endsWith("/") || absDir.endsWith("\\")) {
       // no further checks are needed for C:/ and similar
     } else if (normalizedName.charAt(absDir.length()) != '/') {
       // database must be within the directory
       // (with baseDir=/test, the database name must not be
       // /test2/x and not /test2)
       throw DbException.get(ErrorCode.IO_EXCEPTION_1, normalizedName + " outside " + absDir);
     }
     if (!absolute) {
       // 可能是个bug,应该用absDir替换dir,
       // 否则当设置了baseDir时,还是不能用没有jdbc:h2:mydb这样的url,
       // 但是在getName()中还是抛错,并且错误信息提示设置baseDir
       name = prefix + dir + SysProperties.FILE_SEPARATOR + FileUtils.unwrap(name);
     }
   }
 }
コード例 #2
0
 /**
  * Get the unique and normalized database name (excluding settings).
  *
  * @return the database name
  */
 public String getName() {
   if (!persistent) {
     return name;
   }
   if (nameNormalized == null) {
     if (!SysProperties.IMPLICIT_RELATIVE_PATH) {
       if (!FileUtils.isAbsolute(name)) {
         if (name.indexOf("./") < 0
             && name.indexOf(".\\") < 0
             && name.indexOf(":/") < 0
             && name.indexOf(":\\") < 0) {
           // the name could start with "./", or
           // it could start with a prefix such as "nio:./"
           // for Windows, the path "\test" is not considered
           // absolute as the drive letter is missing,
           // but we consider it absolute
           throw DbException.get(ErrorCode.URL_RELATIVE_TO_CWD, originalURL);
         }
       }
     }
     String suffix = Constants.SUFFIX_PAGE_FILE;
     String n;
     if (FileUtils.exists(name + suffix)) {
       n = FileUtils.toRealPath(name + suffix);
     } else {
       suffix = Constants.SUFFIX_MV_FILE;
       n = FileUtils.toRealPath(name + suffix);
     }
     String fileName = FileUtils.getName(n);
     if (fileName.length() < suffix.length() + 1) { // 例如: 没有设置baseDir且dbName="./"时
       throw DbException.get(ErrorCode.INVALID_DATABASE_NAME_1, name);
     }
     nameNormalized = n.substring(0, n.length() - suffix.length());
   }
   return nameNormalized;
 }