Exemplo n.º 1
0
  /** Maintain the versions of the file we are about to for writing. */
  private static boolean shuffleVersions(String file) {
    File f = new File(file);

    if (!f.exists()) {
      return true;
    }
    int version = 99;

    boolean found = false;

    do {
      String fileVersion = file + String.format("_%02d", version);
      File vf = new File(fileVersion);

      if (vf.exists()) {
        found = true;
      } else {
        version--;
      }

    } while (!found && version >= 0);

    version++;

    String fileVersion = file + String.format("_%02d", version);

    File newf = new File(fileVersion);

    return f.renameTo(newf);
  }
Exemplo n.º 2
0
  private void checkAndLaunchUpdate() {
    Log.i(LOG_FILE_NAME, "Checking for an update");

    int statusCode = 8; // UNEXPECTED_ERROR
    File baseUpdateDir = null;
    if (Build.VERSION.SDK_INT >= 8)
      baseUpdateDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    else baseUpdateDir = new File(Environment.getExternalStorageDirectory().getPath(), "download");

    File updateDir = new File(new File(baseUpdateDir, "updates"), "0");

    File updateFile = new File(updateDir, "update.apk");
    File statusFile = new File(updateDir, "update.status");

    if (!statusFile.exists() || !readUpdateStatus(statusFile).equals("pending")) return;

    if (!updateFile.exists()) return;

    Log.i(LOG_FILE_NAME, "Update is available!");

    // Launch APK
    File updateFileToRun = new File(updateDir, getPackageName() + "-update.apk");
    try {
      if (updateFile.renameTo(updateFileToRun)) {
        String amCmd =
            "/system/bin/am start -a android.intent.action.VIEW "
                + "-n com.android.packageinstaller/.PackageInstallerActivity -d file://"
                + updateFileToRun.getPath();
        Log.i(LOG_FILE_NAME, amCmd);
        Runtime.getRuntime().exec(amCmd);
        statusCode = 0; // OK
      } else {
        Log.i(LOG_FILE_NAME, "Cannot rename the update file!");
        statusCode = 7; // WRITE_ERROR
      }
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error launching installer to update", e);
    }

    // Update the status file
    String status = statusCode == 0 ? "succeeded\n" : "failed: " + statusCode + "\n";

    OutputStream outStream;
    try {
      byte[] buf = status.getBytes("UTF-8");
      outStream = new FileOutputStream(statusFile);
      outStream.write(buf, 0, buf.length);
      outStream.close();
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error writing status file", e);
    }

    if (statusCode == 0) System.exit(0);
  }
  /**
   * Loop through the ZIP file, copying its entries into a new, temporary ZIP file, skipping the
   * entry to be deleted. Then replace the original file with the new one.
   */
  protected Integer deleteEntry() throws XMLDataStoreException {
    try {
      ZipInputStream inStream = this.buildZipInputStream();
      File outFile = this.buildTempFile();
      ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(outFile));

      byte[] buffer = new byte[32768];
      int inCount = 0;
      int outCount = 0;

      // copy all the entries except the one to be deleted
      ZipEntry entry = inStream.getNextEntry();
      while (entry != null) {
        inCount++;
        if (!this.getZipEntryName().equals(entry.getName())) {
          outCount++;
          outStream.putNextEntry(entry);
          int byteCount;
          while ((byteCount = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, byteCount);
          }
          outStream.closeEntry();
        }
        entry = inStream.getNextEntry();
      }
      inStream.close();
      if (outCount == 0) {
        // add a dummy record to an empty file so we can close it
        // this is required by ZipOutputStream
        outStream.putNextEntry(new ZipEntry("delete.txt"));
        outStream.write(
            "This file is a place-holder. The containing ZIP file should be deleted.".getBytes());
        outStream.closeEntry();
      }
      outStream.close();
      if (outCount == inCount) {
        // no entries were removed - just delete the temp file
        outFile.delete();
      } else {
        // at least one entry removed - delete the original file
        this.getFile().delete();
        if (outCount == 0) {
          // NO entries remain - just delete the temp file too
          outFile.delete();
        } else {
          // entries remain - replace original file with temp file
          outFile.renameTo(this.getFile());
        }
      }
      return new Integer(inCount - outCount); // should be 0 or 1
    } catch (IOException ex) {
      throw XMLDataStoreException.ioException(ex);
    }
  }
