-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Soft delete projects by moving them to trash #10440
Merged
Merged
Changes from 29 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
f8f0c08
feat: trash
4e6 0e580d6
feat: move project to trash
4e6 8ffa14f
fix: TrashTest on Windows
4e6 b0ff163
fix: native image
4e6 569261f
Merge branch 'develop' into wip/db/10357-add-recycle
4e6 aa555cc
fix: after merge
4e6 ff4cbec
DEBUG: lazy trash initialization
4e6 3c90113
DEBUG: trash initialization
4e6 80dd6c8
DEBUG: awt initialization
4e6 df166a7
misc: javafmt
4e6 5fcd03c
misc: properties setup
4e6 efd9e05
fix: native image
4e6 bf177bf
feat: jna trash
4e6 170284b
feat: native image resources
4e6 b0d72f0
misc: cleanup properties setup
4e6 e97b44d
misc: legal review
4e6 1d296ef
Merge branch 'develop' into wip/db/10357-add-recycle
4e6 9aa46a8
fix: after merge
4e6 633f78a
Simplify LazyTrash by initializing in static initializer
JaroslavTulach e474e39
Making Platform an enum
JaroslavTulach c82ed2f
Merge branch 'develop' into wip/db/10357-add-recycle
hubertp 0d346f2
Include Jaroslav's prototype
hubertp 9066faa
os dependent
hubertp 081c9d2
fmt
hubertp 8e1e2b8
Fix native image build on linux
hubertp 5a7d9c8
Merge branch 'develop' into wip/db/10357-add-recycle
hubertp 961aa6c
remove jna
hubertp ccb8bd5
update license
hubertp 0de3f1e
nit
hubertp 2301840
fmt
hubertp 114cdca
Fix tests that broke during Platform refactoring
hubertp 20b892f
Workaround for non-AOT mode
hubertp c5aee4a
deal with non-empty directories for fallback delete
hubertp f6d66ab
fix: tests
4e6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
...ava/desktop-environment/src/main/java/org/enso/desktopenvironment/DirectoriesFactory.java
This file was deleted.
Oops, something went wrong.
220 changes: 220 additions & 0 deletions
220
lib/java/desktop-environment/src/main/java/org/enso/desktopenvironment/LinuxTrashBin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
package org.enso.desktopenvironment; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.nio.file.StandardOpenOption; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.temporal.ChronoUnit; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
/** | ||
* The Linux trash implementing the <a | ||
* href="https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html">FreeDesktop.org Trash | ||
* specification</a>. | ||
* | ||
* <p>A trash directory contains two subdirectories, named info and files. The files directory | ||
* contains the trashed files, and the info directory contains the corresponding trashinfo metadata | ||
* for each trashed entry in the files directory. | ||
*/ | ||
final class LinuxTrashBin implements TrashBin { | ||
|
||
private static final String XDG_DATA_HOME = "XDG_DATA_HOME"; | ||
private static final String PATH_TRASH = "Trash"; | ||
private static final String PATH_FILES = "files"; | ||
private static final String PATH_INFO = "info"; | ||
|
||
private final LinuxDirectories directories = new LinuxDirectories(); | ||
|
||
@Override | ||
public boolean isSupported() { | ||
var trashDir = detectTrashDirectory(); | ||
return Files.isDirectory(trashDir.resolve(PATH_FILES)) | ||
&& Files.isDirectory(trashDir.resolve(PATH_INFO)); | ||
} | ||
|
||
@Override | ||
public boolean moveToTrash(Path path) { | ||
var trashDir = detectTrashDirectory(); | ||
|
||
if (Files.exists(path) && isSupported()) { | ||
try { | ||
var trashInfo = TrashInfo.create(trashDir.resolve(PATH_INFO), path); | ||
|
||
try { | ||
Files.move( | ||
path, | ||
trashDir.resolve(PATH_FILES).resolve(trashInfo.fileName), | ||
StandardCopyOption.ATOMIC_MOVE); | ||
return true; | ||
} catch (IOException e) { | ||
boolean isSuccessful; | ||
if (Files.isDirectory(path)) { | ||
isSuccessful = | ||
moveDirectoryToDirectory(path, trashDir.resolve(PATH_FILES), trashInfo.fileName); | ||
} else { | ||
isSuccessful = | ||
moveFileToDirectory(path, trashDir.resolve(PATH_FILES), trashInfo.fileName); | ||
} | ||
|
||
if (!isSuccessful) { | ||
FileUtils.deleteQuietly(trashInfo.path.toFile()); | ||
} | ||
|
||
return isSuccessful; | ||
} | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
|
||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean moveFileToDirectory(Path from, Path to, String fileName) { | ||
var source = from.toFile(); | ||
var destination = to.resolve(fileName).toFile(); | ||
|
||
try { | ||
FileUtils.copyFile(source, destination); | ||
FileUtils.delete(source); | ||
|
||
return true; | ||
} catch (IOException e) { | ||
FileUtils.deleteQuietly(destination); | ||
return false; | ||
} | ||
} | ||
|
||
private static boolean moveDirectoryToDirectory(Path from, Path to, String fileName) { | ||
var source = from.toFile(); | ||
var destination = to.resolve(fileName).toFile(); | ||
try { | ||
FileUtils.copyDirectory(source, destination); | ||
FileUtils.deleteDirectory(source); | ||
|
||
return true; | ||
} catch (IOException e) { | ||
FileUtils.deleteQuietly(destination); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Detect the path to a home trash directory of the current user. | ||
* | ||
* <p>The home trash directory should be automatically created for any new user. If the directory | ||
* does not exist, it will be created. | ||
* | ||
* @return the path to the trash directory. | ||
*/ | ||
private Path detectTrashDirectory() { | ||
var xdgDataHomeOverride = System.getenv(XDG_DATA_HOME); | ||
var xdgDataHome = | ||
xdgDataHomeOverride == null | ||
? directories.getUserHome().resolve(".local").resolve("share") | ||
: Path.of(xdgDataHomeOverride); | ||
|
||
var trashDir = xdgDataHome.resolve(PATH_TRASH); | ||
|
||
try { | ||
Files.createDirectories(trashDir.resolve(PATH_FILES)); | ||
} catch (IOException ignored) { | ||
} | ||
|
||
try { | ||
Files.createDirectories(trashDir.resolve(PATH_INFO)); | ||
} catch (IOException ignored) { | ||
} | ||
|
||
return trashDir; | ||
} | ||
|
||
/** | ||
* The trashinfo metadata file. | ||
* | ||
* @param path the path to this trashinfo file. | ||
* @param fileName the file name that should be used to store the trashed file. | ||
*/ | ||
private record TrashInfo(Path path, String fileName) { | ||
|
||
private static final int SUFFIX_SIZE = 6; | ||
private static final int MAX_ATTEMPTS = Byte.MAX_VALUE; | ||
private static final String TRASHINFO_EXTENSION = ".trashinfo"; | ||
|
||
/** | ||
* Create the .trashinfo file containing the deleted file metadata. | ||
* | ||
* @param trashInfo the path to the trashinfo directory. | ||
* @param toDelete the path to the file that should be deleted. | ||
* @return the trashinfo metadata file. | ||
* @throws IOException if the file creation was unsuccessful. | ||
*/ | ||
public static TrashInfo create(Path trashInfo, Path toDelete) throws IOException { | ||
var builder = new StringBuilder(); | ||
builder.append("[Trash Info]"); | ||
builder.append(System.lineSeparator()); | ||
builder.append("Path="); | ||
builder.append(toDelete.toAbsolutePath()); | ||
builder.append(System.lineSeparator()); | ||
builder.append("DeletionDate="); | ||
builder.append( | ||
LocalDateTime.now() | ||
.truncatedTo(ChronoUnit.SECONDS) | ||
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); | ||
builder.append(System.lineSeparator()); | ||
|
||
return createTrashInfo(trashInfo, toDelete, builder, "", 0); | ||
} | ||
|
||
/** | ||
* Create the .trashinfo file containing the deleted file metadata. | ||
* | ||
* <p>In case of a name clash, when the trash already contains the file with the same name, the | ||
* trashinfo file is created with a random suffix to resolve the conflict. | ||
* | ||
* <p>The file creation is atomic to so that if two processes try trash files with the same | ||
* filename this will result in two different trash files. | ||
* | ||
* @param trashInfo the path to the trashinfo directory. | ||
* @param toDelete the path to the file that should be deleted. | ||
* @param contents the trashinfo file contents. | ||
* @param suffix the trashinfo suffix to resolve the file name conflicts. | ||
* @param attempts the number of attempts to resolve the name clash. | ||
* @return the trashinfo metadata file. | ||
* @throws IOException if the file creation was unsuccessful. | ||
*/ | ||
private static TrashInfo createTrashInfo( | ||
Path trashInfo, Path toDelete, CharSequence contents, String suffix, int attempts) | ||
throws IOException { | ||
if (attempts > MAX_ATTEMPTS) { | ||
throw new IOException("Failed to create trashinfo file. Max attempts reached."); | ||
} | ||
|
||
try { | ||
var fileName = toDelete.getFileName().toString() + suffix; | ||
var path = | ||
Files.writeString( | ||
trashInfo.resolve(fileName + TRASHINFO_EXTENSION), | ||
contents, | ||
StandardCharsets.UTF_8, | ||
StandardOpenOption.CREATE_NEW, | ||
StandardOpenOption.WRITE); | ||
|
||
return new TrashInfo(path, fileName); | ||
} catch (FileAlreadyExistsException e) { | ||
return createTrashInfo( | ||
trashInfo, | ||
toDelete, | ||
contents, | ||
RandomUtils.alphanumericString(SUFFIX_SIZE), | ||
attempts + 1); | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
lib/java/desktop-environment/src/main/java/org/enso/desktopenvironment/MacTrashBin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.enso.desktopenvironment; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.graalvm.nativeimage.UnmanagedMemory; | ||
import org.graalvm.nativeimage.c.CContext; | ||
import org.graalvm.nativeimage.c.function.CFunction; | ||
import org.graalvm.nativeimage.c.type.CCharPointer; | ||
import org.graalvm.nativeimage.c.type.CTypeConversion; | ||
import org.graalvm.word.Pointer; | ||
import org.graalvm.word.WordFactory; | ||
|
||
@CContext(MacTrashBin.CoreServices.class) | ||
final class MacTrashBin implements TrashBin { | ||
|
||
@CFunction | ||
static native int FSPathMakeRefWithOptions( | ||
CCharPointer path, int flags, Pointer buffer, Pointer any); | ||
|
||
@CFunction | ||
static native int FSMoveObjectToTrashSync(Pointer source, Pointer target, int flags); | ||
|
||
@Override | ||
public boolean isSupported() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean moveToTrash(Path path) { | ||
if (Platform.getOperatingSystem().isMacOs()) return moveToTrashImpl(path); | ||
else return false; | ||
} | ||
|
||
private boolean moveToTrashImpl(Path path) { | ||
Pointer source = UnmanagedMemory.malloc(80); | ||
Pointer target = UnmanagedMemory.malloc(80); | ||
|
||
var kFSPathMakeRefDoNotFollowLeafSymlink = 0x01; | ||
var kFSFileOperationDefaultOptions = 0x00; | ||
CTypeConversion.CCharPointerHolder cPath; | ||
try { | ||
cPath = CTypeConversion.toCString(path.toString()); | ||
var r1 = | ||
FSPathMakeRefWithOptions( | ||
cPath.get(), kFSPathMakeRefDoNotFollowLeafSymlink, source, WordFactory.nullPointer()); | ||
var r2 = FSMoveObjectToTrashSync(source, target, kFSFileOperationDefaultOptions); | ||
return r1 == 0 && r2 == 0; | ||
} catch (Throwable error) { | ||
return false; | ||
} finally { | ||
UnmanagedMemory.free(source); | ||
UnmanagedMemory.free(target); | ||
} | ||
} | ||
|
||
public static final class CoreServices implements CContext.Directives { | ||
@Override | ||
public boolean isInConfiguration() { | ||
return Platform.getOperatingSystem().isMacOs(); | ||
} | ||
|
||
@Override | ||
public List<String> getHeaderFiles() { | ||
return Arrays.asList("<CoreServices/CoreServices.h>"); | ||
} | ||
|
||
@Override | ||
public List<String> getLibraries() { | ||
return Arrays.asList("-framework CoreServices"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is quite common in Enso to use
UUID
- which is usually created randomly based on various conditions including time seed. Is not it easier to removeRandomUtils
and just ask forUUID
?