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

« back to all changes in this revision

Viewing changes to parrot/src/pfs_service_xrootd.cc

  • 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) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
/*
 
9
Important: This define must come before all include files,
 
10
in order to indicate that we are working with the 64-bit definition
 
11
of file sizes in struct stat and other places as needed by xrootd.
 
12
Note that we do not define it globally, so that we are sure not to break other
 
13
people's include files.  Note also that we are careful to use
 
14
struct pfs_stat in our own headers, so that our own code isn't
 
15
sensitive to this setting.
 
16
*/
 
17
 
 
18
#define _FILE_OFFSET_BITS 64
 
19
 
 
20
#ifdef HAS_XROOTD
 
21
 
 
22
#include "pfs_service.h"
 
23
 
 
24
extern "C" {
 
25
#include "debug.h"
 
26
#include "stringtools.h"
 
27
#include "domain_name.h"
 
28
#include "link.h"
 
29
#include "file_cache.h"
 
30
#include "full_io.h"
 
31
#include "http_query.h"
 
32
}
 
33
#include <unistd.h>
 
34
#include <string.h>
 
35
#include <stdio.h>
 
36
#include <fcntl.h>
 
37
#include <errno.h>
 
38
#include <stdlib.h>
 
39
#include <sys/stat.h>
 
40
#include <sys/statfs.h>
 
41
#include <pfs_types.h>
 
42
#include <string.h>
 
43
#include <XrdPosix/XrdPosixExtern.hh>
 
44
 
 
45
#define XROOTD_FILE_MODE (S_IFREG | 0555)
 
46
#define XROOTD_DEFAULT_PORT 1094
 
47
 
 
48
static char *translate_file_to_xrootd(pfs_name * name)
 
49
{
 
50
        char file_buf[PFS_PATH_MAX];
 
51
        int string_length = 0;
 
52
        int port_number = XROOTD_DEFAULT_PORT;
 
53
 
 
54
        if(name->port != 0) {
 
55
                port_number = name->port;
 
56
        }
 
57
 
 
58
        string_length = sprintf(file_buf, "root://%s:%i/%s", name->host, port_number, name->rest);
 
59
 
 
60
        return strdup(file_buf);
 
61
}
 
62
 
 
63
 
 
64
class pfs_file_xrootd:public pfs_file {
 
65
private:
 
66
        int file_handle;
 
67
 
 
68
public:
 
69
        pfs_file_xrootd(pfs_name * n, int file_handle):pfs_file(n) {
 
70
                this->file_handle = file_handle;
 
71
        }
 
72
 
 
73
        virtual int close() {
 
74
                debug(D_XROOTD, "close %i", this->file_handle);
 
75
                return XrdPosix_Close(this->file_handle);
 
76
        }
 
77
 
 
78
        virtual pfs_ssize_t read(void *d, pfs_size_t length, pfs_off_t offset) {
 
79
                debug(D_XROOTD, "pread %d %lu %lu",this->file_handle,length,offset);
 
80
                return XrdPosix_Pread(this->file_handle,d,length,offset);
 
81
        }
 
82
 
 
83
        virtual pfs_ssize_t write(const void *d, pfs_size_t length, pfs_off_t offset) {
 
84
                debug(D_XROOTD, "pwrite %d %lu %lu",this->file_handle,length,offset);
 
85
                return XrdPosix_Pwrite(this->file_handle,d,length,offset);
 
86
        }
 
87
 
 
88
        virtual int fstat(struct pfs_stat *buf) {
 
89
                debug(D_XROOTD, "fstat %d",this->file_handle);
 
90
                struct stat lbuf;
 
91
                int result = XrdPosix_Fstat(this->file_handle, &lbuf);
 
92
                if(result == 0) COPY_STAT(lbuf, *buf);
 
93
                return result;
 
94
        }
 
95
 
 
96
        virtual pfs_ssize_t get_size() {
 
97
                struct pfs_stat buf;
 
98
                if(this->fstat(&buf) == 0) {
 
99
                        return buf.st_size;
 
100
                } else {
 
101
                        return -1;
 
102
                }
 
103
        }
 
104
 
 
105
 
 
106
};
 
