Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb8ff40523aa9025d4d32d341b613b35406cdc4a (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
/*******************************************************************************
 * Copyright (c) 2007 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 com.windriver.tcf.rse.ui;

import org.eclipse.rse.core.subsystems.AbstractResource;
import org.eclipse.rse.services.files.IHostFile;

import com.windriver.tcf.api.services.IFileSystem;

public class TCFFileResource extends AbstractResource implements IHostFile {
    
    private final ITCFFileService service;
    private String parent;
    private String name;
    private final IFileSystem.FileAttrs attrs;
    private final boolean root;
    
    public TCFFileResource(ITCFFileService service, String parent, String name,
            IFileSystem.FileAttrs attrs, boolean root) {
        if (name == null) {
            int i = parent.lastIndexOf('/');
            if (i > 0) {
                name  = parent.substring(i + 1);
                parent = parent.substring(0, i);
            }
            else if (i == 0) {
                name  = parent.substring(i + 1);
                parent = "/";
            }
            else {
                name = parent;
                parent = null;
            }
        }
        this.service = service;
        this.parent = parent;
        this.name = name;
        this.attrs = attrs;
        this.root = root;
    }
    
    private String toLocalPath(String path) {
        if (path.length() > 1 && path.charAt(1) == ':') {
            return path.replace('/', '\\');
        }
        else {
            return path.replace('\\', '/');
        }
    }

    public boolean canRead() {
        return attrs != null && service.canRead(attrs);
    }

    public boolean canWrite() {
        return attrs != null && service.canWrite(attrs);
    }

    public boolean exists() {
        return attrs != null;
    }

    public synchronized String getAbsolutePath() {
        if (root) return toLocalPath(name);
        if (parent.endsWith("/")) return toLocalPath(parent + name);
        return toLocalPath(parent + '/' + name);
    }

    public long getModifiedDate() {
        if (attrs == null) return 0;
        if ((attrs.flags & IFileSystem.ATTR_ACMODTIME) == 0) return 0;
        return attrs.mtime;
    }

    public synchronized String getName() {
        return toLocalPath(name);
    }

    public synchronized String getParentPath() {
        return toLocalPath(parent);
    }

    public long getSize() {
        if (attrs == null) return 0;
        if ((attrs.flags & IFileSystem.ATTR_SIZE) == 0) return 0;
        return attrs.size;
    }

    public boolean isArchive() {
        return false;
    }

    public boolean isDirectory() {
        if (attrs == null) return false;
        return attrs.isDirectory();
    }

    public boolean isFile() {
        if (attrs == null) return false;
        return attrs.isFile();
    }

    public synchronized boolean isHidden() {
        return name.startsWith(".");
    }

    public synchronized boolean isRoot() {
        return root;
    }

    public synchronized void renameTo(String path) {
        path = path.replace('\\', '/');
        if (path.equals("/")) {
            parent = name = "/";
            return;
        }
        assert !path.endsWith("/");
        int i = path.lastIndexOf('/');
        parent = path.substring(0, i);
        name = path.substring(i + 1);
    }
}

Back to the top