~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/hdfs_library.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2011- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
 
 
7
#include "hdfs_library.h"
 
8
 
 
9
#include <stdlib.h>
 
10
#include <stdio.h>
 
11
#include <string.h>
 
12
#include <dlfcn.h>
 
13
#include <unistd.h>
 
14
#include <errno.h>
 
15
 
 
16
#include "debug.h"
 
17
 
 
18
#define HDFS_LOAD_FUNC( lval, name ) \
 
19
        hs->lval = dlsym(hs->libhdfs_handle,name); \
 
20
        if(!hs->lval) { \
 
21
                debug(D_NOTICE|D_HDFS,"couldn't find %s in libhdfs.so",name); \
 
22
                goto failure; \
 
23
        }
 
24
 
 
25
void hdfs_library_close(struct hdfs_library *hs)
 
26
{
 
27
        dlclose(hs->libjvm_handle);
 
28
        dlclose(hs->libhdfs_handle);
 
29
        free(hs);
 
30
}
 
31
 
 
32
struct hdfs_library *hdfs_library_open()
 
33
{
 
34
        static int did_warning = 0;
 
35
 
 
36
        if(!getenv("JAVA_HOME") || !getenv("HADOOP_HOME") || !getenv("CLASSPATH") || !getenv("LIBHDFS_PATH") || !getenv("LIBJVM_PATH")) {
 
37
                if(!did_warning) {
 
38
                        debug(D_NOTICE | D_HDFS, "Sorry, to use HDFS, you need to set up Java and Hadoop first.\n");
 
39
                        debug(D_NOTICE | D_HDFS, "Please set JAVA_HOME and HADOOP_HOME appropriately,\n");
 
40
                        debug(D_NOTICE | D_HDFS, "then use chirp_server_hdfs or parrot_run_hdfs as needed.\n");
 
41
                        did_warning = 0;
 
42
                }
 
43
                errno = ENOSYS;
 
44
                return 0;
 
45
        }
 
46
 
 
47
        struct hdfs_library *hs = malloc(sizeof(*hs));
 
48
 
 
49
        const char *libjvm_path = getenv("LIBJVM_PATH");
 
50
        hs->libjvm_handle = dlopen(libjvm_path, RTLD_LAZY);
 
51
        if(!hs->libjvm_handle) {
 
52
                debug(D_NOTICE | D_HDFS, "couldn't dlopen LIBJVM_PATH=%s: %s", libjvm_path, dlerror());
 
53
                free(hs);
 
54
                return 0;
 
55
        }
 
56
 
 
57
 
 
58
        const char *libhdfs_path = getenv("LIBHDFS_PATH");
 
59
        hs->libhdfs_handle = dlopen(libhdfs_path, RTLD_LAZY);
 
60
        if(!hs->libhdfs_handle) {
 
61
                dlclose(hs->libjvm_handle);
 
62
                free(hs);
 
63
                debug(D_NOTICE | D_HDFS, "couldn't dlopen LIBHDFS_PATH=%s: %s", dlerror());
 
64
                return 0;
 
65
        }
 
66
 
 
67
 
 
68
        HDFS_LOAD_FUNC(connect, "hdfsConnect");
 
69
        HDFS_LOAD_FUNC(connect_as_user, "hdfsConnectAsUser");
 
70
        HDFS_LOAD_FUNC(disconnect, "hdfsDisconnect");
 
71
        HDFS_LOAD_FUNC(listdir, "hdfsListDirectory");
 
72
        HDFS_LOAD_FUNC(open, "hdfsOpenFile");
 
73
        HDFS_LOAD_FUNC(close, "hdfsCloseFile");
 
74
        HDFS_LOAD_FUNC(flush, "hdfsFlush");
 
75
        HDFS_LOAD_FUNC(read, "hdfsRead");
 
76
        HDFS_LOAD_FUNC(pread, "hdfsPread");
 
77
        HDFS_LOAD_FUNC(write, "hdfsWrite");
 
78
        HDFS_LOAD_FUNC(exists, "hdfsExists");
 
79
        HDFS_LOAD_FUNC(mkdir, "hdfsCreateDirectory");
 
80
        HDFS_LOAD_FUNC(unlink, "hdfsDelete");
 
81
        HDFS_LOAD_FUNC(rename, "hdfsRename");
 
82
        HDFS_LOAD_FUNC(stat, "hdfsGetPathInfo");
 
83
        HDFS_LOAD_FUNC(free_stat, "hdfsFreeFileInfo");
 
84
        HDFS_LOAD_FUNC(get_hosts, "hdfsGetHosts");
 
85
        HDFS_LOAD_FUNC(free_hosts, "hdfsFreeHosts");
 
86
        HDFS_LOAD_FUNC(get_default_block_size, "hdfsGetDefaultBlockSize");
 
87
        HDFS_LOAD_FUNC(get_capacity, "hdfsGetCapacity");
 
88
        HDFS_LOAD_FUNC(get_used, "hdfsGetUsed");
 
89
        HDFS_LOAD_FUNC(chmod, "hdfsChmod");
 
90
        HDFS_LOAD_FUNC(utime, "hdfsUtime");
 
91
        HDFS_LOAD_FUNC(chdir, "hdfsSetWorkingDirectory");
 
92
 
 
93
        return hs;
 
94
 
 
95
      failure:
 
96
        hdfs_library_close(hs);
 
97
        return 0;
 
98
}