Unix File Attributes

The Unix File Attributes packet consists of the following:

File-Index Type Filename@File-Attributes@Link @Extended-Attributes@ where

@
represents a byte containing a binary zero.
FileIndex
is the sequential file index starting from one assigned by the File daemon.
Type
is one of the following:

#define FT_LNKSAVED   1    /* hard link to file already saved */
#define FT_REGE       2    /* Regular file but empty */
#define FT_REG        3    /* Regular file */
#define FT_LNK        4    /* Soft Link */
#define FT_DIR        5    /* Directory */
#define FT_SPEC       6    /* Special file -- chr, blk, fifo, sock */
#define FT_NOACCESS   7    /* Not able to access */
#define FT_NOFOLLOW   8    /* Could not follow link */
#define FT_NOSTAT     9    /* Could not stat file */
#define FT_NOCHG     10    /* Incremental option, file not changed */
#define FT_DIRNOCHG  11    /* Incremental option, directory not changed */
#define FT_ISARCH    12    /* Trying to save archive file */
#define FT_NORECURSE 13    /* No recursion into directory */
#define FT_NOFSCHG   14    /* Different file system, prohibited */
#define FT_NOOPEN    15    /* Could not open directory */
#define FT_RAW       16    /* Raw block device */
#define FT_FIFO      17    /* Raw fifo device */

Filename
is the fully qualified filename.
File-Attributes
consists of the 13 fields of the stat() buffer in ASCII base64 format separated by spaces. These fields and their meanings are shown below. This stat() packet is in Unix format, and MUST be provided (constructed) for ALL systems.
Link
when the FT code is FT_LNK or FT_LNKSAVED, the item in question is a Unix link, and this field contains the fully qualified link name. When the FT code is not FT_LNK or FT_LNKSAVED, this field is null.
Extended-Attributes
The exact format of this field is operating system dependent. It contains additional or extended attributes of a system dependent nature. Currently, this field is used only on WIN32 systems where it contains a ASCII base64 representation of the WIN32_FILE_ATTRIBUTE_DATA structure as defined by Windows. The fields in the base64 representation of this structure are like the File-Attributes separated by spaces.

The File-attributes consist of the following:

Field No. Stat Name Unix Win98/NT MacOS
1 st_dev Device number of filesystem Drive number vRefNum
2 st_ino Inode number Always 0 fileID/dirID
3 st_mode File mode File mode 777 dirs/apps; 666 docs; 444 locked docs
4 st_nlink Number of links to the file Number of link (only on NTFS) Always 1
5 st_uid Owner ID Always 0 Always 0
6 st_gid Group ID Always 0 Always 0
7 st_rdev Device ID for special files Drive No. Always 0
8 st_size File size in bytes File size in bytes Data fork file size in bytes
9 st_blksize Preferred block size Always 0 Preferred block size
10 st_blocks Number of blocks allocated Always 0 Number of blocks allocated
11 st_atime Last access time since epoch Last access time since epoch Last access time -66 years
12 st_mtime Last modify time since epoch Last modify time since epoch Last access time -66 years
13 st_ctime Inode change time since epoch File create time since epoch File create time -66 years

java代码获取

    /**
     * 判断执行环境是否是linux
     * http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/
     * @return
     */
    public static boolean isLinux(){
        String OS = System.getProperty("os.name").toLowerCase();
        return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );
    }
    /**
     * 判断当前的执行环境是否是windows系统
     * http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/
     * @return
     */
    public static boolean isWindows(){
        String OS = System.getProperty("os.name").toLowerCase();
        return (OS.indexOf("win") >= 0);
    }
    /**
     * 判断参数的第二个元素的文件类型,对windows os无效
     * @param args
     */
    public static void detectUnixHardLink(String[] args){
        if(isWindows()){
            System.out.println("该操作只支持对linux文件类型的判断");
            return;
        }
        String filePath=null;

        try{
            filePath=args[1];
        }catch(ArrayIndexOutOfBoundsException e){
            log.error("文件类型判断少文件地址");
        }
        Path p=new File(filePath).toPath();
        try {
            int type=(Integer)java.nio.file.Files.getAttribute(p, "unix:nlink");
            System.out.println("unix nlink:"+type);
        } catch (IOException | ClassCastException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

原文地址:
http://www.bacula.org/5.1.x-manuals/en/developers/developers/Unix_File_Attributes.html
其它参考:
http://www.cse.scu.edu/~tschwarz/COEN252_09/Lectures/UnixFileSystem.html

你可能感兴趣的:(unix,java)