NCBI C++ ToolKit
vdbfile.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: vdbfile.cpp 101493 2023-12-19 18:34:18Z vasilche $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Eugene Vasilchenko
27  *
28  * File Description:
29  * Access to VDB KFile, that supports remote cached file access
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
36 
37 #include <kfs/file.h>
38 #include <vfs/manager.h>
39 #include <vfs/path.h>
40 
41 #ifndef NCBI_THROW2_FMT
42 # define NCBI_THROW2_FMT(exception_class, err_code, message, extra) \
43  throw NCBI_EXCEPTION2(exception_class, err_code, FORMAT(message), extra)
44 #endif
45 
46 
49 
50 class CSeq_entry;
51 
52 
54 DEFINE_BAM_REF_TRAITS(KFile, const);
55 
56 
57 #ifdef NCBI_OS_MSWIN
58 static inline
59 bool s_HasWindowsDriveLetter(const string& s)
60 {
61  // first symbol is letter, and second symbol is colon (':')
62  return s.length() >= 2 && isalpha(s[0]&0xff) && s[1] == ':';
63 }
64 #endif
65 
66 
67 bool CBamVDBPath::IsPlainAccession(const string& acc_or_path)
68 {
69 #ifdef NCBI_OS_MSWIN
70  return !s_HasWindowsDriveLetter(acc_or_path) &&
71  acc_or_path.find_first_of("/\\") == NPOS;
72 #else
73  return acc_or_path.find('/') == NPOS;
74 #endif
75 }
76 
77 
78 bool CBamVDBPath::HasSchemaPrefix(const string& path)
79 {
80  // Convert Windows path with drive letter
81  // C:\Users\Public -> /C/Users/Public
82  if (path[0] == 'h' &&
83  (NStr::StartsWith(path, "http://") ||
84  NStr::StartsWith(path, "https://"))) {
85  return true;
86  }
87  if (path[0] == 'f' &&
88  (NStr::StartsWith(path, "ftp://"))) {
89  return true;
90  }
91  return false;
92 }
93 
94 
95 bool CBamVDBPath::IsSysPath(const string& acc_or_path)
96 {
97  if (IsPlainAccession(acc_or_path)) {
98  return false;
99  }
100  if (HasSchemaPrefix(acc_or_path)) {
101  return false;
102  }
103  return true;
104 }
105 
106 
107 void CBamVDBPath::x_Init(const CBamVFSManager& mgr, const string& path)
108 {
109  if (IsSysPath(path)) {
110  if (rc_t rc = VFSManagerMakeSysPath(mgr, x_InitPtr(), path.c_str())) {
111  NCBI_THROW2_FMT(CBamException, eInitFailed,
112  "CBamVDBPath(" << path << "): "
113  "cannot create VPath", rc);
114  }
115  }
116  else {
117  if (rc_t rc = VFSManagerMakePath(mgr, x_InitPtr(), path.c_str())) {
118  NCBI_THROW2_FMT(CBamException, eInitFailed,
119  "CBamVDBPath(" << path << "): "
120  "cannot create VPath", rc);
121  }
122  }
123 }
124 
125 
127 {
128  String str;
129  if ( rc_t rc = VPathGetPath(*this, &str) ) {
130  NCBI_THROW2_FMT(CBamException, eInitFailed,
131  "CBamVDBPath::GetString(): "
132  "VPathGetPath() failed", rc);
133  }
134  return CTempString(str.addr, str.size);
135 }
136 
137 
138 CBamVDBFile::CBamVDBFile(const string& path)
139 {
140  CBamVFSManager mgr;
141  x_Init(mgr, CBamVDBPath(mgr, path));
142 }
143 
144 
145 void CBamVDBFile::x_Init(const CBamVFSManager& mgr, const CBamVDBPath& path)
146 {
147  if ( rc_t rc = VFSManagerOpenFileRead(mgr, x_InitPtr(), path) ) {
148  NCBI_THROW2_FMT(CBamException, eFileNotFound,
149  "CBamVDBFile("<<path.GetString()<<"): "
150  "cannot open KFile", rc);
151  }
152  m_Path = path;
153 }
154 
155 
156 size_t CBamVDBFile::GetSize() const
157 {
158  uint64_t size;
159  if ( rc_t rc = KFileSize(*this, &size) ) {
160  NCBI_THROW2_FMT(CBamException, eInitFailed,
161  "CBamVDBFile::GetSize(): "
162  "cannot get size for "<<m_Path.GetString(), rc);
163  }
164  if ( size_t(size) != size ) {
165  NCBI_THROW_FMT(CBamException, eInitFailed,
166  "CBamVDBFile::GetSize(): "
167  "size of "<<m_Path.GetString()<<" is too big: "<<size);
168  }
169  return size_t(size);
170 }
171 
172 
173 size_t CBamVDBFile::Read(size_t pos, char* buffer, size_t buffer_size)
174 {
175  size_t nread;
176  if ( rc_t rc = KFileRead(*this, pos, buffer, buffer_size, &nread) ) {
177  NCBI_THROW2_FMT(CBamException, eInitFailed,
178  "CBamVDBFile::Read(): "
179  "read failed from "<<m_Path.GetString(), rc);
180  }
181  return nread;
182 }
183 
184 
185 size_t CBamVDBFile::ReadAll(size_t pos, char* buffer, size_t buffer_size)
186 {
187  // we cannot use KFileReadAll() as it doesn't actually "read all"
188  size_t nread_total = 0;
189  while ( buffer_size ) {
190  size_t nread;
191  if ( rc_t rc = KFileRead(*this, pos, buffer, buffer_size, &nread) ) {
192  NCBI_THROW2_FMT(CBamException, eInitFailed,
193  "CBamVDBFile::ReadAll(): "
194  "read failed from "<<m_Path.GetString(), rc);
195  }
196  if ( nread == 0 ) {
197  break;
198  }
199  nread_total += nread;
200  pos += nread;
201  buffer += nread;
202  buffer_size -= nread;
203  }
204  return nread_total;
205 }
206 
207 
208 void CBamVDBFile::ReadExactly(size_t pos, char* buffer, size_t buffer_size)
209 {
210  if ( rc_t rc = KFileReadExactly(*this, pos, buffer, buffer_size) ) {
211  NCBI_THROW2_FMT(CBamException, eInitFailed,
212  "CBamVDBFile::ReadExactly(): "
213  "read failed from "<<m_Path.GetString(), rc);
214  }
215 }
216 
217 
uint32_t rc_t
TObject ** x_InitPtr(void)
void x_Init(const CBamVFSManager &mgr, const CBamVDBPath &path)
Definition: vdbfile.cpp:145
size_t GetSize() const
Definition: vdbfile.cpp:156
void ReadExactly(size_t file_pos, char *buffer, size_t buffer_size)
Definition: vdbfile.cpp:208
size_t ReadAll(size_t file_pos, char *buffer, size_t buffer_size)
Definition: vdbfile.cpp:185
size_t Read(size_t file_pos, char *buffer, size_t buffer_size)
Definition: vdbfile.cpp:173
CBamVDBPath m_Path
Definition: vdbfile.hpp:103
static bool IsPlainAccession(const string &acc_or_path)
Definition: vdbfile.cpp:67
void x_Init(const CBamVFSManager &mgr, const string &path)
Definition: vdbfile.cpp:107
static bool IsSysPath(const string &path)
Definition: vdbfile.cpp:95
static bool HasSchemaPrefix(const string &path)
Definition: vdbfile.cpp:78
CTempString GetString() const
Definition: vdbfile.cpp:126
Definition: Seq_entry.hpp:56
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
static const char * str(char *buf, int n)
Definition: stats.c:84
Uint8 uint64_t
#define NCBI_THROW_FMT(exception_class, err_code, message)
The same as NCBI_THROW but with message processed as output to ostream.
Definition: ncbiexpt.hpp:719
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NPOS
Definition: ncbistr.hpp:133
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
Definition: ncbistr.hpp:5412
const struct ncbi::grid::netcache::search::fields::SIZE size
int isalpha(Uchar c)
Definition: ncbictype.hpp:61
static pcre_uint8 * buffer
Definition: pcretest.c:1051
static int buffer_size
Definition: pcretest.c:1050
static bool s_HasWindowsDriveLetter(const string &s)
Definition: vdbfile.cpp:59
DEFINE_BAM_REF_TRAITS(VPath,)
#define NCBI_THROW2_FMT(exception_class, err_code, message, extra)
Definition: vdbfile.cpp:42
Modified on Wed Apr 17 13:10:16 2024 by modify_doxy.py rev. 669887