<commit_before>#define FUSE_USE_VERSION 30
#define _XOPEN_SOURCE 500

#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <ftw.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wfloat-conversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#include <wand/magick_wand.h>
#pragma GCC diagnostic pop

#include "cache.h"
#include "wget.h"


#define PERMISSIONS     0755        /* Cached file permissions. */
#define TMP_DIR_NAME    ".flickrms" /* Where to place cached photos. */
#define PHOTO_TIMEOUT   14400       /* In seconds. */


/* Determines whether to report the true file size in getattr before the file
 * has been downloaded locally. The true file size will always be set and cached
 * after a file has been downloaded (which happens when opening a file). If this
 * option is set, Flickrms will query the remote server to get only the file
 * size for each photo from the response headers but not download the photo.
 * Using the true photo size is recommended for GUI applications as they tend to
 * stat the file before opening.
 * Using a fake file size will be much faster for command line usage.
 */
#define USE_TRUE_PHOTO_SIZE 1       /* 1 will use the true size. 0 will use a fake size. */
#define FAKE_PHOTO_SIZE     1024    /* Only used when USE_TRUE_PHOTO_SIZE is set. */

/* Whether to clean the temporary directory on unmount.
 * The filesystem does not keep track of files that cannot be uploaded to Flickr,
 * such as lock and hidden files created by file browsers, after the file system
 * has been destroyed. This option clears the temp dir used for cached files.
 */
#define CLEAN_TMP_DIR_UMOUNT 1      /* 1 or 0. */


static uid_t uid;   /* The user id of the user that mounted the filesystem */
static gid_t gid;   /* The group id of the user */

static MagickWand *mw;

static char *tmp_path;


/**
 * Helper functions
**/

/*
 * Returns the first index of a '/' or negative if non exists in the
 * path supplied.
 */
static size_t get_slash_index(const char *path, unsigned short *found) {
    size_t i;
    *found = 0;
    if(!path)
        return 0;
    for(i = 0; i < strlen(path); i++) {
        if(path[i] == '/') {
            *found = 1;
            break;
        }
    }
    return i;
}

/* 
 * Internal method for splitting a path of the format:
 * "/photosetname/photoname"
 * into: photoset = "photosetname" and photo = "photoname"
 */
static int get_photoset_photo_from_path(const char *path, char **photoset, char **photo) {
    size_t i;
    unsigned short found;
    char *path_dup;

    path_dup = strdup(path + 1);
    i = get_slash_index(path_dup, &found);

    if(!path || !photoset || !photo || !path_dup)
        return FAIL;

    if(found) {
        path_dup[i] = '\0';
        *photoset = strdup(path_dup);
        *photo = strdup(path_dup + i + 1);
    }
    else {
        *photoset = strdup("");
        *photo = strdup(path_dup);
    }

    free(path_dup);
    return SUCCESS;
}

/*
 * Sets the uid/gid variables to the user's (who mounted the filesystem)
 * uid/gid. Want to only give the user access to their flickr account.
 */
static inline int set_user_variables() {
    uid = getuid();
    gid = getgid();
    return SUCCESS;
}

/*
 * Set the path to the directory that will be used to get the image
 * data from Flickr.
 */
static inline int set_tmp_path() {
    char *home;

    if(!(home = getenv("HOME")))
        return FAIL;

    tmp_path = (char *)malloc(strlen(home) + strlen(TMP_DIR_NAME) + 2);
    if(!tmp_path)
        return FAIL;

    strcpy(tmp_path, home);
    strcat(tmp_path, "/");
    strcat(tmp_path, TMP_DIR_NAME);

    return 0 - mkdir(tmp_path, PERMISSIONS);
}

static int remove_tmp_file(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
    (void)sb;
    (void)typeflag;
    if(ftwbuf->level == 0)
        return SUCCESS;
    return remove(fpath);
}

static inline void remove_tmp_path() {
    nftw(tmp_path, remove_tmp_file, 64, FTW_DEPTH | FTW_PHYS);
    free(tmp_path);
}

static inline int imagemagick_init() {
    MagickWandGenesis();
    mw = NewMagickWand();
    return mw ? SUCCESS : FAIL;
}

static inline void imagemagick_destroy() {
    DestroyMagickWand(mw);
    MagickWandTerminus();
}


/**
 * File system functions
**/

static inline void set_stbuf(struct stat *stbuf, mode_t mode, uid_t uid,
  gid_t gid, off_t size, time_t time, nlink_t nlink) {
    stbuf->st_mode = mode;
    stbuf->st_uid = uid;
    stbuf->st_gid = gid;
    stbuf->st_size = size;
    stbuf->st_mtime = time;
    stbuf->st_nlink = nlink;
}

/*
 * Get photo size if needed.
 */
