1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #pragma once
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #include <hdfs.h>
24 
25 #include "arrow/util/visibility.h"
26 #include "arrow/util/windows_compatibility.h"  // IWYU pragma: keep
27 
28 using std::size_t;
29 
30 struct hdfsBuilder;
31 
32 namespace arrow {
33 
34 class Status;
35 
36 namespace io {
37 namespace internal {
38 
39 #ifndef _WIN32
40 typedef void* LibraryHandle;
41 #else
42 typedef HINSTANCE LibraryHandle;
43 #endif
44 
45 // NOTE(wesm): cpplint does not like use of short and other imprecise C types
46 struct LibHdfsShim {
47   LibraryHandle handle;
48 
49   hdfsBuilder* (*hdfsNewBuilder)(void);
50   void (*hdfsBuilderSetNameNode)(hdfsBuilder* bld, const char* nn);
51   void (*hdfsBuilderSetNameNodePort)(hdfsBuilder* bld, tPort port);
52   void (*hdfsBuilderSetUserName)(hdfsBuilder* bld, const char* userName);
53   void (*hdfsBuilderSetKerbTicketCachePath)(hdfsBuilder* bld,
54                                             const char* kerbTicketCachePath);
55   void (*hdfsBuilderSetForceNewInstance)(hdfsBuilder* bld);
56   hdfsFS (*hdfsBuilderConnect)(hdfsBuilder* bld);
57   int (*hdfsBuilderConfSetStr)(hdfsBuilder* bld, const char* key, const char* val);
58 
59   int (*hdfsDisconnect)(hdfsFS fs);
60 
61   hdfsFile (*hdfsOpenFile)(hdfsFS fs, const char* path, int flags, int bufferSize,
62                            short replication, tSize blocksize);  // NOLINT
63 
64   int (*hdfsCloseFile)(hdfsFS fs, hdfsFile file);
65   int (*hdfsExists)(hdfsFS fs, const char* path);
66   int (*hdfsSeek)(hdfsFS fs, hdfsFile file, tOffset desiredPos);
67   tOffset (*hdfsTell)(hdfsFS fs, hdfsFile file);
68   tSize (*hdfsRead)(hdfsFS fs, hdfsFile file, void* buffer, tSize length);
69   tSize (*hdfsPread)(hdfsFS fs, hdfsFile file, tOffset position, void* buffer,
70                      tSize length);
71   tSize (*hdfsWrite)(hdfsFS fs, hdfsFile file, const void* buffer, tSize length);
72   int (*hdfsFlush)(hdfsFS fs, hdfsFile file);
73   int (*hdfsAvailable)(hdfsFS fs, hdfsFile file);
74   int (*hdfsCopy)(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
75   int (*hdfsMove)(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
76   int (*hdfsDelete)(hdfsFS fs, const char* path, int recursive);
77   int (*hdfsRename)(hdfsFS fs, const char* oldPath, const char* newPath);
78   char* (*hdfsGetWorkingDirectory)(hdfsFS fs, char* buffer, size_t bufferSize);
79   int (*hdfsSetWorkingDirectory)(hdfsFS fs, const char* path);
80   int (*hdfsCreateDirectory)(hdfsFS fs, const char* path);
81   int (*hdfsSetReplication)(hdfsFS fs, const char* path, int16_t replication);
82   hdfsFileInfo* (*hdfsListDirectory)(hdfsFS fs, const char* path, int* numEntries);
83   hdfsFileInfo* (*hdfsGetPathInfo)(hdfsFS fs, const char* path);
84   void (*hdfsFreeFileInfo)(hdfsFileInfo* hdfsFileInfo, int numEntries);
85   char*** (*hdfsGetHosts)(hdfsFS fs, const char* path, tOffset start, tOffset length);
86   void (*hdfsFreeHosts)(char*** blockHosts);
87   tOffset (*hdfsGetDefaultBlockSize)(hdfsFS fs);
88   tOffset (*hdfsGetCapacity)(hdfsFS fs);
89   tOffset (*hdfsGetUsed)(hdfsFS fs);
90   int (*hdfsChown)(hdfsFS fs, const char* path, const char* owner, const char* group);
91   int (*hdfsChmod)(hdfsFS fs, const char* path, short mode);  // NOLINT
92   int (*hdfsUtime)(hdfsFS fs, const char* path, tTime mtime, tTime atime);
93 
InitializeLibHdfsShim94   void Initialize() {
95     this->handle = nullptr;
96     this->hdfsNewBuilder = nullptr;
97     this->hdfsBuilderSetNameNode = nullptr;
98     this->hdfsBuilderSetNameNodePort = nullptr;
99     this->hdfsBuilderSetUserName = nullptr;
100     this->hdfsBuilderSetKerbTicketCachePath = nullptr;
101     this->hdfsBuilderSetForceNewInstance = nullptr;
102     this->hdfsBuilderConfSetStr = nullptr;
103     this->hdfsBuilderConnect = nullptr;
104     this->hdfsDisconnect = nullptr;
105     this->hdfsOpenFile = nullptr;
106     this->hdfsCloseFile = nullptr;
107     this->hdfsExists = nullptr;
108     this->hdfsSeek = nullptr;
109     this->hdfsTell = nullptr;
110     this->hdfsRead = nullptr;
111     this->hdfsPread = nullptr;
112     this->hdfsWrite = nullptr;
113     this->hdfsFlush = nullptr;
114     this->hdfsAvailable = nullptr;
115     this->hdfsCopy = nullptr;
116     this->hdfsMove = nullptr;
117     this->hdfsDelete = nullptr;
118     this->hdfsRename = nullptr;
119     this->hdfsGetWorkingDirectory = nullptr;
120     this->hdfsSetWorkingDirectory = nullptr;
121     this->hdfsCreateDirectory = nullptr;
122     this->hdfsSetReplication = nullptr;
123     this->hdfsListDirectory = nullptr;
124     this->hdfsGetPathInfo = nullptr;
125     this->hdfsFreeFileInfo = nullptr;
126     this->hdfsGetHosts = nullptr;
127     this->hdfsFreeHosts = nullptr;
128     this->hdfsGetDefaultBlockSize = nullptr;
129     this->hdfsGetCapacity = nullptr;
130     this->hdfsGetUsed = nullptr;
131     this->hdfsChown = nullptr;
132     this->hdfsChmod = nullptr;
133     this->hdfsUtime = nullptr;
134   }
135 
136   hdfsBuilder* NewBuilder(void);
137 
138   void BuilderSetNameNode(hdfsBuilder* bld, const char* nn);
139 
140   void BuilderSetNameNodePort(hdfsBuilder* bld, tPort port);
141 
142   void BuilderSetUserName(hdfsBuilder* bld, const char* userName);
143 
144   void BuilderSetKerbTicketCachePath(hdfsBuilder* bld, const char* kerbTicketCachePath);
145 
146   void BuilderSetForceNewInstance(hdfsBuilder* bld);
147 
148   int BuilderConfSetStr(hdfsBuilder* bld, const char* key, const char* val);
149 
150   hdfsFS BuilderConnect(hdfsBuilder* bld);
151 
152   int Disconnect(hdfsFS fs);
153 
154   hdfsFile OpenFile(hdfsFS fs, const char* path, int flags, int bufferSize,
155                     short replication, tSize blocksize);  // NOLINT
156 
157   int CloseFile(hdfsFS fs, hdfsFile file);
158 
159   int Exists(hdfsFS fs, const char* path);
160 
161   int Seek(hdfsFS fs, hdfsFile file, tOffset desiredPos);
162 
163   tOffset Tell(hdfsFS fs, hdfsFile file);
164 
165   tSize Read(hdfsFS fs, hdfsFile file, void* buffer, tSize length);
166 
167   bool HasPread();
168 
169   tSize Pread(hdfsFS fs, hdfsFile file, tOffset position, void* buffer, tSize length);
170 
171   tSize Write(hdfsFS fs, hdfsFile file, const void* buffer, tSize length);
172 
173   int Flush(hdfsFS fs, hdfsFile file);
174 
175   int Available(hdfsFS fs, hdfsFile file);
176 
177   int Copy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
178 
179   int Move(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst);
180 
181   int Delete(hdfsFS fs, const char* path, int recursive);
182 
183   int Rename(hdfsFS fs, const char* oldPath, const char* newPath);
184 
185   char* GetWorkingDirectory(hdfsFS fs, char* buffer, size_t bufferSize);
186 
187   int SetWorkingDirectory(hdfsFS fs, const char* path);
188 
189   int MakeDirectory(hdfsFS fs, const char* path);
190 
191   int SetReplication(hdfsFS fs, const char* path, int16_t replication);
192 
193   hdfsFileInfo* ListDirectory(hdfsFS fs, const char* path, int* numEntries);
194 
195   hdfsFileInfo* GetPathInfo(hdfsFS fs, const char* path);
196 
197   void FreeFileInfo(hdfsFileInfo* hdfsFileInfo, int numEntries);
198 
199   char*** GetHosts(hdfsFS fs, const char* path, tOffset start, tOffset length);
200 
201   void FreeHosts(char*** blockHosts);
202 
203   tOffset GetDefaultBlockSize(hdfsFS fs);
204   tOffset GetCapacity(hdfsFS fs);
205 
206   tOffset GetUsed(hdfsFS fs);
207 
208   int Chown(hdfsFS fs, const char* path, const char* owner, const char* group);
209 
210   int Chmod(hdfsFS fs, const char* path, short mode);  // NOLINT
211 
212   int Utime(hdfsFS fs, const char* path, tTime mtime, tTime atime);
213 
214   Status GetRequiredSymbols();
215 };
216 
217 // TODO(wesm): Remove these exports when we are linking statically
218 Status ARROW_EXPORT ConnectLibHdfs(LibHdfsShim** driver);
219 
220 }  // namespace internal
221 }  // namespace io
222 }  // namespace arrow
223