public void totalExport() { File expf = new File("export"); if (expf.exists()) rmrf(expf); expf.mkdirs(); for (int sto = 0; sto < storeLocs.size(); sto++) { try { String sl = storeLocs.get(sto).getAbsolutePath().replaceAll("/", "-").replaceAll("\\\\", "-"); File estore = new File(expf, sl); estore.mkdir(); File log = new File(estore, LIBRARY_NAME); PrintWriter pw = new PrintWriter(log); for (int i = 0; i < store.getRowCount(); i++) if (store.curStore(i) == sto) { File enc = store.locate(i); File dec = sec.prepareMainFile(enc, estore, false); pw.println(dec.getName()); pw.println(store.getValueAt(i, Storage.COL_DATE)); pw.println(store.getValueAt(i, Storage.COL_TAGS)); synchronized (jobs) { jobs.addLast(expJob(enc, dec)); } } pw.close(); } catch (IOException exc) { exc.printStackTrace(); JOptionPane.showMessageDialog(frm, "Exporting Failed"); return; } } JOptionPane.showMessageDialog(frm, "Exporting to:\n " + expf.getAbsolutePath()); }
public void secureDelete() { int rw = tblItems.getSelectedRow(); if (rw == -1) { JOptionPane.showMessageDialog(frm, "No item selected", "Error", JOptionPane.ERROR_MESSAGE); return; } int idx = tblItems.convertRowIndexToModel(rw); if (JOptionPane.showConfirmDialog( frm, "Delete " + store.plainName(idx) + "?", "Confirm Delete", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) return; File del = store.delete(idx); store.fireTableDataChanged(); if (del != null) { if (del.delete()) { // successful needsSave = true; } else { System.err.println("Delete " + del.getAbsolutePath() + " failed"); } } updateStatus(); }
public void updateStatus() { txaStatus.setText(""); txaStatus.append("size\t" + store.getRowCount() + "\n"); txaStatus.append("total\t" + store.sumLengths() + "KB\n"); txaStatus.append("\nThreads:\n"); for (int i = 0; i < numThreads; i++) { txaStatus.append(" " + i + "- "); String jb = encryptDecryptThreads[i].getCur(); if (jb == null) txaStatus.append("idle\n"); else { txaStatus.append(jobString(jb) + "\n"); } } txaStatus.append("\nJobs:\n"); int c = 6 + numThreads; int i = 0; synchronized (jobs) { for (String s : jobs) { if (c + i < TXA_HEIGHT - 1) txaStatus.append(" - " + jobString(s) + "\n"); else if (c + i == TXA_HEIGHT - 1) { txaStatus.append(" - [" + (jobs.size() - i) + "more ]"); } i++; } } }
public void secureAnalysis() { int rw = tblItems.getSelectedRow(); if (rw != -1) { int idx = tblItems.convertRowIndexToModel(rw); String desc = store.describe(idx); if (JOptionPane.showConfirmDialog(frm, desc, "Details", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) return; } if (JOptionPane.showConfirmDialog(frm, store.tagDesc(), "Tags", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) return; if (JOptionPane.showConfirmDialog( frm, store.storeDesc(), "Storage", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) return; }
public void secureExport(int i) { File expf = getExportTempFile(store.plainName(i)); // check if its already been exported if (expf.exists()) secureUse(expf); else { // otherwise add to work queue File cipf = store.locate(i); if (cipf != null) { synchronized (jobs) { if (priorityExport) jobs.addFirst(expJob(cipf, expf)); else jobs.addLast(expJob(cipf, expf)); } } else System.err.println("Cannot export, missing encrypted file"); } updateStatus(); }
private static void setCurrentVersion() { myRecords.putInt(HEADER_VERSION_OFFSET, VERSION); myRecords.putLong(HEADER_TIMESTAMP_OFFSET, System.currentTimeMillis()); myAttributes.setVersion(VERSION); myContents.setVersion(VERSION); myRecords.putInt(HEADER_CONNECTION_STATUS_OFFSET, SAFELY_CLOSED_MAGIC); }
private static int getVersion() { final int recordsVersion = myRecords.getInt(HEADER_VERSION_OFFSET); if (myAttributes.getVersion() != recordsVersion || myContents.getVersion() != recordsVersion) return -1; return recordsVersion; }
public static boolean isDirty() { return myDirty || myNames.isDirty() || myAttributes.isDirty() || myContents.isDirty() || myRecords.isDirty(); }
public void secureMove() { int rw = tblItems.getSelectedRow(); if (rw == -1) { JOptionPane.showMessageDialog(frm, "No item selected", "Error", JOptionPane.ERROR_MESSAGE); return; } int idx = tblItems.convertRowIndexToModel(rw); String[] opts = new String[storeLocs.size()]; for (int i = 0; i < opts.length; i++) opts[i] = storeLocs.get(i).getAbsolutePath(); JComboBox cmbMove = new JComboBox(opts); cmbMove.setSelectedIndex(store.curStore(idx)); if (JOptionPane.showConfirmDialog(frm, cmbMove, "Move item", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) return; File newLoc = store.move(idx, cmbMove.getSelectedIndex()); if (newLoc == null) System.err.println("move " + store.plainName(idx) + " unsuccessful"); else needsSave = true; }
private static int findAttributePage(int fileId, String attrId, boolean toWrite) throws IOException { checkFileIsValid(fileId); Storage storage = getAttributesStorage(); int encodedAttrId = DbConnection.getAttributeId(attrId); int recordId = getAttributeRecordId(fileId); if (recordId == 0) { if (!toWrite) return 0; recordId = storage.createNewRecord(); setAttributeRecordId(fileId, recordId); } else { DataInputStream attrRefs = storage.readStream(recordId); try { while (attrRefs.available() > 0) { final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs); final int attrAddress = DataInputOutputUtil.readINT(attrRefs); if (attIdOnPage == encodedAttrId) return attrAddress; } } finally { attrRefs.close(); } } if (toWrite) { Storage.AppenderStream appender = storage.appendStream(recordId); DataInputOutputUtil.writeINT(appender, encodedAttrId); int attrAddress = storage.createNewRecord(); DataInputOutputUtil.writeINT(appender, attrAddress); DbConnection.REASONABLY_SMALL.myAttrPageRequested = true; try { appender.close(); } finally { DbConnection.REASONABLY_SMALL.myAttrPageRequested = false; } return attrAddress; } return 0; }
public static void force() { try { w.lock(); if (myRecords != null) { markClean(); } if (myNames != null) { myNames.force(); myAttributes.force(); myContents.force(); myRecords.force(); } } finally { w.unlock(); } }
public static void flushSome() { if (!isDirty() || HeavyProcessLatch.INSTANCE.isRunning()) return; try { w.lock(); if (myFlushingFuture == null) { return; // avoid NPE when close has already taken place } myNames.force(); final boolean attribsFlushed = myAttributes.flushSome(); final boolean contentsFlushed = myContents.flushSome(); if (attribsFlushed && contentsFlushed) { markClean(); myRecords.force(); } } finally { w.unlock(); } }
public static String impJob(File fi, String date, String tags) { if (date == null) date = Storage.curDate(); if (tags == null) tags = Storage.NEW_TAG; return IMPORT_FLAG + "," + fi.getAbsolutePath() + "," + date + "," + tags; }
public Ssys3() { store = new Storage(); tableSorter = new TableRowSorter<Storage>(store); jobs = new LinkedList<String>(); makeGUI(); frm.setSize(800, 600); frm.addWindowListener( new WindowListener() { public void windowActivated(WindowEvent evt) {} public void windowClosed(WindowEvent evt) { try { System.out.println("joining EDT's"); for (EDT edt : encryptDecryptThreads) { edt.weakStop(); try { edt.join(); System.out.println(" - joined"); } catch (InterruptedException e) { System.out.println(" - Not joined"); } } System.out.println("saving storage"); store.saveAll(tempLoc); } catch (IOException e) { e.printStackTrace(); System.err.println( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nFailed to save properly\n\n!!!!!!!!!!!!!!!!!!!!!!!!!"); System.exit(1); } clean(); System.exit(0); } public void windowClosing(WindowEvent evt) { windowClosed(evt); } public void windowDeactivated(WindowEvent evt) {} public void windowDeiconified(WindowEvent evt) {} public void windowIconified(WindowEvent evt) {} public void windowOpened(WindowEvent evt) {} }); ImageIcon ico = new ImageIcon(ICON_NAME); frm.setIconImage(ico.getImage()); frm.setLocationRelativeTo(null); frm.setVisible(true); // load config storeLocs = new ArrayList<File>(); String ossl = "openssl"; int numThreadTemp = 2; boolean priorityDecryptTemp = true; boolean allowExportTemp = false; boolean checkImportTemp = true; try { Scanner sca = new Scanner(CONF_FILE); while (sca.hasNextLine()) { String ln = sca.nextLine(); if (ln.startsWith(CONF_SSL)) { ossl = ln.substring(CONF_SSL.length()); } else if (ln.startsWith(CONF_THREAD)) { try { numThreadTemp = Integer.parseInt(ln.substring(CONF_THREAD.length())); } catch (Exception exc) { // do Nothing } } else if (ln.equals(CONF_STORE)) { while (sca.hasNextLine()) storeLocs.add(new File(sca.nextLine())); } else if (ln.startsWith(CONF_PRIORITY)) { try { priorityDecryptTemp = Boolean.parseBoolean(ln.substring(CONF_PRIORITY.length())); } catch (Exception exc) { // do Nothing } } else if (ln.startsWith(CONF_EXPORT)) { try { allowExportTemp = Boolean.parseBoolean(ln.substring(CONF_EXPORT.length())); } catch (Exception exc) { // do Nothing } } else if (ln.startsWith(CONF_CONFIRM)) { try { checkImportTemp = Boolean.parseBoolean(ln.substring(CONF_CONFIRM.length())); } catch (Exception exc) { // do Nothing } } } sca.close(); } catch (IOException e) { } String osslWorks = OpenSSLCommander.test(ossl); while (osslWorks == null) { ossl = JOptionPane.showInputDialog( frm, "Please input the command used to run open ssl\n We will run \"<command> version\" to confirm\n Previous command: " + ossl, "Find open ssl", JOptionPane.OK_CANCEL_OPTION); if (ossl == null) { System.err.println("Refused to provide openssl executable location"); System.exit(1); } osslWorks = OpenSSLCommander.test(ossl); if (osslWorks == null) JOptionPane.showMessageDialog( frm, "Command " + ossl + " unsuccessful", "Unsuccessful", JOptionPane.ERROR_MESSAGE); } if (storeLocs.size() < 1) JOptionPane.showMessageDialog( frm, "Please select an initial sotrage location\nIf one already exists, or there are more than one, please select it"); while (storeLocs.size() < 1) { JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (jfc.showOpenDialog(frm) != JFileChooser.APPROVE_OPTION) { System.err.println("Refused to provide an initial store folder"); System.exit(1); } File sel = jfc.getSelectedFile(); if (sel.isDirectory()) storeLocs.add(sel); } numThreads = numThreadTemp; priorityExport = priorityDecryptTemp; allowExport = allowExportTemp; checkImports = checkImportTemp; try { PrintWriter pw = new PrintWriter(CONF_FILE); pw.println(CONF_SSL + ossl); pw.println(CONF_THREAD + numThreads); pw.println(CONF_PRIORITY + priorityExport); pw.println(CONF_EXPORT + allowExport); pw.println(CONF_CONFIRM + checkImports); pw.println(CONF_STORE); for (File fi : storeLocs) { pw.println(fi.getAbsolutePath()); } pw.close(); } catch (IOException e) { System.err.println("Failed to save config"); } File chk = null; for (File fi : storeLocs) { File lib = new File(fi, LIBRARY_NAME); if (lib.exists()) { chk = lib; // break; } } char[] pass = null; if (chk == null) { JOptionPane.showMessageDialog( frm, "First time run\n Create your password", "Create Password", JOptionPane.INFORMATION_MESSAGE); char[] p1 = askPassword(); char[] p2 = askPassword(); boolean same = p1.length == p2.length; for (int i = 0; i < Math.min(p1.length, p2.length); i++) { if (p1[i] != p2[i]) same = false; } if (same) { JOptionPane.showMessageDialog( frm, "Password created", "Create Password", JOptionPane.INFORMATION_MESSAGE); pass = p1; } else { JOptionPane.showMessageDialog( frm, "Passwords dont match", "Create Password", JOptionPane.ERROR_MESSAGE); System.exit(1); } } else { pass = askPassword(); } sec = OpenSSLCommander.getCommander(chk, pass, ossl); if (sec == null) { System.err.println("Wrong Password"); System.exit(1); } store.useSecurity(sec); store.useStorage(storeLocs); tempLoc = new File("temp"); if (!tempLoc.exists()) tempLoc.mkdirs(); // load stores try { store.loadAll(tempLoc); store.fireTableDataChanged(); } catch (IOException e) { System.err.println("Storage loading failure"); System.exit(1); } needsSave = false; encryptDecryptThreads = new EDT[numThreads]; for (int i = 0; i < encryptDecryptThreads.length; i++) { encryptDecryptThreads[i] = new EDT(i); encryptDecryptThreads[i].start(); } updateStatus(); txaSearch.grabFocus(); }