Exemplo n.º 4
0
 @Override
 public void save() {
   NBTOutputStream stream = null;
   try {
     Files.createParentDirs(this.file);
     final File temporaryFile =
         File.createTempFile(this.file.getName(), null, this.file.getParentFile());
     temporaryFile.deleteOnExit();
     stream = new NBTOutputStream(new FileOutputStream(temporaryFile));
     stream.writeTag(new CompoundTag(this.name, this.root));
     stream.close();
     this.file.delete();
     temporaryFile.renameTo(this.file);
     temporaryFile.delete();
   } catch (IOException ex) {
     ex.printStackTrace();
   } finally {
     try {
       stream.close();
     } catch (IOException ex2) {
     }
   }
 }
Exemplo n.º 5
0
  private void cleanup(File directory) {
    // Clean up from old installations, removing or renaming files.
    // Note that directory is the parent of the CTP directory
    // unless the original installation was done by Bill Weadock's
    // all-in-one installer for Windows.

    // Get a file pointing to the CTP directory.
    // This might be the current directory, or
    // it might be the CTP child.
    File dir;
    if (directory.getName().equals("RSNA")) dir = directory;
    else dir = new File(directory, "CTP");

    // If CTP.jar exists in this directory, it is a really
    // old CTP main file - not used anymore
    File ctp = new File(dir, "CTP.jar");
    if (ctp.exists()) ctp.delete();

    // These are old names for the Launcher.jar file
    File launcher = new File(dir, "CTP-launcher.jar");
    if (launcher.exists()) launcher.delete();
    launcher = new File(dir, "TFS-launcher.jar");
    if (launcher.exists()) launcher.delete();

    // Delete the obsolete CTP-runner.jar file
    File runner = new File(dir, "CTP-runner.jar");
    if (runner.exists()) runner.delete();

    // Delete the obsolete MIRC-copier.jar file
    File copier = new File(dir, "MIRC-copier.jar");
    if (copier.exists()) copier.delete();

    // Rename the old versions of the properties files
    File oldprops = new File(dir, "CTP-startup.properties");
    File newprops = new File(dir, "CTP-launcher.properties");
    File correctprops = new File(dir, "Launcher.properties");
    if (oldprops.exists()) {
      if (newprops.exists() || correctprops.exists()) oldprops.delete();
      else oldprops.renameTo(correctprops);
    }
    if (newprops.exists()) {
      if (correctprops.exists()) newprops.delete();
      else newprops.renameTo(correctprops);
    }

    // Get rid of obsolete startup and shutdown programs
    File startup = new File(dir, "CTP-startup.jar");
    if (startup.exists()) startup.delete();
    File shutdown = new File(dir, "CTP-shutdown.jar");
    if (shutdown.exists()) shutdown.delete();

    // Get rid of the obsolete linux directory
    File linux = new File(dir, "linux");
    if (linux.exists()) {
      startup = new File(linux, "CTP-startup.jar");
      if (startup.exists()) startup.delete();
      shutdown = new File(linux, "CTP-shutdown.jar");
      if (shutdown.exists()) shutdown.delete();
      linux.delete();
    }

    // clean up the libraries directory
    File libraries = new File(dir, "libraries");
    if (libraries.exists()) {
      // remove obsolete versions of the slf4j libraries
      // and the dcm4che-imageio libraries
      File[] files = libraries.listFiles();
      for (File file : files) {
        if (file.isFile()) {
          String name = file.getName();
          if (name.startsWith("slf4j-") || name.startsWith("dcm4che-imageio-rle")) {
            file.delete();
          }
        }
      }
      // remove the email subdirectory
      File email = new File(libraries, "email");
      deleteAll(email);
      // remove the xml subdirectory
      File xml = new File(libraries, "xml");
      deleteAll(xml);
      // remove the sftp subdirectory
      File sftp = new File(libraries, "sftp");
      deleteAll(xml);
      // move edtftpj.jar to the ftp directory
      File edtftpj = new File(libraries, "edtftpj.jar");
      if (edtftpj.exists()) {
        File ftp = new File(libraries, "ftp");
        ftp.mkdirs();
        File ftpedtftpj = new File(ftp, "edtftpj.jar");
        edtftpj.renameTo(ftpedtftpj);
      }
    }

    // remove the obsolete xml library under dir
    File xml = new File(dir, "xml");
    deleteAll(xml);

    // remove the dicom profiles so any
    // obsolete files will disappear
    File profiles = new File(dir, "profiles");
    File dicom = new File(profiles, "dicom");
    deleteAll(dicom);
    dicom.mkdirs();

    // Remove the index.html file so it will be rebuilt from
    // example-index.html when the system next starts.
    File root = new File(dir, "ROOT");
    if (root.exists()) {
      File index = new File(root, "index.html");
      index.delete();
    }
  }
Exemplo n.º 6
0
  /** Starts main program with the specified arguments. */
  public synchronized boolean run(String args[]) {
    ok = true;
    if (!parseArgs(args)) {
      return false;
    }
    try {
      if (cflag || uflag) {
        if (fname != null) {
          // The name of the zip file as it would appear as its own
          // zip file entry. We use this to make sure that we don't
          // add the zip file to itself.
          zname = fname.replace(File.separatorChar, '/');
          if (zname.startsWith("./")) {
            zname = zname.substring(2);
          }
        }
      }
      if (cflag) {
        Manifest manifest = null;
        InputStream in = null;

        if (!Mflag) {
          if (mname != null) {
            in = new FileInputStream(mname);
            manifest = new Manifest(new BufferedInputStream(in));
          } else {
            manifest = new Manifest();
          }
          addVersion(manifest);
          addCreatedBy(manifest);
          if (isAmbiguousMainClass(manifest)) {
            if (in != null) {
              in.close();
            }
            return false;
          }
          if (ename != null) {
            addMainClass(manifest, ename);
          }
          if (pname != null) {
            if (!addProfileName(manifest, pname)) {
              if (in != null) {
                in.close();
              }
              return false;
            }
          }
        }
        OutputStream out;
        if (fname != null) {
          out = new FileOutputStream(fname);
        } else {
          out = new FileOutputStream(FileDescriptor.out);
          if (vflag) {
            // Disable verbose output so that it does not appear
            // on stdout along with file data
            // error("Warning: -v option ignored");
            vflag = false;
          }
        }
        expand(null, files, false);
        create(new BufferedOutputStream(out, 4096), manifest);
        if (in != null) {
          in.close();
        }
        out.close();
      } else if (uflag) {
        File inputFile = null, tmpFile = null;
        FileInputStream in;
        FileOutputStream out;
        if (fname != null) {
          inputFile = new File(fname);
          tmpFile = createTempFileInSameDirectoryAs(inputFile);
          in = new FileInputStream(inputFile);
          out = new FileOutputStream(tmpFile);
        } else {
          in = new FileInputStream(FileDescriptor.in);
          out = new FileOutputStream(FileDescriptor.out);
          vflag = false;
        }
        InputStream manifest = (!Mflag && (mname != null)) ? (new FileInputStream(mname)) : null;
        expand(null, files, true);
        boolean updateOk = update(in, new BufferedOutputStream(out), manifest, null);
        if (ok) {
          ok = updateOk;
        }
        in.close();
        out.close();
        if (manifest != null) {
          manifest.close();
        }
        if (ok && fname != null) {
          // on Win32, we need this delete
          inputFile.delete();
          if (!tmpFile.renameTo(inputFile)) {
            tmpFile.delete();
            throw new IOException(getMsg("error.write.file"));
          }
          tmpFile.delete();
        }
      } else if (tflag) {
        replaceFSC(files);
        if (fname != null) {
          list(fname, files);
        } else {
          InputStream in = new FileInputStream(FileDescriptor.in);
          try {
            list(new BufferedInputStream(in), files);
          } finally {
            in.close();
          }
        }
      } else if (xflag) {
        replaceFSC(files);
        if (fname != null && files != null) {
          extract(fname, files);
        } else {
          InputStream in =
              (fname == null) ? new FileInputStream(FileDescriptor.in) : new FileInputStream(fname);
          try {
            extract(new BufferedInputStream(in), files);
          } finally {
            in.close();
          }
        }
      } else if (iflag) {
        genIndex(rootjar, files);
      }
    } catch (IOException e) {
      fatalError(e);
      ok = false;
    } catch (Error ee) {
      ee.printStackTrace();
      ok = false;
    } catch (Throwable t) {
      t.printStackTrace();
      ok = false;
    }
    out.flush();
    err.flush();
    return ok;
  }