<commit_before>/* 
 * Copyright (c) 2015-2016, <NAME>. All rights reserved.
 * 
 * “Singularity” Copyright (c) 2016, The Regents of the University of California,
 * through Lawrence Berkeley National Laboratory (subject to receipt of any
 * required approvals from the U.S. Dept. of Energy).  All rights reserved.
 * 
 * This software is licensed under a customized 3-clause BSD license.  Please
 * consult LICENSE file distributed with the sources of this project regarding
 * your rights to use or distribute this software.
 * 
 * NOTICE.  This Software was developed under funding from the U.S. Department of
 * Energy and the U.S. Government consequently retains certain rights. As such,
 * the U.S. Government has been granted for itself and others acting on its
 * behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
 * to reproduce, distribute copies to the public, prepare derivative works, and
 * perform publicly and display publicly, and to permit other to do so. 
 * 
 */


#include <stdio.h>
#include <stdlib.h>
#include <linux/loop.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h> 
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include "config.h"
#include "lib/loop-control.h"
#include "lib/image-util.h"
#include "util/util.h"
#include "util/file.h"
//#include "image.h"
#include "lib/message.h"
#include "lib/privilege.h"
#include "sessiondir.h"

#ifndef LO_FLAGS_AUTOCLEAR
#define LO_FLAGS_AUTOCLEAR 4
#endif

#define MAX_LOOP_DEVS 128

char *loop_dev;
FILE *loop_fp;
int image_loop_file_fd; // This has to be global for the flock to be held

char *singularity_loop_bind(FILE *image_fp) {
    char *sessiondir = singularity_sessiondir_get();
    char *image_loop_file = joinpath(sessiondir, "image_loop_dev");
    struct loop_info64 lo64 = {0};
    int i;

    if ( image_fp == NULL ) {
        singularity_message(ERROR, "Called singularity_loop_bind() with NULL image pointer\n");
        ABORT(255);
    }

    singularity_message(DEBUG, "Opening image loop device file: %s\n", image_loop_file);
    if ( ( image_loop_file_fd = open(image_loop_file, O_CREAT | O_RDWR, 0644) ) < 0 ) { // Flawfinder: ignore
        singularity_message(ERROR, "Could not open image loop device cache file %s: %s\n", image_loop_file, strerror(errno));
        ABORT(255);
    }

    singularity_message(DEBUG, "Checking image is a Singularity\n");
    if ( singularity_image_check(image_fp) < 0 ) {
        singularity_message(ERROR, "File is not a Singularity image!\n");
        ABORT(255);
    }

    singularity_message(DEBUG, "Requesting exclusive flock() on loop_dev lockfile\n");
    if ( flock(image_loop_file_fd, LOCK_EX | LOCK_NB) < 0 ) {
        char *active_loop_dev;
        singularity_message(VERBOSE2, "Did not get exclusive lock on image loop device cache, assuming it is active\n");

        singularity_message(DEBUG, "Waiting to obtain shared lock on loop_dev lockfile\n");
        flock(image_loop_file_fd, LOCK_SH);

        singularity_message(DEBUG, "Obtaining cached loop device name\n");
        if ( ( active_loop_dev = filecat(image_loop_file) ) == NULL ) {
            singularity_message(ERROR, "Could not retrieve active loop device from %s\n", image_loop_file);
            ABORT(255);
        }

        singularity_message(DEBUG, "Returning with active loop device name: %s\n", active_loop_dev);
        return(active_loop_dev);
    }
<commit_msg>Don't check/confirm if image is singularity here<commit_after>/* 
 * Copyright (c) 2015-2016, <NAME>. All rights reserved.
 * 
 * “Singularity” Copyright (c) 2016, The Regents of the University of California,
 * through Lawrence Berkeley National Laboratory (subject to receipt of any
 * required approvals from the U.S. Dept. of Energy).  All rights reserved.
 * 
 * This software is licensed under a customized 3-clause BSD license.  Please
 * consult LICENSE file distributed with the sources of this project regarding
 * your rights to use or distribute this software.
 * 
 * NOTICE.  This Software was developed under funding from the U.S. Department of
 * Energy and the U.S. Government consequently retains certain rights. As such,
 * the U.S. Government has been granted for itself and others acting on its
 * behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
 * to reproduce, distribute copies to the public, prepare derivative works, and
 * perform publicly and display publicly, and to permit other to do so. 
 * 
 */


#include <stdio.h>
#include <stdlib.h>
#include <linux/loop.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h> 
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include "config.h"
#include "lib/loop-control.h"
#include "lib/image-util.h"
#include "util/util.h"
#include "util/file.h"
//#include "image.h"
#include "lib/message.h"
#include "lib/privilege.h"
#include "sessiondir.h"

#ifndef LO_FLAGS_AUTOCLEAR
#define LO_FLAGS_AUTOCLEAR 4
#endif

#define MAX_LOOP_DEVS 128

char *loop_dev;
FILE *loop_fp;
int image_loop_file_fd; // This has to be global for the flock to be held

char *singularity_loop_bind(FILE *image_fp) {
    char *sessiondir = singularity_sessiondir_get();
    char *image_loop_file = joinpath(sessiondir, "image_loop_dev");
    struct loop_info64 lo64 = {0};
    int i;

    if ( image_fp == NULL ) {
        singularity_message(ERROR, "Called singularity_loop_bind() with NULL image pointer\n");
        ABORT(255);
    }

    singularity_message(DEBUG, "Opening image loop device file: %s\n", image_loop_file);
    if ( ( image_loop_file_fd = open(image_loop_file, O_CREAT | O_RDWR, 0644) ) < 0 ) { // Flawfinder: ignore
        singularity_message(ERROR, "Could not open image loop device cache file %s: %s\n", image_loop_file, strerror(errno));
        ABORT(255);
    }

    singularity_message(DEBUG, "Requesting exclusive flock() on loop_dev lockfile\n");
    if ( flock(image_loop_file_fd, LOCK_EX | LOCK_NB) < 0 ) {
        char *active_loop_dev;
        singularity_message(VERBOSE2, "Did not get exclusive lock on image loop device cache, assuming it is active\n");

        singularity_message(DEBUG, "Waiting to obtain shared lock on loop_dev lockfile\n");
        flock(image_loop_file_fd, LOCK_SH);

        singularity_message(DEBUG, "Obtaining cached loop device name\n");
        if ( ( active_loop_dev = filecat(image_loop_file) ) == NULL ) {
            singularity_message(ERROR, "Could not retrieve active loop device from %s\n", image_loop_file);
            ABORT(255);
        }

        singularity_message(DEBUG, "Returning with active loop device name: %s\n", active_loop_dev);
        return(active_loop_dev);
    }

{'fraction_non_alphanumeric': 0.1035956380783967, 'fraction_numerical': 0.011788977306218685, 'mean_word_length': 3.8168914123491837, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 27, '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': '4403291', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2113, 'n_words': 691}