/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. package org.hudsonci.utils.tasks;
  25. import hudson.FilePath;
  26. import hudson.Functions;
  27. import hudson.os.PosixAPI;
  28. import hudson.remoting.VirtualChannel;
  29. import hudson.util.jna.GNUCLibrary;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import java.io.File;
  33. import java.io.IOException;
  34. /**
  35. * Change file(s) permissions on remote node.
  36. *
  37. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  38. * @since 2.1.0
  39. */
  40. public class Chmod
  41. implements FilePath.FileCallable<Void>
  42. {
  43. private static final long serialVersionUID = 1L;
  44. private static final Logger log = LoggerFactory.getLogger(Chmod.class);
  45. private final int mode;
  46. public Chmod(final int mode) {
  47. this.mode = mode;
  48. }
  49. public Void invoke(final File file, final VirtualChannel channel) throws IOException {
  50. if (!Functions.isWindows()) {
  51. process(file);
  52. }
  53. return null;
  54. }
  55. private void process(final File file) {
  56. assert file != null;
  57. if (file.isFile()) {
  58. if (Functions.isMustangOrAbove()) {
  59. if (!file.setExecutable(true, false)) {
  60. log.error("Failed to chmod: {}", file);
  61. }
  62. }
  63. else {
  64. try {
  65. GNUCLibrary.LIBC.chmod(file.getAbsolutePath(), mode);
  66. }
  67. catch (LinkageError e) {
  68. // if JNA is unavailable, fall back
  69. PosixAPI.get().chmod(file.getAbsolutePath(), mode);
  70. }
  71. }
  72. }
  73. else {
  74. File[] children = file.listFiles();
  75. if (children != null) {
  76. for (File child : children) {
  77. process(child);
  78. }
  79. }
  80. }
  81. }
  82. }