<commit_before>package org.openforis.sepal.component.files.internal

import org.openforis.sepal.component.files.api.InvalidPath

class UserDir {
    private final File userDir

    UserDir(File homeDir, String username) {
        userDir = new File(homeDir, username).canonicalFile
        if (!userDir.exists())
            throw new InvalidPath("No user directory for user: $username")
    }

    List<File> list(String path) {
        def dir = toUserDirFile(path)
        if (!dir.isDirectory())
            throw new InvalidPath("Path does not point to a directory. userDir: $userDir, dir: $dir")
        return dir.listFiles().toList()
    }

    void delete(String path) {
        def file = toUserDirFile(path)
        if (file == userDir)
            throw new InvalidPath("Cannot delete users dir: $userDir")
        if (file.isDirectory())
            file.deleteDir()
        else
            file.delete()
    }

    InputStream read(String path) {
        def file = toUserDirFile(path)
        if (!file.exists())
            throw new InvalidPath("Path point to a non-existing file. userDir: $userDir, file: $file")
        if (file.isDirectory())
            throw new InvalidPath("Path point to a directory. userDir: $userDir, file: $file")
        return file.newInputStream()
    }

    private File toUserDirFile(String path) {
        def file = new File(userDir, path).canonicalFile
        assertInUserDir(file)
        return file
    }

    private void assertInUserDir(File file) {
        if (!inUserDir(file))
            throw new InvalidPath("Path outside of user directory. userDir: $userDir, file: ${file}")
    }

    private boolean inUserDir(File dir) {
        dir.absolutePath.startsWith(userDir.absolutePath)
    }
}
<commit_msg>Sorted file list.<commit_after>package org.openforis.sepal.component.files.internal

import org.openforis.sepal.component.files.api.InvalidPath

class UserDir {
    private final File userDir

    UserDir(File homeDir, String username) {
        userDir = new File(homeDir, username).canonicalFile
        if (!userDir.exists())
            throw new InvalidPath("No user directory for user: $username")
    }

    List<File> list(String path) {
        def dir = toUserDirFile(path)
        if (!dir.isDirectory())
            throw new InvalidPath("Path does not point to a directory. userDir: $userDir, dir: $dir")
        return dir.listFiles().toList().sort()
    }

    void delete(String path) {
        def file = toUserDirFile(path)
        if (file == userDir)
            throw new InvalidPath("Cannot delete users dir: $userDir")
        if (file.isDirectory())
            file.deleteDir()
        else
            file.delete()
    }

    InputStream read(String path) {
        def file = toUserDirFile(path)
        if (!file.exists())
            throw new InvalidPath("Path point to a non-existing file. userDir: $userDir, file: $file")
        if (file.isDirectory())
            throw new InvalidPath("Path point to a directory. userDir: $userDir, file: $file")
        return file.newInputStream()
    }

    private File toUserDirFile(String path) {
        def file = new File(userDir, path).canonicalFile
        assertInUserDir(file)
        return file
    }

    private void assertInUserDir(File file) {
        if (!inUserDir(file))
            throw new InvalidPath("Path outside of user directory. userDir: $userDir, file: ${file}")
    }

    private boolean inUserDir(File dir) {
        dir.absolutePath.startsWith(userDir.absolutePath)
    }
}

{'fraction_non_alphanumeric': 0.10262193402875669, 'fraction_numerical': 0.0, 'mean_word_length': 2.5093966369930762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665482', 'n_tokens_mistral': 1008, 'n_tokens_neox': 991, 'n_words': 284}