FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #1622 from ratnikov/refactor-channeldescriptor · MSNexploder/jruby@86f0346 · GitHub

forked from jruby/jruby

Commit 86f0346

Browse files
committed
Merge pull request jruby#1622 from ratnikov/refactor-channeldescriptor
Refactor channeldescriptor to use FileResource
2 parents 32ddb15 + 67f7892 commit 86f0346

14 files changed

Lines changed: 299 additions & 258 deletions

‎core/src/main/java/org/jruby/RubyFile.java‎

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737

3838
import jnr.constants.platform.OpenFlags;
3939
import org.jcodings.Encoding;
40-
import org.jruby.util.io.ModeFlags;
41-
import org.jruby.util.io.OpenFile;
42-
import org.jruby.util.io.ChannelDescriptor;
4340
import java.io.File;
4441
import java.io.FileDescriptor;
4542
import java.io.FileNotFoundException;
@@ -76,23 +73,26 @@
7673
import org.jruby.util.ByteList;
7774
import org.jruby.util.FileResource;
7875
import org.jruby.util.JRubyFile;
76+
import org.jruby.util.ResourceException;
7977
import org.jruby.util.TypeConverter;
80-
import org.jruby.util.io.DirectoryAsFileException;
81-
import org.jruby.util.io.PermissionDeniedException;
82-
import org.jruby.util.io.Stream;
83-
import org.jruby.util.io.ChannelStream;
84-
import org.jruby.util.io.IOOptions;
78+
import org.jruby.util.encoding.Transcoder;
8579
import org.jruby.util.io.BadDescriptorException;
80+
import org.jruby.util.io.ChannelDescriptor;
81+
import org.jruby.util.io.ChannelStream;
82+
import org.jruby.util.io.DirectoryAsFileException;
83+
import org.jruby.util.io.EncodingUtils;
8684
import org.jruby.util.io.FileExistsException;
85+
import org.jruby.util.io.IOEncodable;
86+
import org.jruby.util.io.IOOptions;
8787
import org.jruby.util.io.InvalidValueException;
88+
import org.jruby.util.io.ModeFlags;
89+
import org.jruby.util.io.OpenFile;
8890
import org.jruby.util.io.PipeException;
91+
import org.jruby.util.io.Stream;
8992
import static org.jruby.CompatVersion.*;
9093
import org.jruby.exceptions.RaiseException;
9194
import org.jruby.runtime.Helpers;
9295
import org.jruby.runtime.encoding.EncodingService;
93-
import org.jruby.util.encoding.Transcoder;
94-
import org.jruby.util.io.EncodingUtils;
95-
import org.jruby.util.io.IOEncodable;
9696

