51
68
return path[i] == '/';
73
// Determine if a directory exists.
76
IceUtilInternal::directoryExists(const string& path)
78
IceUtilInternal::structstat st;
79
if(IceUtilInternal::stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
87
// Determine if a regular file exists.
90
IceUtilInternal::fileExists(const string& path)
92
IceUtilInternal::structstat st;
93
if(IceUtilInternal::stat(path, &st) != 0 || !S_ISREG(st.st_mode))
106
IceUtilInternal::stat(const string& path, structstat* buffer)
108
return _wstat(IceUtil::stringToWstring(path).c_str(), buffer);
112
IceUtilInternal::remove(const string& path)
114
return ::_wremove(IceUtil::stringToWstring(path).c_str());
118
IceUtilInternal::rename(const string& from, const string& to)
120
return ::_wrename(IceUtil::stringToWstring(from).c_str(), IceUtil::stringToWstring(to).c_str());
124
IceUtilInternal::rmdir(const string& path)
126
return ::_wrmdir(IceUtil::stringToWstring(path).c_str());
130
IceUtilInternal::mkdir(const string& path, int)
132
return ::_wmkdir(IceUtil::stringToWstring(path).c_str());
136
IceUtilInternal::fopen(const string& path, const string& mode)
138
return ::_wfopen(IceUtil::stringToWstring(path).c_str(), IceUtil::stringToWstring(mode).c_str());
142
IceUtilInternal::open(const string& path, int flags)
146
return ::_wopen(IceUtil::stringToWstring(path).c_str(), flags, _S_IREAD | _S_IWRITE);
150
return ::_wopen(IceUtil::stringToWstring(path).c_str(), flags);
155
IceUtilInternal::getcwd(string& cwd)
157
wchar_t cwdbuf[_MAX_PATH];
158
if(_wgetcwd(cwdbuf, _MAX_PATH) == NULL)
162
cwd = IceUtil::wstringToString(cwdbuf);
167
IceUtilInternal::unlink(const string& path)
169
return _wunlink(IceUtil::stringToWstring(path).c_str());
173
IceUtilInternal::close(int fd)
175
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
182
IceUtilInternal::FileLock::FileLock(const std::string& path) :
183
_fd(INVALID_HANDLE_VALUE),
186
_fd = ::CreateFileW(IceUtil::stringToWstring(path).c_str(), GENERIC_WRITE, 0, NULL,
187
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
190
if(_fd == INVALID_HANDLE_VALUE)
192
throw IceUtil::FileLockException(__FILE__, __LINE__, GetLastError(), _path);
195
if(::LockFile(_fd, 0, 0, 0, 0) == 0)
198
throw IceUtil::FileLockException(__FILE__, __LINE__, GetLastError(), _path);
201
// In Windows implementation we don't write the process pid to the file, as is
202
// not posible to read the file from other process while it is locked here.
206
IceUtilInternal::FileLock::~FileLock()
208
assert(_fd != INVALID_HANDLE_VALUE);
213
#ifdef _STLP_BEGIN_NAMESPACE
217
toFileFlags(ios_base::openmode mode)
220
if(mode & ios_base::app)
224
if(mode & ios_base::trunc)
228
if(mode & ios_base::binary)
232
if((mode & ios_base::in) && !(mode & ios_base::out))
236
else if((mode & ios_base::out) && !(mode & ios_base::in))
238
flags |= _O_WRONLY | _O_CREAT;
243
if(mode & ios_base::trunc)
253
IceUtilInternal::ifstream::ifstream()
257
#ifdef _STLP_BEGIN_NAMESPACE
259
IceUtilInternal::ifstream::ifstream(const string& path, ios_base::openmode mode) : _fd(-1)
264
IceUtilInternal::ifstream::~ifstream()
270
IceUtilInternal::ifstream::close()
272
if(!rdbuf()->close())
274
setstate(ios_base::failbit);
283
IceUtilInternal::ifstream::open(const string& path, ios_base::openmode mode)
285
mode |= ifstream::in;
286
_fd = IceUtilInternal::open(path, toFileFlags(mode));
287
if(_fd < 0 || !rdbuf()->open(_fd, mode))
289
setstate(ios_base::failbit);
291
if(mode & (ios_base::ate || ios_base::app))
293
seekg(ios_base::end);
299
IceUtilInternal::ifstream::ifstream(const string& path, ios_base::openmode mode) : std::ifstream(IceUtil::stringToWstring(path).c_str(), mode)
304
IceUtilInternal::ifstream::open(const string& path, ios_base::openmode mode)
306
std::ifstream::open(IceUtil::stringToWstring(path).c_str(), mode);
311
IceUtilInternal::ofstream::ofstream()
315
#ifdef _STLP_BEGIN_NAMESPACE
317
IceUtilInternal::ofstream::ofstream(const string& path, ios_base::openmode mode) : _fd(-1)
322
IceUtilInternal::ofstream::~ofstream()
328
IceUtilInternal::ofstream::close()
330
if(!rdbuf()->close())
332
setstate(ios_base::failbit);
341
IceUtilInternal::ofstream::open(const string& path, ios_base::openmode mode)
343
mode |= ofstream::out;
344
_fd = IceUtilInternal::open(path, toFileFlags(mode));
345
if(_fd < 0 || !rdbuf()->open(_fd, mode))
347
setstate(ios_base::failbit);
349
if(mode & (ios_base::ate || ios_base::app))
351
seekp(ios_base::end);
357
IceUtilInternal::ofstream::ofstream(const string& path, ios_base::openmode mode) : std::ofstream(IceUtil::stringToWstring(path).c_str(), mode)
362
IceUtilInternal::ofstream::open(const string& path, ios_base::openmode mode)
364
std::ofstream::open(IceUtil::stringToWstring(path).c_str(), mode);
375
IceUtilInternal::stat(const string& path, structstat* buffer)
377
return ::stat(path.c_str(), buffer);
381
IceUtilInternal::remove(const string& path)
383
return ::remove(path.c_str());
387
IceUtilInternal::rename(const string& from, const string& to)
389
return ::rename(from.c_str(), to.c_str());
393
IceUtilInternal::rmdir(const string& path)
395
return ::rmdir(path.c_str());
399
IceUtilInternal::mkdir(const string& path, int perm)
401
return ::mkdir(path.c_str(), perm);
405
IceUtilInternal::fopen(const string& path, const string& mode)
407
return ::fopen(path.c_str(), mode.c_str());
411
IceUtilInternal::open(const string& path, int flags)
415
// By default, create with rw-rw-rw- modified by the user's umask (same as fopen).
416
return ::open(path.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
420
return ::open(path.c_str(), flags);
425
IceUtilInternal::getcwd(string& cwd)
427
char cwdbuf[PATH_MAX];
428
if(::getcwd(cwdbuf, PATH_MAX) == NULL)
437
IceUtilInternal::unlink(const string& path)
439
return ::unlink(path.c_str());
443
IceUtilInternal::close(int fd)
448
IceUtilInternal::FileLock::FileLock(const std::string& path) :
452
_fd = ::open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
455
throw IceUtil::FileLockException(__FILE__, __LINE__, errno, _path);
459
lock.l_type = F_WRLCK; // Write lock
460
lock.l_whence = SEEK_SET; // Begining of file
465
// F_SETLK tells fcntl to not block if it cannot
466
// acquire the lock, if the lock cannot be acquired
467
// it returns -1 without wait.
469
if(::fcntl(_fd, F_SETLK, &lock) == -1)
471
IceUtil::FileLockException ex(__FILE__, __LINE__, errno, _path);
477
// If there is an error after here, we close the fd,
478
// to release the lock.
482
// Now that we have acquire an excluxive write lock,
483
// write the process pid there.
488
if(write(_fd, os.str().c_str(), os.str().size()) == -1)
490
IceUtil::FileLockException ex(__FILE__, __LINE__, errno, _path);
496
IceUtilInternal::FileLock::~FileLock()
502
IceUtilInternal::ifstream::ifstream()
506
IceUtilInternal::ifstream::ifstream(const string& path, ios_base::openmode mode) : std::ifstream(path.c_str(), mode)
511
IceUtilInternal::ifstream::open(const string& path, ios_base::openmode mode)
513
std::ifstream::open(path.c_str(), mode);
516
IceUtilInternal::ofstream::ofstream()
520
IceUtilInternal::ofstream::ofstream(const string& path, ios_base::openmode mode) : std::ofstream(path.c_str(), mode)
525
IceUtilInternal::ofstream::open(const string& path, ios_base::openmode mode)
527
std::ofstream::open(path.c_str(), mode);