Home
Authors History
Donations Cats
News
News FVWM Logo
Logo Competition
Features
Features
Download
Download
Icons and Sounds
Screenshots
Desktop Screenshots Menu Screenshots
Window Decor Screenshots Vectorbuttons
Documentation
Documentation Man pages
FAQ Developer Info
Mailing Lists
Mailing Lists
Links
Links
Customize
 
 Official FVWM Home Page plain theme

FVWM - Perl library - General::FileSystem


General::FileSystem

Section: FVWM Perl library (3)
Updated: 2005-08-24
Source: General/FileSystem.pm
This page contents - Return to main index
 

NAME

General::FileSystem - file system specific functions  

SYNOPSIS

  use General::FileSystem "-die", "-debug";  # die on errors

  eval {
    makePath("/tmp/my-own/dir");

    my $fileContentRef = loadFile("/etc/issue");
    saveFile("/tmp/my-own/dir/issue", $fileContentRef);

    # This is equivalent to the previous two lines, but optimized
    copyFile("/etc/issue", "/tmp/my-own/dir/issue");

    makeDir("/tmp/my-own/dir2", 0711);
    copyFile("/etc/issue", "/tmp/my-own/dir2/issue");
    moveFile("/tmp/my-own/dir2/issue", "/tmp/my-own/dir2/issue2");
    removeFile("/tmp/my-own/dir2/issue2");
    cleanDir("/tmp/my-own/dir2"); # no effect, it's empty already

    removeDir("/tmp/my-own");
  };
  if ($@) {
    print "File System Error: $@";
  };

or just:

  use General::FileSystem;
  copyFile("origin.txt", "backup.txt");
 

DESCRIPTION

This package contains common file operation functions:

loadFile, saveFile, appendFile, removeFile, copyFile, moveFile, makeDir, makePath, cleanDir, removeDir, copyDir, moveDir, listFileNames, findFile, findExecutable, defaultDirPerm, preserveStat, parsePath, getCwd.

On fatal file system errors all functions call the error handler, that may throw exception (die), issue a warning or quietly return undef. You may control this by passing one of the arguments -die, -warn or -quiet in use or by setting $ERROR_HANDLER to one of these values (don't specify a dash in this case).  

REQUIREMENTS

Cwd, File::Basename, File::Copy.  

FUNCTIONS

 

loadFile

usage
  $contentRef = loadFile($fileName)
description
Loads file with given file-name from local filesystem.
parameters
  * fileName - name of the file to be loaded.
returns
Reference to file content string on success, otherwise either dies or warns and returns undef as configured.
 

saveFile

description
Saves file-content to local filesystem with given file-name.
usage
  saveFile($fileName, \$fileContent);
parameters
  * fileName - name of the file to be saved into
  * fileContentRef - reference to file-content string
  * createSubdirs - optional flag (default is 0 - don't create subdirs)
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

appendFile

description
Appends file-append-content to local filesystem with given file-name.
usage
  appendFile($fileName, \$fileAppendContent);
parameters
  * fileName - name of the file to be saved into
  * fileAppendContentRef - reference to file-append-content string
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

removeFile

description
Removes all files from given directory.
usage
  removeFile($fileName);
parameters
  * fileName - name of the file to be deleted
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

makeDir

description
Removes all files from given directory.
usage
  makeDir($PREVIEW_DIR);
parameters
  * directory to make
  * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

makePath

description
Removes all files from given directory.
usage
  makePath($PUBLISH_DIR);
parameters
  * path to make
  * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

copyFile

description
Copies a file to another location.
usage
  copyFile($from, $to);
parameters
  * file name to copy from
  * file name to copy to
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

moveFile

description
Moves (or renames) a file to another location.
usage
  moveFile($from, $to);
parameters
  * file name to move from
  * file name to move to
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

cleanDir

description
Removes all files from given directory.
usage
  cleanDir($PREVIEW_DIR);
parameters
  * directory to clean
  * optional flag:
    0 - don't go recursively, unlink files in first level only
    1 - recursively clean subdirs (default)
    2 - unlink subdirs
    3 - unlink given directory
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

removeDir

description
Entirely removes given directory and its content (if any). This is an alias to cleanDir(3).
usage
  removeDir($TMP_DIR);
parameters
  * directory to clean
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

copyDir

description
Recursively copies all files and subdirectories inside given directory to new location.

Destination directory must not exist. Use: "trap { removeDir($dest); };" to remove it before copying.

usage
  copyDir($dirFrom, $dirTo);
parameters
  * source directory to copy
  * destination directory to copy to (may not exist)
  * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

moveDir

description
Moves (or actually renames) a directory to another location.

Destination directory must not exist. Use: "trap { removeDir($dest); };" to remove it before copying.

usage
  moveDir($dirFrom, $dirTo);
parameters
  * source directory to move from
  * destination directory to move to (must not exist)
returns
1 on success, otherwise either dies or warns and returns undef as configured.
 

listFileNames

description
Returns the file names in the given directory including all types of files (regular, directory, link, other), not including '.' and '..' entries.
usage
  # mini file lister
  $dir = '/home/ftp';
  foreach my $file (@{listFileNames($dir)}) {
    print "File $file\n" if -f "$dir/$file";
    print "Dir  $file\n" if -d "$dir/$file";
  }
parameters
  * directory to list (or array ref of directories)
  * optional flag, 1 means work recursively, the default is 0
returns
Array ref of scalars (file names) on success. Otherwise either dies or warns and returns undef as configured.
 

findFile

description
Searches for the given file in the given directories.

Returns the fully qualified file name.

usage
  my $gtkrc = findFile(".gtkrc", [$home, "$home/.gnome"]);
parameters
  * file name to search for
  * array ref of directories to search in
returns
File name with full path if found, or undef if not found.
 

findExecutable

description
Searches for the given executable file in the directories that are in the environmebt variable $PATH or in the additional parameter.

Returns the fully qualified file name.

usage
  my $gzipExe = findExecutable("gzip", ["/usr/gnu/bin", "/gnu/bin"]);
parameters
  * file name to search for (only executables are tested)
  * optional array ref of directories to search in
returns
File name with full path if found, or undef if not found.
 

defaultDirPerm

description
This functions changes default directory permissions, used in "makeDir", "makePath", "copyDir" and "moveDir" functions.

The default of this package is 0775.

If no parameters specified, the current value is returned.

usage
 defaultDirPerm(0700);
parameters
  * optional default directory permission (integer)
returns
Previous value.
 

preserveStat

description
This functions changes behavior of "copyFile" and "copyDir" functions. If 0 is given as a parameter stats will not be preserved.

TODO: specify values for diferent preserves:

  0 nothing
  1 mode   file mode  (type and permissions)
  2 uid    numeric user ID of file's owner
  4 gid    numeric group ID of file's owner
  8 atime  last access time since the epoch
 16 mtime  last modify time since the epoch
 32 ctime  inode change time (NOT creation time!) since the epo

The default of this package is 0.

If no parameters specified, nothing is set (only current value is returned).

usage
  preserveStat(1);
parameters
  * optional flag (currently 0 or 1)
returns
Previous value.
 

parsePath

usage
  my ($dirName, $baseName) = parsePath($fileName);
examples
  # in: "/data/projects/magazine"  out: ("/data/projects", "magazine")
  # in: "/magazine"                out: ("", "magazine")
  # in: "dir/"                     out: (dir", "")
  # in: "magazine"                 out: (".", "magazine")

  # in: "c:\projects\magazine"     out: ("c:\projects", "magazine")
  # in: "c:\magazine"              out: ("c:", "magazine")
  # in: "c:magazine"               out: ("c:.", "magazine")
description
Returns a list of 2 scalars: directory name and base name. All unix and dos file names supported.

Note, the rule is this: you can join both scalars using a directory delimiter (slash or backslash) and you will always get the the original (logical) file name.

 

getCwd

usage
  my $cwd = getCwd();
description
Returns the current working directory.
 

BUGS

All global functions and constants in this package should probably be instantiated into a class object. As usual there are pros and cons.  

AUTHOR

Mikhael Goikhman <migo@homemail.com>


 

Index

NAME
SYNOPSIS
DESCRIPTION
REQUIREMENTS
FUNCTIONS
loadFile
saveFile
appendFile
removeFile
makeDir
makePath
copyFile
moveFile
cleanDir
removeDir
copyDir
moveDir
listFileNames
findFile
findExecutable
defaultDirPerm
preserveStat
parsePath
getCwd
BUGS
AUTHOR

This document was created by man2html, using the manual pages.
Time: 00:51:36 GMT, August 27, 2005

Last modified on April 26, 2010