All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperic.sigar.cmd.Watch Maven / Gradle / Ivy

/*
 * Copyright (c) 2006-2007 Hyperic, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.hyperic.sigar.cmd;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Date;

import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.DirStat;
import org.hyperic.sigar.FileInfo;
import org.hyperic.sigar.FileWatcher;
import org.hyperic.sigar.FileWatcherThread;
import org.hyperic.sigar.ProcFileMirror;

/**
 * Watch a file or directory displaying attribute changes.
 */
public class Watch {

    private static void printHeader(Sigar sigar, FileInfo info)
        throws SigarException {

        String file = info.getName();
        FileInfo link = sigar.getLinkInfo(file);

        if (link.getType() == FileInfo.TYPE_LNK) {
            try {
                System.out.println(file + " -> " +
                                   new File(file).getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println(link.getTypeChar() + 
                           info.getPermissionsString() + "\t" +
                           info.getUid() + "\t" + info.getGid() + "\t" +
                           info.getSize() + "\t" +
                           new Date(info.getMtime()) + "\t" +
                           file);

        if (info.getType() == FileInfo.TYPE_DIR) {
            info.enableDirStat(true);

            DirStat stats = sigar.getDirStat(file);
            System.out.println("   Files......." + stats.getFiles()); 
            System.out.println("   Subdirs....." + stats.getSubdirs()); 
            System.out.println("   Symlinks...." + stats.getSymlinks()); 
            System.out.println("   Chrdevs....." + stats.getChrdevs()); 
            System.out.println("   Blkdevs....." + stats.getBlkdevs()); 
            System.out.println("   Sockets....." + stats.getSockets()); 
            System.out.println("   Total......." + stats.getTotal());
            System.out.println("   Disk Usage.." + stats.getDiskUsage());
        }
    }

    private static void add(Sigar sigar,
                            FileWatcher watcher,
                            String file,
                            boolean recurse)
        throws SigarException {

        FileInfo info = watcher.add(file);
        printHeader(sigar, info);

        if (!recurse) {
            return;
        }

        if (info.getType() == FileInfo.TYPE_DIR) {
            File[] dirs = 
                new File(info.getName()).listFiles(new FileFilter() {
                    public boolean accept(File file) {
                        return file.isDirectory() && file.canRead();
                    }
                });

            for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy