Module Symbiosis::Utils
In: common/lib/symbiosis/utils.rb
Array Utmp EventMachine::Connection ApacheLogger StandardError CrontabFormatError Enumerable IPAddr ::IPAddr Host Range Domain ConfigFile Prosody Tinydns Apache Webalizer Directory IPListDirectory TemplateDirectory Domains Alert Crontab CrontabRecord StateDB Check Runner TCPConnection Test Process BlacklistDB Template Logtail Pattern Ports Blacklist Monitor ConfigFiles Utils Firewall Symbiosis dot/f_0.png

This module has a number of useful methods that are used everywhere.

Methods

Public Instance methods

Allow arbitrary parameters in parent_dir to be retrieved.

  • false is returned if the file does not exist, or is not readable
  • true is returned if the file exists, but is of zero length
  • otherwise the files contents are returned as a string.

This function locks an open filehandle fh using flock.

If the lock is unavailable it raises Errno::ENOLCK.

N.B. This lock is realeased if the filehandle is closed, or if the file itself is subsquently opened and closed.

This function uses the FileUtils mkdir_p command to make a directory. It adds the extra options of :uid and :gid to allow these to be set in one fell swoop.

This has been written to avoid the TOCTTOU race conditions between creating a directory, and chowning it, to make sure that we don‘t accidentally chown a file on the end of a symlink

It returns the name of the directory created.

If a numeric argument is given, it is rounded to the nearest whole number, and returned as an Integer.

If a string is given, the method attempts to parse it. The quota can be a decimal, followed optionally by a space, and optionally by a "prefix". Prefixes it understands are:

 * k, M, G, T, P as powers of 10
 * ki, Mi, Gi, Ti, Pi as powers of 2.

The answer is given as an Integer.

An argument error is given if the string cannot be parsed, or the argument is neither a Numeric or String object.

This function generates a string of random numbers and letters from the sequence A-Z, a-z, 0-9 minus 0, O, o, 1, I, i, l.

This method opens a file in a safe manner, avoiding symlink attacks and TOCTTOU race conditions.

The mode can be a string or an integer, but must not be "w" or "w+", or have File::TRUNC set, to avoid truncating the file on opening.

opts is an options hash in which the uid, gid, and mode file bits can be specified.

  • :uid is the User ID, e.g. 1000.
  • :gid is the Group ID, e.g. 1000.
  • :mode is the permissions, e.g. 0644.

By default mode is set using the current umask.

Records a parameter.

  • true is stored as an empty file
  • false or nil causes the file to be removed, if it exists.
  • Anything else is converted to a string and stored.

If a file is created, or written to, then the permissions are set such that the file is owned by the same owner/group as the parent_dir, and readable by everyone, but writable only by the owner (0644).

Directories owned by system users/groups will not be written to.

Show either the manual, or the brief usage text.

Many of our utility scripts have integrated documentation at their head.

This method will show the manual to the caller.

Many of our utility scripts have integrated documentation at their head.

This method will show brief usage-information to the caller.

This function removes any lock set by lock() on a filehandle, fh.

[Validate]