9797
/**
9898
* Ruby File class equivalent in java.
@@ -1299,23 +1299,16 @@ private ChannelDescriptor sysopen(String path, ModeFlags modes, int perm) {
12991299
// TODO: check if too many open files, GC and try again
13001300

13011301
return descriptor;
1302-
} catch (PermissionDeniedException pde) {
1303-
// PDException can be thrown only when creating the file and
1304-
// permission is denied. See JavaDoc of PermissionDeniedException.
1305-
throw getRuntime().newErrnoEACCESError(path);
1306-
} catch (FileNotFoundException fnfe) {
1307-
// FNFException can be thrown in both cases, when the file
1308-
// is not found, or when permission is denied.
1309-
if (Ruby.isSecurityRestricted() || new File(path).exists()) {
1310-
throw getRuntime().newErrnoEACCESError(path);
1311-
}
1312-
throw getRuntime().newErrnoENOENTError(path);
1313-
} catch (DirectoryAsFileException dafe) {
1314-
throw getRuntime().newErrnoEISDirError();
1315-
} catch (FileExistsException fee) {
1316-
throw getRuntime().newErrnoEEXISTError(path);
1317-
} catch (IOException ioe) {
1318-
throw getRuntime().newIOErrorFromException(ioe);
1302+
} catch (ResourceException resourceException) {
1303+
throw resourceException.newRaiseException(getRuntime());
1304+
} catch (FileNotFoundException ignored) {
1305+
throw new IllegalStateException("For compile compatibility only");
1306+
} catch (DirectoryAsFileException ignored) {
1307+
throw new IllegalStateException("For compile compatibility only");
1308+
} catch (FileExistsException ignored) {
1309+
throw new IllegalStateException("For compile compatibility only");
1310+
} catch (IOException ignored) {
1311+
throw new IllegalStateException("For compile compatibility only");
13191312
}
13201313
}
13211314

@@ -1325,34 +1318,18 @@ private Stream fopen(String path, ModeFlags flags) {
13251318
getRuntime(),
13261319
path,
13271320
flags);
1321+
} catch (InvalidValueException ex) {
1322+
throw getRuntime().newErrnoEINVALError();
1323+
} catch (PipeException ex) {
1324+
throw new IllegalStateException("For compile compatibility only");
13281325
} catch (BadDescriptorException e) {
1329-
throw getRuntime().newErrnoEBADFError();
1330-
} catch (PermissionDeniedException pde) {
1331-
// PDException can be thrown only when creating the file and
1332-
// permission is denied. See JavaDoc of PermissionDeniedException.
1333-
throw getRuntime().newErrnoEACCESError(path);
1326+
throw new IllegalStateException("For compile compatibility only");
13341327
} catch (FileNotFoundException ex) {
1335-
// FNFException can be thrown in both cases, when the file
1336-
// is not found, or when permission is denied.
1337-
// FIXME: yes, this is indeed gross.
1338-
String message = ex.getMessage();
1339-
1340-
if (message.contains(/*P*/"ermission denied") ||
1341-
message.contains(/*A*/"ccess is denied")) {
1342-
throw getRuntime().newErrnoEACCESError(path);
1343-
}
1344-
1345-
throw getRuntime().newErrnoENOENTError(path);
1346-
} catch (DirectoryAsFileException ex) {
1347-
throw getRuntime().newErrnoEISDirError();
1328+
throw new IllegalStateException("For compile compatibility only");
13481329
} catch (FileExistsException ex) {
1349-
throw getRuntime().newErrnoEEXISTError(path);
1330+
throw new IllegalStateException("For compile compatibility only");
13501331
} catch (IOException ex) {
1351-
throw getRuntime().newIOErrorFromException(ex);
1352-
} catch (InvalidValueException ex) {
1353-
throw getRuntime().newErrnoEINVALError();
1354-
} catch (PipeException ex) {
1355-
throw getRuntime().newErrnoEPIPEError();
1332+
throw new IllegalStateException("For compile compatibility only");
13561333
} catch (SecurityException ex) {
13571334
throw getRuntime().newErrnoEACCESError(path);
13581335
}

‎core/src/main/java/org/jruby/RubyIO.java‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
package org.jruby;
3737

3838
import org.jruby.runtime.Helpers;
39+
import org.jruby.util.ResourceException;
3940
import org.jruby.util.StringSupport;
41+
import org.jruby.util.io.DirectoryAsFileException;
4042
import org.jruby.util.io.EncodingUtils;
43+
import org.jruby.util.io.FileExistsException;
4144
import org.jruby.util.io.ModeFlags;
4245
import org.jruby.util.io.SelectBlob;
4346
import jnr.constants.platform.Fcntl;
@@ -87,8 +90,6 @@
8790
import org.jruby.util.io.ChannelStream;
8891
import org.jruby.util.io.InvalidValueException;
8992
import org.jruby.util.io.PipeException;
90-
import org.jruby.util.io.FileExistsException;
91-
import org.jruby.util.io.DirectoryAsFileException;
9293
import org.jruby.util.io.STDIO;
9394
import org.jruby.util.io.OpenFile;
9495
import org.jruby.util.io.ChannelDescriptor;
@@ -348,11 +349,7 @@ protected void reopenPath(Ruby runtime, IRubyObject[] args) {
348349
openFile.setPath(path);
349350

350351
if (openFile.getMainStream() == null) {
351-
try {
352-
openFile.setMainStream(ChannelStream.fopen(runtime, path, modes.getModeFlags()));
353-
} catch (FileExistsException fee) {
354-
throw runtime.newErrnoEEXISTError(path);
355-
}
352+
openFile.setMainStream(ChannelStream.fopen(runtime, path, modes.getModeFlags()));
356353

357354
if (openFile.getPipeStream() != null) {
358355
openFile.getPipeStream().fclose();
@@ -366,14 +363,16 @@ protected void reopenPath(Ruby runtime, IRubyObject[] args) {
366363
// TODO: pipe handler to be reopened with path and "w" mode
367364
}
368365
}
366+
} catch (InvalidValueException e) {
367+
throw runtime.newErrnoEINVALError();
369368
} catch (PipeException pe) {
370-
throw runtime.newErrnoEPIPEError();
369+
throw new IllegalStateException("For compile compatibility only");
371370
} catch (IOException ex) {
372-
throw runtime.newIOErrorFromException(ex);
371+
throw new IllegalStateException("For compile compatibility only");
373372
} catch (BadDescriptorException ex) {
374-
throw runtime.newErrnoEBADFError();
375-
} catch (InvalidValueException e) {
376-
throw runtime.newErrnoEINVALError();
373+
throw new IllegalStateException("For compile compatibility only");
374+
} catch (FileExistsException fee) {
375+
throw new IllegalStateException("For compile compatibility only");
377376
}
378377
}
379378

@@ -1241,16 +1240,19 @@ private static IRubyObject sysopenCommon(IRubyObject recv, IRubyObject[] args, B
12411240
runtime.getJRubyClassLoader());
12421241
// always a new fileno, so ok to use internal only
12431242
fileno = descriptor.getFileno();
1243+
} catch (ResourceException resourceException) {
1244+
throw resourceException.newRaiseException(runtime);
1245+
} catch (FileNotFoundException ignored) {
1246+
throw new IllegalStateException("For compile compatibility only");
1247+
} catch (DirectoryAsFileException ignored) {
1248+
throw new IllegalStateException("For compile compatibility only");
1249+
} catch (FileExistsException ignored) {
1250+
throw new IllegalStateException("For compile compatibility only");
1251+
} catch (IOException ignored) {
1252+
throw new IllegalStateException("For compile compatibility only");
12441253
}
1245-
catch (FileNotFoundException fnfe) {
1246-
throw runtime.newErrnoENOENTError(path);
1247-
} catch (DirectoryAsFileException dafe) {
1248-
throw runtime.newErrnoEISDirError(path);
1249-
} catch (FileExistsException fee) {
1250-
throw runtime.newErrnoEEXISTError(path);
1251-
} catch (IOException ioe) {
1252-
throw runtime.newIOErrorFromException(ioe);
1253-
}
1254+
1255+
12541256
return runtime.newFixnum(fileno);
12551257
}
12561258

‎core/src/main/java/org/jruby/util/EmptyFileResource.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import jnr.posix.FileStat;
44
import jnr.posix.POSIX;
5-
import org.jruby.exceptions.RaiseException;
65
import org.jruby.runtime.ThreadContext;
6+
import org.jruby.util.io.ChannelDescriptor;
7+
import org.jruby.util.io.ModeFlags;
78

89
class EmptyFileResource implements FileResource {
910
// All empty resources are the same and immutable, so may as well
@@ -82,4 +83,9 @@ public JRubyFile hackyGetJRubyFile() {
8283
// should be okay for now.
8384
return JRubyNonExistentFile.NOT_EXIST;
8485
}
86+
87+
@Override
88+
public ChannelDescriptor openDescriptor(ModeFlags flags, POSIX posix, int perm) throws ResourceException {
89+
throw new ResourceException.NotFound(absolutePath());
90+
}
8591
}

‎core/src/main/java/org/jruby/util/FileResource.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import jnr.posix.FileStat;
44
import jnr.posix.POSIX;
5+
import org.jruby.util.io.ChannelDescriptor;
6+
import org.jruby.util.io.ModeFlags;
57