static int process_photo(const char *photoset, const char *photo, cached_information *ci) {
    if(USE_TRUE_PHOTO_SIZE && ci->size == PHOTO_SIZE_UNSET) {
        int photo_size = get_url_content_length(ci->uri);

        if(photo_size < 0)
            return FAIL;

        ci->size = (unsigned int)photo_size;<commit_msg>Using FAKE_PHOTO_SIZE if USE_TRUE_PHOTO_SIZE is not set.<commit_after>#define FUSE_USE_VERSION 30
#define _XOPEN_SOURCE 500

#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <ftw.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wfloat-conversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#include <wand/magick_wand.h>
#pragma GCC diagnostic pop

#include "cache.h"
#include "wget.h"


#define PERMISSIONS     0755        /* Cached file permissions. */
#define TMP_DIR_NAME    ".flickrms" /* Where to place cached photos. */
#define PHOTO_TIMEOUT   14400       /* In seconds. */


/* Determines whether to report the true file size in getattr before the file
 * has been downloaded locally. The true file size will always be set and cached
 * after a file has been downloaded (which happens when opening a file). If this
 * option is set, Flickrms will query the remote server to get only the file
 * size for each photo from the response headers but not download the photo.
 * Using the true photo size is recommended for GUI applications as they tend to
 * stat the file before opening.
 * Using a fake file size will be much faster for command line usage.
 */
#define USE_TRUE_PHOTO_SIZE 1       /* 1 will use the true size. 0 will use a fake size. */
#define FAKE_PHOTO_SIZE     1024    /* Only used when USE_TRUE_PHOTO_SIZE is set. */

/* Whether to clean the temporary directory on unmount.
 * The filesystem does not keep track of files that cannot be uploaded to Flickr,
 * such as lock and hidden files created by file browsers, after the file system
 * has been destroyed. This option clears the temp dir used for cached files.
 */
#define CLEAN_TMP_DIR_UMOUNT 1      /* 1 or 0. */


static uid_t uid;   /* The user id of the user that mounted the filesystem */
static gid_t gid;   /* The group id of the user */

static MagickWand *mw;

static char *tmp_path;


/**
 * Helper functions
**/

/*
 * Returns the first index of a '/' or negative if non exists in the
 * path supplied.
 */
static size_t get_slash_index(const char *path, unsigned short *found) {
    size_t i;
    *found = 0;
    if(!path)
        return 0;
    for(i = 0; i < strlen(path); i++) {
        if(path[i] == '/') {
            *found = 1;
            break;
        }
    }
    return i;
}

/* 
 * Internal method for splitting a path of the format:
 * "/photosetname/photoname"
 * into: photoset = "photosetname" and photo = "photoname"
 */
static int get_photoset_photo_from_path(const char *path, char **photoset, char **photo) {
    size_t i;
    unsigned short found;
    char *path_dup;

    path_dup = strdup(path + 1);
    i = get_slash_index(path_dup, &found);

    if(!path || !photoset || !photo || !path_dup)
        return FAIL;

    if(found) {
        path_dup[i] = '\0';
        *photoset = strdup(path_dup);
        *photo = strdup(path_dup + i + 1);
    }
    else {
        *photoset = strdup("");
        *photo = strdup(path_dup);
    }

    free(path_dup);
    return SUCCESS;
}

/*
 * Sets the uid/gid variables to the user's (who mounted the filesystem)
 * uid/gid. Want to only give the user access to their flickr account.
 */
static inline int set_user_variables() {
    uid = getuid();
    gid = getgid();
    return SUCCESS;
}

/*
 * Set the path to the directory that will be used to get the image
 * data from Flickr.
 */
static inline int set_tmp_path() {
    char *home;

    if(!(home = getenv("HOME")))
        return FAIL;

    tmp_path = (char *)malloc(strlen(home) + strlen(TMP_DIR_NAME) + 2);
    if(!tmp_path)
        return FAIL;

    strcpy(tmp_path, home);
    strcat(tmp_path, "/");
    strcat(tmp_path, TMP_DIR_NAME);

    return 0 - mkdir(tmp_path, PERMISSIONS);
}

static int remove_tmp_file(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
    (void)sb;
    (void)typeflag;
    if(ftwbuf->level == 0)
        return SUCCESS;
    return remove(fpath);
}

static inline void remove_tmp_path() {
    nftw(tmp_path, remove_tmp_file, 64, FTW_DEPTH | FTW_PHYS);
    free(tmp_path);
}

static inline int imagemagick_init() {
    MagickWandGenesis();
    mw = NewMagickWand();
    return mw ? SUCCESS : FAIL;
}

static inline void imagemagick_destroy() {
    DestroyMagickWand(mw);
    MagickWandTerminus();
}


/**
 * File system functions
**/

static inline void set_stbuf(struct stat *stbuf, mode_t mode, uid_t uid,
  gid_t gid, off_t size, time_t time, nlink_t nlink) {
    stbuf->st_mode = mode;
    stbuf->st_uid = uid;
    stbuf->st_gid = gid;
    stbuf->st_size = size;
    stbuf->st_mtime = time;
    stbuf->st_nlink = nlink;
}

/*
 * Get photo size if needed.
 */
static int process_photo(const char *photoset, const char *photo, cached_information *ci) {
    if(ci->size == PHOTO_SIZE_UNSET) {
        int photo_size = FAKE_PHOTO_SIZE;

        if(USE_TRUE_PHOTO_SIZE) {
            photo_size = get_url_content_length(ci->uri);

            if(photo_size < 0)
                return FAIL;
        }

        ci->size = (unsigned int)photo_size;
{'fraction_non_alphanumeric': 0.10631711328238368, 'fraction_numerical': 0.007158750120924833, 'mean_word_length': 3.0557081208316985, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7436561', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3350, 'n_words': 1155}