PageRenderTime 19ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-plugin-utils/src/main/java/org/hudsonci/utils/tasks/Chmod.java

http://github.com/hudson/hudson
Java | 92 lines | 52 code | 10 blank | 30 comment | 10 complexity | c15f1400c5e1302554b8a878fa50abd4 MD5 | raw file
 1/**
 2 * The MIT License
 3 *
 4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
 5 *
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25package org.hudsonci.utils.tasks;
26
27import hudson.FilePath;
28import hudson.Functions;
29import hudson.os.PosixAPI;
30import hudson.remoting.VirtualChannel;
31import hudson.util.jna.GNUCLibrary;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.io.File;
36import java.io.IOException;
37
38/**
39 * Change file(s) permissions on remote node.
40 *
41 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
42 * @since 2.1.0
43 */
44public class Chmod
45    implements FilePath.FileCallable<Void>
46{
47    private static final long serialVersionUID = 1L;
48
49    private static final Logger log = LoggerFactory.getLogger(Chmod.class);
50
51    private final int mode;
52
53    public Chmod(final int mode) {
54        this.mode = mode;
55    }
56
57    public Void invoke(final File file, final VirtualChannel channel) throws IOException {
58        if (!Functions.isWindows()) {
59            process(file);
60        }
61        return null;
62    }
63
64    private void process(final File file) {
65        assert file != null;
66
67        if (file.isFile()) {
68            if (Functions.isMustangOrAbove()) {
69                if (!file.setExecutable(true, false)) {
70                    log.error("Failed to chmod: {}", file);
71                }
72            }
73            else {
74                try {
75                    GNUCLibrary.LIBC.chmod(file.getAbsolutePath(), mode);
76                }
77                catch (LinkageError e) {
78                    // if JNA is unavailable, fall back
79                    PosixAPI.get().chmod(file.getAbsolutePath(), mode);
80                }
81            }
82        }
83        else {
84            File[] children = file.listFiles();
85            if (children != null) {
86                for (File child : children) {
87                    process(child);
88                }
89            }
90        }
91    }
92}