68
/**
79
* This is a shared interface for files loaded as {@link java.io.File} and {@link java.util.zip.ZipEntry}.
@@ -33,4 +35,6 @@ public interface FileResource {
3335
// JRubyFile if this resource is backed by one, and NOT_FOUND JRubyFile
3436
// otherwise.
3537
JRubyFile hackyGetJRubyFile();
38+
39+
ChannelDescriptor openDescriptor(ModeFlags flags, POSIX posix, int perm) throws ResourceException;
3640
}

‎core/src/main/java/org/jruby/util/JarDirectoryResource.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package org.jruby.util;
22

3+
import jnr.posix.POSIX;
4+
import org.jruby.Ruby;
5+
import org.jruby.util.io.ChannelDescriptor;
6+
import org.jruby.util.io.ModeFlags;
7+
38
/**
49
* Represents a directory in a jar.
510
*
@@ -53,4 +58,11 @@ public String[] list() {
5358
public boolean isRoot() {
5459
return "/".equals(path);
5560
}
61+
62+
@Override
63+
public ChannelDescriptor openDescriptor(ModeFlags flags, POSIX posix, int perm) throws ResourceException {
64+
// opening a directory seems to blow up with EACCESS in jruby (although MRI allows instantiation but blows up on read).
65+
// So mimicking that for now.
66+
throw new ResourceException.PermissionDenied(absolutePath());
67+
}
5668
}

‎core/src/main/java/org/jruby/util/JarFileResource.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package org.jruby.util;
22

3-
import java.util.jar.JarFile;
3+
import jnr.posix.POSIX;
4+
import org.jruby.Ruby;
5+
import org.jruby.util.io.ChannelDescriptor;
6+
import org.jruby.util.io.ModeFlags;
7+
8+
import java.io.InputStream;
9+
import java.nio.channels.Channels;
410
import java.util.jar.JarEntry;
511

612
/**
@@ -14,10 +20,12 @@
1420
*/
1521
class JarFileResource extends JarResource {
1622
private final JarEntry entry;
23+
private final InputStream entryStream;
1724

18-
JarFileResource(String jarPath, JarEntry entry) {
25+
JarFileResource(String jarPath, JarEntry entry, InputStream entryStream) {
1926
super(jarPath);
2027
this.entry = entry;
28+
this.entryStream = entryStream;
2129
}
2230

2331
@Override
@@ -50,4 +58,9 @@ public String[] list() {
5058
// Files cannot be listed
5159
return null;
5260
}
61+
62+
@Override
63+
public ChannelDescriptor openDescriptor(ModeFlags flags, POSIX posix, int perm) throws ResourceException {
64+
return new ChannelDescriptor(Channels.newChannel(entryStream), flags);
65+
}
5366
}

‎core/src/main/java/org/jruby/util/JarResource.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import jnr.posix.FileStat;
44
import jnr.posix.POSIX;
5+
import org.jruby.util.io.ChannelDescriptor;
6+
import org.jruby.util.io.ModeFlags;
7+
58
import java.io.IOException;
9+
import java.io.InputStream;
610
import java.util.regex.Matcher;
711
import java.util.regex.Pattern;
812
import java.util.jar.JarEntry;
913
import java.util.jar.JarFile;
1014

11-
public abstract class JarResource implements FileResource {
15+
abstract class JarResource implements FileResource {
1216
private static Pattern PREFIX_MATCH = Pattern.compile("^(?:jar:)?(?:file:)?(.*)$");
1317

1418
private static final JarCache jarCache = new JarCache();
@@ -53,9 +57,14 @@ private static JarResource createJarResource(String jarPath, String path) {
5357
return new JarDirectoryResource(jarPath, path, entries);
5458
}
5559

56-
JarEntry jarEntry = index.getJarEntry(path);
57-
if (jarEntry != null) {
58-
return new JarFileResource(jarPath, jarEntry);
60+
try {
61+
JarEntry jarEntry = index.getJarEntry(path);
62+
if (jarEntry != null) {
63+
InputStream jarEntryStream = index.jar.getInputStream(jarEntry);
64+
return new JarFileResource(path, jarEntry, jarEntryStream);
65+
}
66+
} catch (IOException ioe) {
67+
// Probably not a jar entry then
5968
}
6069

6170
return null;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL