use of org.dcache.namespace.FileType in project dcache by dCache.
the class PnfsManagerV3 method createEntry.
public void createEntry(PnfsCreateEntryMessage pnfsMessage) {
checkArgument(pnfsMessage.getFileAttributes().isDefined(TYPE));
FileAttributes assign = pnfsMessage.getFileAttributes();
FileType type = assign.removeFileType();
Subject subject = pnfsMessage.getSubject();
String path = pnfsMessage.getPnfsPath();
try {
File file = new File(path);
checkMask(subject, file.getParent(), pnfsMessage.getAccessMask());
Set<FileAttribute> requested = pnfsMessage.getAcquire();
FileAttributes attrs = null;
switch(type) {
case DIR:
LOGGER.info("create directory {}", path);
checkMkdirAllowed(pnfsMessage);
PnfsId pnfsId = _nameSpaceProvider.createDirectory(subject, path, assign);
pnfsMessage.setPnfsId(pnfsId);
// the createEntry seem to be ok.
try {
LOGGER.info("Trying to get storageInfo for {}", pnfsId);
/* If we were allowed to create the entry above, then
* we also ought to be allowed to read it here. Hence
* we use ROOT as the subject.
*/
attrs = _nameSpaceProvider.getFileAttributes(ROOT, pnfsId, requested);
} catch (CacheException e) {
LOGGER.warn("Can't determine storageInfo: {}", e.toString());
}
break;
case REGULAR:
LOGGER.info("create file {}", path);
checkRestriction(pnfsMessage, UPLOAD);
requested.add(FileAttribute.STORAGEINFO);
requested.add(FileAttribute.PNFSID);
attrs = _nameSpaceProvider.createFile(subject, path, assign, requested);
StorageInfo info = attrs.getStorageInfo();
if (info.getKey("path") == null) {
info.setKey("path", path);
}
info.setKey("uid", Integer.toString(assign.getOwnerIfPresent().orElse(-1)));
info.setKey("gid", Integer.toString(assign.getGroupIfPresent().orElse(-1)));
// REVISIT: consider removing xattr injection once pools can accept FileAttribute.XATTR
if (assign.isDefined(XATTR)) {
assign.getXattrs().forEach((k, v) -> info.setKey(STORAGE_INFO_XATTR_PREFIX + k, v));
}
pnfsMessage.setPnfsId(attrs.getPnfsId());
break;
case LINK:
checkArgument(pnfsMessage instanceof PnfsCreateSymLinkMessage);
String destination = ((PnfsCreateSymLinkMessage) pnfsMessage).getDestination();
LOGGER.info("create symlink {} to {}", path, destination);
checkRestrictionOnParent(pnfsMessage, MANAGE);
pnfsId = _nameSpaceProvider.createSymLink(subject, path, destination, assign);
pnfsMessage.setPnfsId(pnfsId);
break;
default:
throw new IllegalArgumentException("Unsupported type: " + type);
}
pnfsMessage.setFileAttributes(attrs);
pnfsMessage.setSucceeded();
} catch (CacheException e) {
pnfsMessage.setFailed(e.getRc(), e.getMessage());
} catch (RuntimeException e) {
LOGGER.error("Bug found when creating entry:", e);
pnfsMessage.setFailed(CacheException.UNEXPECTED_SYSTEM_EXCEPTION, e);
}
}
use of org.dcache.namespace.FileType in project dcache by dCache.
the class Transfer method readNameSpaceEntryAsync.
private ListenableFuture<Void> readNameSpaceEntryAsync(boolean allowWrite, long timeout) {
Set<FileAttribute> attr = EnumSet.of(PNFSID, TYPE, STORAGEINFO, SIZE);
attr.addAll(_additionalAttributes);
attr.addAll(PoolMgrSelectReadPoolMsg.getRequiredAttributes());
Set<AccessMask> mask;
if (allowWrite) {
mask = EnumSet.of(AccessMask.READ_DATA, AccessMask.WRITE_DATA);
} else {
mask = EnumSet.of(AccessMask.READ_DATA);
}
PnfsId pnfsId = getPnfsId();
PnfsGetFileAttributes request;
if (pnfsId != null) {
request = new PnfsGetFileAttributes(pnfsId, attr);
if (_path != null) {
// Needed for restriction check.
request.setPnfsPath(_path.toString());
}
} else {
request = new PnfsGetFileAttributes(_path.toString(), attr);
}
request.setAccessMask(mask);
request.setUpdateAtime(true);
ListenableFuture<PnfsGetFileAttributes> reply = _pnfs.requestAsync(request, timeout);
setStatusUntil("PnfsManager: Fetching storage info", reply);
return CellStub.transformAsync(reply, msg -> {
FileAttributes attributes = msg.getFileAttributes();
/* We can only transfer regular files.
*/
FileType type = attributes.getFileType();
if (type == FileType.DIR || type == FileType.SPECIAL) {
throw new NotFileCacheException("Not a regular file");
}
/* I/O mode must match completeness of the file.
*/
if (!attributes.getStorageInfo().isCreatedOnly()) {
setWrite(false);
} else if (allowWrite) {
setWrite(true);
} else {
throw new FileIsNewCacheException();
}
setFileAttributes(attributes);
return immediateFuture(null);
});
}
use of org.dcache.namespace.FileType in project dcache by dCache.
the class MonitoringNameSpaceProviderTest method shouldNotifyOnPnfsIdDelete.
@Test
public void shouldNotifyOnPnfsIdDelete() throws Exception {
PnfsId parent = new PnfsId("000000000000000000000000000000000001");
PnfsId target = new PnfsId("000000000000000000000000000000000002");
given(inner.find(any(), eq(target))).willReturn(singleLink(parent, "foo"));
given(inner.deleteEntry(any(), any(), eq(target), any())).willReturn(FileAttributes.of().fileType(FileType.REGULAR).accessTime(42L).build());
FileAttributes result = monitor.deleteEntry(TEST_USER, EnumSet.allOf(FileType.class), target, EnumSet.of(FileAttribute.ACCESS_TIME));
verify(inner).deleteEntry(eq(TEST_USER), eq(EnumSet.allOf(FileType.class)), eq(target), (Set<FileAttribute>) argThat(hasItem(FileAttribute.ACCESS_TIME)));
assertThat(result.getAccessTime(), is(equalTo(42L)));
verify(receiver).notifyChildEvent(EventType.IN_DELETE, parent, "foo", FileType.REGULAR);
verify(receiver).notifySelfEvent(EventType.IN_DELETE_SELF, target, FileType.REGULAR);
verify(receiver, never()).notifyMovedEvent(any(), any(), any(), any(), any());
}
use of org.dcache.namespace.FileType in project dcache by dCache.
the class PnfsManagerV3 method deleteEntry.
public void deleteEntry(PnfsDeleteEntryMessage pnfsMessage) {
String path = pnfsMessage.getPnfsPath();
PnfsId pnfsId = pnfsMessage.getPnfsId();
Subject subject = pnfsMessage.getSubject();
Set<FileType> allowed = pnfsMessage.getAllowedFileTypes();
Set<FileAttribute> requested = pnfsMessage.getRequestedAttributes();
try {
if (path == null && pnfsId == null) {
throw new InvalidMessageCacheException("pnfsid or path have to be defined for PnfsDeleteEntryMessage");
}
checkMask(pnfsMessage);
checkRestriction(pnfsMessage, DELETE);
FileAttributes attributes;
if (path != null) {
LOGGER.info("delete PNFS entry for {}", path);
if (pnfsId != null) {
attributes = _nameSpaceProvider.deleteEntry(subject, allowed, pnfsId, path, requested);
} else {
requested.add(PNFSID);
attributes = _nameSpaceProvider.deleteEntry(subject, allowed, path, requested);
pnfsMessage.setPnfsId(attributes.getPnfsId());
}
} else {
LOGGER.info("delete PNFS entry for {}", pnfsId);
attributes = _nameSpaceProvider.deleteEntry(subject, allowed, pnfsId, requested);
}
pnfsMessage.setFileAttributes(attributes);
pnfsMessage.setSucceeded();
} catch (FileNotFoundCacheException e) {
pnfsMessage.setFailed(CacheException.FILE_NOT_FOUND, e.getMessage());
} catch (CacheException e) {
LOGGER.warn("Failed to delete entry: {}", e.getMessage());
pnfsMessage.setFailed(e.getRc(), e.getMessage());
} catch (RuntimeException e) {
LOGGER.error("Failed to delete entry", e);
pnfsMessage.setFailed(CacheException.UNEXPECTED_SYSTEM_EXCEPTION, e);
}
}
use of org.dcache.namespace.FileType in project dcache by dCache.
the class MonitoringNameSpaceProvider method rename.
@Override
public void rename(Subject subject, PnfsId target, String sourcePath, String destinationPath, boolean overwrite) throws CacheException {
Optional<Link> sourceParent = findParent(sourcePath);
PnfsId resolvedTarget = null;
try {
resolvedTarget = target == null ? super.pathToPnfsid(Subjects.ROOT, sourcePath, true) : target;
} catch (FileNotFoundCacheException e) {
} catch (CacheException e) {
LOGGER.error("Failed to resolve path {}: {}", sourcePath, e.toString());
}
super.rename(subject, target, sourcePath, destinationPath, overwrite);
if (resolvedTarget == null) {
LOGGER.error("Earlier failure in monitoring lookup didn't fail move.");
return;
}
// The cookie is a unique identifier for this move operation.
byte[] cookieBytes = Hashing.murmur3_128().newHasher().putString(resolvedTarget.toString(), StandardCharsets.UTF_8).putString(sourcePath, StandardCharsets.UTF_8).putString(destinationPath, StandardCharsets.UTF_8).putLong(// FIXME replace with generation ID
System.currentTimeMillis()).hash().asBytes();
String cookie = BaseEncoding.base64().omitPadding().encode(cookieBytes);
Optional<Link> destinationParent = findParent(destinationPath);
FileType type = super.getFileAttributes(Subjects.ROOT, resolvedTarget, FILETYPE).getFileType();
eventReceiver.notifySelfEvent(EventType.IN_MOVE_SELF, resolvedTarget, type);
sourceParent.ifPresent(l -> eventReceiver.notifyMovedEvent(EventType.IN_MOVED_FROM, l.getParent(), l.getName(), cookie, type));
destinationParent.ifPresent(l -> eventReceiver.notifyMovedEvent(EventType.IN_MOVED_TO, l.getParent(), l.getName(), cookie, type));
}
Aggregations