Search in sources :

Example 6 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class FileHelper method chown.

public static boolean chown(final File f, String owner, String group, boolean recursive) throws IOException {
    if (!f.exists())
        throw new FileNotFoundException("Cannot chown a non-existant file!");
    if (owner == null)
        owner = "";
    else if (group == null)
        group = "";
    final String ownerGroupPair;
    if (owner.isEmpty() && group.isEmpty())
        throw new IllegalArgumentException("Must specify an owner or a group to change ownership to");
    else if (group.isEmpty())
        ownerGroupPair = owner;
    else
        ownerGroupPair = owner + "." + group;
    try {
        final String[] cmd;
        if (recursive)
            cmd = new String[] { "chown", "--recursive", ownerGroupPair, f.getPath() };
        else
            cmd = new String[] { "chown", ownerGroupPair, f.getPath() };
        Execed call = Exec.rootUtility(cmd);
        int returnCode = call.waitForExit();
        return returnCode == 0;
    } catch (Exception e) {
        log.error("[FileHelper] {chown} Failure: " + e.getMessage(), e);
        return false;
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class FileHelper method chmod.

/**
 * Performs a chmod (which assumes this system is Linux/UNIX/Solaris/etc), replacing the permissions on <code>f</code> with
 * the permissions on <code>copyOf</code>
 *
 * @param f
 * @param copyOf
 * 		the file to use the permissions from
 *
 * @return
 *
 * @throws IOException
 */
public static boolean chmod(String as, File f, File copyOf) {
    if (!f.exists()) {
        log.error("[FileHelper] {chmod} Non-existant file: " + f.getPath());
        return false;
    }
    if (!copyOf.exists()) {
        log.error("[FileHelper] {chmod} Non-existant file: " + copyOf.getPath());
        return false;
    }
    try {
        Execed call = Exec.utilityAs(as, "chmod", "--reference=" + copyOf.getPath(), f.getPath());
        int returnCode = call.waitForExit();
        return returnCode == 0;
    } catch (Exception e) {
        log.error("[LockRecord] {chmod} Failure: " + e.getMessage(), e);
        return false;
    }
}
Also used : Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 8 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class NginxService method reload.

/**
 * Reload the nginx configuration
 */
public void reload() {
    try {
        final Execed process = Exec.rootUtility(new File(binPath, "nginx-reload").getAbsolutePath());
        process.waitForExit(new Timeout(30, TimeUnit.SECONDS).start(), 0);
    } catch (IOException e) {
        throw new RuntimeException("Error executing nginx-reload command", e);
    }
}
Also used : Timeout(com.peterphi.std.threading.Timeout) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException) File(java.io.File)

Example 9 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class OpenSSLPKCS12 method PEMtoP12.

/**
 * @param pem
 * 		the PEM file containing the keys & certificates to put in a P12
 * @param pemPassword
 * 		The password for any encrypted keys in the PEM
 * @param toP12
 * 		The PKCS12 file
 * @param p12Password
 * 		the password to put on the PKCS12 keystore
 *
 * @throws IOException
 * 		if a catastrophic unexpected failure occurs during execution
 * @throws IllegalArgumentException
 * 		if the PEM keystore doesn't exist
 * @throws IllegalStateException
 * 		if openssl exits with a failure condition
 */
public static void PEMtoP12(File pem, String pemPassword, File toP12, String p12Password) throws IOException {
    if (!pem.exists())
        throw new IllegalArgumentException("pem file does not exist: " + pem.getPath());
    Execed openssl = Exec.utilityAs(null, OPENSSL, "pkcs12", "-nodes", "-in", pem.getPath(), "-out", toP12.getPath(), "-export", "-passin", "pass:" + pemPassword, "-passout", "pass:" + p12Password);
    int returnCode = openssl.waitForExit();
    if (returnCode != 0)
        throw new IllegalStateException("Unexpected openssl exit code " + returnCode + "; output:\n" + openssl.getStandardOut());
}
Also used : Execed(com.peterphi.std.system.exec.Execed)

Example 10 with Execed

use of com.peterphi.std.system.exec.Execed in project stdlib by petergeneric.

the class MediaInfoCommand method inspect.

public MediaInfo inspect(File mediafile) throws IOException {
    log.debug("Retrieving mediainfo output for " + mediafile.getAbsolutePath());
    if (!mediafile.exists())
        throw new IllegalArgumentException("Media file " + mediafile + " does not exist!");
    Exec exec = new Exec(mediainfo.getAbsolutePath(), "--output=XML", "--full", mediafile.getAbsolutePath());
    final Execed process = exec.start();
    final Deadline timeOut = new Deadline(Timeout.ONE_MINUTE);
    final int result = process.waitForExit(timeOut);
    if (result == 0) {
        return parse(process.getStandardOut());
    } else {
        throw new IOException("MediaInfo failed with non-zero code: " + result);
    }
}
Also used : Exec(com.peterphi.std.system.exec.Exec) Deadline(com.peterphi.std.threading.Deadline) Execed(com.peterphi.std.system.exec.Execed) IOException(java.io.IOException)

Aggregations

Execed (com.peterphi.std.system.exec.Execed)10 IOException (java.io.IOException)8 FileNotFoundException (java.io.FileNotFoundException)4 Timeout (com.peterphi.std.threading.Timeout)3 File (java.io.File)3 Exec (com.peterphi.std.system.exec.Exec)1 Deadline (com.peterphi.std.threading.Deadline)1