bool CNFSFile::Open(const CURL& url) { int ret = 0; Close(); // we can't open files like nfs://file.f or nfs://server/file.f // if a file matches the if below return false, it can't exist on a nfs share. if (!IsValidFile(url.GetFileName())) { CLog::Log(LOGNOTICE,"NFS: Bad URL : '%s'",url.GetFileName().c_str()); return false; } CStdString filename = ""; CSingleLock lock(gNfsConnection); if(!gNfsConnection.Connect(url, filename)) return false; m_pNfsContext = gNfsConnection.GetNfsContext(); m_exportPath = gNfsConnection.GetContextMapId(); ret = gNfsConnection.GetImpl()->nfs_open(m_pNfsContext, filename.c_str(), O_RDONLY, &m_pFileHandle); if (ret != 0) { CLog::Log(LOGINFO, "CNFSFile::Open: Unable to open file : '%s' error : '%s'", url.GetFileName().c_str(), gNfsConnection.GetImpl()->nfs_get_error(m_pNfsContext)); m_pNfsContext = NULL; m_exportPath.clear(); return false; } CLog::Log(LOGDEBUG,"CNFSFile::Open - opened %s",url.GetFileName().c_str()); m_url=url; struct __stat64 tmpBuffer; if( Stat(&tmpBuffer) ) { m_url.Reset(); Close(); return false; } m_fileSize = tmpBuffer.st_size;//cache the size of this file // We've successfully opened the file! return true; }
bool CNFSFile::OpenForWrite(const CURL& url, bool bOverWrite) { int ret = 0; // we can't open files like nfs://file.f or nfs://server/file.f // if a file matches the if below return false, it can't exist on a nfs share. if (!IsValidFile(url.GetFileName())) return false; Close(); CSingleLock lock(gNfsConnection); CStdString filename = ""; if(!gNfsConnection.Connect(url,filename)) return false; m_pNfsContext = gNfsConnection.GetNfsContext(); m_exportPath = gNfsConnection.GetContextMapId(); if (bOverWrite) { CLog::Log(LOGWARNING, "FileNFS::OpenForWrite() called with overwriting enabled! - %s", filename.c_str()); //create file with proper permissions ret = gNfsConnection.GetImpl()->nfs_creat(m_pNfsContext, filename.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, &m_pFileHandle); //if file was created the file handle isn't valid ... so close it and open later if(ret == 0) { gNfsConnection.GetImpl()->nfs_close(m_pNfsContext,m_pFileHandle); m_pFileHandle = NULL; } } ret = gNfsConnection.GetImpl()->nfs_open(m_pNfsContext, filename.c_str(), O_RDWR, &m_pFileHandle); if (ret || m_pFileHandle == NULL) { // write error to logfile CLog::Log(LOGERROR, "CNFSFile::Open: Unable to open file : '%s' error : '%s'", filename.c_str(), gNfsConnection.GetImpl()->nfs_get_error(gNfsConnection.GetNfsContext())); m_pNfsContext = NULL; m_exportPath.clear(); return false; } m_url=url; struct __stat64 tmpBuffer = {0}; //only stat if file was not created if(!bOverWrite) { if(Stat(&tmpBuffer)) { m_url.Reset(); Close(); return false; } m_fileSize = tmpBuffer.st_size;//cache filesize of this file } else//file was created - filesize is zero { m_fileSize = 0; } // We've successfully opened the file! return true; }