📄 file_unix.cpp
字号:
//
// File_UNIX.cpp
//
// $Id: //poco/Main/Foundation/src/File_UNIX.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/File_UNIX.h"
#include "Foundation/Exception.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <utime.h>
#include <string.h>
Foundation_BEGIN
FileImp::FileImp()
{
}
FileImp::FileImp(const std::string& path): _path(path)
{
std::string::size_type n = _path.size();
if (n > 0 && _path[n - 1] == '/')
_path.resize(n - 1);
}
FileImp::~FileImp()
{
}
void FileImp::setPathImp(const std::string& path)
{
_path = path;
}
bool FileImp::existsImp() const
{
poco_assert (!_path.empty());
struct stat st;
return stat(_path.c_str(), &st) == 0;
}
bool FileImp::canReadImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
{
if (st.st_uid == geteuid())
return (st.st_mode & S_IRUSR) != 0;
else if (st.st_gid == getegid())
return (st.st_mode & S_IRGRP) != 0;
else
return (st.st_mode & S_IROTH) != 0;
}
else handleError(_path);
return false;
}
bool FileImp::canWriteImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
{
if (st.st_uid == geteuid())
return (st.st_mode & S_IWUSR) != 0;
else if (st.st_gid == getegid())
return (st.st_mode & S_IWGRP) != 0;
else
return (st.st_mode & S_IWOTH) != 0;
}
else handleError(_path);
return false;
}
bool FileImp::isFileImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
return S_ISREG(st.st_mode);
else
handleError(_path);
return false;
}
bool FileImp::isDirectoryImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
return S_ISDIR(st.st_mode);
else
handleError(_path);
return false;
}
Timestamp FileImp::createdImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
return Timestamp::fromEpochTime(st.st_mtime);
else
handleError(_path);
return 0;
}
Timestamp FileImp::getLastModifiedImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
return Timestamp::fromEpochTime(st.st_mtime);
else
handleError(_path);
return 0;
}
void FileImp::setLastModifiedImp(const Timestamp& ts)
{
poco_assert (!_path.empty());
struct utimbuf tb;
tb.actime = ts.epochTime();
tb.modtime = ts.epochTime();
if (utime(_path.c_str(), &tb) != 0)
handleError(_path);
}
FileImp::FileSizeImp FileImp::getSizeImp() const
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) == 0)
return st.st_size;
else
handleError(_path);
return 0;
}
void FileImp::setSizeImp(FileSizeImp size)
{
poco_assert (!_path.empty());
if (truncate(_path.c_str(), size) != 0)
handleError(_path);
}
void FileImp::setWriteableImp(bool flag)
{
poco_assert (!_path.empty());
struct stat st;
if (stat(_path.c_str(), &st) != 0)
handleError(_path);
mode_t mode;
if (flag)
{
mode = st.st_mode | S_IWUSR;
}
else
{
mode_t wmask = S_IWUSR | S_IWGRP | S_IWOTH;
mode = st.st_mode & ~wmask;
}
if (chmod(_path.c_str(), mode) != 0)
handleError(_path);
}
void FileImp::copyToImp(const std::string& path) const
{
poco_assert (!_path.empty());
int sd = open(_path.c_str(), O_RDONLY);
if (sd == -1) handleError(_path);
struct stat st;
if (fstat(sd, &st) != 0)
{
close(sd);
handleError(_path);
}
const long blockSize = st.st_blksize;
int dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode & S_IRWXU);
if (dd == -1)
{
close(sd);
handleError(path);
}
char* buffer = new char[blockSize];
try
{
int n;
while ((n = read(sd, buffer, blockSize)) > 0)
{
if (write(dd, buffer, n) != n)
handleError(path);
}
if (n < 0)
handleError(_path);
}
catch (...)
{
delete [] buffer;
close(sd);
close(dd);
throw;
}
delete [] buffer;
close(sd);
if (fsync(dd) != 0)
{
close(dd);
handleError(path);
}
if (close(dd) != 0)
handleError(path);
}
void FileImp::renameToImp(const std::string& path)
{
poco_assert (!_path.empty());
if (rename(_path.c_str(), path.c_str()) != 0)
handleError(_path);
}
void FileImp::removeImp()
{
poco_assert (!_path.empty());
int rc;
if (isDirectoryImp())
rc = rmdir(_path.c_str());
else
rc = unlink(_path.c_str());
if (rc) handleError(_path);
}
bool FileImp::createFileImp()
{
poco_assert (!_path.empty());
int n = open(_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (n != -1)
{
close(n);
return true;
}
if (n == -1 && errno == EEXIST)
return false;
else
handleError(_path);
return false;
}
bool FileImp::createDirectoryImp()
{
poco_assert (!_path.empty());
if (existsImp() && isDirectoryImp())
return false;
if (mkdir(_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
handleError(_path);
return true;
}
void FileImp::handleError(const std::string& path)
{
switch (errno)
{
case EIO:
throw IOException(path);
case EPERM:
throw FileAccessDeniedException("insufficient permissions", path);
case EACCES:
throw FileAccessDeniedException(path);
case ENOENT:
throw FileNotFoundException(path);
case ENOTDIR:
throw OpenFileException("not a directory", path);
case EISDIR:
throw OpenFileException("not a file", path);
case EROFS:
throw FileReadOnlyException(path);
case EEXIST:
throw FileExistsException(path);
case ENOSPC:
throw FileException("no space left on device", path);
case EDQUOT:
throw FileException("disk quota exceeded", path);
case ENOTEMPTY:
throw FileException("directory not empty", path);
case ENAMETOOLONG:
throw PathSyntaxException(path);
default:
throw FileException(strerror(errno), path);
}
}
Foundation_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -