<commit_before>package com.ettrema.http.fs;

import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.CollectionResource;
import com.bradmcevoy.http.CopyableResource;
import com.bradmcevoy.http.DeletableResource;
import com.bradmcevoy.http.GetableResource;
import com.bradmcevoy.http.LockInfo;
import com.bradmcevoy.http.LockResult;
import com.bradmcevoy.http.LockTimeout;
import com.bradmcevoy.http.LockToken;
import com.bradmcevoy.http.LockingCollectionResource;
import com.bradmcevoy.http.MakeCollectionableResource;
import com.bradmcevoy.http.MoveableResource;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.PutableResource;
import com.bradmcevoy.http.Range;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.XmlWriter;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 *
 */
public class FsDirectoryResource extends FsResource implements MakeCollectionableResource, PutableResource, CopyableResource, DeletableResource, MoveableResource, PropFindableResource, LockingCollectionResource, GetableResource {

    public FsDirectoryResource( FileSystemResourceFactory factory, File dir ) {
        super( factory, dir );
        if( !dir.exists() )
            throw new IllegalArgumentException( "Directory does not exist: " + dir.getAbsolutePath() );
        if( !dir.isDirectory() )
            throw new IllegalArgumentException( "Is not a directory: " + dir.getAbsolutePath() );
    }

    public CollectionResource createCollection( String name ) {
        File fnew = new File( file, name );
        boolean ok = fnew.mkdir();
        if( !ok )
            throw new RuntimeException( "Failed to create: " + fnew.getAbsolutePath() );
        return new FsDirectoryResource( factory, fnew );
    }

    public Resource child( String name ) {
        File fchild = new File( file, name );
        return factory.resolveFile( fchild );

    }

    public List<? extends Resource> getChildren() {
        ArrayList<FsResource> list = new ArrayList<FsResource>();
        File[] files = this.file.listFiles();
        if( files != null ) {
            for( File fchild : files ) {
                FsResource res = factory.resolveFile( fchild );
                list.add( res );
            }
        }
        return list;<commit_msg>added null check in getChildren. Shouldnt be necessary but there seems to be some strange case where files are returned by listFiles which then can't be found<commit_after>package com.ettrema.http.fs;

import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.CollectionResource;
import com.bradmcevoy.http.CopyableResource;
import com.bradmcevoy.http.DeletableResource;
import com.bradmcevoy.http.GetableResource;
import com.bradmcevoy.http.LockInfo;
import com.bradmcevoy.http.LockResult;
import com.bradmcevoy.http.LockTimeout;
import com.bradmcevoy.http.LockToken;
import com.bradmcevoy.http.LockingCollectionResource;
import com.bradmcevoy.http.MakeCollectionableResource;
import com.bradmcevoy.http.MoveableResource;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.PutableResource;
import com.bradmcevoy.http.Range;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.XmlWriter;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class FsDirectoryResource extends FsResource implements MakeCollectionableResource, PutableResource, CopyableResource, DeletableResource, MoveableResource, PropFindableResource, LockingCollectionResource, GetableResource {

    private static final Logger log = LoggerFactory.getLogger(FsDirectoryResource.class);

    public FsDirectoryResource( FileSystemResourceFactory factory, File dir ) {
        super( factory, dir );
        if( !dir.exists() )
            throw new IllegalArgumentException( "Directory does not exist: " + dir.getAbsolutePath() );
        if( !dir.isDirectory() )
            throw new IllegalArgumentException( "Is not a directory: " + dir.getAbsolutePath() );
    }

    public CollectionResource createCollection( String name ) {
        File fnew = new File( file, name );
        boolean ok = fnew.mkdir();
        if( !ok )
            throw new RuntimeException( "Failed to create: " + fnew.getAbsolutePath() );
        return new FsDirectoryResource( factory, fnew );
    }

    public Resource child( String name ) {
        File fchild = new File( file, name );
        return factory.resolveFile( fchild );

    }

    public List<? extends Resource> getChildren() {
        ArrayList<FsResource> list = new ArrayList<FsResource>();
        File[] files = this.file.listFiles();
        if( files != null ) {
            for( File fchild : files ) {
                FsResource res = factory.resolveFile( fchild );
                if( res != null ) {
                    list.add( res );
                } else {
                    log.error("Couldnt resolve file {}", fchild.getAbsolutePath());
                }
            }
        }
        return list;
{'fraction_non_alphanumeric': 0.09695484774238712, 'fraction_numerical': 0.00035001750087504374, 'mean_word_length': 3.561053471667997, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4335480', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1651, 'n_words': 387}