Skip to content
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 34 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f8f0c08
feat: trash
4e6 Jul 1, 2024
0e580d6
feat: move project to trash
4e6 Jul 3, 2024
8ffa14f
fix: TrashTest on Windows
4e6 Jul 4, 2024
b0ff163
fix: native image
4e6 Jul 4, 2024
569261f
Merge branch 'develop' into wip/db/10357-add-recycle
4e6 Jul 4, 2024
aa555cc
fix: after merge
4e6 Jul 4, 2024
ff4cbec
DEBUG: lazy trash initialization
4e6 Jul 5, 2024
3c90113
DEBUG: trash initialization
4e6 Jul 5, 2024
80dd6c8
DEBUG: awt initialization
4e6 Jul 5, 2024
df166a7
misc: javafmt
4e6 Jul 8, 2024
5fcd03c
misc: properties setup
4e6 Jul 8, 2024
efd9e05
fix: native image
4e6 Jul 8, 2024
bf177bf
feat: jna trash
4e6 Jul 9, 2024
170284b
feat: native image resources
4e6 Jul 9, 2024
b0d72f0
misc: cleanup properties setup
4e6 Jul 9, 2024
e97b44d
misc: legal review
4e6 Jul 9, 2024
1d296ef
Merge branch 'develop' into wip/db/10357-add-recycle
4e6 Jul 9, 2024
9aa46a8
fix: after merge
4e6 Jul 9, 2024
633f78a
Simplify LazyTrash by initializing in static initializer
JaroslavTulach Jul 21, 2024
e474e39
Making Platform an enum
JaroslavTulach Jul 21, 2024
c82ed2f
Merge branch 'develop' into wip/db/10357-add-recycle
hubertp Jul 23, 2024
0d346f2
Include Jaroslav's prototype
hubertp Jul 23, 2024
9066faa
os dependent
hubertp Jul 23, 2024
081c9d2
fmt
hubertp Jul 23, 2024
8e1e2b8
Fix native image build on linux
hubertp Jul 24, 2024
5a7d9c8
Merge branch 'develop' into wip/db/10357-add-recycle
hubertp Jul 25, 2024
961aa6c
remove jna
hubertp Jul 25, 2024
ccb8bd5
update license
hubertp Jul 25, 2024
0de3f1e
nit
hubertp Jul 25, 2024
2301840
fmt
hubertp Jul 26, 2024
114cdca
Fix tests that broke during Platform refactoring
hubertp Jul 26, 2024
20b892f
Workaround for non-AOT mode
hubertp Jul 26, 2024
c5aee4a
deal with non-empty directories for fallback delete
hubertp Jul 26, 2024
f6d66ab
fix: tests
4e6 Jul 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ val directoryWatcherVersion = "0.18.0"
val flatbuffersVersion = "24.3.25"
val guavaVersion = "32.0.0-jre"
val jlineVersion = "3.23.0"
val jnaVersion = "5.14.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding JNA just get location of thrashbin on Mac and Windows seems like an overkill to me. As far as I know JNA needs a .so or .dll library to work and it first of all unpacks it to /tmp directory and then loads it. I am even surprised this process works in SVM.

val jgitVersion = "6.7.0.202309050840-r"
val kindProjectorVersion = "0.13.2"
val mockitoScalaVersion = "1.17.14"
Expand Down Expand Up @@ -1118,9 +1119,14 @@ lazy val `project-manager` = (project in file("lib/scala/project-manager"))
staticOnLinux = true,
initializeAtRuntime = Seq(
"scala.util.Random",
"com.sun.jna",
"zio.internal.ZScheduler$$anon$4",
"zio.Runtime$",
"zio.FiberRef$"
),
additionalOptions = Seq(
"-H:+AddAllCharsets",
"-H:+IncludeAllLocales"
)
)
.dependsOn(VerifyReflectionSetup.run)
Expand Down Expand Up @@ -2829,8 +2835,10 @@ lazy val `desktop-environment` =
.settings(
frgaalJavaCompilerSetting,
libraryDependencies ++= Seq(
"junit" % "junit" % junitVersion % Test,
"com.github.sbt" % "junit-interface" % junitIfVersion % Test
"commons-io" % "commons-io" % commonsIoVersion,
"net.java.dev.jna" % "jna-platform" % jnaVersion,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please write a test and make sure none of the JNA classes is visible in engine? Something like CompilerDependenciesTest but in the engine/runner and engine/runtime-fat-jar?

"junit" % "junit" % junitVersion % Test,
"com.github.sbt" % "junit-interface" % junitIfVersion % Test
)
)

Expand Down
10 changes: 10 additions & 0 deletions distribution/project-manager/THIRD-PARTY/NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ The license file can be found at `licenses/APACHE2.0`.
Copyright notices related to this dependency can be found in the directory `jakarta.inject.jakarta.inject-api-2.0.1`.


'jna', licensed under the Apache-2.0, is distributed with the project-manager.
The license file can be found at `licenses/APACHE2.0`.
Copyright notices related to this dependency can be found in the directory `net.java.dev.jna.jna-5.14.0`.


'jna-platform', licensed under the Apache-2.0, is distributed with the project-manager.
The license file can be found at `licenses/APACHE2.0`.
Copyright notices related to this dependency can be found in the directory `net.java.dev.jna.jna-platform-5.14.0`.


'commons-compress', licensed under the Apache-2.0, is distributed with the project-manager.
The license file can be found at `licenses/APACHE2.0`.
Copyright notices related to this dependency can be found in the directory `org.apache.commons.commons-compress-1.23.0`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (c) 2007 Timothy Wall, All Rights Reserved

Copyright (c) 2007 Wayne Meissner, All Rights Reserved

Copyright (c) 2007-2008 Timothy Wall, All Rights Reserved

Copyright (c) 2007-2012 Timothy Wall, All Rights Reserved

Copyright (c) 2007-2013 Timothy Wall, All Rights Reserved

Copyright (c) 2007-2015 Timothy Wall, All Rights Reserved

Copyright (c) 2009 Timothy Wall, All Rights Reserved

Copyright (c) 2011 Timothy Wall, All Rights Reserved

Copyright (c) 2012 Timothy Wall, All Rights Reserved

Copyright (c) 2017 Matthias Bläsing, All Rights Reserved

Copyright (c) 2018 Matthias Bläsing

Copyright (c) 2019 Matthias Bläsing, All Rights Reserved

Copyright (c) 2021, Matthias Bläsing, All Rights Reserved

Copyright (c) 2022 Carlos Ballesteros, All Rights Reserved

Copyright 2007 Timothy Wall
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Copyright (c) 2007 Olivier Chafik

Copyright (c) 2007 Olivier Chafik, All Rights Reserved

Copyright (c) 2007 Timothy Wall, All Rights Reserved

Copyright (c) 2007, 2013 Timothy Wall, Markus Karg, All Rights Reserved

Copyright (c) 2007-2008 Timothy Wall, All Rights Reserved

Copyright (c) 2007-2013 Timothy Wall, All Rights Reserved

Copyright (c) 2008 Timothy Wall, All Rights Reserved

Copyright (c) 2010 Daniel Doubrovkine, All Rights Reserved

Copyright (c) 2010 EugineLev, All Rights Reserved

Copyright (c) 2010 Timothy Wall, All Rights Reserved

Copyright (c) 2010, 2013 Daniel Doubrovkine, Markus Karg, All Rights Reserved

Copyright (c) 2010,2011 Daniel Doubrovkine, All Rights Reserved

Copyright (c) 2011 Daniel Doubrovkine, All Rights Reserved

Copyright (c) 2011 Denis Tulskiy

Copyright (c) 2011 Timothy Wall, All Rights Reserved

Copyright (c) 2012 Tobias Wolf, All Rights Reserved

Copyright (c) 2013 Ralf Hamberger, Markus Karg, All Rights Reserved

Copyright (c) 2013 Tobias Wolf, All Rights Reserved

Copyright (c) 2014 Dr David H. Akehurst (itemis), All Rights Reserved

Copyright (c) 2014 Reinhard Pointner, All Rights Reserved

Copyright (c) 2015 Adam Marcionek, All Rights Reserved

Copyright (c) 2015 Andreas "PAX" L\u00FCck, All Rights Reserved

Copyright (c) 2015 Daniel Widdis

Copyright (c) 2015 Goldstein Lyor, 2021 Daniel Widdis, All Rights Reserved

Copyright (c) 2015 Goldstein Lyor, All Rights Reserved

Copyright (c) 2015 Michael Freeman, All Rights Reserved

Copyright (c) 2016 Adam Marcionek, All Rights Reserved

Copyright (c) 2016 Minoru Sakamoto, All Rights Reserved

Copyright (c) 2017 Daniel Widdis, All Rights Reserved

Copyright (c) 2017 Matthias Bläsing, All Rights Reserved

Copyright (c) 2018 Daniel Widdis, All Rights Reserved

Copyright (c) 2018 Matthias Bläsing, All Rights Reserved

Copyright (c) 2018 Roshan Muralidharan, All Rights Reserved

Copyright (c) 2018 Václav Haisman, All Rights Reserved

Copyright (c) 2018, 2021 Daniel Widdis, All Rights Reserved

Copyright (c) 2018,2020,2021 Daniel Widdis, All Rights Reserved

Copyright (c) 2019 Daniel Widdis

Copyright (c) 2019 Daniel Widdis, All Rights Reserved

Copyright (c) 2019 Keve Müller

Copyright (c) 2019, 2021 Daniel Widdis

Copyright (c) 2020 Daniel Widdis, All Rights Reserved

Copyright (c) 2020 Torbjörn Svensson, All Rights Reserved

Copyright (c) 2022 Daniel Widdis, All Rights Reserved

Copyright (c) 2023 Reinhard Pointner, All Rights Reserved

Copyright 2010 Digital Rapids Corp.

