mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 22:50:38 +03:00
Get rid of transitional lfs API
Those kind of functions should be placed in druntime, but the *64 (v) functions as a public interface are actually deprecated and shouldn't be used anymore. Druntime takes care that the normal stat, fopen, calls support large files, if __USE_FILE_OFFSET64 is defined (which is what the new C API does as well) Note: It's actually important to use the functions from core.sys.posix.stdio as the functions in core.stdc.stdio are not large file aware. This is probably a bug in druntime.
This commit is contained in:
parent
7a04239be8
commit
f989b219cc
3 changed files with 59 additions and 162 deletions
157
std/file.d
157
std/file.d
|
@ -73,110 +73,7 @@ version (Windows)
|
||||||
}
|
}
|
||||||
else version (Posix)
|
else version (Posix)
|
||||||
{
|
{
|
||||||
version (OSX)
|
deprecated alias stat_t struct_stat64;
|
||||||
{
|
|
||||||
import core.stdc.config : c_long;
|
|
||||||
// struct prefix to distinguish it from the stat() function.
|
|
||||||
// Ported from /usr/include/sys/stat.h on an OS X Lion box.
|
|
||||||
struct struct_stat64
|
|
||||||
{
|
|
||||||
dev_t st_dev; /// device
|
|
||||||
mode_t st_mode;
|
|
||||||
nlink_t st_nlink; /// link count
|
|
||||||
ulong st_ino; /// file serial number
|
|
||||||
uid_t st_uid; /// user ID of file's owner
|
|
||||||
gid_t st_gid; /// user ID of group's owner
|
|
||||||
dev_t st_rdev; /// if device then device number
|
|
||||||
|
|
||||||
time_t st_atime;
|
|
||||||
c_long st_atimensec;
|
|
||||||
time_t st_mtime;
|
|
||||||
c_long st_mtimensec;
|
|
||||||
time_t st_ctime;
|
|
||||||
c_long st_ctimensec;
|
|
||||||
time_t st_birthtime;
|
|
||||||
c_long st_birthtimensec;
|
|
||||||
|
|
||||||
off_t st_size;
|
|
||||||
blkcnt_t st_blocks; /// number of allocated 512 byte blocks
|
|
||||||
blksize_t st_blksize; /// optimal I/O block size
|
|
||||||
|
|
||||||
uint st_flags;
|
|
||||||
uint st_gen;
|
|
||||||
int st_lspare; /* RESERVED: DO NOT USE! */
|
|
||||||
long st_qspare[2]; /* RESERVED: DO NOT USE! */
|
|
||||||
}
|
|
||||||
|
|
||||||
extern(C) int fstat64(int, struct_stat64*);
|
|
||||||
extern(C) int stat64(in char*, struct_stat64*);
|
|
||||||
extern(C) int lstat64(in char*, struct_stat64*);
|
|
||||||
}
|
|
||||||
else version (FreeBSD)
|
|
||||||
{
|
|
||||||
alias core.sys.posix.sys.stat.stat_t struct_stat64;
|
|
||||||
alias core.sys.posix.sys.stat.fstat fstat64;
|
|
||||||
alias core.sys.posix.sys.stat.stat stat64;
|
|
||||||
alias core.sys.posix.sys.stat.lstat lstat64;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
version(D_LP64)
|
|
||||||
{
|
|
||||||
struct struct_stat64
|
|
||||||
{
|
|
||||||
ulong st_dev;
|
|
||||||
ulong st_ino;
|
|
||||||
ulong st_nlink;
|
|
||||||
uint st_mode;
|
|
||||||
uint st_uid;
|
|
||||||
uint st_gid;
|
|
||||||
int __pad0;
|
|
||||||
ulong st_rdev;
|
|
||||||
long st_size;
|
|
||||||
long st_blksize;
|
|
||||||
long st_blocks;
|
|
||||||
long st_atime;
|
|
||||||
ulong st_atimensec;
|
|
||||||
long st_mtime;
|
|
||||||
ulong st_mtimensec;
|
|
||||||
long st_ctime;
|
|
||||||
ulong st_ctimensec;
|
|
||||||
long[3] __unused;
|
|
||||||
}
|
|
||||||
static assert(struct_stat64.sizeof == 144);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
struct struct_stat64 // distinguish it from the stat() function
|
|
||||||
{
|
|
||||||
ulong st_dev; /// device
|
|
||||||
uint __pad1;
|
|
||||||
uint st_ino; /// file serial number
|
|
||||||
uint st_mode; /// file mode
|
|
||||||
uint st_nlink; /// link count
|
|
||||||
uint st_uid; /// user ID of file's owner
|
|
||||||
uint st_gid; /// user ID of group's owner
|
|
||||||
ulong st_rdev; /// if device then device number
|
|
||||||
uint __pad2;
|
|
||||||
align(4) ulong st_size;
|
|
||||||
int st_blksize; /// optimal I/O block size
|
|
||||||
ulong st_blocks; /// number of allocated 512 byte blocks
|
|
||||||
int st_atime;
|
|
||||||
uint st_atimensec;
|
|
||||||
int st_mtime;
|
|
||||||
uint st_mtimensec;
|
|
||||||
int st_ctime;
|
|
||||||
uint st_ctimensec;
|
|
||||||
|
|
||||||
ulong st_ino64;
|
|
||||||
}
|
|
||||||
//static assert(struct_stat64.sizeof == 88); // copied from d1, but it's currently 96 bytes, not 88.
|
|
||||||
}
|
|
||||||
|
|
||||||
extern(C) int fstat64(int, struct_stat64*);
|
|
||||||
extern(C) int stat64(in char*, struct_stat64*);
|
|
||||||
extern(C) int lstat64(in char*, struct_stat64*);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
|
@ -319,9 +216,8 @@ void[] read(in char[] name, size_t upTo = size_t.max)
|
||||||
cenforce(fd != -1, name);
|
cenforce(fd != -1, name);
|
||||||
scope(exit) core.sys.posix.unistd.close(fd);
|
scope(exit) core.sys.posix.unistd.close(fd);
|
||||||
|
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
cenforce(fstat64(fd, &statbuf) == 0, name);
|
cenforce(fstat(fd, &statbuf) == 0, name);
|
||||||
//cenforce(core.sys.posix.sys.stat.fstat(fd, &statbuf) == 0, name);
|
|
||||||
|
|
||||||
immutable initialAlloc = to!size_t(statbuf.st_size
|
immutable initialAlloc = to!size_t(statbuf.st_size
|
||||||
? min(statbuf.st_size + 1, maxInitialAlloc)
|
? min(statbuf.st_size + 1, maxInitialAlloc)
|
||||||
|
@ -556,8 +452,8 @@ ulong getSize(in char[] name)
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
cenforce(stat64(toStringz(name), &statbuf) == 0, name);
|
cenforce(stat(toStringz(name), &statbuf) == 0, name);
|
||||||
return statbuf.st_size;
|
return statbuf.st_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -599,9 +495,9 @@ void getTimes(in char[] name,
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
|
|
||||||
cenforce(stat64(toStringz(name), &statbuf) == 0, name);
|
cenforce(stat(toStringz(name), &statbuf) == 0, name);
|
||||||
|
|
||||||
fileAccessTime = SysTime(unixTimeToStdTime(statbuf.st_atime));
|
fileAccessTime = SysTime(unixTimeToStdTime(statbuf.st_atime));
|
||||||
fileModificationTime = SysTime(unixTimeToStdTime(statbuf.st_mtime));
|
fileModificationTime = SysTime(unixTimeToStdTime(statbuf.st_mtime));
|
||||||
|
@ -777,9 +673,9 @@ SysTime timeLastModified(in char[] name)
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
|
|
||||||
cenforce(stat64(toStringz(name), &statbuf) == 0, name);
|
cenforce(stat(toStringz(name), &statbuf) == 0, name);
|
||||||
|
|
||||||
return SysTime(unixTimeToStdTime(statbuf.st_mtime));
|
return SysTime(unixTimeToStdTime(statbuf.st_mtime));
|
||||||
}
|
}
|
||||||
|
@ -832,9 +728,9 @@ SysTime timeLastModified(in char[] name, SysTime returnIfMissing)
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
|
|
||||||
return stat64(toStringz(name), &statbuf) != 0 ?
|
return stat(toStringz(name), &statbuf) != 0 ?
|
||||||
returnIfMissing :
|
returnIfMissing :
|
||||||
SysTime(unixTimeToStdTime(statbuf.st_mtime));
|
SysTime(unixTimeToStdTime(statbuf.st_mtime));
|
||||||
}
|
}
|
||||||
|
@ -893,8 +789,8 @@ unittest
|
||||||
so it's safer to use stat instead.
|
so it's safer to use stat instead.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
return stat64(toStringz(name), &statbuf) == 0;
|
return stat(toStringz(name), &statbuf) == 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -938,9 +834,9 @@ uint getAttributes(in char[] name)
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
|
|
||||||
cenforce(stat64(toStringz(name), &statbuf) == 0, name);
|
cenforce(stat(toStringz(name), &statbuf) == 0, name);
|
||||||
|
|
||||||
return statbuf.st_mode;
|
return statbuf.st_mode;
|
||||||
}
|
}
|
||||||
|
@ -971,8 +867,8 @@ uint getLinkAttributes(in char[] name)
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
struct_stat64 lstatbuf = void;
|
stat_t lstatbuf = void;
|
||||||
cenforce(lstat64(toStringz(name), &lstatbuf) == 0, name);
|
cenforce(lstat(toStringz(name), &lstatbuf) == 0, name);
|
||||||
return lstatbuf.st_mode;
|
return lstatbuf.st_mode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1743,14 +1639,15 @@ assert(!de2.isFile);
|
||||||
+/
|
+/
|
||||||
@property uint linkAttributes();
|
@property uint linkAttributes();
|
||||||
|
|
||||||
version(Windows) alias void* struct_stat64;
|
version(Windows)
|
||||||
|
alias void* stat_t;
|
||||||
|
|
||||||
/++
|
/++
|
||||||
$(BLUE This function is Posix-Only.)
|
$(BLUE This function is Posix-Only.)
|
||||||
|
|
||||||
The $(D stat) struct gotten from calling $(D stat).
|
The $(D stat) struct gotten from calling $(D stat).
|
||||||
+/
|
+/
|
||||||
@property struct_stat64 statBuf();
|
@property stat_t statBuf();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else version(Windows)
|
else version(Windows)
|
||||||
|
@ -1945,7 +1842,7 @@ else version(Posix)
|
||||||
return _lstatMode;
|
return _lstatMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@property struct_stat64 statBuf()
|
@property stat_t statBuf()
|
||||||
{
|
{
|
||||||
_ensureStatDone();
|
_ensureStatDone();
|
||||||
|
|
||||||
|
@ -1997,7 +1894,7 @@ else version(Posix)
|
||||||
if(_didStat)
|
if(_didStat)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
enforce(stat64(toStringz(_name), &_statBuf) == 0,
|
enforce(stat(toStringz(_name), &_statBuf) == 0,
|
||||||
"Failed to stat file `" ~ _name ~ "'");
|
"Failed to stat file `" ~ _name ~ "'");
|
||||||
|
|
||||||
_didStat = true;
|
_didStat = true;
|
||||||
|
@ -2012,9 +1909,9 @@ else version(Posix)
|
||||||
if(_didLStat)
|
if(_didLStat)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
|
|
||||||
enforce(lstat64(toStringz(_name), &statbuf) == 0,
|
enforce(lstat(toStringz(_name), &statbuf) == 0,
|
||||||
"Failed to stat file `" ~ _name ~ "'");
|
"Failed to stat file `" ~ _name ~ "'");
|
||||||
|
|
||||||
_lstatMode = statbuf.st_mode;
|
_lstatMode = statbuf.st_mode;
|
||||||
|
@ -2026,7 +1923,7 @@ else version(Posix)
|
||||||
|
|
||||||
string _name; /// The file or directory represented by this DirEntry.
|
string _name; /// The file or directory represented by this DirEntry.
|
||||||
|
|
||||||
struct_stat64 _statBuf = void; /// The result of stat().
|
stat_t _statBuf = void; /// The result of stat().
|
||||||
uint _lstatMode; /// The stat mode from lstat().
|
uint _lstatMode; /// The stat mode from lstat().
|
||||||
ubyte _dType; /// The type of the file.
|
ubyte _dType; /// The type of the file.
|
||||||
|
|
||||||
|
@ -2151,8 +2048,8 @@ void copy(in char[] from, in char[] to)
|
||||||
cenforce(fd != -1, from);
|
cenforce(fd != -1, from);
|
||||||
scope(exit) core.sys.posix.unistd.close(fd);
|
scope(exit) core.sys.posix.unistd.close(fd);
|
||||||
|
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
cenforce(fstat64(fd, &statbuf) == 0, from);
|
cenforce(fstat(fd, &statbuf) == 0, from);
|
||||||
//cenforce(core.sys.posix.sys.stat.fstat(fd, &statbuf) == 0, from);
|
//cenforce(core.sys.posix.sys.stat.fstat(fd, &statbuf) == 0, from);
|
||||||
|
|
||||||
auto toz = toStringz(to);
|
auto toz = toStringz(to);
|
||||||
|
|
|
@ -122,8 +122,8 @@ class MmFile
|
||||||
fd = fildes;
|
fd = fildes;
|
||||||
|
|
||||||
// Adjust size
|
// Adjust size
|
||||||
struct_stat64 statbuf = void;
|
stat_t statbuf = void;
|
||||||
errnoEnforce(fstat64(fd, &statbuf) == 0);
|
errnoEnforce(fstat(fd, &statbuf) == 0);
|
||||||
if (prot & PROT_WRITE && size > statbuf.st_size)
|
if (prot & PROT_WRITE && size > statbuf.st_size)
|
||||||
{
|
{
|
||||||
// Need to make the file size bytes big
|
// Need to make the file size bytes big
|
||||||
|
@ -316,8 +316,8 @@ class MmFile
|
||||||
fd = .open(namez, oflag, fmode);
|
fd = .open(namez, oflag, fmode);
|
||||||
errnoEnforce(fd != -1, "Could not open file "~filename);
|
errnoEnforce(fd != -1, "Could not open file "~filename);
|
||||||
|
|
||||||
struct_stat64 statbuf;
|
stat_t statbuf;
|
||||||
if (fstat64(fd, &statbuf))
|
if (fstat(fd, &statbuf))
|
||||||
{
|
{
|
||||||
//printf("\tfstat error, errno = %d\n", errno);
|
//printf("\tfstat error, errno = %d\n", errno);
|
||||||
.close(fd);
|
.close(fd);
|
||||||
|
|
48
std/stdio.d
48
std/stdio.d
|
@ -43,19 +43,16 @@ version (linux)
|
||||||
{
|
{
|
||||||
// Specific to the way Gnu C does stdio
|
// Specific to the way Gnu C does stdio
|
||||||
version = GCC_IO;
|
version = GCC_IO;
|
||||||
extern(C) FILE* fopen64(const char*, const char*);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
version (OSX)
|
version (OSX)
|
||||||
{
|
{
|
||||||
version = GENERIC_IO;
|
version = GENERIC_IO;
|
||||||
alias core.stdc.stdio.fopen fopen64;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
version (FreeBSD)
|
version (FreeBSD)
|
||||||
{
|
{
|
||||||
version = GENERIC_IO;
|
version = GENERIC_IO;
|
||||||
alias core.stdc.stdio.fopen fopen64;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
version(Windows)
|
version(Windows)
|
||||||
|
@ -63,14 +60,9 @@ version(Windows)
|
||||||
// core.stdc.stdio.fopen expects file names to be
|
// core.stdc.stdio.fopen expects file names to be
|
||||||
// encoded in CP_ACP on Windows instead of UTF-8.
|
// encoded in CP_ACP on Windows instead of UTF-8.
|
||||||
/+ Waiting for druntime pull 299
|
/+ Waiting for druntime pull 299
|
||||||
alias core.sys.windows.c.stdio._wfopen fopen64;
|
+/
|
||||||
+/ extern (C) nothrow FILE* _wfopen(in wchar* filename, in wchar* mode);
|
extern (C) nothrow FILE* _wfopen(in wchar* filename, in wchar* mode);
|
||||||
alias _wfopen fopen64;
|
|
||||||
|
|
||||||
alias std.utf.toUTF16z toOSStringz;
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
alias std.string.toStringz toOSStringz;
|
|
||||||
|
|
||||||
version (DIGITAL_MARS_STDIO)
|
version (DIGITAL_MARS_STDIO)
|
||||||
{
|
{
|
||||||
|
@ -124,13 +116,6 @@ else version (GCC_IO)
|
||||||
size_t size, size_t n, _iobuf *stream);
|
size_t size, size_t n, _iobuf *stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
version (linux)
|
|
||||||
{
|
|
||||||
// declare fopen64 if not already
|
|
||||||
static if (!is(typeof(fopen64)))
|
|
||||||
extern (C) FILE* fopen64(in char*, in char*);
|
|
||||||
}
|
|
||||||
|
|
||||||
alias fputc_unlocked FPUTC;
|
alias fputc_unlocked FPUTC;
|
||||||
alias fputwc_unlocked FPUTWC;
|
alias fputwc_unlocked FPUTWC;
|
||||||
alias fgetc_unlocked FGETC;
|
alias fgetc_unlocked FGETC;
|
||||||
|
@ -1877,26 +1862,41 @@ size_t readln(ref char[] buf, dchar terminator = '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convenience function that forwards to $(D std.c.stdio.fopen)
|
* Convenience function that forwards to $(D core.stdc.stdio.fopen)
|
||||||
* (to $(D std.c.stdio._wfopen) on Windows)
|
* (to $(D _wfopen) on Windows)
|
||||||
* with appropriately-constructed C-style strings.
|
* with appropriately-constructed C-style strings.
|
||||||
*/
|
*/
|
||||||
private FILE* fopen(in char[] name, in char[] mode = "r")
|
private FILE* fopen(in char[] name, in char[] mode = "r")
|
||||||
{
|
{
|
||||||
return fopen64(toOSStringz(name), toOSStringz(mode));
|
version(Windows)
|
||||||
|
return _wfopen(toUTF16z(name), toUTF16z(mode));
|
||||||
|
else version(Posix)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* The new opengroup large file support API is transparently
|
||||||
|
* included in the normal C bindings. http://opengroup.org/platform/lfs.html#1.0
|
||||||
|
* if _FILE_OFFSET_BITS in druntime is 64, off_t is 64 bit and
|
||||||
|
* the normal functions work fine. If not, then large file support
|
||||||
|
* probably isn't available. Do not use the old transitional API
|
||||||
|
* (the native extern(C) fopen64, http://www.unix.org/version2/whatsnew/lfs20mar.html#3.0)
|
||||||
|
*/
|
||||||
|
return core.sys.posix.stdio.fopen(toStringz(name), toStringz(mode));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return core.stdc.stdio.fopen(toStringz(name), toStringz(mode));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
version (Posix)
|
version (Posix)
|
||||||
{
|
{
|
||||||
extern(C) FILE* popen(const char*, const char*);
|
/***********************************
|
||||||
|
|
||||||
/***********************************
|
|
||||||
* Convenience function that forwards to $(D std.c.stdio.popen)
|
* Convenience function that forwards to $(D std.c.stdio.popen)
|
||||||
* with appropriately-constructed C-style strings.
|
* with appropriately-constructed C-style strings.
|
||||||
*/
|
*/
|
||||||
FILE* popen(in char[] name, in char[] mode = "r")
|
FILE* popen(in char[] name, in char[] mode = "r")
|
||||||
{
|
{
|
||||||
return popen(toOSStringz(name), toOSStringz(mode));
|
return core.sys.posix.stdio.popen(toStringz(name), toStringz(mode));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue