Projet gus05 de développement Java
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Le deal à ne pas rater :
Jeux, jouets et Lego : le deuxième à -50% (large sélection)
Voir le deal

Trouver le numéro de série du lecteur et son filesystem grâce à JNA

Aller en bas

Trouver le numéro de série du lecteur et son filesystem grâce à JNA Empty Trouver le numéro de série du lecteur et son filesystem grâce à JNA

Message  Gus Mer 9 Jan - 19:28

Nous avons vu qu'il était possible avec Java de lister les lecteurs des disques durs et de trouver leurs noms ainsi que leurs tailles. Pour aller plus loin, il faut utiliser l'API JNA (Java Native Access). Le code source ci-dessous permet de trouver pour chaque lecteur, le numéro de série associé ainsi que le système de fichiers utilisé (NTFS, FAT32...). Par ailleurs la variable fileSysFlags permet de caractériser le système de fichiers (je n'ai pas eu le temps de développer)

Code:
import com.sun.jna.Library;
import com.sun.jna.Native;

public class GetVolumeInformation {

   private static String b2s(byte b[])
   {
      int len = 0;
      while(b[len]!=0) ++len;
      return new String(b,0,len);
   }
   
   public static void main(String[] args)
   {
      Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
      int drives = kernel32.GetLogicalDrives();
      for (int i = 0; i < 32; ++i)
      {
         if((drives & (1 << i)) == 0) continue;
         
         String path = String.format("%c:\\", (char) ((int) 'A' + i));
         byte[] volName = new byte[256];
         byte[] fsName = new byte[256];
         int[] volSerNbr = new int[1];
         int[] maxCompLen = new int[1];
         int[] fileSysFlags = new int[1];
         
         boolean found = kernel32.GetVolumeInformationA(path, volName, 256, volSerNbr, maxCompLen, fileSysFlags, fsName, 256);
         
         if(found)
         {
            System.out.println("Path: "+path);
            System.out.println("Volume Serial Number: "+volSerNbr[0]);
            System.out.println("volume Name: "+b2s(volName));
            System.out.println("File System Name: "+b2s(fsName));
            System.out.println("File System Flags: "+ fileSysFlags[0]);
            System.out.println();
         }
         else
         {
            System.out.println(path+" (not found)");
         }
      }
   }

   public interface Kernel32 extends Library
   {
      boolean GetVolumeInformationA(String path, byte[] volName, int volumeNameSize,
            int[] volSerNbr, int[] maxCompLen, int[] fileSysFlags, byte[] fsName, int fileSystemNameSize);
      int GetLogicalDrives();
   }
}

Il reste à exploiter la valeur de l'entier stocké dans le tableau fileSysFlags.
Pour cela, il faut aller voir la doc officiel de la fonction GetVolumeInformationA :
http://msdn.microsoft.com/en-us/library/aa364993%28VS.85%29.aspx

J'ai préparé une classe avec les constantes nécessaires. Il reste à faire la méthode de test sur la valeur...
Code:
public class LP_FILESYSTEM_FLAGS {

   public static final int FILE_CASE_SENSITIVE_SEARCH =       0x00000001;      //The specified volume supports case-sensitive file names.
   public static final int FILE_CASE_PRESERVED_NAMES =       0x00000002;      //The specified volume supports preserved case of file names when it places a name on disk.
   public static final int FILE_UNICODE_ON_DISK =             0x00000004;      //The specified volume supports Unicode in file names as they appear on disk.
   public static final int FILE_PERSISTENT_ACLS =             0x00000008;      //The specified volume preserves and enforces access control lists (ACL). For example, the NTFS file system preserves and enforces ACLs, and the FAT file system does not.
   
   public static final int FILE_FILE_COMPRESSION =          0x00000010;      //The specified volume supports file-based compression.
   public static final int FILE_VOLUME_QUOTAS =             0x00000020;      //The specified volume supports disk quotas.
   public static final int FILE_SUPPORTS_SPARSE_FILES =       0x00000040;      //The specified volume supports sparse files.
   public static final int FILE_SUPPORTS_REPARSE_POINTS =       0x00000080;      //The specified volume supports reparse points.ReFS:  ReFS supports reparse points but does not index them so FindFirstVolumeMountPoint and FindNextVolumeMountPoint will not function as expected.
   
   public static final int FILE_VOLUME_IS_COMPRESSED =       0x00008000;      //The specified volume is a compressed volume, for example, a DoubleSpace volume.
   
   public static final int FILE_SUPPORTS_OBJECT_IDS =          0x00010000;      //The specified volume supports object identifiers.
   public static final int FILE_SUPPORTS_ENCRYPTION =          0x00020000;      //The specified volume supports the Encrypted File System (EFS). For more information, see File Encryption.
   public static final int FILE_NAMED_STREAMS =             0x00040000;      //The specified volume supports named streams.
   public static final int FILE_READ_ONLY_VOLUME =          0x00080000;      //The specified volume is read-only.
   
   public static final int FILE_SEQUENTIAL_WRITE_ONCE =       0x00100000;      //The specified volume supports a single sequential write.
   public static final int FILE_SUPPORTS_TRANSACTIONS =       0x00200000;      //The specified volume supports transactions. For more information, see About KTM.
   public static final int FILE_SUPPORTS_HARD_LINKS =          0x00400000;      //The specified volume supports hard links. For more information, see Hard Links and Junctions. Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP:  This value is not supported until Windows Server 2008 R2 and Windows 7.
   public static final int FILE_SUPPORTS_EXTENDED_ATTRIBUTES = 0x00800000;      //The specified volume supports extended attributes. An extended attribute is a piece of application-specific metadata that an application can associate with a file and is not part of the file's data. Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP:  This value is not supported until Windows Server 2008 R2 and Windows 7.
   
   public static final int FILE_SUPPORTS_OPEN_BY_FILE_ID =    0x01000000;      //The file system supports open by FileID. For more information, see FILE_ID_BOTH_DIR_INFO.Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP:  This value is not supported until Windows Server 2008 R2 and Windows 7.
   public static final int FILE_SUPPORTS_USN_JOURNAL =       0x02000000;      //The specified volume supports update sequence number (USN) journals. For more information, see Change Journal Records.Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP:  This value is not supported until Windows Server 2008 R2 and Windows 7.
}
Gus
Gus
Admin

Messages : 249
Date d'inscription : 01/09/2009

http://www.gus05.com

Revenir en haut Aller en bas

Revenir en haut

- Sujets similaires

 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum