📄 wy_lnkfile.cpp
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2003*/#define WYLIB_SOURCE#include "wy_lnkfile.h"#include "wy_atdestroy.h"#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <cstdlib>#include <new>#include <typeinfo>const char Wy_LnkFile::class_name[]="Wy_LnkFile";Wy_LnkFile::Wy_LnkFile() try : _pname() {}catch(const WyStr::Reply& e) { WY_THROW( Wy_LnkFile::Reply(e) );}catch(const WyRet& e) { WY_NDEBUG_MSG(e); WY_THROW( WyRet(e) );};Wy_LnkFile::Wy_LnkFile(const Wy_LnkFile &src)try : _pname(src._pname) {}catch(const WyStr::Reply& e) { WY_THROW( Reply(e) );}catch(const WyRet& e) { WY_NDEBUG_MSG(e); WY_THROW( WyRet(e) );};Wy_LnkFile::Wy_LnkFile(const char* pathname)try { struct stat stt; if(::lstat(pathname,&stt)!=0) { WY_THROW( Reply(WyReply(errno)) ); } if(S_ISLNK(stt.st_mode)==false) { WY_THROW( Reply(Wym_ENODEV) ); } _pname=pathname;}catch(const Reply&) { throw;}catch(const WyStr::Reply& e) { WY_THROW( Reply(e) );}catch(const WyRet& e) { WY_NDEBUG_MSG(e); WY_THROW( WyRet(e) );};Wy_LnkFile::Wy_LnkFile(const WyStr& pathname)try { struct stat stt; if(::lstat(pathname.c_str(),&stt)!=0) { WY_THROW( Reply(WyReply(errno)) ); } if(S_ISLNK(stt.st_mode)==false) { WY_THROW( Reply(Wym_ENODEV) ); } _pname=pathname;}catch(const Reply&) { throw;}catch(const WyStr::Reply& e) { WY_THROW( Reply(e) );}catch(const WyRet& e) { WY_NDEBUG_MSG(e); WY_THROW( WyRet(e) );};Wy_LnkFile::Wy_LnkFile(Wy_LnkFile &src, Wy::ByMove_t) WY__TSPC() : _pname(src._pname,Wy::ByMove){};Wy_LnkFile::~Wy_LnkFile(){ Wy__Base::NoCancel noc; // explicit nocancel _pname.reset();};bool Wy_LnkFile::is_default(void) const WY__TSPC(){ return _pname.is_default(); };WyRet Wy_LnkFile::stat(WyFileStat& filestat) const{ if(_pname.is_default()) { WY_RETURN(Wym_EBADF); } // // Assert that ::lstat failure should not modify the argument // if(::lstat(_pname.c_str(),filestat.wy_stat_ptr())==0) { return(Ok); } WY_RETURN(errno);};WyRet Wy_LnkFile::reset(void){ _pname.reset(); return(Ok);};WyRet Wy_LnkFile::reset(const Wy_LnkFile& lnkf){ const WyRet r( _pname.reset(lnkf._pname) ); if(r!=Ok) { if(r==Wym_ELOOP) { return(Ok); } WY_RETURN(r); } return(Ok);};WyRet Wy_LnkFile::reset(const char* pathname)try { if(_pname==pathname) { return(Ok); } struct stat stt; if(::lstat(pathname,&stt)!=0) { WY_RETURN(errno); } if(S_ISLNK(stt.st_mode)==false) { WY_RETURN(Wym_ENODEV); } _pname=pathname; return(Ok);}catch(const WyStr::Reply& e) { WY_RETURN(e);};WyRet Wy_LnkFile::reset(const WyStr& pathname){ const WyRet r( this->reset(pathname.c_str()) ); if(r!=Ok) { WY_RETURN(r); } return(Ok);};WyRet Wy_LnkFile::_swap(Wy_LnkFile& lnkf) WY__TSPC(){ if(typeid(*this)!=typeid(lnkf)) { WY_RETURN(Wym_EBADTYPE); } _pname.swap(lnkf._pname); return(Ok);};Wy_LnkFile* Wy_LnkFile::_alloc(WyRet& r) consttry { if(WY__EQU_TYPEID(*this,Wy_LnkFile)==false) { r=Wym_ENOSYS; WY_HERE(r); // _alloc not overridden return(NULL); } Wy_LnkFile* p=new(std::nothrow) Wy_LnkFile(); if(p==NULL) { r=Wym_ENOMEM; WY_HERE(r); } else { r=Ok; } return(p);}catch(const Reply& e) { r=e; WY_HERE(r); return(0);}catch(const WyRet& e) { WY_NDEBUG_MSG(e); WY_THROW( WyRet(e) );};WyRet Wy_LnkFile::read(WyStr& value){ if(_pname.is_default()) { WY_RETURN(Wym_EBADF); } const size_t PBufSize=Wy::_MaxPathSize+1; char *pbuf= (char*)std::malloc(PBufSize); if(pbuf==0) { WY_RETURN(Wym_ENOMEM); } Wy_AtDestroy<void,char*> rrid((void(*)(char*))std::free,pbuf); const int v=::readlink(_pname.c_str(),pbuf,PBufSize); if(v<0) { WY_RETURN(errno); } if(size_t(v)>=PBufSize) { WY_RETURN(Wym_EIO); } const WyRet r=value.reset( WyCSeg(pbuf,v) ); if(r!=Ok) { WY_RETURN(r); } return(Ok);};WyRet Wy_LnkFile::create(const char* oldpath,const char* newpath){ if(::symlink(oldpath,newpath)!=0) { WY_RETURN(errno); } return(Ok);};WyRet Wy_LnkFile::create(const WyStr& oldpath,const WyStr& newpath){ if(::symlink(oldpath.c_str(),newpath.c_str())!=0) { WY_RETURN(errno); } return(Ok);};WyRet Wy_LnkFile::create(const char* oldpath,const WyStr& newpath){ if(::symlink(oldpath,newpath.c_str())!=0) { WY_RETURN(errno); } return(Ok);};WyRet Wy_LnkFile::create(const WyStr& oldpath,const char* newpath){ if(::symlink(oldpath.c_str(),newpath)!=0) { WY_RETURN(errno); } return(Ok);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -