2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.versioning.system.cvss;
44
import org.netbeans.lib.cvsclient.file.DefaultFileHandler;
45
import org.netbeans.lib.cvsclient.file.FileReadOnlyHandler;
46
import org.netbeans.modules.versioning.system.cvss.util.Utils;
47
import org.openide.filesystems.FileUtil;
48
import org.openide.filesystems.FileObject;
49
import org.openide.filesystems.FileLock;
50
import org.openide.util.Utilities;
53
import java.io.IOException;
54
import java.io.OutputStream;
55
import java.io.FileOutputStream;
58
* Cvs client library FileHandler that performs
59
* operations using openide filesystems.
61
* <p>It writes user's data files. Folders, temporary
62
* files and metadata files are written directly by
63
* the cvsclient library.
65
* <p>It supresses FilesystemHandler event propagating
70
class CvsLiteFileHandler extends DefaultFileHandler implements FileReadOnlyHandler {
72
protected boolean createNewFile(File file) throws IOException {
73
boolean exists = file.isFile();
77
File parent = file.getParentFile();
78
FileObject fo = Utils.mkfolders(parent);
80
FilesystemHandler.ignoreEvents(true);
82
fo.createData(file.getName());
83
} catch (IOException e) {
84
// #69639: Try File I/O instead
85
return file.createNewFile();
88
FilesystemHandler.ignoreEvents(false);
94
protected OutputStream createOutputStream(File file) throws IOException {
95
file = FileUtil.normalizeFile(file);
96
FileObject fo = FileUtil.toFileObject(file);
98
// #69639: Try File I/O instead
99
return new FileOutputStream(file);
101
FileLock lock = fo.lock();
102
OutputStream stream = null;
104
stream = fo.getOutputStream(lock);
105
} catch (IOException e) {
109
return new LockedOutputStream(lock, stream);
112
public void removeLocalFile(String pathname) throws IOException {
113
File fileToDelete = new File(pathname);
114
fileToDelete = FileUtil.normalizeFile(fileToDelete);
115
FileObject fo = FileUtil.toFileObject(fileToDelete);
117
// #69639: Try File I/O instead
118
fileToDelete.delete();
122
FilesystemHandler.ignoreEvents(true);
125
FilesystemHandler.ignoreEvents(false);
129
public void setFileReadOnly(File file, boolean readOnly) throws IOException {
130
String [] command = new String[3];
131
// TODO: update for JDK 6
132
if (Utilities.isWindows()) {
133
command[0] = "attrib";
134
command[1] = readOnly ? "+R" : "-R";
136
command[0] = "chmod";
137
command[1] = readOnly ? "u-w" : "u+w";
139
command[2] = file.getAbsolutePath();
141
Runtime.getRuntime().exec(command);
142
} catch (Exception e) {
143
// probably does not work, ignore
147
private static class LockedOutputStream extends OutputStream {
149
private final OutputStream peer;
150
private final FileLock lock;
152
public LockedOutputStream(FileLock lock, OutputStream peer) {
157
public void close() throws IOException {
160
FilesystemHandler.ignoreEvents(true);
163
FilesystemHandler.ignoreEvents(false);
167
public void flush() throws IOException {
171
public void write(byte b[]) throws IOException {
175
public void write(byte b[], int off, int len) throws IOException {
176
peer.write(b, off, len);
179
public void write(int b) throws IOException {