Skip to content

Commit c4dc033

Browse files
committed
[irods#178] Add support for adjusting the size of data objects.
1 parent d1d12e6 commit c4dc033

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

irods-vfs-impl/src/main/java/org/irods/nfsrods/vfs/IRODSVirtualFileSystem.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,34 @@ public void setattr(Inode _inode, Stat _stat) throws IOException
14011401

14021402
if (_stat.isDefined(Stat.StatAttribute.SIZE))
14031403
{
1404-
log_.warn("setattr - Adjusting the size is not supported");
1404+
IRODSAccount acct = getCurrentIRODSUser().getAccount();
1405+
1406+
try
1407+
{
1408+
final var path = getPath(toInodeNumber(_inode));
1409+
log_.debug("setattr - Setting data size of [{}] to [{}] bytes.", path.toString(), _stat.getSize());
1410+
factory_.getDataObjectAO(acct).truncateReplica(path.toString(), _stat.getSize());
1411+
1412+
// Because iRODS timestamps are stored in seconds, operations that trigger
1413+
// an mtime update may not be detected by NFSRODS if the operations happen
1414+
// within the same second.
1415+
//
1416+
// To get around this limitation, NFSRODS must manually clear the cache
1417+
// so that the user sees the updates.
1418+
final var parentPath = path.getParent();
1419+
listOpCache_.remove(acct.getUserName() + "#" + parentPath.toString());
1420+
statObjectCache_.remove(acct.getUserName() + "_" + parentPath.toString());
1421+
statObjectCache_.remove(acct.getUserName() + "_" + path.toString());
1422+
}
1423+
catch (JargonException e)
1424+
{
1425+
log_.error(e.getMessage());
1426+
throw new IOException(e);
1427+
}
1428+
finally
1429+
{
1430+
closeCurrentConnection();
1431+
}
14051432
}
14061433
}
14071434

test/irods_tests.bats

+42
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,45 @@ teardown() {
273273
cd ..
274274
rmdir ${DIRECTORY}
275275
}
276+
277+
@test "#178: adjust size of file" {
278+
local FILENAME='truncate_test.txt'
279+
local MESSAGE='hello, world!'
280+
281+
# Create a non-empty file.
282+
echo "${MESSAGE}" > ${FILENAME}
283+
run cat ${FILENAME}
284+
#echo "# status = $status" >&3
285+
#echo "# output = $output" >&3
286+
[ "$status" -eq 0 ]
287+
[ "$output" = "${MESSAGE}" ]
288+
289+
# Overwrite the contents of the file with a new message.
290+
# The use of ">" is the real test. If the NFSRODS code is correct, the
291+
# file will be truncated to zero first.
292+
local NEW_MESSAGE='hello, NFSRODS!'
293+
echo "${NEW_MESSAGE}" > ${FILENAME}
294+
run cat ${FILENAME}
295+
#echo "# status = $status" >&3
296+
#echo "# output = $output" >&3
297+
[ "$status" -eq 0 ]
298+
[ "$output" = "${NEW_MESSAGE}" ]
299+
300+
# Increase the size of the file.
301+
local NEW_DATA_SIZE=256
302+
truncate -s${NEW_DATA_SIZE} ${FILENAME}
303+
run stat -c%s ${FILENAME}
304+
#echo "# status = $status" >&3
305+
#echo "# output = $output" >&3
306+
[ "$status" -eq 0 ]
307+
[ "$output" = "${NEW_DATA_SIZE}" ]
308+
309+
# Show the original content is still there.
310+
# Null bytes were appended during the "truncate" command
311+
# and will not appear in the output.
312+
run cat ${FILENAME}
313+
#echo "# status = $status" >&3
314+
#echo "# output = $output" >&3
315+
[ "$status" -eq 0 ]
316+
[ "$output" = "${NEW_MESSAGE}" ]
317+
}

0 commit comments

Comments
 (0)