File: cpl_vsil_hdfs.cpp

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (446 lines) | stat: -rw-r--r-- 14,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**********************************************************************
 *
 * Project:  CPL - Common Portability Library
 * Purpose:  Implement VSI large file api for HDFS
 * Author:   James McClain, <jmcclain@azavea.com>
 *
 **********************************************************************
 * Copyright (c) 2010-2015, Even Rouault <even dot rouault at spatialys.com>
 * Copyright (c) 2018, Azavea
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

//! @cond Doxygen_Suppress

#include <string>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#if !defined(_MSC_VER)
#include <unistd.h>
#endif

#include <cstring>
#include <climits>

#include "cpl_port.h"
#include "cpl_vsi.h"

#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_vsi_virtual.h"

#ifdef HDFS_ENABLED

#include "hdfs.h"

/************************************************************************/
/* ==================================================================== */
/*                        VSIHdfsHandle                               */
/* ==================================================================== */
/************************************************************************/

#define SILENCE(expr)                                                          \
    {                                                                          \
        int hOldStderr = dup(2);                                               \
        int hNewStderr = open("/dev/null", O_WRONLY);                          \
                                                                               \
        if ((hOldStderr != -1) && (hNewStderr != -1) &&                        \
            (dup2(hNewStderr, 2) != -1))                                       \
        {                                                                      \
            close(hNewStderr);                                                 \
            expr;                                                              \
            dup2(hOldStderr, 2);                                               \
            close(hOldStderr);                                                 \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            if (hOldStderr != -1)                                              \
                close(hOldStderr);                                             \
            if (hNewStderr != -1)                                              \
                close(hNewStderr);                                             \
            expr;                                                              \
        }                                                                      \
    }

class VSIHdfsHandle final : public VSIVirtualHandle
{
  private:
    CPL_DISALLOW_COPY_ASSIGN(VSIHdfsHandle)

    hdfsFile poFile = nullptr;
    hdfsFS poFilesystem = nullptr;
    std::string oFilename;
    bool bEOF = false;

  public:
#if __cplusplus >= 201103L
    static constexpr const char *VSIHDFS = "/vsihdfs/";
#else
    static const char *VSIHDFS = "/vsihdfs/";
#endif
    VSIHdfsHandle(hdfsFile poFile, hdfsFS poFilesystem, const char *pszFilename,
                  bool bReadOnly);
    ~VSIHdfsHandle() override;

    int Seek(vsi_l_offset nOffset, int nWhence) override;
    vsi_l_offset Tell() override;
    size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
    size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
    vsi_l_offset Length();
    int Eof() override;
    int Flush() override;
    int Close() override;
};

VSIHdfsHandle::VSIHdfsHandle(hdfsFile _poFile, hdfsFS _poFilesystem,
                             const char *pszFilename, bool /*_bReadOnly*/)
    : poFile(_poFile), poFilesystem(_poFilesystem), oFilename(pszFilename)
{
}

VSIHdfsHandle::~VSIHdfsHandle()
{
    Close();
}

int VSIHdfsHandle::Seek(vsi_l_offset nOffset, int nWhence)
{
    bEOF = false;
    switch (nWhence)
    {
        case SEEK_SET:
            return hdfsSeek(poFilesystem, poFile, nOffset);
        case SEEK_CUR:
            return hdfsSeek(poFilesystem, poFile, nOffset + Tell());
        case SEEK_END:
            return hdfsSeek(poFilesystem, poFile,
                            static_cast<tOffset>(Length()) - nOffset);
        default:
            return -1;
    }
}

vsi_l_offset VSIHdfsHandle::Tell()
{
    return hdfsTell(poFilesystem, poFile);
}

size_t VSIHdfsHandle::Read(void *pBuffer, size_t nSize, size_t nMemb)
{
    if (nSize == 0 || nMemb == 0)
        return 0;

    size_t bytes_wanted = nSize * nMemb;
    size_t bytes_read = 0;

    while (bytes_read < bytes_wanted)
    {
        tSize bytes = 0;
        size_t bytes_to_request = bytes_wanted - bytes_read;

        // The `Read` function can take 64-bit arguments for its
        // read-request size, whereas `hdfsRead` may only take a 32-bit
        // argument.  If the former requests an amount larger than can
        // be encoded in a signed 32-bit number, break the request into
        // 2GB batches.
        bytes = hdfsRead(
            poFilesystem, poFile, static_cast<char *>(pBuffer) + bytes_read,
            bytes_to_request > INT_MAX ? INT_MAX : bytes_to_request);

        if (bytes > 0)
        {
            bytes_read += bytes;
        }
        if (bytes == 0)
        {
            bEOF = true;
            return bytes_read / nSize;
        }
        else if (bytes < 0)
        {
            bEOF = false;
            return 0;
        }
    }

    return bytes_read / nSize;
}

size_t VSIHdfsHandle::Write(const void *, size_t, size_t)
{
    CPLError(CE_Failure, CPLE_AppDefined, "HDFS driver is read-only");
    return -1;
}

vsi_l_offset VSIHdfsHandle::Length()
{
    hdfsFileInfo *poInfo = hdfsGetPathInfo(poFilesystem, oFilename.c_str());
    if (poInfo != nullptr)
    {
        tOffset nSize = poInfo->mSize;
        hdfsFreeFileInfo(poInfo, 1);
        return static_cast<vsi_l_offset>(nSize);
    }
    return -1;
}

int VSIHdfsHandle::Eof()
{
    return bEOF;
}

int VSIHdfsHandle::Flush()
{
    return hdfsFlush(poFilesystem, poFile);
}

int VSIHdfsHandle::Close()
{
    int retval = 0;

    if (poFilesystem != nullptr && poFile != nullptr)
        retval = hdfsCloseFile(poFilesystem, poFile);
    poFile = nullptr;
    poFilesystem = nullptr;

    return retval;
}

class VSIHdfsFilesystemHandler final : public VSIFilesystemHandler
{
  private:
    CPL_DISALLOW_COPY_ASSIGN(VSIHdfsFilesystemHandler)

    hdfsFS poFilesystem = nullptr;
    CPLMutex *hMutex = nullptr;

  public:
    VSIHdfsFilesystemHandler();
    ~VSIHdfsFilesystemHandler() override;

    void EnsureFilesystem();
    VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess,
                           bool bSetError,
                           CSLConstList /* papszOptions */) override;
    int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
             int nFlags) override;
    int Unlink(const char *pszFilename) override;
    int Mkdir(const char *pszDirname, long nMode) override;
    int Rmdir(const char *pszDirname) override;
    char **ReadDir(const char *pszDirname) override;
    int Rename(const char *oldpath, const char *newpath) override;
};

VSIHdfsFilesystemHandler::VSIHdfsFilesystemHandler()
{
}

VSIHdfsFilesystemHandler::~VSIHdfsFilesystemHandler()
{
    if (hMutex != nullptr)
    {
        CPLDestroyMutex(hMutex);
        hMutex = nullptr;
    }

    if (poFilesystem != nullptr)
        hdfsDisconnect(poFilesystem);
    poFilesystem = nullptr;
}

void VSIHdfsFilesystemHandler::EnsureFilesystem()
{
    CPLMutexHolder oHolder(&hMutex);
    if (poFilesystem == nullptr)
        poFilesystem = hdfsConnect("default", 0);
}

VSIVirtualHandle *
VSIHdfsFilesystemHandler::Open(const char *pszFilename, const char *pszAccess,
                               bool, CSLConstList /* papszOptions */)
{
    EnsureFilesystem();

    if (strchr(pszAccess, 'w') != nullptr || strchr(pszAccess, 'a') != nullptr)
    {
        CPLError(CE_Failure, CPLE_AppDefined, "HDFS driver is read-only");
        return nullptr;
    }

    if (strncmp(pszFilename, VSIHdfsHandle::VSIHDFS,
                strlen(VSIHdfsHandle::VSIHDFS)) != 0)
    {
        return nullptr;
    }
    else
    {
        const char *pszPath = pszFilename + strlen(VSIHdfsHandle::VSIHDFS);

        // Open HDFS file, sending Java stack traces to /dev/null.
        hdfsFile poFile = nullptr;
        SILENCE(poFile =
                    hdfsOpenFile(poFilesystem, pszPath, O_RDONLY, 0, 0, 0));

        if (poFile != nullptr)
        {
            VSIHdfsHandle *poHandle =
                new VSIHdfsHandle(poFile, poFilesystem, pszPath, true);
            return poHandle;
        }
    }
    return nullptr;
}

int VSIHdfsFilesystemHandler::Stat(const char *pszeFilename,
                                   VSIStatBufL *pStatBuf, int)
{
    EnsureFilesystem();

    hdfsFileInfo *poInfo = hdfsGetPathInfo(
        poFilesystem, pszeFilename + strlen(VSIHdfsHandle::VSIHDFS));

    if (poInfo != nullptr)
    {
        pStatBuf->st_dev =
            static_cast<dev_t>(0); /* ID of device containing file */
        pStatBuf->st_ino = static_cast<ino_t>(0); /* inode number */
        switch (poInfo->mKind)
        { /* protection */
            case tObjectKind::kObjectKindFile:
                pStatBuf->st_mode = S_IFREG;
                break;
            case tObjectKind::kObjectKindDirectory:
                pStatBuf->st_mode = S_IFDIR;
                break;
            default:
                CPLError(CE_Failure, CPLE_AppDefined,
                         "Unrecognized object kind");
        }
        pStatBuf->st_nlink = static_cast<nlink_t>(0); /* number of hard links */
        pStatBuf->st_uid = getuid();                  /* user ID of owner */
        pStatBuf->st_gid = getgid();                  /* group ID of owner */
        pStatBuf->st_rdev =
            static_cast<dev_t>(0); /* device ID (if special file) */
        pStatBuf->st_size =
            static_cast<off_t>(poInfo->mSize); /* total size, in bytes */
        pStatBuf->st_blksize = static_cast<blksize_t>(
            poInfo->mBlockSize); /* blocksize for filesystem I/O */
        pStatBuf->st_blocks =
            static_cast<blkcnt_t>((poInfo->mBlockSize >> 9) +
                                  1); /* number of 512B blocks allocated */
        pStatBuf->st_atime =
            static_cast<time_t>(poInfo->mLastAccess); /* time of last access */
        pStatBuf->st_mtime = static_cast<time_t>(
            poInfo->mLastMod); /* time of last modification */
        pStatBuf->st_ctime = static_cast<time_t>(
            poInfo->mLastMod); /* time of last status change */
        hdfsFreeFileInfo(poInfo, 1);
        return 0;
    }

    return -1;
}

int VSIHdfsFilesystemHandler::Unlink(const char *)
{
    CPLError(CE_Failure, CPLE_AppDefined, "HDFS driver is read-only");
    return -1;
}

int VSIHdfsFilesystemHandler::Mkdir(const char *, long)
{
    CPLError(CE_Failure, CPLE_AppDefined, "HDFS driver is read-only");
    return -1;
}

int VSIHdfsFilesystemHandler::Rmdir(const char *)
{
    CPLError(CE_Failure, CPLE_AppDefined, "HDFS driver is read-only");
    return -1;
}

char **VSIHdfsFilesystemHandler::ReadDir(const char *pszDirname)
{
    EnsureFilesystem();

    int mEntries = 0;
    hdfsFileInfo *paoInfo = hdfsListDirectory(
        poFilesystem, pszDirname + strlen(VSIHdfsHandle::VSIHDFS), &mEntries);
    char **retval = nullptr;

    if (paoInfo != nullptr)
    {
        CPLStringList aosNames;
        for (int i = 0; i < mEntries; ++i)
            aosNames.AddString(paoInfo[i].mName);
        retval = aosNames.StealList();
        hdfsFreeFileInfo(paoInfo, mEntries);
        return retval;
    }
    return nullptr;
}

int VSIHdfsFilesystemHandler::Rename(const char *, const char *)
{
    CPLError(CE_Failure, CPLE_AppDefined, "HDFS driver is read-only");
    return -1;
}

#endif

//! @endcond

#ifdef HDFS_ENABLED

/************************************************************************/
/*                       VSIInstallHdfsHandler()                        */
/************************************************************************/

/**
 * \brief Install /vsihdfs/ file system handler (requires JVM and HDFS support)
 *
 * @since GDAL 2.4.0
 */
void VSIInstallHdfsHandler()
{
    VSIFileManager::InstallHandler(VSIHdfsHandle::VSIHDFS,
                                   new VSIHdfsFilesystemHandler);
}

#else

/************************************************************************/
/*                       VSIInstallHdfsHandler()                        */
/************************************************************************/

/**
 * \brief Install /vsihdfs/ file system handler (non-functional stub)
 *
 * @since GDAL 2.4.0
 */
void VSIInstallHdfsHandler(void)
{
    // Not supported.
}

#endif