Click here to Skip to main content
15,888,733 members
Articles / Programming Languages / C++

A Namespace Extension Toolkit

Rate me:
Please Sign up or sign in to vote.
4.78/5 (10 votes)
21 Mar 2006CPOL12 min read 84.8K   1.7K   42  
This article shows you how to build your own Windows Explorer interfaces to custom data.
/*
  Galaxy Filesystem Toolkit
  Copyright (C) 2005 Chad Yoshikawa
  http://www.ececs.uc.edu/~yoshikco
  yoshikco@ececs.uc.edu

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package galaxy.nfs;

import galaxy.java.*;
import galaxy.nfs.mount.*;
import galaxy.nfs.v2.*;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

import java.util.Date;
import java.util.Vector;
import java.util.Properties;
import java.io.IOException;
import java.net.*;

// Do not reference the timing code in the release version
// import com.vladium.utils.timing.ITimer;
// import com.vladium.utils.timing.TimerFactory;

import org.acplt.oncrpc.*;


/**
 * A collection of printing/conversion/path methods
 * for the NFS/Mount data types
 *
 * @author <a href="mailto:yoshikco@ececs.uc.edu">Chad Yoshikawa</a>
 * @version 1.0
 */
public class Util implements nfs, mount {

    // public static ITimer timer =  TimerFactory.newTimer ();
    public static Vector myMessages = new Vector();

    ////////////////////////////////////////////////////////////////////
    // Printing methods
    ////////////////////////////////////////////////////////////////////

    /// Print out the directory entries to the screen
    public static void PrintDirEntries(entry entries) {
        entry current =  entries;
        while (current!=null) {
            PrintDirEntry(current);
            current = current.nextentry;
        }
    }

    public static void PrintDirEntry(entry e) {
        System.out.println("Directory entry name = "+e.name.value);
        System.out.println("Directory entry id = "+e.fileid);
    }

    /// Print the list of currently mounted filesystems
    public static void PrintMountList(mountlist list) {
        mountlist current = list;

        while (current.value!=null) {
            PrintMount(current.value);
            current = current.value.ml_next;
        }
    }

    public static void PrintMount(mountbody mount) {
        System.out.println("name = "+mount.ml_hostname.value);
        System.out.println("dirpath = "+mount.ml_directory.value);
    }


    /// Print the list of possible exports
    public static void PrintExportList(exports list) {
        exports current = list;

        while (current.value!=null) {
            PrintExport(current.value);
            current = current.value.ex_next;
        }
    }

    public static void PrintExport(exportnode item) {
        String path = item.ex_dir.value;
        System.out.println("Dirpath = "+path);

        PrintGroups(item.ex_groups);
    }

    public static void PrintGroups(groups g) {
        groups current = g;
        while (current.value!=null) {
            PrintGroup(current.value);
            current = current.value.gr_next;
        }
    }

    public static void PrintGroup(groupnode item) {
        if (item.gr_name.value!=null) 
            System.out.println("Group = "+item.gr_name.value);
    }


    ////////////////////////////////////////////////////////////////////
    // Conversion methods
    ////////////////////////////////////////////////////////////////////

    /**
     * Convert a filehandle returned by mount into one that nfs can use.
     * (just an array copy)
     *
     * @param dir a <code>fhandle</code> value
     * @return a <code>nfs_fh</code> value
     */
    public static nfs_fh ConvertMountHandleToNfsHandle(fhandle dir) {
        nfs_fh ret = new nfs_fh();
        ret.data = new byte[dir.value.length];
        // Copy the filehandle, although we really shouldn't have to do this.
        // The filehandles should be the same type if we run things through 
        System.arraycopy(dir.value,0,ret.data,0,dir.value.length);
        return (ret);
    }

    /**
     * Convert milliseconds since epoch to 100 nanosecond ticks
     * since Jan 1, 1601 12AM
     *
     * @param time number of usec since epoch
     * @return ticks since Jan 1, 1601 12AM
     */
    public static long ConvertTime(nfstime time) {
        long ticks=0;
        long usec = time.seconds*1000000 + time.useconds;
        long epoch_difference = 11644473600000L; //ms
        usec+=epoch_difference*1000;
        ticks = usec*(10); // convert from usec to 100 ns ticks
        return (ticks);
    }


    public static nfstime Now() {
        nfstime ret = new nfstime();
        Date now = new Date();
        long ms = now.getTime();

        ret.seconds = (int)(ms / 1000);
        ret.useconds = (int)((ms - (ret.seconds*1000))*1000);

        return (ret);
    }

    public static String ConvertFileHandleToString(nfs_fh handle) {
        String ret = BytesToString(handle.data);
        return (ret);
    }

