-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tsirkin,

On 8/16/2009 3:19 AM, Tsirkin Evgeny wrote:
> echo -n > catalina.out

This is not a truncation: this is a file replacement.

In bash, the command '>' truncates a file, it doesn't redirect "nothing"
to it. It's a special case of the IO redirection. This works in other
shells, too, though I'm not sure of exactly which shells support it.

Since there seems to be some confusion between deleting, replacing, and
truncating files, let me spell it out for everyone. The following C code
will truncate a file and leave all existing writers unaffected:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  int fd;
  char *msg;
  int size;

  if(argc < 2) {
    printf("Usage: %s filename\n", argv[0]);

    return 1;
  }

  fd = open(argv[1], O_WRONLY | O_TRUNC);

  if(-1 == fd) {
    size = 7 + strlen(argv[1]);
    msg = malloc(size * sizeof(char));

    snprintf(msg, size, "open: %s", argv[1]);
    perror(msg);

    free(msg);

    return 2;
  }

  if(close(fd)) {
    perror("close");

    return 2;
  }

  return 0;
}

Or, if you prefer Java to C, you can do it this way:

import java.io.IOException;
import java.io.File;
import java.io.RandomAccessFile;

public class Truncate

{
  public static void main(String[] args) throws Exception
  {
    if (args.length != 1)
    {
      System.err.println("Usage: "
                        + Truncate.class.getName() + " filename");

      System.exit(1);
    }

    File f = new File(args[0]);

    if(!f.exists())
    {
      System.err.println(args[0] + ": no such file or directory");

      System.exit(2);
    }

    try
    {
      new RandomAccessFile(f, "rw").setLength(0);
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();

      System.exit(2);
    }

    System.exit(0);
  }
}

The Java version is somewhat more complicated than necessary, but I
wanted both versions to act the same.

These source files do not erase the files, nor do they delete them. They
open them for writing and explicitly truncate the existing contents. The
directory entry remains in-tact. Any live file descriptors pointing to
the existing file will continue to work. I encourage the curious to
simply try it: these sources will truncate your catalina.out and Tomcat
will be perfectly content to continue to log to that file, and you can
see the messages and everything.

Hopefully, we can put this issue to bed, now.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqSteAACgkQ9CaO5/Lv0PCKrQCdHiwA09L85cQ419EIj6sGF8TJ
YiQAn3913vjUQ9BLjXOCO9+fh0QR9yig
=BzfB
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to