Explorar el Código

File.update should create file if it doesn't exist (#6912)

Ben Morris hace 7 años
padre
commit
df6853da2f

+ 3 - 0
std/cpp/_std/sys/io/File.hx

@@ -60,6 +60,9 @@ class File {
 	}
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput {
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		return untyped new FileOutput(NativeFile.file_open(path,(if( binary ) "rb+" else "r+")));
 	}
 

+ 3 - 0
std/cs/_std/sys/io/File.hx

@@ -86,6 +86,9 @@ class File {
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput
 	{
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		#if std_buffer //standardize 4kb buffers
 		var stream = new cs.system.io.FileStream(path, OpenOrCreate, Write, ReadWrite, 4096);
 		#else

+ 3 - 0
std/hl/_std/sys/io/File.hx

@@ -74,6 +74,9 @@ typedef FileHandle = hl.Abstract<"hl_fdesc">;
 	}
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput {
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		var f = file_open(Sys.getPath(path),3,binary);
 		if( f == null ) throw new Sys.SysError("Can't open "+path+" for update");
 		return @:privateAccess new FileOutput(f);

+ 3 - 0
std/lua/_std/sys/io/File.hx

@@ -41,6 +41,9 @@ class File {
 	}
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput {
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		return new FileOutput(Io.open(path, binary ? "r+b" : "r+"));
 	}
 

+ 3 - 0
std/neko/_std/sys/io/File.hx

@@ -59,6 +59,9 @@ enum FileHandle {
 	}
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput {
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		return untyped new FileOutput(file_open(path.__s,(if( binary ) "rb+" else "r+").__s));
 	}
 

+ 3 - 0
std/php/_std/sys/io/File.hx

@@ -57,6 +57,9 @@ import php.Global;
 	}
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput {
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		return untyped new FileOutput(fopen(path, binary ? "rb+" : "r+"));
 	}
 

+ 3 - 0
std/python/_std/sys/io/File.hx

@@ -78,6 +78,9 @@ class File {
 	}
 
 	public static function update( path : String, binary : Bool = true ) : FileOutput {
+		if (!FileSystem.exists(path)) {
+			write(path).close();
+		}
 		var mode = if (binary) "rb+" else "r+";
 		var f = python.lib.Builtins.open(path, mode, -1, null, null, binary ? null : "");
 

+ 8 - 0
tests/unit/src/unitstd/sys/io/File.unit.hx

@@ -1,9 +1,11 @@
 #if sys
 var filename = '.sys.io.file.testfile';
 if (sys.FileSystem.exists(filename)) sys.FileSystem.deleteFile(filename);
+sys.FileSystem.exists(filename) == false;
 
 // test file write
 var fw = sys.io.File.write(filename);
+sys.FileSystem.exists(filename) == true;
 fw.writeString("apple\n");
 fw.close();
 sys.io.File.getContent(filename) == "apple\n";
@@ -31,5 +33,11 @@ fu.writeString("banana\n");
 fu.close();
 sys.io.File.getContent(filename) == "cherry\nbanana\n";
 
+// File.update should create the file if it doesn't exist
+sys.FileSystem.deleteFile(filename);
+var fu = sys.io.File.update(filename);
+fu.close();
+sys.FileSystem.exists(filename) == true;
+
 sys.FileSystem.deleteFile(filename);
 #end