    public static String BytesToString(byte[] bs) {
        StringBuffer ret = new StringBuffer(bs.length);
        for (int i = 0; i < bs.length; i++) {
            String hex = Integer.toHexString( /*0x0100 +*/ (bs[i] & 0x00FF)) /*.substring(1)*/;
            ret.append((hex.length() < 2 ? "0" : "") + hex);
        }
        return ret.toString();
    }

    ///////////////////////////////////////////////////////////////////
    // Path related functions
    //////////////////////////////////////////////////////////////////

    /**
     * return everything but the last path component
     *
     * @param path a <code>String</code> value
     * @return a <code>String</code> value
     */
    public static String GetPath(String path) {
        String ret;
        Assert("GetPath",path.startsWith("/"),"Path "+path+" doesn't start with a slash");
        ret = RemoveSlashFromEndOfPath(path);
        int index = ret.lastIndexOf('/');
        if (index == (-1)) {
            // must have been just the path '/' passed to us
            ret = "/";
        } else {
            ret = ret.substring(0,index);
        }
        return ret;
    }

    public static String GetFilename(String path) {
        String ret;
        Assert("GetFilename",path.startsWith("/"),"Path "+path+" doesn't start with a slash");
        ret = RemoveSlashFromEndOfPath(path);
        int index = ret.lastIndexOf('/');
        if (index == (-1)) {
            // must have been just the path '/' passed to us
            ret = "";
        } else {
            ret = ret.substring(index+1,ret.length());
        }
        return (ret);
    }

    public static String RemoveSlashFromStartOfPath(String path) {
        int i = 0;
        int length = path.length();
        while ((i<length) && (path.charAt(i) == '/')) {
            i++;
        }
        if (i==(length)) {
            return "";
        } else {
            return path.substring(i,length);
        }
    }

    public static String RemoveSlashFromEndOfPath(String path) {
        int i = path.length() -1;
        while ((i>=0) && (path.charAt(i) == '/')) {
            i--;
        }

        if (i==(-1)) {
            return "";
        } else {
            return path.substring(0,i+1);
        }
    }

    // make it so the path has a single slash at the end
    public static String AddSlashToEndOfPath(String path) {
        String ret;
        ret = RemoveSlashFromEndOfPath(path) + "/";
        return (ret);
    }

    public static String CatPath(String root, String path) {
        String ret;

        ret = AddSlashToEndOfPath(root) + RemoveSlashFromStartOfPath(path);

        return (ret);
    }


    public static String[] RemoveSpecialFiles(String[] files) {
        Vector vec = new Vector();
        for (int i=0;i<files.length;i++) {
            if (files[i].equals(".") || files[i].equals("..")) {
                continue;
            }
            vec.add(files[i]);
        }
        String[] ret = new String[vec.size()];
        vec.copyInto(ret);
        return (ret);
    }
    ///////////////////////////
    // Debugging Methods
    ///////////////////////////
    public static void Warn(String method, String arg, Exception ex) {
        Warn(method,"Error accessing "+arg+":"+ex);
    }


    public static void Warn(String method, String msg) {
        Driver.logger.warn("NFSFileSystem."+method+" - "+msg);
    }


    public static void Debug(String method, String msg) {
        Driver.logger.debug("NFSFileSystem."+method+" - "+msg);
    }

    public static void Assert(String method, boolean condition, String msg) {
        if (!condition) {
            Debug(method,"Assertion failed "+msg);
            // halt?
        }
    }

    static class TimeStampedMessage {
        double Time;
        String Message;
        public TimeStampedMessage(double time, String msg) {
            Time = time;
            Message = msg;
        }
    }


    /**
     * Log a message and timestamp it.
     *
     * @param msg a <code>String</code> value
     * @return a <code>double</code> value
     */
    public static double TraceTime(String msg) {
        double ret = (double)System.currentTimeMillis();// timer.getRawTime();
        TimeStampedMessage tsm  = new TimeStampedMessage(ret,msg);

        // Save the message and the time somewhere
        myMessages.add(tsm);
        return (ret);
    }

    /**
     * Print all the trace messages logged thus far, and clear the log
     *
     */
    public static void PrintTrace() {
        synchronized(myMessages) {
            for (int i=0;i<myMessages.size();i++) {
                TimeStampedMessage tsm = (TimeStampedMessage)myMessages.get(i);
                Driver.logger.info(tsm.Time+" "+tsm.Message);
            }
            myMessages.clear();
        } // end sync
    } // end method

    /**
     * Clear the trace log.
     *
     */
    public static void ClearTrace() {
        myMessages.clear();
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions