1
+ /*******************************************************************************
2
+ * Copyright (c) 2024, 2024 Vector Informatik GmbH and others.
3
+ *
4
+ * This program and the accompanying materials
5
+ * are made available under the terms of the Eclipse Public License v2.0
6
+ * which accompanies this distribution, and is available at
7
+ * https://www.eclipse.org/legal/epl-2.0/
8
+ *
9
+ * SPDX-License-Identifier: EPL-2.0
10
+ * Contributors:
11
+ * Vector Informatik GmbH - initial API and implementation
12
+ *******************************************************************************/
13
+ package org .eclipse .core .tests .filesystem ;
14
+
15
+ import java .io .ByteArrayOutputStream ;
16
+ import java .io .IOException ;
17
+ import java .io .InputStream ;
18
+ import java .io .OutputStream ;
19
+ import java .net .URI ;
20
+ import java .nio .file .Files ;
21
+ import java .nio .file .LinkOption ;
22
+ import java .nio .file .Path ;
23
+ import java .nio .file .attribute .FileTime ;
24
+ import org .eclipse .core .filesystem .EFS ;
25
+ import org .eclipse .core .filesystem .IFileInfo ;
26
+ import org .eclipse .core .filesystem .IFileStore ;
27
+ import org .eclipse .core .filesystem .provider .FileInfo ;
28
+ import org .eclipse .core .filesystem .provider .FileStore ;
29
+ import org .eclipse .core .runtime .CoreException ;
30
+ import org .eclipse .core .runtime .IProgressMonitor ;
31
+ import org .eclipse .core .runtime .IStatus ;
32
+ import org .eclipse .core .runtime .Status ;
33
+
34
+ class OnCloseWritingFileStore extends FileStore {
35
+
36
+ private final Path filePath ;
37
+
38
+ public OnCloseWritingFileStore (Path file ) {
39
+ this .filePath = file ;
40
+ }
41
+
42
+ @ Override
43
+ public OutputStream openOutputStream (int options , IProgressMonitor monitor ) {
44
+ return new ByteArrayOutputStream () {
45
+ @ Override
46
+ public void close () throws IOException {
47
+ Files .write (filePath , this .toByteArray ());
48
+ }
49
+ };
50
+ }
51
+
52
+ @ Override
53
+ public void putInfo (IFileInfo info , int options , IProgressMonitor monitor ) throws CoreException {
54
+ if ((options & EFS .SET_LAST_MODIFIED ) != 0 ) {
55
+ FileTime lastModified = FileTime .fromMillis (info .getLastModified ());
56
+ try {
57
+ Files .setLastModifiedTime (filePath , lastModified );
58
+ } catch (IOException e ) {
59
+ if (!Files .exists (filePath , LinkOption .NOFOLLOW_LINKS )) {
60
+ throw new CoreException (new Status (IStatus .ERROR , "CopyBugFileStore" , "File does not exist" , e ));
61
+ }
62
+ throw new CoreException (new Status (IStatus .ERROR , "CopyBugFileStore" , "Failed to set attribute" , e ));
63
+ }
64
+ }
65
+ }
66
+
67
+ @ Override
68
+ public IFileInfo fetchInfo (int options , IProgressMonitor monitor ) throws CoreException {
69
+ FileInfo info = new FileInfo ();
70
+ try {
71
+ info .setLastModified (Files .getLastModifiedTime (filePath ).toMillis ());
72
+ info .setLength (Files .size (filePath ));
73
+ info .setExists (Files .exists (filePath ));
74
+ } catch (IOException e ) {
75
+ throw new CoreException (new Status (IStatus .ERROR , "TestFileStore" , "Failed to fetch file info" , e ));
76
+ }
77
+ return info ;
78
+ }
79
+
80
+ @ Override
81
+ public String getName () {
82
+ return filePath .getFileName ().toString ();
83
+ }
84
+
85
+ @ Override
86
+ public IFileStore getParent () {
87
+ throw new UnsupportedOperationException ();
88
+ }
89
+
90
+ @ Override
91
+ public String [] childNames (int options , IProgressMonitor monitor ) throws CoreException {
92
+ throw new UnsupportedOperationException ();
93
+ }
94
+
95
+ @ Override
96
+ public IFileStore getChild (String name ) {
97
+ throw new UnsupportedOperationException ();
98
+ }
99
+
100
+ @ Override
101
+ public InputStream openInputStream (int options , IProgressMonitor monitor ) throws CoreException {
102
+ throw new UnsupportedOperationException ();
103
+ }
104
+
105
+ @ Override
106
+ public URI toURI () {
107
+ throw new UnsupportedOperationException ();
108
+ }
109
+ }
0 commit comments