// ********************************************************************** // // Copyright (c) 2004 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the // ICE_LICENSE file included in this distribution. // // ********************************************************************** #include #include #include #include #ifdef _WIN32 # include #else # include #endif using namespace std; using namespace Ice; using namespace IcePatch2; IcePatch2::FileServerI::FileServerI(const std::string& dataDir, const FileInfoSeq& infoSeq) : _dataDir(normalize(dataDir)), _dataDirWithSlash(_dataDir + "/") { FileTree0& tree0 = const_cast(_tree0); getFileTree0(infoSeq, tree0); } FileInfoSeq IcePatch2::FileServerI::getFileInfoSeq(Int node, const Current&) const { if(node < 0 || node > 255) { throw NodeOutOfRangeException(); } return _tree0.nodes[node].files; } ByteSeqSeq IcePatch2::FileServerI::getChecksumSeq(const Current&) const { ByteSeqSeq checksums(256); for(int node = 0; node < 256; ++node) { checksums[node] = _tree0.nodes[node].checksum; } return checksums; } ByteSeq IcePatch2::FileServerI::getChecksum(const Current&) const { return _tree0.checksum; } ByteSeq IcePatch2::FileServerI::getFileCompressed(const string& pa, Int pos, Int num, const Current&) const { string path = normalize(pa) + ".bz2"; if(path.compare(0, _dataDirWithSlash.size(), _dataDirWithSlash) != 0) { FileAccessException ex; ex.reason = "`" + pa + "' is not a path in `" + _dataDir + "'"; throw ex; } if(num <= 0 || pos < 0) { return ByteSeq(); } if(num > 256 * 1024) { num = 256 * 1024; } #ifdef _WIN32 int fd = open(path.c_str(), _O_RDONLY | _O_BINARY); #else int fd = open(path.c_str(), O_RDONLY); #endif if(fd == -1) { FileAccessException ex; ex.reason = "cannot open `" + path + "' for reading: " + strerror(errno); throw ex; } if(lseek(fd, static_cast(pos), SEEK_SET) != static_cast(pos)) { close(fd); ostringstream posStr; posStr << pos; FileAccessException ex; ex.reason = "cannot seek position " + posStr.str() + " in file `" + path + "': " + strerror(errno); throw ex; } ByteSeq bytes(num); #ifdef _WIN32 long r; #else ssize_t r; #endif if((r = read(fd, &bytes[0], static_cast(num))) == -1) { close(fd); FileAccessException ex; ex.reason = "cannot read `" + path + "': " + strerror(errno); throw ex; } close(fd); bytes.resize(r); return bytes; }