107
 
 
108
class pfs_service_xrootd:public pfs_service {
 
109
public:
 
110
        virtual pfs_file * open(pfs_name * name, int flags, mode_t mode) {
 
111
                int file_handle;
 
112
                char *file_url = NULL;
 
113
 
 
114
                debug(D_XROOTD, "Opening file: %s", name->rest);
 
115
 
 
116
                if((flags & O_ACCMODE) != O_RDONLY) {
 
117
                        errno = EROFS;
 
118
                        return 0;
 
119
                }
 
120
 
 
121
                file_url = translate_file_to_xrootd(name);
 
122
                file_handle = XrdPosix_Open(file_url, flags, mode);
 
123
                free(file_url);
 
124
 
 
125
                if(file_handle>=0) {
 
126
                        return new pfs_file_xrootd(name, file_handle);
 
127
                } else {
 
128
                        return 0;
 
129
                }
 
130
 
 
131
        }
 
132
 
 
133
        virtual pfs_dir * getdir( pfs_name *name ) {
 
134
                DIR *dir;
 
135
                struct dirent *d;
 
136
 
 
137
                char *file_url = NULL;
 
138
                file_url = translate_file_to_xrootd(name);
 
139
 
 
140
                debug(D_XROOTD,"getdir %s",file_url);
 
141
 
 
142
                dir = XrdPosix_Opendir(file_url);
 
143
                if(!dir) {
 
144
                        free(file_url);
 
145
                        return 0;
 
146
                }
 
147
 
 
148
                pfs_dir *pdir = new pfs_dir(name);
 
149
 
 
150
                while((d=XrdPosix_Readdir(dir))) {
 
151
                        pdir->append(d->d_name);
 
152
                }
 
153
                XrdPosix_Closedir(dir);
 
154
 
 
155
                free(file_url);
 
156
 
 
157
                return pdir;
 
158
        }
 
159
 
 
160
        virtual int statfs(pfs_name * name, struct pfs_statfs *buf) {
 
161
                struct statfs lbuf;
 
162
                char *file_url = translate_file_to_xrootd(name);
 
163
                debug(D_XROOTD, "statfs %s",file_url);
 
164
                int result = XrdPosix_Statfs((const char *) file_url, &lbuf);
 
165
                free(file_url);
 
166
                COPY_STATFS(lbuf, *buf);
 
167
                return result;
 
168
        }
 
169
 
 
170
        virtual int stat(pfs_name * name, struct pfs_stat *buf) {
 
171
                struct stat lbuf;
 
172
                char *file_url = translate_file_to_xrootd(name);
 
173
                debug(D_XROOTD, "stat %s",file_url);
 
174
                int result = XrdPosix_Stat((const char *) file_url, &lbuf);
 
175
                free(file_url);
 
176
                COPY_STAT(lbuf, *buf);
 
177
                return result;
 
178
        }
 
179
 
 
180
        virtual int lstat(pfs_name * name, struct pfs_stat *buf) {
 
181
                return this->stat(name, buf);
 
182
        }
 
183
 
 
184
        virtual int unlink( pfs_name *name ) {
 
185
                char *file_url = translate_file_to_xrootd(name);
 
186
                debug(D_XROOTD, "unlink %s",file_url);
 
187
                int result = XrdPosix_Unlink((const char *) file_url);
 
188
                free(file_url);
 
189
                return result;
 
190
        }
 
191
 
 
192
        virtual int access( pfs_name *name, mode_t mode ) {
 
193
                char *file_url = translate_file_to_xrootd(name);
 
194
                debug(D_XROOTD, "access %s %o",file_url,mode);
 
195
                int result = XrdPosix_Access((const char *) file_url,mode);
 
196
                free(file_url);
 
197
                return result;
 
198
        }
 
199
 
 
200
        virtual int truncate( pfs_name *name, pfs_off_t length ) {
 
201
                char *file_url = translate_file_to_xrootd(name);
 
202
                debug(D_XROOTD, "truncate %s %lld",file_url,length);
 
203
                int result = XrdPosix_Truncate(file_url,length);
 
204
                free(file_url);
 
205
                return result;
 
206
        }
 
207
 
 
208
        virtual int chdir( pfs_name *name, char *newpath ) {
 
209
                struct pfs_stat info;
 
210
                if(this->stat(name,&info)>=0) {
 
211
                        if(S_ISDIR(info.st_mode)) {
 
212
                                sprintf(newpath,"/%s/%s:%d%s",name->service_name,name->host,name->port,name->rest);
 
213
                                return 0;
 
214
                        } else {
 
215
                                errno = ENOTDIR;
 
216
                                return -1;
 
217
                        }
 
218
                } else {
 
219
                        return -1;
 
220
                }
 
221
        }
 
222
 
 
223
        virtual int rename( pfs_name *oldname, pfs_name *newname ) {
 
224
                char *old_url = translate_file_to_xrootd(oldname);
 
225
                char *new_url = translate_file_to_xrootd(newname);
 
226
 
 
227
                debug(D_XROOTD,"rename %s %s",old_url,new_url);
 
228
                int result = XrdPosix_Rename(old_url,new_url);
 
229
 
 
230
                free(old_url);
 
231
                free(new_url);
 
232
 
 
233
                return result;
 
234
        }
 
235
 
 
236
        virtual int mkdir( pfs_name *name, mode_t mode) {
 
237
                char *file_url = translate_file_to_xrootd(name);
 
238
                debug(D_XROOTD, "mkdir %s %o",file_url,mode);
 
239
                int result = XrdPosix_Mkdir((const char *) file_url,mode);
 
240
                free(file_url);
 
241
                return result;
 
242
        }
 
243
 
 
244
        virtual int rmdir( pfs_name *name ) {
 
245
                char *file_url = translate_file_to_xrootd(name);
 
246
                debug(D_XROOTD, "rmdir %s",file_url);
 
247
                int result = XrdPosix_Rmdir((const char *) file_url);
 
248
                free(file_url);
 
249
                return result;
 
250
        }
 
251
 
 
252
        virtual int is_seekable() {
 
253
                return 1;
 
254
        }
 
255
 
 
256
 
 
257
};
 
258
 
 
259
static pfs_service_xrootd pfs_service_xrootd_instance;
 
260
pfs_service *pfs_service_xrootd = &pfs_service_xrootd_instance;
 
261
 
 
262
#endif