📄 file_vms.cpp
字号:
//
// File_VMS.cpp
//
// $Id: //poco/Main/Foundation/src/File_VMS.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_VMS.h"
#include "Foundation/Exception.h"
#include "Foundation/Path.h"
#include <stdio.h>
#include <unistd.h>
#include <unixio.h>
#include <file.h>
#include <stat.h>
#include <errno.h>
#include <utime.h>
#include <lib$routines.h>
#include <rmsdef.h>
#include <starlet.h>
#include <lnmdef.h>
#include <ssdef.h>
#include <descrip.h>
Foundation_BEGIN
FileImp::FileImp()
{
}
FileImp::FileImp(const std::string& path): _path(path)
{
if (!_path.empty())
{
Path p(_path);
p.makeFile();
_path = p.toString();
}
}
FileImp::~FileImp()
{
}
void FileImp::setPathImp(const std::string& path)
{
_path = path;
}
bool FileImp::existsImp() const
{
poco_assert (!_path.empty());
return access(_path.c_str(), F_OK) == 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(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(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());
// copying a file correctly under OpenVMS is non-trivial,
// so we just invoke the COPY program.
std::string cmd = "COPY ";
cmd.append(_path);
cmd.append(" ");
cmd.append(path);
if (system(cmd.c_str()) != 0)
throw FileException("COPY command failed", _path);
}
void FileImp::renameToImp(const std::string& path)
{
poco_assert (!_path.empty());
POCO_DESCRIPTOR_STRING(oldNameDsc, _path);
POCO_DESCRIPTOR_STRING(newNameDsc, path);
int res;
if ((res = lib$rename_file(&oldNameDsc, &newNameDsc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) != 1)
{
switch (res & 0x0FFFFFFF)
{
case RMS$_FNF:
throw FileNotFoundException(_path);
case RMS$_DEV:
case RMS$_DNF:
throw PathNotFoundException(_path);
case RMS$_SYN:
throw PathSyntaxException(path);
case RMS$_RMV:
throw FileAccessDeniedException(_path);
case RMS$_PRV:
throw FileAccessDeniedException("insufficient privileges", _path);
default:
throw FileException(path);
}
}
}
void FileImp::removeImp()
{
poco_assert (!_path.empty());
int rc;
if (isDirectoryImp())
{
setWriteableImp(true);
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);
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;
Path p(_path);
p.makeDirectory();
if (mkdir(p.toString().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 + -