From: <nr...@us...> - 2008-08-03 04:49:29
|
Revision: 5063 http://jython.svn.sourceforge.net/jython/?rev=5063&view=rev Author: nriley Date: 2008-08-03 04:49:27 +0000 (Sun, 03 Aug 2008) Log Message: ----------- Don't create file opened with r+; better validate file modes (fixes test_mailbox). Modified Paths: -------------- branches/asm/src/org/python/core/io/FileIO.java Modified: branches/asm/src/org/python/core/io/FileIO.java =================================================================== --- branches/asm/src/org/python/core/io/FileIO.java 2008-08-03 04:47:37 UTC (rev 5062) +++ branches/asm/src/org/python/core/io/FileIO.java 2008-08-03 04:49:27 UTC (rev 5063) @@ -40,6 +40,9 @@ /** true if the file is in appending mode */ private boolean appending = false; + /** true if the file is opened for reading and writing */ + private boolean plus = false; + /** * Construct a FileIO instance for the specified file name. * @@ -56,13 +59,17 @@ File fullPath = new RelativeFile(name); String rafMode = "r" + (writable ? "w" : ""); try { + if (plus && mode.charAt(0) == 'r' && !fullPath.isFile()) { + writable = false; + throw new FileNotFoundException(""); + } file = new RandomAccessFile(fullPath, rafMode); fileChannel = file.getChannel(); } catch (FileNotFoundException fnfe) { if (fullPath.isDirectory()) { throw Py.IOError(errno.EISDIR, "Is a directory"); } - if ( (rafMode.equals("rw") && !fullPath.canWrite()) || + if ( (writable && !fullPath.canWrite()) || fnfe.getMessage().endsWith("(Permission denied)")) { throw Py.IOError(errno.EACCES, "Permission denied: '" + name + "'"); } @@ -99,30 +106,29 @@ */ private void parseMode(String mode) { boolean rwa = false; - boolean plus = false; for (int i = 0; i < mode.length(); i++) { switch (mode.charAt(i)) { case 'r': - if (rwa) { + if (plus || rwa) { badMode(); } readable = rwa = true; break; case 'w': - if (rwa) { + if (plus || rwa) { badMode(); } writable = rwa = true; break; case 'a': - if (rwa) { + if (plus || rwa) { badMode(); } appending = writable = rwa = true; break; case '+': - if (plus) { + if (plus || !rwa) { badMode(); } readable = writable = plus = true; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |