Example usage for org.apache.commons.io FileUtils sizeOfDirectory

List of usage examples for org.apache.commons.io FileUtils sizeOfDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils sizeOfDirectory.

Prototype

public static long sizeOfDirectory(File directory) 

Source Link

Document

Counts the size of a directory recursively (sum of the length of all files).

Usage

From source file:com.thoughtworks.go.publishers.GoArtifactsManipulator.java

public void publish(DefaultGoPublisher goPublisher, String destPath, File source, JobIdentifier jobIdentifier) {
    if (!source.exists()) {
        String message = "Failed to find " + source.getAbsolutePath();
        goPublisher.taggedConsumeLineWithPrefix(PUBLISH_ERR, message);
        bomb(message);//from  w w w  .ja  va2s.  c om
    }

    int publishingAttempts = 0;
    Throwable lastException = null;
    while (publishingAttempts < PUBLISH_MAX_RETRIES) {
        File tmpDir = null;
        try {
            publishingAttempts++;

            tmpDir = FileUtil.createTempFolder();
            File dataToUpload = new File(tmpDir, source.getName() + ".zip");
            zipUtil.zip(source, dataToUpload, Deflater.BEST_SPEED);

            long size = 0;
            if (source.isDirectory()) {
                size = FileUtils.sizeOfDirectory(source);
            } else {
                size = source.length();
            }

            goPublisher.taggedConsumeLineWithPrefix(PUBLISH,
                    "Uploading artifacts from " + source.getAbsolutePath() + " to " + getDestPath(destPath));

            String normalizedDestPath = FilenameUtils.separatorsToUnix(destPath);
            String url = urlService.getUploadUrlOfAgent(jobIdentifier, normalizedDestPath, publishingAttempts);

            int statusCode = httpService.upload(url, size, dataToUpload,
                    artifactChecksums(source, normalizedDestPath));

            if (statusCode == HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE) {
                String message = String.format(
                        "Artifact upload for file %s (Size: %s) was denied by the server. This usually happens when server runs out of disk space.",
                        source.getAbsolutePath(), size);
                goPublisher.taggedConsumeLineWithPrefix(PUBLISH_ERR, message);
                LOGGER.error(
                        "[Artifact Upload] Artifact upload was denied by the server. This usually happens when server runs out of disk space.");
                publishingAttempts = PUBLISH_MAX_RETRIES;
                bomb(message + ".  HTTP return code is " + statusCode);
            }
            if (statusCode < HttpServletResponse.SC_OK
                    || statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES) {
                bomb("Failed to upload " + source.getAbsolutePath() + ".  HTTP return code is " + statusCode);
            }
            return;
        } catch (Throwable e) {
            String message = "Failed to upload " + source.getAbsolutePath();
            LOGGER.error(message, e);
            goPublisher.taggedConsumeLineWithPrefix(PUBLISH_ERR, message);
            lastException = e;
        } finally {
            FileUtils.deleteQuietly(tmpDir);
        }
    }
    if (lastException != null) {
        throw new RuntimeException(lastException);
    }
}

From source file:com.linkedin.pinot.controller.validation.StorageQuotaChecker.java

/**
 * check if the segment represented by segmentFile is within the storage quota
 * @param segmentFile untarred segment. This should not be null.
 *                    segmentFile must exist on disk and must be a directory
 * @param tableNameWithType table name without type (OFFLINE/REALTIME) information
 * @param segmentName name of the segment being added
 * @param timeoutMsec timeout in milliseconds for reading table sizes from server
 *
 *//*from  ww w .  j a  va 2 s  .  c o  m*/
