# if defined(__linux__)
# include <sys/statfs.h>
# else
+# include <sys/param.h>
# include <sys/mount.h>
# endif
//
static PathCompList_t&
-s_PathMakeCanonical(PathCompList_t& CList, char separator, bool is_absolute)
+s_PathMakeCanonical(PathCompList_t& CList, bool is_absolute)
{
PathCompList_t::iterator ci, ri; // component and removal iterators
{
PathCompList_t CList;
bool is_absolute = PathIsAbsolute(Path, separator);
- s_PathMakeCanonical(PathToComponents(Path, CList, separator), separator, is_absolute);
+ s_PathMakeCanonical(PathToComponents(Path, CList, separator), is_absolute);
if ( is_absolute )
return ComponentsToAbsolutePath(CList, separator);
}
PathCompList_t CList;
- CList.push_back(cwd_buf);
+ PathToComponents(cwd_buf, CList);
CList.push_back(Path);
- return ComponentsToAbsolutePath(s_PathMakeCanonical(CList, separator, true), separator);
+ return ComponentsToAbsolutePath(s_PathMakeCanonical(CList, true), separator);
}
//
return Basename + "." + Extension;
}
+//
+std::string
+Kumu::PathJoin(const std::string& Path1, const std::string& Path2, char separator)
+{
+ return Path1 + separator + Path2;
+}
+
+//
+std::string
+Kumu::PathJoin(const std::string& Path1, const std::string& Path2, const std::string& Path3, char separator)
+{
+ return Path1 + separator + Path2 + separator + Path3;
+}
+
+//
+std::string
+Kumu::PathJoin(const std::string& Path1, const std::string& Path2,
+ const std::string& Path3, const std::string& Path4, char separator)
+{
+ return Path1 + separator + Path2 + separator + Path3 + separator + Path4;
+}
+
//
Kumu::PathList_t&
Kumu::FindInPaths(const IPathMatch& Pattern, const Kumu::PathList_t& SearchPaths,
//
#ifdef KM_WIN32
+//
+Kumu::DirScanner::DirScanner(void) : m_Handle(-1) {}
+
//
//
Result_t
// POSIX directory scanner
+//
+Kumu::DirScanner::DirScanner(void) : m_Handle(NULL) {}
+
//
Result_t
Kumu::DirScanner::Open(const char* filename)
if ( ( m_Handle = opendir(filename) ) == NULL )
{
- if ( errno == ENOENT )
- result = RESULT_ENDOFFILE;
-
- else
- result = RESULT_FAIL;
+ switch ( errno )
+ {
+ case ENOENT:
+ case ENOTDIR:
+ result = RESULT_NOTAFILE;
+ case EACCES:
+ result = RESULT_NO_PERM;
+ case ELOOP:
+ case ENAMETOOLONG:
+ result = RESULT_PARAM;
+ case EMFILE:
+ case ENFILE:
+ result = RESULT_STATE;
+ default:
+ DefaultLogSink().Error("DirScanner::Open(%s): %s\n", filename, strerror(errno));
+ result = RESULT_FAIL;
+ }
}
return result;
if ( m_Handle == NULL )
return RESULT_FILEOPEN;
- if ( closedir(m_Handle) == -1 )
- return RESULT_FAIL;
+ if ( closedir(m_Handle) == -1 ) {
+ switch ( errno )
+ {
+ case EBADF:
+ case EINTR:
+ return RESULT_STATE;
+ default:
+ DefaultLogSink().Error("DirScanner::Close(): %s\n", strerror(errno));
+ return RESULT_FAIL;
+ }
+ }
m_Handle = NULL;
return RESULT_OK;
//------------------------------------------------------------------------------------------
-// note: when moving to KM_fileio, don't forget to write the Win32 versions
-// note: add error messages and remove RESULT_FAIL form DirScanner
-
-#ifdef KM_WIN32
-#else // KM_WIN32
+//
+// Attention Windows users: make sure to use the proper separator character
+// with these functions.
+//
// given a path string, create any missing directories so that PathIsDirectory(Path) is true.
//
Kumu::CreateDirectoriesInPath(const std::string& Path)
{
bool abs = PathIsAbsolute(Path);
- assert(abs);
PathCompList_t PathComps, TmpPathComps;
PathToComponents(Path, PathComps);
if ( ! PathIsDirectory(tmp_path) )
{
+#ifdef KM_WIN32
+ if ( mkdir(tmp_path.c_str()) != 0 )
+#else // KM_WIN32
if ( mkdir(tmp_path.c_str(), 0775) != 0 )
+#endif // KM_WIN32
{
DefaultLogSink().Error("CreateDirectoriesInPath mkdir %s: %s\n",
tmp_path.c_str(), strerror(errno));
return RESULT_OK;
}
-#endif // KM_WIN32
-
-#ifdef KM_WIN32
-#else // KM_WIN32
//
Result_t
Result_t
h__DeletePath(const std::string& pathname)
{
- fprintf(stderr, "h__DeletePath %s\n", pathname.c_str());
+ if ( pathname.empty() )
+ return RESULT_NULL_STR;
+
Result_t result = RESULT_OK;
if ( ! PathIsDirectory(pathname) )
return h__DeletePath(c_pathname);
}
-#endif // KM_WIN32
//------------------------------------------------------------------------------------------
//
-#ifdef KM_WIN32
-#else // KM_WIN32
-
-//
Result_t
Kumu::FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu::fsize_t& total_space)
{
+#ifdef KM_WIN32
+ ULARGE_INTEGER lTotalNumberOfBytes;
+ ULARGE_INTEGER lTotalNumberOfFreeBytes;
+
+ BOOL fResult = ::GetDiskFreeSpaceEx(path.c_str(), NULL, &lTotalNumberOfBytes, &lTotalNumberOfFreeBytes);
+ if (fResult) {
+ free_space = static_cast<Kumu::fsize_t>(lTotalNumberOfFreeBytes.QuadPart);
+ total_space = static_cast<Kumu::fsize_t>(lTotalNumberOfBytes.QuadPart);
+ return RESULT_OK;
+ }
+ HRESULT LastError = ::GetLastError();
+
+ DefaultLogSink().Error("FreeSpaceForPath GetDiskFreeSpaceEx %s: %lu\n", path.c_str(), ::GetLastError());
+ return RESULT_FAIL;
+#else // KM_WIN32
struct statfs s;
if ( statfs(path.c_str(), &s) == 0 )
DefaultLogSink().Error("FreeSpaceForPath statfs %s: %s\n", path.c_str(), strerror(errno));
return RESULT_FAIL;
+#endif // KM_WIN32
}
-#endif // KM_WIN32
//
// end KM_fileio.cpp