Skip to content

Commit

Permalink
fix(s3stream/wal): check isBlockDev by prefix in some rare cases
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Yu <[email protected]>
  • Loading branch information
Chillax-0v0 committed Jan 20, 2025
1 parent df4c558 commit ee2a7b9
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions s3stream/src/main/java/com/automq/stream/s3/wal/util/WALUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.automq.stream.utils.CommandResult;
import com.automq.stream.utils.CommandUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
Expand All @@ -24,6 +27,7 @@
import java.util.zip.CRC32;

import io.netty.buffer.ByteBuf;
import jnr.posix.POSIX;
import jnr.posix.POSIXFactory;

import static com.automq.stream.s3.wal.common.RecordHeader.RECORD_HEADER_MAGIC_CODE;
Expand All @@ -36,6 +40,8 @@ public class WALUtil {
"4096"
));

private static final Logger LOGGER = LoggerFactory.getLogger(WALUtil.class);

public static Record generateRecord(ByteBuf body, ByteBuf emptyHeader, int crc, long start) {
return generateRecord(body, emptyHeader, crc, start, true);
}
Expand Down Expand Up @@ -158,15 +164,25 @@ public static boolean isBlockDevice(String path) {
if (!new File(path).exists()) {
return false;
}
boolean isBlockDevice;

POSIX posix;
try {
isBlockDevice = POSIXFactory.getPOSIX()
.stat(path)
.isBlockDev();
posix = POSIXFactory.getNativePOSIX();
} catch (Exception e) {
LOGGER.warn("Failed to get native POSIX, fallback to check by prefix", e);
return isBlockDeviceByPrefix(path);
}

try {
return posix.stat(path).isBlockDev();
} catch (Exception e) {
// In some OS (like Windows), the isBlockDev() method may throw an IllegalStateException.
isBlockDevice = false;
LOGGER.warn("Failed to check if {} is a block device, fallback to check by prefix", path, e);
return isBlockDeviceByPrefix(path);
}
return isBlockDevice;
}

private static boolean isBlockDeviceByPrefix(String path) {
return path.startsWith("/dev/");
}
}

0 comments on commit ee2a7b9

Please sign in to comment.