package com.github.mjdev.libaums.fs.fat32;

import android.util.Log;
import com.github.mjdev.libaums.driver.BlockDeviceDriver;
import com.github.mjdev.libaums.fs.FileSystem;
import com.github.mjdev.libaums.fs.UsbFile;
import java.io.IOException;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class Fat32FileSystem implements FileSystem {
    private static final String TAG = "Fat32FileSystem";
    private Fat32BootSector bootSector;
    private FAT fat;
    private FsInfoStructure fsInfoStructure;
    private FatDirectory rootDirectory;

    private Fat32FileSystem(BlockDeviceDriver blockDeviceDriver) throws IOException {
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(512);
        blockDeviceDriver.read(0L, byteBufferAllocate);
        Fat32BootSector fat32BootSector = Fat32BootSector.read(byteBufferAllocate);
        this.bootSector = fat32BootSector;
        this.fsInfoStructure = FsInfoStructure.read(blockDeviceDriver, fat32BootSector.getFsInfoStartSector() * this.bootSector.getBytesPerSector());
        FAT fat = new FAT(blockDeviceDriver, this.bootSector, this.fsInfoStructure);
        this.fat = fat;
        this.rootDirectory = FatDirectory.readRoot(blockDeviceDriver, fat, this.bootSector);
        Log.d(TAG, this.bootSector.toString());
    }

    public static Fat32FileSystem read(BlockDeviceDriver blockDeviceDriver) throws IOException {
        return new Fat32FileSystem(blockDeviceDriver);
    }

    @Override // com.github.mjdev.libaums.fs.FileSystem
    public UsbFile getRootDirectory() {
        return this.rootDirectory;
    }

    @Override // com.github.mjdev.libaums.fs.FileSystem
    public String getVolumeLabel() {
        String volumeLabel = this.rootDirectory.getVolumeLabel();
        return volumeLabel == null ? this.bootSector.getVolumeLabel() : volumeLabel;
    }

    @Override // com.github.mjdev.libaums.fs.FileSystem
    public long getCapacity() {
        return this.bootSector.getTotalNumberOfSectors() * ((long) this.bootSector.getBytesPerSector());
    }

    @Override // com.github.mjdev.libaums.fs.FileSystem
    public long getOccupiedSpace() {
        return getCapacity() - getFreeSpace();
    }

    @Override // com.github.mjdev.libaums.fs.FileSystem
    public long getFreeSpace() {
        return this.fsInfoStructure.getFreeClusterCount() * ((long) this.bootSector.getBytesPerCluster());
    }

    @Override // com.github.mjdev.libaums.fs.FileSystem
    public int getChunkSize() {
        return this.bootSector.getBytesPerCluster();
    }
}
