Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c86e17d46b4784cbb22ca245174829c5935736a (plain) (blame)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*******************************************************************************
 * Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.internal.tcf.services.remote;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.tm.tcf.core.Base64;
import org.eclipse.tm.tcf.core.Command;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IFileSystem;


public class FileSystemProxy implements IFileSystem {
    
    private final class FileHandle implements IFileHandle {
        final String id;
        
        FileHandle(String id) {
            this.id = id;
        }

        public IFileSystem getService() {
            return FileSystemProxy.this;
        }
        
        public String toString() {
            return "[File Handle '" + id + "']";
        }
    }
    
    private static final class Status extends FileSystemException {
        
        private static final long serialVersionUID = -1636567076145085980L;
        
        private final int status;
        
        Status(int status, String message) {
            super(message);
            this.status = status; 
        }
        
        Status(Exception x) {
            super(x);
            this.status = STATUS_FAILURE; 
        }
        
        public int getStatus() {
            return status;
        }
    }
    
    private abstract class FileSystemCommand extends Command {
        
        FileSystemCommand(String command, Object[] args) {
            super(channel, FileSystemProxy.this, command, args);
        }
        
        @SuppressWarnings("unchecked")
        public Status toFSError(Object data) {
            if (data == null) return null;
            Map<String,Object> map = (Map<String,Object>)data;
            Number error_code = (Number)map.get(ERROR_CODE);
            String cmd = getCommandString();
            if (cmd.length() > 72) cmd = cmd.substring(0, 72) + "...";
            return new Status(error_code.intValue(),
                    "TCF command exception:" +
                    "\nCommand: " + cmd +
                    "\nException: " + toErrorString(data) +
                    "\nError code: " + error_code);
        }
    }
    
    private final IChannel channel;
    
    public FileSystemProxy(IChannel channel) {
        this.channel = channel;
    }

    public IToken close(IFileHandle handle, final DoneClose done) {
        assert handle.getService() == this;
        String id = ((FileHandle)handle).id;
        return new FileSystemCommand("close", new Object[]{ id }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneClose(token, s);
            }
        }.token;
    }

    public IToken setstat(String path, FileAttrs attrs, final DoneSetStat done) {
        Object dt = toObject(attrs);
        return new FileSystemCommand("setstat", new Object[]{ path, dt }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneSetStat(token, s);
            }
        }.token;
    }

    public IToken fsetstat(IFileHandle handle, FileAttrs attrs, final DoneSetStat done) {
        assert handle.getService() == this;
        String id = ((FileHandle)handle).id;
        Object dt = toObject(attrs);
        return new FileSystemCommand("fsetstat", new Object[]{ id, dt }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneSetStat(token, s);
            }
        }.token;
    }

    public IToken stat(String path, final DoneStat done) {
        return new FileSystemCommand("stat", new Object[]{ path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                FileAttrs a = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) a = toFileAttrs(args[1]);
                }
                done.doneStat(token, s, a);
            }
        }.token;
    }

    public IToken fstat(IFileHandle handle, final DoneStat done) {
        assert handle.getService() == this;
        String id = ((FileHandle)handle).id;
        return new FileSystemCommand("fstat", new Object[]{ id }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                FileAttrs a = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) a = toFileAttrs(args[1]);
                }
                done.doneStat(token, s, a);
            }
        }.token;
    }

    public IToken lstat(String path, final DoneStat done) {
        return new FileSystemCommand("lstat", new Object[]{ path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                FileAttrs a = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) a = toFileAttrs(args[1]);
                }
                done.doneStat(token, s, a);
            }
        }.token;
    }

    public IToken mkdir(String path, FileAttrs attrs, final DoneMkDir done) {
        Object dt = toObject(attrs);
        return new FileSystemCommand("mkdir", new Object[]{ path, dt }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneMkDir(token, s);
            }
        }.token;
    }

    public IToken open(String file_name, int flags, FileAttrs attrs, final DoneOpen done) {
        Object dt = toObject(attrs);
        return new FileSystemCommand("open", new Object[]{ file_name, new Integer(flags), dt }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                FileHandle h = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) h = toFileHandle(args[1]);
                }
                done.doneOpen(token, s, h);
            }
        }.token;
    }

    public IToken opendir(String path, final DoneOpen done) {
        return new FileSystemCommand("opendir", new Object[]{ path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                FileHandle h = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) h = toFileHandle(args[1]);
                }
                done.doneOpen(token, s, h);
            }
        }.token;
    }

    public IToken read(IFileHandle handle, long offset, int len, final DoneRead done) {
        assert handle.getService() == this;
        String id = ((FileHandle)handle).id;
        return new FileSystemCommand("read", new Object[]{
                id, Long.valueOf(offset), Integer.valueOf(len) }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                byte[] b = null;
                boolean eof = false;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 3;
                    s = toFSError(args[1]);
                    if (s == null) {
                        String str = (String)args[0];
                        if (str != null) b = Base64.toByteArray(str.toCharArray());
                        eof = ((Boolean)args[2]).booleanValue();
                    }
                }
                done.doneRead(token, s, b, eof);
            }
        }.token;
    }

    public IToken readdir(IFileHandle handle, final DoneReadDir done) {
        assert handle.getService() == this;
        String id = ((FileHandle)handle).id;
        return new FileSystemCommand("readdir", new Object[]{ id }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                DirEntry[] b = null;
                boolean eof = false;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 3;
                    s = toFSError(args[1]);
                    if (s == null) {
                        b = toDirEntryArray(args[0]);
                        eof = ((Boolean)args[2]).booleanValue();
                    }
                }
                done.doneReadDir(token, s, b, eof);
            }
        }.token;
    }

    public IToken roots(final DoneRoots done) {
        return new FileSystemCommand("roots", null) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                DirEntry[] b = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[1]);
                    if (s == null) b = toDirEntryArray(args[0]);
                }
                done.doneRoots(token, s, b);
            }
        }.token;
    }

    public IToken readlink(String path, final DoneReadLink done) {
        return new FileSystemCommand("readlink", new Object[]{ path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                String p = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) p = (String)args[1];
                }
                done.doneReadLink(token, s, p);
            }
        }.token;
    }

    public IToken realpath(String path, final DoneRealPath done) {
        return new FileSystemCommand("realpath", new Object[]{ path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                String p = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 2;
                    s = toFSError(args[0]);
                    if (s == null) p = (String)args[1];
                }
                done.doneRealPath(token, s, p);
            }
        }.token;
    }

    public IToken remove(String file_name, final DoneRemove done) {
        return new FileSystemCommand("remove", new Object[]{ file_name }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneRemove(token, s);
            }
        }.token;
    }

    public IToken rename(String old_path, String new_path, final DoneRename done) {
        return new FileSystemCommand("rename", new Object[]{ old_path, new_path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneRename(token, s);
            }
        }.token;
    }

    public IToken rmdir(String path, final DoneRemove done) {
        return new FileSystemCommand("rmdir", new Object[]{ path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneRemove(token, s);
            }
        }.token;
    }

    public IToken symlink(String link_path, String target_path, final DoneSymLink done) {
        return new FileSystemCommand("symlink", new Object[]{ link_path, target_path }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneSymLink(token, s);
            }
        }.token;
    }

    public IToken write(IFileHandle handle, long offset, byte[] data,
            int data_pos, int data_size, final DoneWrite done) {
        assert handle.getService() == this;
        String id = ((FileHandle)handle).id;
        return new FileSystemCommand("write", new Object[]{
                id, Long.valueOf(offset), Base64.toBase64(data, data_pos, data_size) }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneWrite(token, s);
            }
        }.token;
    }

    public IToken copy(String src_path, String dst_path,
            boolean copy_permissions, boolean copy_uidgid, final DoneCopy done) {
        return new FileSystemCommand("copy", new Object[]{
                src_path, dst_path, Boolean.valueOf(copy_permissions),
                Boolean.valueOf(copy_uidgid) }) {
            public void done(Exception error, Object[] args) {
                Status s = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 1;
                    s = toFSError(args[0]);
                }
                done.doneCopy(token, s);
            }
        }.token;
    }

    public IToken user(final DoneUser done) {
        return new FileSystemCommand("user", null) {
            @Override
            public void done(Exception error, Object[] args) {
                Status s = null;
                int r_uid = 0;
                int e_uid = 0;
                int r_gid = 0;
                int e_gid = 0;
                String home = null;
                if (error != null) {
                    s = new Status(error);
                }
                else {
                    assert args.length == 5;
                    r_uid = ((Number)args[0]).intValue();
                    e_uid = ((Number)args[1]).intValue();
                    r_gid = ((Number)args[2]).intValue();
                    e_gid = ((Number)args[3]).intValue();
                    home = (String)args[4];
                }
                done.doneUser(token, s, r_uid, e_uid, r_gid, e_gid, home);
            }
        }.token;
    }

    public String getName() {
        return NAME;
    }
    
    private Object toObject(FileAttrs attrs) {
        if (attrs == null) return null;
        Map<String,Object> m = new HashMap<String,Object>();
        if (attrs.attributes != null) m.putAll(attrs.attributes);
        if ((attrs.flags & ATTR_SIZE) != 0) {
            m.put("Size", Long.valueOf(attrs.size));
        }
        if ((attrs.flags & ATTR_UIDGID) != 0) {
            m.put("UID", Integer.valueOf(attrs.uid));
            m.put("GID", Integer.valueOf(attrs.gid));
        }
        if ((attrs.flags & ATTR_PERMISSIONS) != 0) {
            m.put("Permissions", Integer.valueOf(attrs.permissions));
        }
        if ((attrs.flags & ATTR_ACMODTIME) != 0) {
            m.put("ATime", Long.valueOf(attrs.atime));
            m.put("MTime", Long.valueOf(attrs.mtime));
        }
        return m;
    }
    
    @SuppressWarnings("unchecked")
    private FileAttrs toFileAttrs(Object o) {
        if (o == null) return null;
        Map<String,Object> m = new HashMap<String,Object>((Map<String,Object>)o);
        int flags = 0;
        long size = 0;
        int uid = 0;
        int gid = 0;
        int permissions = 0;
        long atime = 0;
        long mtime = 0;
        Number n = (Number)m.remove("Size");
        if (n != null) {
            size = n.longValue();
            flags |= ATTR_SIZE;
        }
        Number n1 = (Number)m.remove("UID");
        Number n2 = (Number)m.remove("GID");
        if (n1 != null && n2 != null) {
            uid = n1.intValue();
            gid = n2.intValue();
            flags |= ATTR_UIDGID;
        }
        n = (Number)m.remove("Permissions");
        if (n != null) {
            permissions = n.intValue();
            flags |= ATTR_PERMISSIONS;
        }
        n1 = (Number)m.remove("ATime");
        n2 = (Number)m.remove("MTime");
        if (n1 != null && n2 != null) {
            atime = n1.longValue();
            mtime = n2.longValue();
            flags |= ATTR_ACMODTIME;
        }
        return new FileAttrs(flags, size, uid, gid, permissions, atime, mtime, m);
    }
    
    private FileHandle toFileHandle(Object o) {
        if (o == null) return null;
        return new FileHandle(o.toString());
    }
    
    @SuppressWarnings("unchecked")
    private DirEntry[] toDirEntryArray(Object o) {
        if (o == null) return null;
        Collection<Map<String,Object>> c = (Collection<Map<String,Object>>)o;
        DirEntry[] res = new DirEntry[c.size()];
        int i = 0;
        for (Map<String,Object> m : c) {
            res[i++] = new DirEntry(
                    (String)m.get("FileName"),
                    (String)m.get("LongName"),
                    toFileAttrs(m.get("Attrs")));
        }
        return res;
    }
}

Back to the top