/** * Unlock this file and abort this change. * * <p>The temporary file (if created) is deleted before returning. */ public void unlock() { final LockFile tmp = myLock; if (tmp != null) { myLock = null; tmp.unlock(); } }
/** * Save the configuration as a Git text style configuration file. * * <p><b>Warning:</b> Although this method uses the traditional Git file locking approach to * protect against concurrent writes of the configuration file, it does not ensure that the file * has not been modified since the last read, which means updates performed by other objects * accessing the same backing file may be lost. * * @throws IOException the file could not be written. */ public void save() throws IOException { final byte[] out; final String text = toText(); if (utf8Bom) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(0xEF); bos.write(0xBB); bos.write(0xBF); bos.write(text.getBytes(RawParseUtils.UTF8_CHARSET.name())); out = bos.toByteArray(); } else { out = Constants.encode(text); } final LockFile lf = new LockFile(getFile(), fs); if (!lf.lock()) throw new LockFailedException(getFile()); try { lf.setNeedSnapshot(true); lf.write(out); if (!lf.commit()) throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile())); } finally { lf.unlock(); } snapshot = lf.getCommitSnapshot(); hash = hash(out); // notify the listeners fireConfigChangedEvent(); }
/** * Write the entry records from memory to disk. * * <p>The cache must be locked first by calling {@link #lock()} and receiving true as the return * value. Applications are encouraged to lock the index, then invoke {@link #read()} to ensure the * in-memory data is current, prior to updating the in-memory entries. * * <p>Once written the lock is closed and must be either committed with {@link #commit()} or * rolled back with {@link #unlock()}. * * @throws IOException the output file could not be created. The caller no longer holds the lock. */ public void write() throws IOException { final LockFile tmp = myLock; requireLocked(tmp); try { writeTo(new SafeBufferedOutputStream(tmp.getOutputStream())); } catch (IOException err) { tmp.unlock(); throw err; } catch (RuntimeException err) { tmp.unlock(); throw err; } catch (Error err) { tmp.unlock(); throw err; } }