Copyright 2014 Martin Steiger
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ final class DirectoriesFactory {
private static final Directories INSTANCE = initDirectories();

private static Directories initDirectories() {
if (Platform.isLinux()) {
return new LinuxDirectories();
}

if (Platform.isMacOs()) {
return new MacOsDirectories();
}

if (Platform.isWindows()) {
return new WindowsDirectories();
}

throw new UnsupportedOperationException("Unsupported OS '" + Platform.getOsName() + "'");
return switch (Platform.getOperatingSystem()) {
case LINUX -> new LinuxDirectories();
case MACOS -> new MacOsDirectories();
case WINDOWS -> new WindowsDirectories();
};
}

private DirectoriesFactory() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.enso.desktopenvironment;

import com.sun.jna.platform.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* JNA trash implementation calling native platform-dependent APIs.
*
* <p>Native {@link FileUtils} are implemented for Windows and macOS platforms.
*/
final class JnaTrash implements Trash {

@Override
public boolean isSupported() {
return FileUtils.getInstance().hasTrash();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mac OS X implementation is using:

Copy link
Member

@JaroslavTulach JaroslavTulach Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maven project capable to build target/Trash binary that moves provided file to Mac OS X trash:

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..5da15f0
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.apidesign.demo.trash</groupId>
+    <artifactId>Trash</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>21</maven.compiler.source>
+        <maven.compiler.target>21</maven.compiler.target>
+        <exec.mainClass>org.apidesign.demo.trash.Trash</exec.mainClass>
+    </properties>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.graalvm.buildtools</groupId>
+                <artifactId>native-maven-plugin</artifactId>
+                <version>0.10.2</version>
+                <extensions>true</extensions>
+                <executions>
+                    <execution>
+                        <id>build-native</id>
+                        <goals>
+                            <goal>compile-no-fork</goal>
+                        </goals>
+                        <phase>package</phase>
+                    </execution>
+                    <execution>
+                        <id>test-native</id>
+                        <goals>
+                            <goal>test</goal>
+                        </goals>
+                        <phase>test</phase>
+                    </execution>
+                </executions>
+                <configuration>
+                    <mainClass>org.apidesign.demo.trash.Trash</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/src/main/java/org/apidesign/demo/trash/Trash.java b/src/main/java/org/apidesign/demo/trash/Trash.java
new file mode 100644
index 0000000..d69262c
--- /dev/null
+++ b/src/main/java/org/apidesign/demo/trash/Trash.java
@@ -0,0 +1,35 @@
+package org.apidesign.demo.trash;
+
+import org.graalvm.nativeimage.UnmanagedMemory;
+import org.graalvm.nativeimage.c.function.CFunction;
+import org.graalvm.nativeimage.c.function.CLibrary;
+import org.graalvm.nativeimage.c.type.CCharPointer;
+import org.graalvm.nativeimage.c.type.CTypeConversion;
+import org.graalvm.word.Pointer;
+import org.graalvm.word.WordFactory;
+
+@CLibrary("-framework CoreServices")
+public class Trash {
+
+    public static void main(String[] args) {
+        System.out.println("Moving " + args[0] + " to trash");
+        Pointer ref = WordFactory.nullPointer();
+        Pointer source = UnmanagedMemory.malloc(80);
+        Pointer target = UnmanagedMemory.malloc(80);
+
+        var kFSPathMakeRefDoNotFollowLeafSymlink = 0x01;
+        var kFSFileOperationDefaultOptions = 0x00;
+        try (var path = CTypeConversion.toCString(args[0])) {
+            var r1 = FSPathMakeRefWithOptions(path.get(), kFSPathMakeRefDoNotFollowLeafSymlink, source, ref);
+            System.err.println("result one: " + r1);
+            var r2 = FSMoveObjectToTrashSync(source, target, kFSFileOperationDefaultOptions);
+            System.err.println("result two: " + r2);
+        }
+    }
+
+    @CFunction
+    static native int FSPathMakeRefWithOptions(CCharPointer path, int flags, Pointer buffer, Pointer any);
+
+    @CFunction
+    static native int FSMoveObjectToTrashSync(Pointer source, Pointer target, int flags);
+}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improved version that also conditionally enables itself only on Mac:

package org.apidesign.demo.trash;

import java.util.Arrays;
import java.util.List;

import org.graalvm.nativeimage.UnmanagedMemory;
import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.constant.CConstant;
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(Trash.CoreServices.class)
public class Trash {

    public static void main(String[] args) {
        System.out.println("Moving " + args[0] + " to trash");
        Pointer ref = WordFactory.nullPointer();
        Pointer source = UnmanagedMemory.malloc(80);
        Pointer target = UnmanagedMemory.malloc(80);

        try (var path = CTypeConversion.toCString(args[0])) {
            var r1 = FSPathMakeRefWithOptions(path.get(), kFSPathMakeRefDoNotFollowLeafSymlink(), source, ref);
            System.err.println("result one: " + r1);
            var r2 = FSMoveObjectToTrashSync(source, target, kFSFileOperationDefaultOptions());
            System.err.println("result two: " + r2);
        }
        UnmanagedMemory.free(source);
        UnmanagedMemory.free(target);
    }

    @CConstant
    static native int kFSPathMakeRefDoNotFollowLeafSymlink();

    @CConstant
    static native int kFSFileOperationDefaultOptions();

    @CFunction
    static native int FSPathMakeRefWithOptions(CCharPointer path, int flags, Pointer buffer, Pointer any);

    @CFunction
    static native int FSMoveObjectToTrashSync(Pointer source, Pointer target, int flags);

    public static final class CoreServices implements CContext.Directives {
        @Override
        public boolean isInConfiguration() {
            return System.getProperty("os.name").contains("Mac");
        }

        @Override
        public List<String> getHeaderFiles() {
            return Arrays.asList("<CoreServices/CoreServices.h>");
        }

        @Override
        public List<String> getLibraries() {
            return Arrays.asList("-framework CoreServices");
        }

    }
}

}

@Override
public boolean moveToTrash(Path path) {
if (Files.exists(path) && isSupported()) {
try {
FileUtils.getInstance().moveToTrash(path.toFile());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Windows implementation is defined at W32FileUtils and it seems to call into:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation of move to trash for Windows:

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..c5d905e
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.apidesign.demo</groupId>
+    <artifactId>trash</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>21</maven.compiler.source>
+        <maven.compiler.target>21</maven.compiler.target>
+        <exec.mainClass>org.apidesign.demo.trash.Trash</exec.mainClass>
+    </properties>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.graalvm.buildtools</groupId>
+                <artifactId>native-maven-plugin</artifactId>
+                <version>0.10.2</version>
+                <extensions>true</extensions>
+                <executions>
+                    <execution>
+                        <id>build-native</id>
+                        <goals>
+                            <goal>compile-no-fork</goal>
+                        </goals>
+                        <phase>package</phase>
+                    </execution>
+                    <execution>
+                        <id>test-native</id>
+                        <goals>
+                            <goal>test</goal>
+                        </goals>
+                        <phase>test</phase>
+                    </execution>
+                </executions>
+                <configuration>
+                    <mainClass>org.apidesign.demo.trash.Trash</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/src/main/java/org/apidesign/demo/trash/Trash.java b/src/main/java/org/apidesign/demo/trash/Trash.java
new file mode 100644
index 0000000..1a536b0
--- /dev/null
+++ b/src/main/java/org/apidesign/demo/trash/Trash.java
@@ -0,0 +1,64 @@
+package org.apidesign.demo.trash;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.graalvm.nativeimage.StackValue;
+import org.graalvm.nativeimage.c.CContext;
+import org.graalvm.nativeimage.c.constant.CConstant;
+import org.graalvm.nativeimage.c.function.CFunction;
+import org.graalvm.nativeimage.c.struct.CField;
+import org.graalvm.nativeimage.c.struct.CStruct;
+import org.graalvm.nativeimage.c.type.CCharPointer;
+import org.graalvm.nativeimage.c.type.CTypeConversion;
+import org.graalvm.word.PointerBase;
+
+@CContext(Trash.ShellApi.class)
+public class Trash {
+
+    public static void main(String[] args) {
+        System.out.println("Moving " + args[0] + " to trash");
+        try (var file = CTypeConversion.toCString(args[0] + "\0")) {
+            var fileop = StackValue.get(SHFileOperation.class);
+            fileop.wFunc(FO_DELETE());
+            fileop.pFrom(file.get());
+            fileop.fFlags((short) (FOF_ALLOWUNDO() | FOF_NO_UI()));
+            var res = SHFileOperationA(fileop);
+            System.out.println("res: 0x" + Integer.toHexString(res));
+        }
+    }
+
+    @CConstant
+    public static native int FO_DELETE();
+    @CConstant
+    public static native int FOF_ALLOWUNDO();
+    @CConstant
+    public static native int FOF_NO_UI();
+
+    @CStruct(value="SHFILEOPSTRUCTA")
+    static interface SHFileOperation extends PointerBase {
+        @CField
+        void pFrom(CCharPointer from);
+        @CField
+        void wFunc(int op);
+        @CField
+        void fFlags(short flags);
+    }
+
+    @CFunction
+    static native int SHFileOperationA(SHFileOperation fileop);
+
+    public static final class ShellApi implements CContext.Directives {
+
+        @Override
+        public List<String> getHeaderFiles() {
+            return Arrays.asList("<windows.h>");
+        }
+
+        @Override
+        public List<String> getLibraries() {
+            return Arrays.asList("shell32");
+        }
+
+    }    
+}


return true;
} catch (IOException ignored) {
return false;
}
}

return false;
}
}
Loading
Loading