/*
 * @(#)SecFile.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2002 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 */

import java.io.*;
import java.util.StringTokenizer;
import netscape.security.PrivilegeManager;

/**
 * Extensions to the File class. Run the methods in a Netscape
 * privilege environment.
 *
 * @version 20021029
 * @author Jörg P. M. Haeger
 */
class SecFile extends File2 {
	public SecFile(File parent, String child) {
		super(parent, child);
	}

	public SecFile(String pathname) {
		super(pathname);
	}

	public boolean canRead() {
		if (CDcreator.isNetscapeVM())
		try {
			PrivilegeManager.enablePrivilege(
				"UniversalFileRead");
		}
		catch (Exception e) {
		}
		return super.canRead();
	}

	public boolean isDirectory() {
		if (CDcreator.isNetscapeVM())
		try {
			PrivilegeManager.enablePrivilege(
				"UniversalFileRead");
		}
		catch (Exception e) {
		}
		return super.isDirectory();
	}

	public long length() {
		if (CDcreator.isNetscapeVM())
		try {
			PrivilegeManager.enablePrivilege(
				"UniversalFileRead");
		}
		catch (Exception e) {
		}
		return super.length();
	}

	public File[] listFiles() {
		if (CDcreator.isNetscapeVM())
		try {
			PrivilegeManager.enablePrivilege(
				"UniversalFileRead");
		}
		catch (Exception e) {
		}

		String names[] = list();
		if (names == null)
			return null;

/*		try {
			Process ls = Runtime.getRuntime().exec(
				"ls -l " + getPath());
			BufferedReader in = new BufferedReader(
				new InputStreamReader(
					ls.getInputStream()));
			while (true) {
				String line = in.readLine();
				if (line == null)
					break;
				StringTokenizer tokenizer = new StringTokenizer(
					line, "\t ");

				int mode = 0;
				String modeStr = tokenizer.nextToken();
				if (modeStr.charAt(0) != '-')
					continue;
				for (int i = 1; i <= 9; i++) {
					mode *= 2;
					if (modeStr.charAt(i) != '-')
						mode += 1;
				}

				tokenizer.nextToken();
				tokenizer.nextToken();
				tokenizer.nextToken();
				long size = Long.parseLong(tokenizer.nextToken());
				tokenizer.nextToken();
				tokenizer.nextToken();
				tokenizer.nextToken();
				String name = tokenizer.nextToken();
				System.out.println(
					"<" + name + "> "
					+ Integer.toOctalString(mode) + " " + size);
			}
		}
		catch (Exception e) {
		}
*/
		File[] files = new File[names.length];
		for (int i = 0; i < files.length; i++)
			files[i] = new SecFile(this, names[i]);
		return files;
	}
}
