2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2011-05-25 12:39:13 +02:00
|
|
|
/*
|
|
|
|
* higher-level interface on top of sysdep/filesystem.h
|
|
|
|
*/
|
|
|
|
|
2009-04-18 18:14:48 +02:00
|
|
|
#include "precompiled.h"
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/file/file_system.h"
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
|
2011-03-23 15:43:35 +01:00
|
|
|
#include "lib/sysdep/filesystem.h"
|
2009-08-04 21:57:53 +02:00
|
|
|
|
2011-05-25 12:39:13 +02:00
|
|
|
|
|
|
|
bool DirectoryExists(const OsPath& path)
|
|
|
|
{
|
|
|
|
WDIR* dir = wopendir(path);
|
|
|
|
if(dir)
|
|
|
|
{
|
|
|
|
wclosedir(dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileExists(const OsPath& pathname)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
const bool exists = wstat(pathname, &s) == 0;
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u64 FileSize(const OsPath& pathname)
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
ENSURE(wstat(pathname, &s) == 0);
|
|
|
|
return s.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Status GetFileInfo(const OsPath& pathname, FileInfo* pfileInfo)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
struct stat s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
|
|
if(wstat(pathname, &s) != 0)
|
|
|
|
WARN_RETURN(StatusFromErrno());
|
|
|
|
|
|
|
|
*pfileInfo = FileInfo(pathname.Filename(), s.st_size, s.st_mtime);
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-04 21:57:53 +02:00
|
|
|
struct DirDeleter
|
|
|
|
{
|
2010-01-05 20:44:30 +01:00
|
|
|
void operator()(WDIR* osDir) const
|
2009-08-04 21:57:53 +02:00
|
|
|
{
|
2010-01-05 20:44:30 +01:00
|
|
|
const int ret = wclosedir(osDir);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(ret == 0);
|
2009-08-04 21:57:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status GetDirectoryEntries(const OsPath& path, FileInfos* files, DirectoryNames* subdirectoryNames)
|
2009-08-04 21:57:53 +02:00
|
|
|
{
|
|
|
|
// open directory
|
|
|
|
errno = 0;
|
2011-03-23 14:36:20 +01:00
|
|
|
WDIR* pDir = wopendir(path);
|
2009-08-04 21:57:53 +02:00
|
|
|
if(!pDir)
|
2011-05-03 14:38:42 +02:00
|
|
|
return StatusFromErrno(); // NOWARN
|
2010-01-05 20:44:30 +01:00
|
|
|
shared_ptr<WDIR> osDir(pDir, DirDeleter());
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
errno = 0;
|
2010-01-05 20:44:30 +01:00
|
|
|
struct wdirent* osEnt = wreaddir(osDir.get());
|
2009-08-04 21:57:53 +02:00
|
|
|
if(!osEnt)
|
|
|
|
{
|
|
|
|
// no error, just no more entries to return
|
|
|
|
if(!errno)
|
|
|
|
return INFO::OK;
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-08-04 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2011-03-23 17:14:47 +01:00
|
|
|
for(size_t i = 0; osEnt->d_name[i] != '\0'; i++)
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(Path::Validate(osEnt->d_name[i]));
|
2011-03-23 14:36:20 +01:00
|
|
|
const OsPath name(osEnt->d_name);
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
// get file information (mode, size, mtime)
|
|
|
|
struct stat s;
|
|
|
|
#if OS_WIN
|
2010-01-05 20:44:30 +01:00
|
|
|
// .. return wdirent directly (much faster than calling stat).
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(wreaddir_stat_np(osDir.get(), &s));
|
2009-08-04 21:57:53 +02:00
|
|
|
#else
|
|
|
|
// .. call regular stat().
|
|
|
|
errno = 0;
|
2011-03-23 14:36:20 +01:00
|
|
|
const OsPath pathname = path / name;
|
|
|
|
if(wstat(pathname, &s) != 0)
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-08-04 21:57:53 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if(files && S_ISREG(s.st_mode))
|
|
|
|
files->push_back(FileInfo(name, s.st_size, s.st_mtime));
|
2011-03-23 14:36:20 +01:00
|
|
|
else if(subdirectoryNames && S_ISDIR(s.st_mode) && name != L"." && name != L"..")
|
2009-08-04 21:57:53 +02:00
|
|
|
subdirectoryNames->push_back(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status CreateDirectories(const OsPath& path, mode_t mode)
|
2009-08-04 21:57:53 +02:00
|
|
|
{
|
2011-03-21 18:53:13 +01:00
|
|
|
if(path.empty())
|
|
|
|
return INFO::OK;
|
|
|
|
|
|
|
|
struct stat s;
|
2011-03-23 14:36:20 +01:00
|
|
|
if(wstat(path, &s) == 0)
|
2009-08-04 21:57:53 +02:00
|
|
|
{
|
2011-03-21 18:53:13 +01:00
|
|
|
if(!S_ISDIR(s.st_mode)) // encountered a file
|
2009-08-04 21:57:53 +02:00
|
|
|
WARN_RETURN(ERR::FAIL);
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
2009-11-14 11:57:08 +01:00
|
|
|
// If we were passed a path ending with '/', strip the '/' now so that
|
2011-03-23 14:36:20 +01:00
|
|
|
// we can consistently use Parent to find parent directory names
|
|
|
|
if(path.IsDirectory())
|
|
|
|
return CreateDirectories(path.Parent(), mode);
|
2009-11-14 11:57:08 +01:00
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(CreateDirectories(path.Parent(), mode));
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
errno = 0;
|
2011-03-23 14:36:20 +01:00
|
|
|
if(wmkdir(path, mode) != 0)
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status DeleteDirectory(const OsPath& path)
|
2009-08-04 21:57:53 +02:00
|
|
|
{
|
|
|
|
// note: we have to recursively empty the directory before it can
|
|
|
|
// be deleted (required by Windows and POSIX rmdir()).
|
|
|
|
|
|
|
|
FileInfos files; DirectoryNames subdirectoryNames;
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(GetDirectoryEntries(path, &files, &subdirectoryNames));
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
// delete files
|
|
|
|
for(size_t i = 0; i < files.size(); i++)
|
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
const OsPath pathname = path / files[i].Name();
|
2009-08-04 21:57:53 +02:00
|
|
|
errno = 0;
|
2011-03-23 14:36:20 +01:00
|
|
|
if(wunlink(pathname) != 0)
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-08-04 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// recurse over subdirectoryNames
|
|
|
|
for(size_t i = 0; i < subdirectoryNames.size(); i++)
|
2011-05-03 14:38:42 +02:00
|
|
|
RETURN_STATUS_IF_ERR(DeleteDirectory(path / subdirectoryNames[i]));
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
errno = 0;
|
2011-03-23 14:36:20 +01:00
|
|
|
if(wrmdir(path) != 0)
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|