public
Last active

A script to remove erroneous duplicate ACL permissions on Mac OS 10.6

  • Download Gist
.rvmrc
Shell
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
#!/usr/bin/env bash
 
# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory
 
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
# Only full ruby name is supported here, for short names use:
# echo "rvm use 1.8.7" > .rvmrc
environment_id="ruby-1.8.7-p249@aclfix"
 
# Uncomment the following lines if you want to verify rvm version per project
# rvmrc_rvm_version="1.14.3 (latest)" # 1.10.1 seams as a safe start
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
# return 1
# }
 
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
if [[ $- == *i* ]] # check for interactive shells
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
rvm --create use "$environment_id" || {
echo "Failed to create RVM environment '${environment_id}'."
return 1
}
fi
 
# If you use bundler, this might be useful to you:
# if [[ -s Gemfile ]] && {
# ! builtin command -v bundle >/dev/null ||
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
# }
# then
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
# gem install bundler
# fi
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
# then
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
# fi
Gemfile
Ruby
1 2 3
source 'https://rubygems.org'
 
gem 'escape'
aclfix
Ruby
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
#!/usr/bin/env ruby
 
# == Synopsis
# A script to remove erroneous duplicate ACL permissions on Mac OS 10.6
#
# == Usage
# aclfix.rb [options] source_file
#
# For help use: aclfix.rb -h
#
# == Options
# -h, --help Displays help message
# -v, --version Display the version, then exit
# -q, --quiet Output as little as possible, overrides verbose
# -V, --verbose Verbose output
# -R, --recursive Recursively processes directories
#
# == Copyright
# Copyright (c) 2011 Tim van der Horst. Licensed under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
# Application skeleton sourced from here: http://blog.toddwerth.com/entries/show/5
 
require 'rubygems'
require 'escape'
 
require 'optparse'
require 'rdoc/usage'
require 'ostruct'
require 'date'
 
 
class App
VERSION = "1.0.5"
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
# Set defaults
@options = OpenStruct.new
@options.verbose = false
@options.quiet = false
@options.recursive = false
end
# Parse options, check arguments, then process the command
def run
if parsed_options? && arguments_valid?
puts "Start at #{DateTime.now}" if @options.verbose
output_options if @options.verbose # [Optional]
process_arguments
process_command
puts "Finished at #{DateTime.now}" if @options.verbose
else
output_usage
end
end
protected
def parsed_options?
# Specify options
opts = OptionParser.new
opts.on('-R', '--recursive') { @options.recursive = true }
opts.on('-v', '--version') { output_version ; exit 0 }
opts.on('-h', '--help') { output_help }
opts.on('-V', '--verbose') { @options.verbose = true }
opts.on('-q', '--quiet') { @options.quiet = true }
# TO DO - add additional options
opts.parse!(@arguments) rescue return false
process_options
true
end
# Performs post-parse processing on options
def process_options
@options.verbose = false if @options.quiet
end
def output_options
puts "Options:"
@options.marshal_dump.each do |name, val|
puts " #{name} = #{val}"
end
end
# True if required arguments were provided
def arguments_valid?
true
end
# Setup the arguments
def process_arguments
# TO DO - place in local vars, etc
end
def output_help
output_version
RDoc::usage() #exits app
end
def output_usage
RDoc::usage('usage') # gets usage from comments above
end
def output_version
puts "#{File.basename(__FILE__)} version #{VERSION}"
end
def colorize(text, color_code)
"#{color_code}#{text}\e[0m"
end
def red(text); colorize(text, "\e[31m"); end
def green(text); colorize(text, "\e[32m"); end
def bold(text); colorize(text, "\e[1m"); end
def header(file, already_done)
if not already_done and not @options.quiet
puts bold "#{Dir.pwd}/#{file}:"
end
true
end
def process_file(file)
done = false
if File.exists? file
result = `#{Escape.shell_command(["ls", "-led", "--", file])}`
if result.empty?
done = header(file, done)
warn red "ls had an error!"
else
@acl = result.split("\n")[1..-1].collect do |ace|
ace = ace.split(": ", 2)
ace[0] = ace[0].to_i
ace
end
if @acl.length > 1
if @options.verbose
done = header(file, done)
puts "#{@acl.length} ACEs present"
end
@unique = Array.new
@removal = Array.new
@acl.each do |index, rule|
if @unique.include? rule
@removal.push index
else
@unique.push rule
end
end
if @removal.length > 0
if not @options.quiet
done = header(file, done)
puts green "#{@unique.length} unique ACEs present (#{@acl.length} total)"
end
@removal.reverse!
@removal.each do |index|
if system("chmod", "-a#", index.to_s, file)
if @options.verbose
done = header(file, done)
puts "Removing ACE at index #{index}..."
end
else
if not @options.quiet
done = header(file, done)
warn red "Can't remove ACE at index #{index}!"
end
end
end
end
elsif @acl.length == 1
if @options.verbose
done = header(file, done)
puts "Only one ACE"
end
else
if @options.verbose
done = header(file, done)
puts "No ACL permissions"
end
end
if File.directory? file and @options.recursive
Dir.chdir(file) do
Dir.entries('.').each do |entry|
if entry != '.' and entry != '..'
process_file(entry)
end
end
end
end
end
elsif not @options.quiet
done = header(file, done)
warn red 'No such file or directory'
end
end
def process_command
@arguments.each do |file|
process_file(file)
end
end
end
 
app = App.new(ARGV, STDIN)
app.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.