X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.cpp;h=805a9e8dba0072270d90a8cb695faa681e710e3a;hp=90326093b8b643751446d83658a4b2583dbf3fbb;hb=959bb34629f786cd64f666029407e60bdd37d418;hpb=82d74297d5b469b0a506d7010a84ab5115cd88ee diff --git a/source/io/file.cpp b/source/io/file.cpp index 9032609..805a9e8 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -17,18 +17,22 @@ File::File(const string &fn, Mode m, CreateMode cm) { if(!(m&M_RDWR)) throw invalid_argument("File::File mode"); - if(cm&~(C_CREATE|C_TRUNCATE)) + if(cm&~(C_CREATE|C_TRUNCATE|C_EXCLUSIVE)) + throw invalid_argument("File::File create"); + if((cm&C_EXCLUSIVE) && (!(cm&C_CREATE) || (cm&C_TRUNCATE))) throw invalid_argument("File::File create"); mode = m; #ifdef WIN32 int flags = 0; + int share_flags = 0; int create_flags = OPEN_EXISTING; if(mode&M_READ) flags |= GENERIC_READ; - else if(mode&M_WRITE) + + if(mode&M_WRITE) { flags |= GENERIC_WRITE; @@ -37,16 +41,21 @@ File::File(const string &fn, Mode m, CreateMode cm) case C_NONE: create_flags = OPEN_EXISTING; break; case C_CREATE: create_flags = OPEN_ALWAYS; break; case C_TRUNCATE: create_flags = TRUNCATE_EXISTING; break; - case C_CREATE+C_TRUNCATE: create_flags = CREATE_ALWAYS; break; + case C_OVERWRITE: create_flags = CREATE_ALWAYS; break; + case C_NEW: create_flags = CREATE_NEW; break; } } + else + share_flags = FILE_SHARE_READ; - *handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); + *handle = CreateFile(fn.c_str(), flags, share_flags, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); if(!handle) { int err = GetLastError(); if(err==ERROR_FILE_NOT_FOUND) throw file_not_found(fn); + else if(err==ERROR_FILE_EXISTS) + throw file_already_exists(fn); else throw system_error(format("CreateFile(%s)", fn), err); } @@ -66,6 +75,8 @@ File::File(const string &fn, Mode m, CreateMode cm) flags |= O_CREAT; if(cm&C_TRUNCATE) flags |= O_TRUNC; + if(cm&C_EXCLUSIVE) + flags |= O_EXCL; } if(mode&M_APPEND) flags |= O_APPEND; @@ -78,6 +89,8 @@ File::File(const string &fn, Mode m, CreateMode cm) int err = errno; if(err==ENOENT) throw file_not_found(fn); + else if(err==EEXIST) + throw file_already_exists(fn); else throw system_error(format("open(%s)", fn), err); } @@ -88,7 +101,6 @@ File::~File() { signal_flush_required.emit(); sys_close(handle); - signal_closed.emit(); } void File::set_block(bool b) @@ -154,5 +166,11 @@ SeekOffset File::tell() const return sys_seek(const_cast(handle), 0, S_CUR); } +const Handle &File::get_handle(Mode m) +{ + check_access(m); + return handle; +} + } // namespace IO } // namespace Msp