Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/bcgsc/ntCard
06 April 2024, 01:58:22 UTC
  • Code
  • Branches (12)
  • Releases (0)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/azure-macOS
    • refs/heads/low_kmer_sample_check
    • refs/heads/master
    • refs/heads/refactor
    • refs/heads/spacedseeds
    • refs/tags/1.0.0
    • refs/tags/1.0.1
    • refs/tags/1.2.0
    • refs/tags/1.2.1
    • refs/tags/1.2.2
    • refs/tags/v1.1.0
    • refs/tags/v1.1.1
    No releases to show
  • 75d2835
  • /
  • Common
  • /
  • Uncompress.cpp
Raw File Download
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
origin badgecontent badge Iframe embedding
swh:1:cnt:3c86c89038c0107b0870e32dc18efb7cd101bb2d
origin badgedirectory badge Iframe embedding
swh:1:dir:944d1666550734b66e20e560551c63ecc7d4f5c9
origin badgerevision badge
swh:1:rev:741c9417282570a71c4042f0d2022493f26c4e49
origin badgesnapshot badge
swh:1:snp:9765a9dc233d6d78dbee568f9c2afba0f84bcc2b
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: 741c9417282570a71c4042f0d2022493f26c4e49 authored by Johnathan Wong on 27 January 2020, 22:56:49 UTC
release 1.2.1 (#40)
Tip revision: 741c941
Uncompress.cpp
/** Uncompress input files using pipes.
 * Hook the standard file opening functions, open, fopen and fopen64.
 * If the extension of the file being opened indicates the file is
 * compressed (.gz, .bz2, .xz), open a pipe to a program that
 * decompresses that file (gunzip, bunzip2 or xzdec) and return a
 * handle to the open pipe.
 * @author Shaun Jackman <sjackman@bcgsc.ca>
 */


#include "Fcontrol.h"
#include "SignalHandler.h"
#include "StringUtil.h"
#include <cassert>
#include <cstdio> // for perror
#include <cstdlib>
#include <dlfcn.h>
#include <string>
#include <unistd.h>

using namespace std;

static const char* wgetExec(const string& path)
{
    return
        startsWith(path, "http://") ? "wget -O-" :
        startsWith(path, "https://") ? "wget -O-" :
        startsWith(path, "ftp://") ? "wget -O-" :
        NULL;
}

static const char* zcatExec(const string& path)
{
    return
        endsWith(path, ".ar") ? "ar -p" :
        endsWith(path, ".tar") ? "tar -xOf" :
        endsWith(path, ".tar.Z") ? "tar -zxOf" :
        endsWith(path, ".tar.gz") ? "tar -zxOf" :
        endsWith(path, ".tar.bz2") ? "tar -jxOf" :
        endsWith(path, ".tar.xz") ?
        "tar --use-compress-program=xzdec -xOf" :
        endsWith(path, ".Z") ? "gunzip -c" :
        endsWith(path, ".gz") ? "gunzip -c" :
        endsWith(path, ".bz2") ? "bunzip2 -c" :
        endsWith(path, ".xz") ? "xzdec -c" :
        endsWith(path, ".zip") ? "unzip -p" :
        endsWith(path, ".bam") ? "samtools view -h" :
        endsWith(path, ".jf") ? "jellyfish dump" :
        endsWith(path, ".jfq") ? "jellyfish qdump" :
        endsWith(path, ".sra") ? "fastq-dump -Z --split-spot" :
        endsWith(path, ".url") ? "wget -O- -i" :
        NULL;
}

extern "C" {

    /** Open a pipe to uncompress the specified file.
     * Not thread safe.
     * @return a file descriptor
     */
    static int uncompress(const char *path)
    {
        const char *wget = wgetExec(path);
        const char *zcat = wget != NULL ? wget : zcatExec(path);
        assert(zcat != NULL);

        int fd[2];
        if (pipe(fd) == -1)
            return -1;
        int err = setCloexec(fd[0]);
        assert(err == 0);
        (void)err;

        char arg0[16], arg1[16], arg2[16];
        int n = sscanf(zcat, "%s %s %s", arg0, arg1, arg2);
        assert(n == 2 || n == 3);

        /* It would be more portable to use fork than vfork, but fork can
         * fail with ENOMEM when the process calling fork is using a lot
         * of memory. A workaround for this problem is to set
         * sysctl vm.overcommit_memory=1
         */
#if HAVE_WORKING_VFORK
        pid_t pid = vfork();
#else
        pid_t pid = fork();
#endif
        if (pid == -1)
            return -1;

        if (pid == 0) {
            dup2(fd[1], STDOUT_FILENO);
            close(fd[1]);
            if (n == 2)
                execlp(arg0, arg0, arg1, path, NULL);
            else
                execlp(arg0, arg0, arg1, arg2, path, NULL);
            // Calling perror after vfork is not allowed, but we're about
            // to exit and an error message would be really helpful.
            perror(arg0);
            _exit(EXIT_FAILURE);
        } else {
            close(fd[1]);
            return fd[0];
        }
    }

    /** Open a pipe to uncompress the specified file.
     * @return a FILE pointer
     */
    static FILE* funcompress(const char* path)
    {
        int fd = uncompress(path);
        if (fd == -1) {
            perror(path);
            exit(EXIT_FAILURE);
        }
        return fdopen(fd, "r");
    }

    typedef FILE* (*fopen_t)(const char *path, const char *mode);

    /** If the specified file is compressed, return a pipe that
     * uncompresses it.
     */
    FILE *fopen(const char *path, const char *mode)
    {
        static fopen_t real_fopen;
        if (real_fopen == NULL)
            real_fopen = (fopen_t)dlsym(RTLD_NEXT, "fopen");
        if (real_fopen == NULL) {
            fprintf(stderr, "error: dlsym fopen: %s\n", dlerror());
            exit(EXIT_FAILURE);
        }

        // open a web address
        if (wgetExec(path) != NULL)
            return funcompress(path);

        // to check if the file exists, we need to attempt to open it
        FILE* stream = real_fopen(path, mode);
        if (string(mode) != "r" || !stream || zcatExec(path) == NULL)
            return stream;
        else {
            fclose(stream);
            return funcompress(path);
        }
    }

    /** If the specified file is compressed, return a pipe that
     * uncompresses it.
     */
    FILE *fopen64(const char *path, const char *mode)
    {
        static fopen_t real_fopen64;
        if (real_fopen64 == NULL)
            real_fopen64 = (fopen_t)dlsym(RTLD_NEXT, "fopen64");
        if (real_fopen64 == NULL) {
            fprintf(stderr, "error: dlsym fopen64: %s\n", dlerror());
            exit(EXIT_FAILURE);
        }

        // open a web address
        if (wgetExec(path) != NULL)
            return funcompress(path);

        // to check if the file exists, we need to attempt to open it
        FILE* stream = real_fopen64(path, mode);
        if (string(mode) != "r" || !stream || zcatExec(path) == NULL)
            return stream;
        else {
            fclose(stream);
            return funcompress(path);
        }
    }

    typedef int (*open_t)(const char *path, int flags, mode_t mode);

    /** If the specified file is compressed, return a pipe that
     * uncompresses it.
     */
    int open(const char *path, int flags, mode_t mode)
    {
        static open_t real_open;
        if (real_open == NULL)
            real_open = (open_t)dlsym(RTLD_NEXT, "open");
        if (real_open == NULL) {
            fprintf(stderr, "error: dlsym open: %s\n", dlerror());
            exit(EXIT_FAILURE);
        }

        // open a web address
        if (wgetExec(path) != NULL)
            return uncompress(path);

        // to check if the file exists, we need to attempt to open it
        int filedesc = real_open(path, flags, mode);
        if (mode != ios_base::in || filedesc < 0
                || zcatExec(path) == NULL)
            return filedesc;
        else {
            close(filedesc);
            return uncompress(path);
        }
    }

} // extern "C"


/** Initialize the uncompress module. */
bool uncompress_init()
{
    signalInit();
    return true;
}

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top