diff --git a/server/src/main/java/com/usatiuk/dhfs/storage/files/service/DhfsFileServiceImpl.java b/server/src/main/java/com/usatiuk/dhfs/storage/files/service/DhfsFileServiceImpl.java index f5a1564e..7617b08d 100644 --- a/server/src/main/java/com/usatiuk/dhfs/storage/files/service/DhfsFileServiceImpl.java +++ b/server/src/main/java/com/usatiuk/dhfs/storage/files/service/DhfsFileServiceImpl.java @@ -339,6 +339,11 @@ public class DhfsFileServiceImpl implements DhfsFileService { @Override public Optional read(String fileUuid, long offset, int length) { + if (length < 0) + throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Length should be more than zero: " + length)); + if (offset < 0) + throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Offset should be more than zero: " + offset)); + var fileOpt = jObjectManager.get(fileUuid); if (fileOpt.isEmpty()) { Log.error("File not found when trying to read: " + fileUuid); @@ -453,6 +458,9 @@ public class DhfsFileServiceImpl implements DhfsFileService { @Override public Long write(String fileUuid, long offset, byte[] data) { + if (offset < 0) + throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Offset should be more than zero: " + offset)); + var fileOpt = jObjectManager.get(fileUuid); if (fileOpt.isEmpty()) { Log.error("File not found when trying to read: " + fileUuid); @@ -590,6 +598,9 @@ public class DhfsFileServiceImpl implements DhfsFileService { @Override public Boolean truncate(String fileUuid, long length) { + if (length < 0) + throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Length should be more than zero: " + length)); + var fileOpt = jObjectManager.get(fileUuid); if (fileOpt.isEmpty()) { Log.error("File not found when trying to read: " + fileUuid); @@ -628,17 +639,17 @@ public class DhfsFileServiceImpl implements DhfsFileService { var removedChunks = new LinkedHashSet(); if (curSize < length) { - int combinedSize = (int) (length - curSize); + long combinedSize = (length - curSize); long start = curSize; // Hack - HashMap zeroCache = new HashMap<>(); + HashMap zeroCache = new HashMap<>(); { - int cur = 0; + long cur = 0; while (cur < combinedSize) { - int end; + long end; if (targetChunkSize <= 0) end = combinedSize; @@ -651,7 +662,7 @@ public class DhfsFileServiceImpl implements DhfsFileService { } if (!zeroCache.containsKey(end - cur)) - zeroCache.put(end - cur, UnsafeByteOperations.unsafeWrap(new byte[end - cur])); + zeroCache.put(end - cur, UnsafeByteOperations.unsafeWrap(new byte[Math.toIntExact(end - cur)])); ChunkData newChunkData = createChunk(zeroCache.get(end - cur)); ChunkInfo newChunkInfo = new ChunkInfo(newChunkData.getHash(), newChunkData.getBytes().size()); diff --git a/server/src/main/java/com/usatiuk/dhfs/storage/fuse/DhfsFuse.java b/server/src/main/java/com/usatiuk/dhfs/storage/fuse/DhfsFuse.java index 4d9989e4..d07fc865 100644 --- a/server/src/main/java/com/usatiuk/dhfs/storage/fuse/DhfsFuse.java +++ b/server/src/main/java/com/usatiuk/dhfs/storage/fuse/DhfsFuse.java @@ -147,6 +147,8 @@ public class DhfsFuse extends FuseStubFS { @Override public int read(String path, Pointer buf, long size, long offset, FuseFileInfo fi) { + if (size < 0) return -ErrorCodes.EINVAL(); + if (offset < 0) return -ErrorCodes.EINVAL(); try { var fileOpt = fileService.open(path); if (fileOpt.isEmpty()) return -ErrorCodes.ENOENT(); @@ -163,6 +165,7 @@ public class DhfsFuse extends FuseStubFS { @Override public int write(String path, Pointer buf, long size, long offset, FuseFileInfo fi) { + if (offset < 0) return -ErrorCodes.EINVAL(); try { var fileOpt = fileService.open(path); if (fileOpt.isEmpty()) return -ErrorCodes.ENOENT(); @@ -239,6 +242,7 @@ public class DhfsFuse extends FuseStubFS { @Override public int truncate(String path, long size) { + if (size < 0) return -ErrorCodes.EINVAL(); try { var fileOpt = fileService.open(path); if (fileOpt.isEmpty()) return -ErrorCodes.ENOENT(); diff --git a/server/src/main/java/com/usatiuk/dhfs/storage/objects/jrepository/JObjectWriteback.java b/server/src/main/java/com/usatiuk/dhfs/storage/objects/jrepository/JObjectWriteback.java index e2d6777e..1b96f9b2 100644 --- a/server/src/main/java/com/usatiuk/dhfs/storage/objects/jrepository/JObjectWriteback.java +++ b/server/src/main/java/com/usatiuk/dhfs/storage/objects/jrepository/JObjectWriteback.java @@ -173,7 +173,7 @@ public class JObjectWriteback { if (_objects.size() < limit) { if (overload) { overload = false; - Log.info("Writeback cache enabled"); + Log.trace("Writeback cache enabled"); } _objects.put(name, Pair.of(System.currentTimeMillis(), object)); _objects.notifyAll(); @@ -184,7 +184,7 @@ public class JObjectWriteback { try { if (!overload) { overload = true; - Log.info("Writeback cache disabled"); + Log.trace("Writeback cache disabled"); } flushOneImmediate(object.getMeta(), object.getData()); } catch (Exception e) {