@Test
  public void testContinueOnSomeDbDirectoriesMissing() throws Exception {
    File targetDir1 = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    File targetDir2 = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    try {
      assertTrue(targetDir1.mkdirs());
      assertTrue(targetDir2.mkdirs());

      if (!targetDir1.setWritable(false, false)) {
        System.err.println(
            "Cannot execute 'testContinueOnSomeDbDirectoriesMissing' because cannot mark directory non-writable");
        return;
      }

      RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(TEMP_URI);
      rocksDbBackend.setDbStoragePaths(targetDir1.getAbsolutePath(), targetDir2.getAbsolutePath());

      try {
        rocksDbBackend.initializeForJob(getMockEnvironment(), "foobar", IntSerializer.INSTANCE);
      } catch (Exception e) {
        e.printStackTrace();
        fail("Backend initialization failed even though some paths were available");
      }
    } finally {
      //noinspection ResultOfMethodCallIgnored
      targetDir1.setWritable(true, false);
      FileUtils.deleteDirectory(targetDir1);
      FileUtils.deleteDirectory(targetDir2);
    }
  }
Beispiel #2
1
  public static void main(String[] args) {

    // create new file
    try {
      File file = new File("src/IOTasks/output/test.txt");
      if (file.createNewFile()) {
        System.out.println("File is created");
      } else {
        System.out.println("File already exisits");
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    // construct filepath
    try {
      String filename = "file.txt";
      String workingDirectory = System.getProperty("user.dir");

      // String absoluteFilePath = workingDirectory + File.separator + filename;
      // File file1 = new File(absoluteFilePath);

      File file1 = new File(workingDirectory, filename);
      file1.createNewFile();

      System.out.println("Final pathname : " + file1.getAbsolutePath());

      file1.setExecutable(false);
      file1.setReadOnly();

      if (file1.exists()) {
        System.out.println("executable : " + file1.canExecute());
        System.out.println("writable : " + file1.canWrite());
      }

      file1.setWritable(true);

      File file2 = new File("src/IOTasks/input/file.jpg");

      long lastModifiedTime = file2.lastModified();
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY hh:mm:ss");
      System.out.println("Last modified : " + sdf.format(lastModifiedTime));

      String newLastModified = "31/01/2015 10:15:21";
      // convert to miliseconds in long
      Date newDate = sdf.parse(newLastModified);
      file2.setLastModified(newDate.getTime());

      long size = file2.length();
      System.out.println(size / 1024 + " kB");

    } catch (IOException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    }

    // get file size

  }
 /** Generate a local password and save it in the local-password file. */
 public void postConstruct() {
   logger.fine("Generating local password");
   SecureRandom random = new SecureRandom();
   byte[] pwd = new byte[PASSWORD_BYTES];
   random.nextBytes(pwd);
   password = toHex(pwd);
   File localPasswordFile = new File(env.getConfigDirPath(), LOCAL_PASSWORD_FILE);
   PrintWriter w = null;
   try {
     if (!localPasswordFile.exists()) {
       localPasswordFile.createNewFile();
       /*
        * XXX - There's a security hole here.
        * Between the time the file is created and the permissions
        * are changed to prevent others from opening it, someone
        * else could open it and wait for the data to be written.
        * Java needs the ability to create a file that's readable
        * only by the owner; coming in JDK 7.
        */
       localPasswordFile.setWritable(false, false); // take from all
       localPasswordFile.setWritable(true, true); // owner only
       localPasswordFile.setReadable(false, false); // take from all
       localPasswordFile.setReadable(true, true); // owner only
     }
     w = new PrintWriter(localPasswordFile);
     w.println(password);
   } catch (IOException ex) {
     // ignore errors
     logger.log(Level.FINE, "Exception writing local password file", ex);
   } finally {
     if (w != null) w.close();
   }
 }
  @Override
  public boolean add(String value) {
    File f;
    try {
      f = File.createTempFile(prefix, ".tmp", dir);
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    f.deleteOnExit();
    if (f.setReadable(false, false) == false
        || f.setReadable(true, true) == false
        || f.setWritable(false, false) == false
        || f.setWritable(true, true) == false) {
      return false;
    }

    FileWriter fw = null;
    try {
      fw = new FileWriter(f);
      fw.append(value);
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    } finally {
      if (fw != null) {
        try {
          fw.close();
        } catch (IOException e) {
        }
      }
    }
    list.add(f.getName());
    return true;
  }
Beispiel #5
0
  public FileManager(File dirCurRequest) throws IOException {
    // Создаем временную директорию
    this.tmpRqstDir = dirCurRequest;
    if (!tmpRqstDir.exists()) {
      tmpRqstDir.mkdirs();
      tmpRqstDir.setWritable(true, false);
      tmpRqstDir.getParentFile().setWritable(true, false);
    }

    // Создается файл pipeline.xml
    pipeline_xml = new File(tmpRqstDir, "pipeline.xml");
    pipeline_xml.setWritable(true, false);

    // Создается файл run_pl.err
    run_pl_err = new File(tmpRqstDir, "run_pl.err");
    run_pl_err.createNewFile();
    run_pl_err.setWritable(true, false);

    // Создаются временные input/output файлы
    Iterator<String> inputs = inputFileNames.iterator();
    while (inputs.hasNext()) {
      inputFiles.add(new IOFile(tmpRqstDir, inputs.next(), TypeIOFile.INPUT));
    }
    Iterator<String> outputs = outputFileNames.iterator();
    while (outputs.hasNext()) {
      outputFiles.add(new IOFile(tmpRqstDir, outputs.next(), TypeIOFile.OUTPUT));
    }

    // Создаются временные файлы стандартного вывода и стандартного вывода ошибок модуля.

  }
  @Test
  public void testFailWhenNoLocalStorageDir() throws Exception {
    File targetDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    try {
      assertTrue(targetDir.mkdirs());

      if (!targetDir.setWritable(false, false)) {
        System.err.println(
            "Cannot execute 'testFailWhenNoLocalStorageDir' because cannot mark directory non-writable");
        return;
      }

      RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(TEMP_URI);
      rocksDbBackend.setDbStoragePath(targetDir.getAbsolutePath());

      try {
        rocksDbBackend.initializeForJob(getMockEnvironment(), "foobar", IntSerializer.INSTANCE);
      } catch (Exception e) {
        assertTrue(e.getMessage().contains("No local storage directories available"));
        assertTrue(e.getMessage().contains(targetDir.getAbsolutePath()));
      }
    } finally {
      //noinspection ResultOfMethodCallIgnored
      targetDir.setWritable(true, false);
      FileUtils.deleteDirectory(targetDir);
    }
  }
Beispiel #7
0
  @Test
  public void testGetMemoFilePermissionsDirectory() throws Exception {
    String uuid = UUID.randomUUID().toString();
    File directory = new File(System.getProperty("java.io.tmpdir"), uuid);
    memoizer = new Memoizer(reader, 0, directory);

    // Check non-existing memo directory returns null
    assertEquals(memoizer.getMemoFile(id), null);

    // Create memoizer directory and memoizer reader
    directory.mkdirs();
    memoizer = new Memoizer(reader, 0, directory);

    // Check existing non-writeable memo directory returns null
    if (File.separator.equals("/")) {
      // File.setWritable() does not work properly on Windows
      directory.setWritable(false);
      assertEquals(memoizer.getMemoFile(id), null);
    }

    // Check existing writeable memo diretory returns a memo file
    directory.setWritable(true);
    String memoDir = idDir.getAbsolutePath();
    memoDir = memoDir.substring(memoDir.indexOf(File.separator) + 1);
    File memoFile = new File(directory, memoDir);
    memoFile = new File(memoFile, "." + TEST_FILE + ".bfmemo");
    assertEquals(memoizer.getMemoFile(id).getAbsolutePath(), memoFile.getAbsolutePath());
  }
 @SuppressLint("NewApi")
 public _SharedPreferencesImpl_JSON(Context context, String name, int mode) {
   setCharset("utf-8");
   try {
     File tempFile = new File(context.getApplicationInfo().dataDir + "/shared_prefs");
     if (tempFile.exists()) {
       if (!tempFile.isDirectory()) {
         if (!tempFile.delete() && !tempFile.mkdirs()) {
           throw new CouldNotCreateStorage(
               tempFile, "Сann't create a storage for the preferences.");
         }
         if (VERSION.SDK_INT >= 9) {
           tempFile.setWritable(true);
           tempFile.setReadable(true);
         }
       }
     } else {
       if (!tempFile.mkdirs()) {
         throw new CouldNotCreateStorage(tempFile, "Сann't create a storage for the preferences.");
       }
       if (VERSION.SDK_INT >= 9) {
         tempFile.setWritable(true);
         tempFile.setReadable(true);
       }
     }
     tempFile = new File(tempFile, name + ".json");
     if (!tempFile.exists() && !tempFile.createNewFile()) {
       throw new CouldNotCreateStorage(tempFile, "Сann't create a storage for the preferences.");
     }
     if (VERSION.SDK_INT >= 9) {
       switch (mode) {
         case Context.MODE_WORLD_WRITEABLE:
           tempFile.setWritable(true, false);
           tempFile.setReadable(true, false);
           break;
         case Context.MODE_WORLD_READABLE:
           tempFile.setWritable(true, true);
           tempFile.setReadable(true, false);
           break;
         case Context.MODE_PRIVATE:
         default:
           tempFile.setWritable(true, true);
           tempFile.setReadable(true, true);
           break;
       }
     }
     file = tempFile;
     fileTag = file.getAbsolutePath().intern();
     if (getReference().data == null) {
       getReference().data = readDataFromFile(file);
     }
   } catch (IOException e) {
     throw new RuntimeException("IOException", e);
   }
 }
Beispiel #9
0
 static boolean ag(String var0) {
   if (version() < 9) {
     return false;
   } else {
     File var1 = new File(var0);
     var1.setReadable(false, false);
     var1.setWritable(false, false);
     var1.setReadable(true, true);
     var1.setWritable(true, true);
     return true;
   }
 }
Beispiel #10
0
  @Test
  public void testGetMemoFilePermissionsInPlace() throws Exception {
    memoizer = new Memoizer(reader);

    // Check non-writeable file directory returns null for in-place caching
    if (File.separator.equals("/")) {
      // File.setWritable() does not work properly on Windows
      idDir.setWritable(false);
      assertEquals(memoizer.getMemoFile(id), null);
    }
    // Check writeable file directory returns memo file beside file
    idDir.setWritable(true);
    File memoFile = new File(idDir, "." + TEST_FILE + ".bfmemo");
    assertEquals(memoizer.getMemoFile(id).getAbsolutePath(), memoFile.getAbsolutePath());
  }
  public boolean build() throws IOException {
    settingsFile = File.createTempFile("libgdx-setup-settings", ".gradle");
    buildFile = File.createTempFile("libgdx-setup-build", ".gradle");
    if (!settingsFile.exists()) {
      settingsFile.createNewFile();
    }
    if (!buildFile.exists()) {
      buildFile.createNewFile();
    }
    settingsFile.setWritable(true);
    buildFile.setWritable(true);
    try {
      FileWriter settingsWriter = new FileWriter(settingsFile.getAbsoluteFile());
      BufferedWriter settingsBw = new BufferedWriter(settingsWriter);
      String settingsContents = "include ";
      for (ProjectType module : modules) {
        settingsContents += "'" + module.getName() + "'";
        if (modules.indexOf(module) != modules.size() - 1) {
          settingsContents += ", ";
        }
      }
      settingsBw.write(settingsContents);
      settingsBw.close();
      settingsWriter.close();

      FileWriter buildWriter = new FileWriter(buildFile.getAbsoluteFile());
      BufferedWriter buildBw = new BufferedWriter(buildWriter);

      BuildScriptHelper.addBuildScript(modules, buildBw);
      BuildScriptHelper.addAllProjects(buildBw);
      for (ProjectType module : modules) {
        BuildScriptHelper.addProject(module, dependencies, buildBw);
      }

      // Add task here for now
      buildBw.write("\n");
      buildBw.write("tasks.eclipse.doLast {\n");
      buildBw.write("    delete \".project\"\n");
      buildBw.write("}");

      buildBw.close();
      buildWriter.close();
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
  }
  @Test
  public void testClose_cannotDelete() throws Exception {

    File file =
        new File(tempFolder.getRoot(), "testClose_cannotDelete") {
          int deleteCalls = 0;

          @Override
          public boolean delete() {
            if (deleteCalls == 0) {
              deleteCalls++;
              return false;
            } else {
              return super.delete();
            }
          }
        };
    assertTrue(file.createNewFile());
    assertTrue(file.exists());
    PurgeOnCloseFileInputStream purgeOnCloseFileInputStream = new PurgeOnCloseFileInputStream(file);
    purgeOnCloseFileInputStream.close();
    assertTrue(file.exists());

    assertTrue(file.setReadable(true));
    assertTrue(file.setWritable(true));
    assertTrue(file.delete());
  }
Beispiel #13
0
 @Test
 public void testExistingFilePlainOutputToFileWithoutReadAndRwPermissions() throws Exception {
   final File file = temporaryFolder.newFile("file.output");
   // That works fine on Linux/Unix, but ....
   // It's not possible to make a file unreadable in Windows NTFS for owner.
   // http://stackoverflow.com/a/4354686
   // https://github.com/google/google-oauth-java-client/issues/55#issuecomment-69403681
   // assertTrue(file.setReadable(false, false));
   assertTrue(file.setWritable(false, false));
   exit.expectSystemExitWithStatus(-1);
   exit.checkAssertionAfterwards(
       new Assertion() {
         @Override
         public void checkAssertion() throws IOException {
           assertEquals(
               "Permission denied : '" + file.getCanonicalPath() + "'." + System.lineSeparator(),
               systemOut.getLog());
           assertEquals("", systemErr.getLog());
         }
       });
   Main.main(
       "-c",
       getPath("config-classname.xml"),
       "-f",
       "plain",
       "-o",
       file.getCanonicalPath(),
       getPath("InputMain.java"));
 }
  private void good1() throws Throwable {

    String fn = ".\\src\\testcases\\CWE379_File_Creation_in_Insecure_Dir\\basic\\insecureDir";
    File dir = new File(fn);
    if (dir.exists()) {
      IO.writeLine("Directory already exists");
      if (dir.delete()) {
        IO.writeLine("Directory deleted");
      } else {
        return;
      }
    }
    if (!dir.getParentFile().canWrite()) {
      IO.writeLine("Cannot write to parent dir");
    }

    /* FIX: explicitly set directory permissions */
    dir.setExecutable(false, true);
    dir.setReadable(true);
    dir.setWritable(false, true);
    try {
      boolean success = dir.mkdir();
      if (success) {
        IO.writeLine("Directory created");
        File file = new File(dir.getAbsolutePath() + "\\newFile.txt");
        file.createNewFile();
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
  private String Commands2script(String[] cmdLine, String... aScriptNames) {
    String aScriptName = "script.sh";
    if (aScriptNames.length > 0) {
      aScriptName = aScriptNames[0];
    }
    FileWriter out = null;
    String scriptName = "/data/data/" + mPackName + "/" + aScriptName;
    File file = new File(scriptName);
    if (file.exists()) {
      file.delete();
    }
    if (D) Log.d(TAG, "Create");
    try {
      file.createNewFile();
      file.setExecutable(true, false);
      file.setWritable(true, false);
      file.setReadable(true, false);
      out = new FileWriter(file);
      out.write("#! /system/bin/sh" + "\n");
      for (String s : cmdLine) {
        out.write(s + "\n");
        if (D) Log.d(TAG, s);
      }
      out.write("\n");
      out.close();
      String s = exec(mSU + " -c " + scriptName);
      if (!D) file.delete();
      return s;

    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }
Beispiel #16
0
  @Test
  public void testSaveXml() throws FileNotFoundException, IllegalArgumentException, IOException {

    File testFile = new File("res/tmp.xml");
    testFile.setWritable(true);
    File testFile2 = new File("res/tmp2.xml");

    XmlTree sudoku = new XmlTree("sudoku");
    sudoku.addAttribute(new XmlAttribute("id", "7845"));
    sudoku.addAttribute(new XmlAttribute("type", "6"));
    sudoku.addAttribute(new XmlAttribute("complexity", "3"));

    XmlTree fieldmap1 = new XmlTree("fieldmap");
    fieldmap1.addAttribute(new XmlAttribute("editable", "true"));
    fieldmap1.addAttribute(new XmlAttribute("solution", "9"));
    XmlTree position1 = new XmlTree("position");
    position1.addAttribute(new XmlAttribute("x", "1"));
    position1.addAttribute(new XmlAttribute("y", "8"));
    fieldmap1.addChild(position1);
    sudoku.addChild(fieldmap1);

    XmlTree fieldmap2 = new XmlTree("fieldmap");
    fieldmap2.addAttribute(new XmlAttribute("editable", "true"));
    fieldmap2.addAttribute(new XmlAttribute("solution", "4"));
    XmlTree position2 = new XmlTree("position");
    position2.addAttribute(new XmlAttribute("x", "2"));
    position2.addAttribute(new XmlAttribute("y", "6"));
    fieldmap2.addChild(position2);
    sudoku.addChild(fieldmap2);

    System.out.println(helper.buildXmlStructure(sudoku));

    helper.saveXml(sudoku, testFile);

    XmlTree sudokuTest = helper.loadXml(testFile);

    System.out.println("------------------------------------------");
    System.out.println(helper.buildXmlStructure(sudokuTest));

    assertEquals(sudokuTest.getName(), sudoku.getName());
    assertEquals(sudokuTest.getNumberOfChildren(), sudoku.getNumberOfChildren());

    int i = 0;
    for (Iterator<XmlTree> iterator = sudokuTest.getChildren(); iterator.hasNext(); ) {
      XmlTree sub = iterator.next();
      if (i == 0) {
        assertEquals(sub.getAttributeValue("solution"), "9");
      } else if (i == 1) {
        assertEquals(sub.getAttributeValue("solution"), "4");
      }
      assertEquals(sub.getAttributeValue("editable"), "true");
      assertEquals(sub.getName(), "fieldmap");
      assertEquals(sub.getNumberOfChildren(), 1);
      i++;
    }
    assertEquals(i, 2);

    XmlTree atomicTest = new XmlTree("sudoku", "xyz");
    helper.saveXml(atomicTest, testFile2);
  }
 public static void setReadOnlyAttribute(@NotNull String path, boolean readOnlyFlag) {
   final boolean writableFlag = !readOnlyFlag;
   final File file = new File(path);
   if (!file.setWritable(writableFlag) && file.canWrite() != writableFlag) {
     LOG.warn("Can't set writable attribute of '" + path + "' to " + readOnlyFlag);
   }
 }
  /* good1() changes true to false */
  private void good1() throws Throwable {
    if (false) {
      /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      IO.writeLine("Benign, fixed string");
    } else {

      File tempFile = null;

      try {
        tempFile = File.createTempFile("temp", "1234");
        IO.writeLine(tempFile.toString());

        /* FIX: Call deleteOnExit() so that the file will be deleted */
        tempFile.deleteOnExit();

        /* Set the permissions to avoid insecure temporary file incidentals  */
        if (!tempFile.setWritable(true, true)) {
          IO.logger.log(Level.WARNING, "Could not set Writable permissions");
        }
        if (!tempFile.setReadable(true, true)) {
          IO.logger.log(Level.WARNING, "Could not set Readable permissions");
        }
        if (!tempFile.setExecutable(false)) {
          IO.logger.log(Level.WARNING, "Could not set Executable permissions");
        }
      } catch (IOException exceptIO) {
        IO.logger.log(Level.WARNING, "Could not create temporary file", exceptIO);
      }
    }
  }
Beispiel #19
0
 @Test
 public void testExistingTargetFilePlainOutputToFileWithoutRwPermissions() throws Exception {
   final File file = temporaryFolder.newFile("file.output");
   assertTrue(file.setReadable(true, true));
   assertTrue(file.setWritable(false, false));
   exit.expectSystemExitWithStatus(-1);
   exit.checkAssertionAfterwards(
       new Assertion() {
         @Override
         public void checkAssertion() throws IOException {
           assertEquals(
               "Permission denied : '" + file.getCanonicalPath() + "'." + System.lineSeparator(),
               systemOut.getLog());
           assertEquals("", systemErr.getLog());
         }
       });
   Main.main(
       "-c",
       getPath("config-classname.xml"),
       "-f",
       "plain",
       "-o",
       file.getCanonicalPath(),
       getPath("InputMain.java"));
 }
Beispiel #20
0
  public static HttpURLConnection download(final HttpURLConnection con, final File file)
      throws IOException {
    if (file.exists()) {
      con.setIfModifiedSince(file.lastModified());
      con.connect();
      if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
        log.fine("Using " + file.getName() + " from cache");
        con.disconnect();
        return con;
      }
    }

    log.fine("Downloading new " + file.getName());

    final byte[] buffer = downloadBinary(con);

    if (!file.exists()) {
      file.createNewFile();
    }
    if (file.exists() && (!file.canRead() || file.canWrite())) {
      file.setReadable(true);
      file.setWritable(true);
    }
    if (file.exists() && file.canRead() && file.canWrite()) {
      final FileOutputStream fos = new FileOutputStream(file);
      fos.write(buffer);
      fos.flush();
      fos.close();
    }

    file.setLastModified(con.getLastModified());

    con.disconnect();
    return con;
  }
 @SuppressWarnings("ResultOfMethodCallIgnored") // intended, nothing useful can be done
 @SuppressLint("SetWorldReadable") // intended, default permission
 private static void setFilePermissions(File outputFile) {
   // Try change permission to rwxr-xr-x
   outputFile.setReadable(true, false);
   outputFile.setExecutable(true, false);
   outputFile.setWritable(true);
 }
Beispiel #22
0
  public static void writeChannel() {
    FileOutputStream fileOut = null;
    FileChannel channel = null;
    try {
      InputStream input = FileChannelIoMain.class.getClassLoader().getResourceAsStream("test.txt");
      byte[] byteBuffer = new byte[1024];

      int size = 0;
      StringBuilder builer = new StringBuilder();
      while ((size = input.read(byteBuffer)) != -1) {
        builer.append(new String(byteBuffer));
      }
      System.out.println(builer.toString());

      File file = new File("./test.txt");
      if (!file.exists()) {
        file.createNewFile();
        file.setWritable(true);
      }
      fileOut = new FileOutputStream(file, true);
      channel = fileOut.getChannel();

      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      buffer.putChar('\n');

      buffer.put("我是一个中国人,我的家乡在北京".getBytes("utf-8"));

      buffer.mark();

      buffer.putChar('&');
      buffer.putChar('#');

      buffer.reset();
      buffer.flip();

      // buffer.rewind();

      channel.write(buffer);

      buffer.clear();

    } catch (Throwable e) {
      e.printStackTrace();
    } finally {
      try {
        if (fileOut != null) {
          fileOut.close();
          fileOut = null;
        }
        if (channel != null) {
          channel.close();
          channel = null;
        }
      } catch (Throwable e) {
        e.printStackTrace();
      }
    }
  }
Beispiel #23
0
 public static void writeStringToFile(File file, String content) {
   try {
     FileUtils.writeStringToFile(file, content);
   } catch (IOException ioe) {
     throw new CLIException(REASON_OTHER, ioe);
   }
   file.setReadable(true, true);
   file.setWritable(true, true);
 }
  @Test
  public void testNonAccessibleFile() throws IOException {
    Configuration config = new DefaultConfiguration("myname");
    final File file = temporaryFolder.newFile("file.output");
    file.setReadable(true, false);
    file.setWritable(false, false);

    PropertyCacheFile cache = new PropertyCacheFile(config, file.getAbsolutePath());
  }
Beispiel #25
0
  /**
   * 코드저장소 프로젝트명을 변경하고 결과를 반환한다.
   *
   * @param projectName
   * @return 코드저장소 이름 변경성공시 true / 실패시 false
   */
  @Override
  public boolean renameTo(String projectName) {

    File src = new File(getRepoPrefix() + this.ownerName + "/" + this.projectName);
    File dest = new File(getRepoPrefix() + this.ownerName + "/" + projectName);
    src.setWritable(true);

    return src.renameTo(dest);
  }
Beispiel #26
0
  public static File createTempDirectory(final String fileName) throws IOException {
    final File temp = File.createTempFile(fileName, String.valueOf(System.currentTimeMillis()));
    temp.setReadable(true);
    temp.setWritable(true);

    if (!temp.delete()) {
      throw new IOException("Could not delete temporary file : " + temp.getAbsolutePath());
    }
    mkdirs(temp);
    return temp;
  }
Beispiel #27
0
  protected boolean writeImage(String dstPath, String type, RenderedOp image) {
    boolean success = false;
    //		JAI doesn't natively support GIF encoding, but Java ImageIO does.
    if (type.toLowerCase().equals("gif")) {
      File dst = new File(dstPath);
      // if the file doesn't exist, create it and make sure we can write to it.
      if (!dst.exists()) {
        try {
          dst.createNewFile();
        } catch (IOException ioe) {
          this.core.app.logError(
              ErrorReporter.errorMsg(this.getClass(), "resizeImg")
                  + "Failed to create tmp img at \""
                  + dstPath
                  + "\".",
              ioe);
        }
      }
      if (!dst.canWrite()) {
        dst.setWritable(true);
      }

      try {
        ImageIO.write(image, type.toUpperCase(), dst);
      } catch (IOException ioe) {
        this.core.app.logError(
            ErrorReporter.errorMsg(this.getClass(), "resizeImg")
                + "Failed to write image data to \""
                + dstPath
                + "\".",
            ioe);
      }

      // clean up file handle
      dst = null;
    } else {
      if (type.toLowerCase().equals("jpg")) {
        type = "jpeg";
      }

      JAI.create("filestore", image, dstPath, type);
    }

    if (image != null && new File(dstPath).exists()) {
      success = true;
    }

    // JAI Cleanup
    image.dispose();
    image = null;
    type = null;

    return success;
  }
Beispiel #28
0
 @Test
 public void testSaveXmlIOException() throws IllegalArgumentException, IOException {
   thrown.expect(IOException.class);
   File file = new File("res/tmp.xml");
   file.setWritable(false);
   // this test will fail if you use linux and the file gets created on a
   // ntfs partition.
   // seems that the java file implementation uses linux tools like chmod -
   // chmod doesnt work on ntfs...
   assertFalse(file.canWrite());
   helper.saveXml(new XmlTree("sudoku", ""), file);
 }
Beispiel #29
0
  /**
   * Extract the specified library file to the target folder.
   *
   * @param libFolderForCurrentOS
   * @param libraryFileName
   * @param targetFolder
   * @return
   */
  private File extractLibraryFile(
      String libFolderForCurrentOS, String libraryFileName, String targetFolder) {
    final String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName;

    // Attach UUID to the native library file to ensure multiple class
    // loaders can read it multiple times.
    final String uuid = UUID.randomUUID().toString();
    final String extractedLibFileName =
        String.format("nativelib-%s-%s-%s", getVersion(), uuid, libraryFileName);

    File extractedLibFile = new File(targetFolder, extractedLibFileName);

    try {
      // Extract a native library file into the target directory
      InputStream reader = NativeLibLoader.class.getResourceAsStream(nativeLibraryFilePath);
      FileOutputStream writer = new FileOutputStream(extractedLibFile);
      try {
        byte[] buffer = new byte[8192];
        int bytesRead = 0;
        while ((bytesRead = reader.read(buffer)) != -1) {
          writer.write(buffer, 0, bytesRead);
        }
      } finally {
        // Delete the extracted lib file on JVM exit.
        extractedLibFile.deleteOnExit();

        if (writer != null) writer.close();
        if (reader != null) reader.close();
      }

      // Set executable (x) flag to enable Java to
      // load the native library.
      extractedLibFile.setReadable(true);
      extractedLibFile.setWritable(true, true);
      extractedLibFile.setExecutable(true);

      // Check whether the contents are properly copied
      // from the resource folder.
      {
        InputStream nativeIn = NativeLibLoader.class.getResourceAsStream(nativeLibraryFilePath);
        InputStream extractedLibIn = new FileInputStream(extractedLibFile);
        try {
          if (!contentsEquals(nativeIn, extractedLibIn)) throw new IOException();
        } finally {
          if (nativeIn != null) nativeIn.close();
          if (extractedLibIn != null) extractedLibIn.close();
        }
      }
      return new File(targetFolder, extractedLibFileName);
    } catch (IOException e) {
      return null;
    }
  }
  @Test
  public void testContinueOnSomeDbDirectoriesMissing() throws Exception {
    File targetDir1 = tempFolder.newFolder();
    File targetDir2 = tempFolder.newFolder();

    String checkpointPath = tempFolder.newFolder().toURI().toString();
    RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(checkpointPath);

    try {

      if (!targetDir1.setWritable(false, false)) {
        System.err.println(
            "Cannot execute 'testContinueOnSomeDbDirectoriesMissing' because cannot mark directory non-writable");
        return;
      }

      rocksDbBackend.setDbStoragePaths(targetDir1.getAbsolutePath(), targetDir2.getAbsolutePath());

      try {
        Environment env = getMockEnvironment();
        rocksDbBackend.createKeyedStateBackend(
            env,
            env.getJobID(),
            "foobar",
            IntSerializer.INSTANCE,
            1,
            new KeyGroupRange(0, 0),
            new KvStateRegistry().createTaskRegistry(env.getJobID(), new JobVertexID()));
      } catch (Exception e) {
        e.printStackTrace();
        fail("Backend initialization failed even though some paths were available");
      }
    } finally {
      //noinspection ResultOfMethodCallIgnored
      targetDir1.setWritable(true, false);
      FileUtils.deleteDirectory(targetDir1);
      FileUtils.deleteDirectory(targetDir2);
    }
  }