8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 20:43:03 +01:00
firebird-mirror/src/jrd/os/posix/path_utils.cpp

183 lines
3.9 KiB
C++
Raw Normal View History

/*
* PROGRAM: JRD FileSystem Path Handler
* MODULE: path_utils.cpp
* DESCRIPTION: POSIX_specific class for file path management
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by John Bellardo
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2002 John Bellardo <bellardo at cs.ucsd.edu>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#include "../jrd/os/path_utils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
/// The POSIX implementation of the path_utils abstraction.
const char PathUtils::dir_sep = '/';
const char* PathUtils::up_dir_link = "..";
class PosixDirItr : public PathUtils::dir_iterator
{
public:
2004-03-16 18:35:03 +01:00
PosixDirItr(MemoryPool& p, const Firebird::PathName& path)
2004-12-09 03:53:19 +01:00
: dir_iterator(p, path), dir(0), file(p), done(false)
2004-03-16 18:35:03 +01:00
{
init();
}
PosixDirItr(const Firebird::PathName& path)
2004-12-09 03:53:19 +01:00
: dir_iterator(path), dir(0), done(false)
2004-03-16 18:35:03 +01:00
{
init();
}
~PosixDirItr();
const PosixDirItr& operator++();
const Firebird::PathName& operator*() { return file; }
operator bool() { return !done; }
private:
DIR *dir;
Firebird::PathName file;
2004-12-09 03:53:19 +01:00
bool done;
2004-03-16 18:35:03 +01:00
void init();
};
2004-03-16 18:35:03 +01:00
void PosixDirItr::init()
{
dir = opendir(dirPrefix.c_str());
if (!dir)
2004-12-09 03:53:19 +01:00
done = true;
else
++(*this);
}
PosixDirItr::~PosixDirItr()
{
if (dir)
closedir(dir);
dir = 0;
2004-12-09 03:53:19 +01:00
done = true;
}
const PosixDirItr& PosixDirItr::operator++()
{
if (done)
return *this;
struct dirent *ent = readdir(dir);
if (ent == NULL)
{
2004-12-09 03:53:19 +01:00
done = true;
}
else
{
PathUtils::concatPath(file, dirPrefix, ent->d_name);
}
return *this;
}
PathUtils::dir_iterator *PathUtils::newDirItr(MemoryPool& p, const Firebird::PathName& path)
{
return FB_NEW(p) PosixDirItr(p, path);
}
void PathUtils::splitLastComponent(Firebird::PathName& path, Firebird::PathName& file,
const Firebird::PathName& orgPath)
{
2004-12-06 11:17:00 +01:00
Firebird::PathName::size_type pos = orgPath.rfind(dir_sep);
if (pos == Firebird::PathName::npos)
{
path = "";
file = orgPath;
return;
}
path.erase();
path.append(orgPath, 0, pos); // skip the directory separator
file.erase();
2004-11-30 07:18:39 +01:00
file.append(orgPath, pos + 1, orgPath.length() - pos - 1);
}
void PathUtils::concatPath(Firebird::PathName& result,
const Firebird::PathName& first,
const Firebird::PathName& second)
{
if (second.length() == 0)
{
result = first;
return;
}
if (first.length() == 0)
{
result = second;
return;
}
2004-12-06 11:17:00 +01:00
if (first[first.length() - 1] != dir_sep &&
second[0] != dir_sep)
{
result = first + dir_sep + second;
return;
}
2004-12-06 11:17:00 +01:00
if (first[first.length() - 1] == dir_sep &&
second[0] == dir_sep)
{
result = first;
result.append(second, 1, second.length() - 1);
return;
}
result = first + second;
}
// We don't work correctly with MBCS.
void PathUtils::ensureSeparator(Firebird::PathName& in_out)
{
if (in_out.length() == 0)
in_out = PathUtils::dir_sep;
if (in_out[in_out.length() - 1] != PathUtils::dir_sep)
in_out += PathUtils::dir_sep;
}
bool PathUtils::isRelative(const Firebird::PathName& path)
{
if (path.length() > 0)
return path[0] != dir_sep;
return false;
}
bool PathUtils::isSymLink(const Firebird::PathName& path)
{
struct stat st, lst;
if (stat(path.c_str(), &st) != 0)
return false;
if (lstat(path.c_str(), &lst) != 0)
return false;
return st.st_ino != lst.st_ino;
}
2004-12-06 11:17:00 +01:00
bool PathUtils::canAccess(const Firebird::PathName& path, int mode)
{
return access(path.c_str(), mode) == 0;
}
2004-12-06 11:17:00 +01:00