public QuotaCheckerResponse isSegmentStorageWithinQuota(@Nonnull File segmentFile,
        @Nonnull String tableNameWithType, @Nonnull String segmentName, @Nonnegative int timeoutMsec) {
    Preconditions.checkNotNull(segmentFile);
    Preconditions.checkNotNull(tableNameWithType);
    Preconditions.checkNotNull(segmentName);
    Preconditions.checkArgument(timeoutMsec > 0, "Timeout value must be > 0, input: %s", timeoutMsec);
    Preconditions.checkArgument(segmentFile.exists(), "Segment file: %s does not exist", segmentFile);
    Preconditions.checkArgument(segmentFile.isDirectory(), "Segment file: %s is not a directory", segmentFile);

    // 1. Read table config
    // 2. read table size from all the servers
    // 3. update predicted segment sizes
    // 4. is the updated size within quota
    QuotaConfig quotaConfig = tableConfig.getQuotaConfig();
    int numReplicas = tableConfig.getValidationConfig().getReplicationNumber();
    final String tableName = tableConfig.getTableName();

    if (quotaConfig == null) {
        // no quota configuration...so ignore for backwards compatibility
        return new QuotaCheckerResponse(true, "Quota configuration not set for table: " + tableNameWithType);
    }

    long allowedStorageBytes = numReplicas * quotaConfig.storageSizeBytes();
    if (allowedStorageBytes < 0) {
        return new QuotaCheckerResponse(true,
                "Storage quota is not configured for table: " + tableNameWithType);
    }

    long incomingSegmentSizeBytes = FileUtils.sizeOfDirectory(segmentFile);

    // read table size
    TableSizeReader.TableSubTypeSizeDetails tableSubtypeSize = tableSizeReader
            .getTableSubtypeSize(tableNameWithType, timeoutMsec);

    // If the segment exists(refresh), get the existing size
    TableSizeReader.SegmentSizeDetails sizeDetails = tableSubtypeSize.segments.get(segmentName);
    long existingSegmentSizeBytes = sizeDetails != null ? sizeDetails.estimatedSizeInBytes : 0;

    long estimatedFinalSizeBytes = tableSubtypeSize.estimatedSizeInBytes - existingSegmentSizeBytes
            + incomingSegmentSizeBytes;
    if (estimatedFinalSizeBytes <= allowedStorageBytes) {
        return new QuotaCheckerResponse(true, String.format(
                "Estimated size: %d bytes is within the configured quota of %d (bytes) for table %s. Incoming segment size: %d (bytes)",
                estimatedFinalSizeBytes, allowedStorageBytes, tableName, incomingSegmentSizeBytes));
    } else {
        return new QuotaCheckerResponse(false, String.format(
                "Estimated size: %d bytes exceeds the configured quota of %d (bytes) for table %s. Incoming segment size: %d (bytes)",
                estimatedFinalSizeBytes, allowedStorageBytes, tableName, incomingSegmentSizeBytes));
    }
}

From source file:com.tyndalehouse.step.tools.modules.ConvertXmlToOSISModule.java

private void convertToXml(final String moduleName, final File osisSource) throws Exception {
    LOGGER.debug("Reading [{}]", moduleName);

    SAXParserFactory spf = SAXParserFactory.newInstance();
    final SAXParser saxParser = spf.newSAXParser();
    final ExtractHeaderInformationSax header = new ExtractHeaderInformationSax();
    saxParser.parse(osisSource, header);

    LOGGER.debug(/* w  ww  . j  a v  a2s.  c  o m*/
            "title:[{}], description:[{}], copyright:[{}], license:[{}], language:[{}], versification:[{}]",
            header.getTitle(), header.getDescription(), header.getCopyright(), header.getLicense(),
            header.getLanguage(), header.getVersification());

    String sanitizedModuleName = moduleName.replace("-", "").toLowerCase();

    File outputDirectory = new File(BASE_OUTPUT, sanitizedModuleName);
    outputDirectory.mkdirs();
    BASE_ERRORS.mkdirs();
    //        
    //        LOGGER.debug("Converting [{}] to OSIS Module", sanitizedModuleName);
    //        Process p = Runtime.getRuntime().exec(String.format("osis2mod %s %s -z -v %s", outputDirectory.getAbsolutePath(), osisSource.getAbsolutePath(), header.getVersification()));
    //        LOGGER.debug("Conversion of [{}] finished.", sanitizedModuleName);
    //        outputErrors(p, moduleName);
    //        p.waitFor();

    outputConfFile(header, sanitizedModuleName, FileUtils.sizeOfDirectory(outputDirectory));

}

From source file:de.uzk.hki.da.cb.UnpackNoBagitAction.java

@Override
public boolean implementation() throws IOException {
    long size = 0L;
    if (sipContainerOnIngestAreaIsDir())
        size = FileUtils.sizeOfDirectory(sipContainerInIngestAreaNoBagit());
    else//from  ww w  .  j ava2s  . co  m
        size = sipContainerInIngestAreaNoBagit().length();

    if (!ingestGateNoBagit.canHandle(size)) {
        logger.warn(
                "ResourceMonitor prevents further processing of package due to space limitations. Setting job back to start state.");
        return false;
    }

    wa.ingestSIP(sipContainerInIngestAreaNoBagit());

    moveSipDir();

    throwUserExceptionIfDuplicatesExist();
    throwUserExceptionIfNotPremisConsistent();

    // Is the last step of action because it should only happen after validity has been proven. 
    logger.info("Removing SIP from IngestArea");
    if (!sipContainerOnIngestAreaIsDir()) {
        sipContainerInIngestAreaNoBagit().delete();
    } else
        FolderUtils.deleteDirectorySafe(sipContainerInIngestAreaNoBagit());
    return true;
}

From source file:com.splunk.shuttl.archiver.model.Bucket.java

private Long setSizeOnLocalBucket() {
    return isUriSet() && !isRemote() ? FileUtils.sizeOfDirectory(directory) : null;
}

From source file:de.uzk.hki.da.cb.UnpackAction.java

@Override
public boolean implementation() throws IOException {
    long size = 0L;
    if (sipContainerOnIngestAreaIsDir())
        size = FileUtils.sizeOfDirectory(sipContainerOnIngestArea());
    else//from   w  ww  .  j a  v  a2s . c o  m
        size = sipContainerOnIngestArea().length();

    if (!ingestGate.canHandle(size)) {
        //         JmsMessage jms = new JmsMessage(C.QUEUE_TO_CLIENT,C.QUEUE_TO_SERVER,o.getIdentifier() + " - Please check WorkArea space limitations: " + ingestGate.getFreeDiskSpacePercent() +" % free needed " );
        //         super.getJmsMessageServiceHandler().sendJMSMessage(jms);   
        logger.warn(
                "ResourceMonitor prevents further processing of package due to space limitations. Setting job back to start state.");
        return false;
    }

    wa.ingestSIP(sipContainerOnIngestArea());
    if (!sipContainerOnIngestAreaIsDir()) {
        unpack(wa.sipFile());
        expandDirInto();
        wa.sipFile().delete();
    } else {
        moveSipDir();
        expandDirInto();
    }

    throwUserExceptionIfNotBagitConsistent();
    throwUserExceptionIfDuplicatesExist();
    throwUserExceptionIfNotPremisConsistent();

    // Is the last step of action because it should only happen after validity has been proven. 
    logger.info("Removing SIP from IngestArea");
    if (!sipContainerOnIngestAreaIsDir()) {
        sipContainerOnIngestArea().delete();
    } else
        FolderUtils.deleteDirectorySafe(sipContainerOnIngestArea());
    return true;
}

From source file:com.splunk.shuttl.archiver.usecases.ImportCsvFunctionalTest.java

private long sizeOfBucket(Bucket b) {
    return FileUtils.sizeOfDirectory(b.getDirectory());
}

From source file:com.esofthead.mycollab.module.file.service.impl.FileRawContentServiceImpl.java

@Override
public long getSize(String path) {
    File file = new File(baseFolder + "/" + path);
    if (file.exists()) {
        if (file.isFile()) {
            return FileUtils.sizeOf(file);
        } else if (file.isDirectory()) {
            return FileUtils.sizeOfDirectory(file);
        } else {/* w  ww. j  a  v a 2 s. c o m*/
            return 0;
        }
    }

    return 0;
}

From source file:com.baasbox.metrics.BaasBoxMetric.java

private static void setGauges() {
    //memory gauges
    registry.register(name(GAUGE_MEMORY_MAX_ALLOCABLE), new CachedGauge<Long>(10, TimeUnit.MINUTES) {
        @Override//from   w  w  w  .  java  2s . com
        public Long loadValue() {
            Runtime rt = Runtime.getRuntime();
            long maxMemory = rt.maxMemory();
            return maxMemory;
        }
    });

    registry.register(name(GAUGE_MEMORY_CURRENT_ALLOCATE), new Gauge<Long>() {
        @Override
        public Long getValue() {
            Runtime rt = Runtime.getRuntime();
            long totalMemory = rt.totalMemory();
            return totalMemory;
        }
    });

    registry.register(name(GAUGE_MEMORY_USED), new Gauge<Long>() {
        @Override
        public Long getValue() {
            Runtime rt = Runtime.getRuntime();
            long freeMemory = rt.freeMemory();
            long totalMemory = rt.totalMemory();
            return totalMemory - freeMemory;
        }
    });
    registry.register(name(GAUGE_FILESYSTEM_DATAFILE_SPACE_LEFT),
            new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public Long loadValue() {
                    return new File(BBConfiguration.getDBDir()).getFreeSpace();
                }
            });

    registry.register(name(GAUGE_FILESYSTEM_BACKUPDIR_SPACE_LEFT),
            new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public Long loadValue() {
                    return new File(BBConfiguration.getDBBackupDir()).getFreeSpace();
                }
            });

    registry.register(name(GAUGE_DB_DATA_SIZE), new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
        @Override
        public Long loadValue() {
            boolean opened = false;
            try {
                if (DbHelper.getConnection() == null || DbHelper.getConnection().isClosed()) {
                    DbHelper.open(BBConfiguration.getAPPCODE(), BBConfiguration.getBaasBoxAdminUsername(),
                            BBConfiguration.getBaasBoxAdminUsername());
                    opened = true;
                }
                return DbHelper.getConnection().getSize();
            } catch (InvalidAppCodeException e) {
                throw new RuntimeException(e);
            } finally {
                if (opened)
                    DbHelper.close(DbHelper.getConnection());
            }
        }
    });

    registry.register(name(GAUGE_DB_DATA_DIRECTORY_SIZE),
            new CachedGauge<Long>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public Long loadValue() {
                    return FileUtils.sizeOfDirectory(new File(BBConfiguration.getDBDir()));
                }
            });
    registry.register(name(GAUGE_DB_MAX_SIZE_THRESHOLD),
            new CachedGauge<BigInteger>(CACHE_TIMEOUT, TimeUnit.MINUTES) {
                @Override
                public BigInteger loadValue() {
                    return BBConfiguration.getDBSizeThreshold();
                }
            });

}

From source file:com.withbytes.tentaculo.traverser.windows.WindowsTraverserTest.java

/**
 * Test of backup method, of class WindowsTraverser.
 *///from  www  . j a v  a2 s  . com
@Test
public void testBackup() throws Exception {
    //Create folder to use as destination
    File destination = new File(TestsConfiguration.RESOURCES_PATH + "test");
    destination.delete();
    destination.mkdir();

    IPathTranslator translator = mock(WindowsPathTranslator.class);
    WindowsTraverser instance = new WindowsTraverser();
    File probe;

    //Test cases
    String sourceFile = TestsConfiguration.RESOURCES_PATH + "two\\2.yml";
    String sourceDirectory = TestsConfiguration.RESOURCES_PATH + "two";
    String sourceNonExistant = TestsConfiguration.RESOURCES_PATH + "falsepath";

    when(translator.translatePath(anyString())).thenReturn(sourceFile).thenReturn(sourceDirectory)
            .thenReturn(sourceNonExistant);

    //Checking file copy
    assertEquals(true, instance.backup(sourceFile, translator, destination.getAbsolutePath()));
    probe = new File(destination.getAbsolutePath(), "2.yml");
    assertEquals(true, probe.exists());
    FileUtils.cleanDirectory(destination);

    //Checking directory copy
    assertEquals(true, instance.backup(sourceDirectory, translator, destination.getAbsolutePath()));
    probe = new File(destination.getAbsolutePath(), "two");
    assertEquals(true, probe.exists());
    probe = new File(destination.getAbsolutePath(), "two\\1.yml");
    assertEquals(true, probe.exists());
    FileUtils.cleanDirectory(destination);

    //Checking source doesn't exists
    assertEquals(false, instance.backup(sourceNonExistant, translator, destination.getAbsolutePath()));
    assertEquals(0, FileUtils.sizeOfDirectory(destination));

    //Verify mock
    verify(translator, times(3)).translatePath(anyString());

    //Remove existent destination folder and files
    FileUtils.deleteQuietly(destination);
}