001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.io;
019:
020: import org.apache.log4j.Logger;
021:
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.OutputStreamWriter;
026: import java.io.Writer;
027:
028: /**
029: * @author mog
030: * @version $Id: SafeFileWriter.java 1513 2006-10-13 22:41:08Z tdsoul $
031: */
032: public class SafeFileWriter extends Writer {
033: private File _actualFile;
034: private OutputStreamWriter _out;
035: private File _tempFile;
036: private boolean failed = false;
037:
038: /**
039: * @see java.io.File#File(java.io.File)
040: */
041: public SafeFileWriter(File file) throws IOException {
042: _actualFile = file;
043:
044: if (!_actualFile.getAbsoluteFile().getParentFile().canWrite()) {
045: throw new IOException("Can't write to target dir");
046: }
047:
048: File dir = _actualFile.getParentFile();
049:
050: if (dir == null) {
051: dir = new File(".");
052: }
053:
054: _tempFile = File.createTempFile(_actualFile.getName(), null,
055: dir);
056: _out = new OutputStreamWriter(new FileOutputStream(_tempFile),
057: "UTF-8");
058: }
059:
060: /**
061: * @see java.io.File#File(java.lang.String)
062: */
063: public SafeFileWriter(String fileName) throws IOException {
064: this (new File(fileName));
065: }
066:
067: public void close() throws IOException {
068: _out.flush();
069: _out.close();
070:
071: if (!failed) {
072: Logger.getLogger(SafeFileWriter.class).debug(
073: "Renaming " + _tempFile + " (" + _tempFile.length()
074: + ") to " + _actualFile);
075:
076: if (_actualFile.exists() && !_actualFile.delete()) {
077: throw new IOException("delete() failed");
078: }
079:
080: if (!_tempFile.exists()) {
081: throw new IOException("source doesn't exist");
082: }
083:
084: if (!_tempFile.renameTo(_actualFile)) {
085: throw new IOException("renameTo(" + _tempFile + ", "
086: + _actualFile + ") failed");
087: }
088: }
089: }
090:
091: public void flush() throws IOException {
092: _out.flush();
093: }
094:
095: public void write(char[] cbuf, int off, int len) throws IOException {
096: try {
097: _out.write(cbuf, off, len);
098: } catch (IOException e) {
099: failed = true;
100: throw e;
101: }
102: }
103: }
|