AOLserver

# File Manipulation Functions

$Header: /cvsroot/aolserver/aolserver.com/docs/devel/tcl/api/file.html,v 1.1 2002/03/07 19:15:35 kriston Exp $

## ns_chmod

Change a file's access permissions.

### Syntax

ns_chmod filename mode

### Description

ns_chmod changes the specified file's permissions to mode, in the same manner as the Unix chmod(1) command-line utility.

## ns_cp

Copy one file to another.

### Syntax

ns_cp -preserve file1 file2

### Description

ns_cp copies the contents of file1 to file2, just like the Unix "cp" command. The default directory is the home directory for the server.

If -preserve is specified, the copied file will retain the creation time, modification time, owner, and mode of the original file, just like the Unix "cp -p" command.

## ns_cpfp

Copy a specified number of bytes from one file to another.

### Syntax

ns_cpfp fileid1 fileid2 ?nbytes?

### Description

ns_cpfp copies information from one file (fileid1) to another (fileid2). If you specify a number of bytes in the nbytes argument, only the specified number of bytes will be copied. By default, the entire file is copied.

## ns_ftruncate

Truncate an open file to a specified length.

### Syntax

ns_ftruncate fileid ?length?

### Description

ns_ftruncate causes the open file specified by fileid to have a size of length bytes. If length is not specified, it causes the file to have a size of zero bytes. The file must be open and be a regular file.

## ns_getcsv

Read CSV data from a file.

### Syntax

ns_getcsv fileId varName

### Description

ns_getcsv reads one line of CSV data from the specified file and and sets the variable specified by varName to a list of the elements in that row. Any quotes in the line of data are removed. ns_getcsv returns the number of elements in the row.

If there are no more lines in the file to read, varName does not get set, and ns_getcsv returns -1 as the number of elements in the row.

A CSV file is an ASCII file containing rows of data in the following syntax:

      * Elements in each row must be separated by commas.
      * Each row must end with either \n, \r, or \r\n.
      * Elements that contain commas, newlines, or carriage returns can be
        surrounded by double quotes (e.g., "foo,bar").
      * If an element surrounded by double quotes also contains a double
        quote, you can escape the interior double quote by using two
        double quotes (e.g., "Evander ""Real Deal"" Holyfield"
      * White space that isn't inside double quotes is ignored. For
        example, the line: 'bob, sarah, mary' will result in the list {bob
        sarah mary}.
      * Null entries are okay, either embedded in the line or at the end
        of a line. For example, the line: 'bob,,sarah, ' will result in
        the list: {bob {} sarah {}}.

## ns_gifsize

Determine the width and height of a GIF87 or GIF89 image.

### Syntax

ns_gifsize file

### Description

ns_gifsize returns a list of two elements, the width and height of the GIF file specified by file. An error is generated if the file does not exist or if it is not a GIF file.

## ns_jpegsize

Determine the width and height of a JPEG image.

### Syntax

ns_jpegsize file

### Description

ns_jpegsize returns a list of two elements, the width and height of the JPEG file specified by file. An error is generated if the file does not exist or if it is not a JPEG file.

## ns_link

Create a link.

### Syntax

ns_link ?-nocomplain? filename1 filename2

### Description

ns_link creates a link named filename2 that points to the file specified by filename1. If the link fails, a Tcl error is generated, unless -nocomplain is specified.

## ns_mkdir

Create a directory.

### Syntax

ns_mkdir path

### Description

ns_mkdir creates the directory named PATH, just like the Unix mkdir command. By default, under Unix the directory is created with the file permissions set to 0755 (rwxr-xr-x.). These permissions can be modified by setting the umask parameter for the server.

## ns_mktemp

Return a unique file name based on a template.

### Syntax

ns_mktemp template

### Description

ns_mktemp returns a unique filename based on the template you specify. The string in the template should contain a string with 6 trailing Xs. ns_mktemp will replace the Xs with a letter and the current process id. The letter will be chosen to make the filename unique. For example, ns_mktemp "/tmp/fooXXXXXX" will return something like: "/tmp/fooa000BH".

ns_mktemp is analogous to the Unix C library mktemp function.

## ns_rename

Rename a file.

### Syntax

ns_rename file1 file2

### Description

ns_rename renames the first file (file1) to the file name specified by file2. Make sure that the files and the directories in which the files exist are read/write accessible to the username that's running the AOLserver.

## ns_rmdir

Remove a directory.

### Syntax

ns_rmdir path

### Description

ns_rmdir removes the directory named path, just like the Unix rmdir command. The directory must already be empty.

## ns_rollfile

Roll an arbitrary file.

### Syntax

ns_rollfile file backupMax

### Description

This function rolls the specified file, keeping a number of backup copies up to backupMax.

## ns_symlink

Create a symbolic link.

### Syntax

ns_symlink ?-nocomplain? filename1 filename2

### Description

ns_symlink creates a symbolic link named filename2 that points to the file specified by filename1. If the link fails, a Tcl error is generated, unless -nocomplain is specified.

## ns_unlink

Remove a file.

### Syntax

ns_unlink [-nocomplain] filename

### Description

ns_unlink attempts to remove the file filename. If -nocomplain is not passed in and the removal fails, a Tcl error is generated.

## ns_truncate

Truncate a file to a specified length.

### Syntax

ns_truncate filename ?length?

### Description

ns_truncate causes the file specified by filename to have a size of length bytes. If length is not specified, it causes filename to have a size of zero bytes. The file must exist and be a regular file.

## ns_tmpnam

Generate a temporary file name.

### Syntax

ns_tmpnam

### Description

ns_tmpnam generates a filename that can safely be used for a temporary file. ns_tmpnam calls tmpnam(), and the results will depend on your operating system. On Irix, for example, tmpnam() always generates a file name using the path-prefix defined as P_tmpdir in the header file, which is "/var/tmp/".

Alternatively, you can use the ns_mktemp function to create a unique file name.

## ns_writefp

Write file contents to the connection.

### Syntax

ns_writefp fileid ?nbytes?

### Description

ns_writefp writes the contents of the specified file to the connection. You can specify the number of bytes to be copied in the nbytes argument. By default, the entire file is copied.

### Example

    ns_register_proc GET /sampleWriteFp sampleWriteFp
    proc sampleWriteFp { conn ignore } {
        set file [open "/home/people/juan/machine.txt" r]
        ns_writefp $file
        close $file
    }

## ns_normalizepath

Normalize a path.

### Syntax

ns_normalizepath path

### Description

ns_normalizepath removes any extraneous slashes from the path and resolves "." and ".." references. For example: [ns_normalizepath /dog/cat/../.. /rat/../../dog//mouse/..]

returns: /dog