| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- //
- // File_VMS.cpp
- //
- // $Id: //poco/1.4/Foundation/src/File_VMS.cpp#1 $
- //
- // Library: Foundation
- // Package: Filesystem
- // Module: File
- //
- // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // SPDX-License-Identifier: BSL-1.0
- //
- #include "Poco/File_VMS.h"
- #include "Poco/Exception.h"
- #include "Poco/Path.h"
- #include "Poco/String.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>
- namespace Poco {
- FileImpl::FileImpl()
- {
- }
- FileImpl::FileImpl(const std::string& path): _path(path)
- {
- if (!_path.empty())
- {
- Path p(_path);
- p.makeFile();
- _path = p.toString();
- }
- }
- FileImpl::~FileImpl()
- {
- }
- void FileImpl::swapImpl(FileImpl& file)
- {
- std::swap(_path, file._path);
- }
- void FileImpl::setPathImpl(const std::string& path)
- {
- _path = path;
- }
- bool FileImpl::existsImpl() const
- {
- poco_assert (!_path.empty());
- return access(_path.c_str(), F_OK) == 0;
- }
- bool FileImpl::canReadImpl() 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 handleLastErrorImpl(_path);
- return false;
- }
- bool FileImpl::canWriteImpl() 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 handleLastErrorImpl(_path);
- return false;
- }
- bool FileImpl::canExecuteImpl() const
- {
- Path p(_path);
- return icompare(p.getExtension(), "exe") == 0;
- }
- bool FileImpl::isFileImpl() const
- {
- poco_assert (!_path.empty());
- struct stat st;
- if (stat(_path.c_str(), &st) == 0)
- return S_ISREG(st.st_mode);
- else
- handleLastErrorImpl(_path);
- return false;
- }
- bool FileImpl::isDirectoryImpl() const
- {
- poco_assert (!_path.empty());
- struct stat st;
- if (stat(_path.c_str(), &st) == 0)
- return S_ISDIR(st.st_mode);
- else
- handleLastErrorImpl(_path);
- return false;
- }
- bool FileImpl::isLinkImpl() const
- {
- return false;
- }
- bool FileImpl::isLinkImpl() const
- {
- return false;
- }
- bool FileImpl::isHiddenImpl() const
- {
- return false;
- }
- Timestamp FileImpl::createdImpl() const
- {
- poco_assert (!_path.empty());
- struct stat st;
- if (stat(_path.c_str(), &st) == 0)
- return Timestamp(st.st_ctime);
- else
- handleLastErrorImpl(_path);
- return 0;
- }
- Timestamp FileImpl::getLastModifiedImpl() const
- {
- poco_assert (!_path.empty());
- struct stat st;
- if (stat(_path.c_str(), &st) == 0)
- return Timestamp(st.st_mtime);
- else
- handleLastErrorImpl(_path);
- return 0;
- }
- void FileImpl::setLastModifiedImpl(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)
- handleLastErrorImpl(_path);
- }
- FileImpl::FileSizeImpl FileImpl::getSizeImpl() const
- {
- poco_assert (!_path.empty());
- struct stat st;
- if (stat(_path.c_str(), &st) == 0)
- return st.st_size;
- else
- handleLastErrorImpl(_path);
- return 0;
- }
- void FileImpl::setSizeImpl(FileSizeImpl size)
- {
- poco_assert (!_path.empty());
- if (truncate(_path.c_str(), size) != 0)
- handleLastErrorImpl(_path);
- }
- void FileImpl::setWriteableImpl(bool flag)
- {
- poco_assert (!_path.empty());
- struct stat st;
- if (stat(_path.c_str(), &st) != 0)
- handleLastErrorImpl(_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)
- handleLastErrorImpl(_path);
- }
- void FileImpl::setExecutableImpl(bool flag)
- {
- // not supported
- }
- void FileImpl::copyToImpl(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 FileImpl::renameToImpl(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 FileImpl::removeImpl()
- {
- poco_assert (!_path.empty());
- int rc;
- if (isDirectoryImpl())
- {
- setWriteableImpl(true);
- rc = rmdir(_path.c_str());
- }
- else
- {
- rc = unlink(_path.c_str());
- }
- if (rc) handleLastErrorImpl(_path);
- }
- bool FileImpl::createFileImpl()
- {
- 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
- handleLastErrorImpl(_path);
- return false;
- }
- bool FileImpl::createDirectoryImpl()
- {
- poco_assert (!_path.empty());
- if (existsImpl() && isDirectoryImpl())
- return false;
- Path p(_path);
- p.makeDirectory();
- if (mkdir(p.toString().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
- handleLastErrorImpl(_path);
- return true;
- }
- void FileImpl::handleLastErrorImpl(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);
- }
- }
- } // namespace Poco
|