Utility functions.
- assert_valid_app_root
- assert_valid_directory
- assert_valid_file
- assert_valid_groupname
- assert_valid_username
- marshal_exception
- normalize_path
- print_exception
- unmarshal_exception
Assert that app_root is a valid Ruby on Rails application root. Raises ArgumentError if that is not the case.
[ show source ]
# File lib/passenger/utils.rb, line 55
55: def assert_valid_app_root(app_root)
56: assert_valid_directory(app_root)
57: assert_valid_file("#{app_root}/config/environment.rb")
58: end
Assert that path is a directory. Raises ArgumentError if it isn‘t.
[ show source ]
# File lib/passenger/utils.rb, line 61
61: def assert_valid_directory(path)
62: if !File.directory?(path)
63: raise ArgumentError, "'#{path}' is not a valid directory."
64: end
65: end
Assert that path is a file. Raises ArgumentError if it isn‘t.
[ show source ]
# File lib/passenger/utils.rb, line 68
68: def assert_valid_file(path)
69: if !File.file?(path)
70: raise ArgumentError, "'#{path}' is not a valid file."
71: end
72: end
Assert that groupname is a valid group name. Raises ArgumentError if that is not the case.
[ show source ]
# File lib/passenger/utils.rb, line 83
83: def assert_valid_groupname(groupname)
84: # If groupname does not exist then getgrnam() will raise an ArgumentError.
85: groupname && Etc.getgrnam(groupname)
86: end
Assert that username is a valid username. Raises ArgumentError if that is not the case.
[ show source ]
# File lib/passenger/utils.rb, line 76
76: def assert_valid_username(username)
77: # If username does not exist then getpwnam() will raise an ArgumentError.
78: username && Etc.getpwnam(username)
79: end
[ show source ]
# File lib/passenger/utils.rb, line 88
88: def marshal_exception(exception)
89: data = {
90: :message => exception.message,
91: :class => exception.class.to_s,
92: :backtrace => exception.backtrace
93: }
94: if exception.is_a?(InitializationError)
95: data[:is_initialization_error] = true
96: if exception.child_exception
97: data[:child_exception] = marshal_exception(exception.child_exception)
98: end
99: else
100: begin
101: data[:exception] = Marshal.dump(exception)
102: rescue ArgumentError, TypeError
103: e = UnknownError.new(exception.message, exception.class.to_s,
104: exception.backtrace)
105: data[:exception] = Marshal.dump(e)
106: end
107: end
108: return Marshal.dump(data)
109: end
Return the absolute version of path. This path is guaranteed to to be "normal", i.e. it doesn‘t contain stuff like ".." or "/", and it correctly respects symbolic links.
Raises SystemCallError if something went wrong. Raises ArgumentError if path is nil.
[ show source ]
# File lib/passenger/utils.rb, line 46
46: def normalize_path(path)
47: raise ArgumentError, "The 'path' argument may not be nil" if path.nil?
48: return Pathname.new(path).realpath.to_s
49: rescue Errno::ENOENT => e
50: raise ArgumentError, e.message
51: end
Print the given exception, including the stack trace, to STDERR.
current_location is a string which describes where the code is currently at. Usually the current class name will be enough.
[ show source ]
# File lib/passenger/utils.rb, line 142
142: def print_exception(current_location, exception)
143: if !exception.is_a?(SystemExit)
144: STDERR.puts(exception.backtrace_string(current_location))
145: STDERR.flush
146: end
147: end
[ show source ]
# File lib/passenger/utils.rb, line 111
111: def unmarshal_exception(data)
112: hash = Marshal.load(data)
113: if hash[:is_initialization_error]
114: if hash[:child_exception]
115: child_exception = unmarshal_exception(hash[:child_exception])
116: else
117: child_exception = nil
118: end
119:
120: case hash[:class]
121: when AppInitError.to_s
122: exception_class = AppInitError
123: when FrameworkInitError.to_s
124: exception_class = FrameworkInitError
125: else
126: exception_class = InitializationError
127: end
128: return exception_class.new(hash[:message], child_exception)
129: else
130: begin
131: return Marshal.load(hash[:exception])
132: rescue ArgumentError, TypeError
133: return UnknownError.new(hash[:message], hash[:class], hash[:backtrace])
134: end
135: end
136: end