"Fossies" - the Fresh Open Source Software Archive

Member "lessfs-1.7.0/mklessfs.c" (16 Nov 2013, 4986 Bytes) of package /linux/misc/old/lessfs-1.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mklessfs.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.13_vs_1.7.0.

    1 /*
    2  *   Lessfs: A data deduplicating filesystem.
    3  *   Copyright (C) 2008 Mark Ruijter <mruijter@lessfs.com>
    4  *
    5  *   This program is s_free software;  you can redistribute it and/or modify
    6  *   it under the terms of the GNU General Public License as published by
    7  *   the Free Software Foundation; either version 3 of the License, or
    8  *   (at your option) any later version.
    9  *
   10  *   This program is distributed in the hope that it will be useful,
   11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
   12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   13  *   the GNU General Public License for more details.
   14  *
   15  *   You should have received a copy of the GNU General Public License
   16  *   along with this program;  if not, write to the Free Software
   17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   18  */
   19 
   20 #ifdef HAVE_CONFIG_H
   21 #include <config.h>
   22 #endif
   23 #ifndef LFATAL
   24 #include "lib_log.h"
   25 #endif
   26 
   27 #include <unistd.h>
   28 #include <sys/types.h>
   29 #include <stdio.h>
   30 #include <stdlib.h>
   31 #include <string.h>
   32 #include <errno.h>
   33 #include <sys/types.h>
   34 #include <sys/stat.h>
   35 #include <fcntl.h>
   36 #include <semaphore.h>
   37 #include <pthread.h>
   38 #include <fuse.h>
   39 
   40 #include <tcutil.h>
   41 #include <tchdb.h>
   42 #include <tcbdb.h>
   43 #include <stdlib.h>
   44 #include <stdbool.h>
   45 
   46 #include "lib_cfg.h"
   47 #include "lib_safe.h"
   48 #include "lib_str.h"
   49 #include "retcodes.h"
   50 #ifdef LZO
   51 #include "lib_lzo.h"
   52 #else
   53 #include "lib_qlz.h"
   54 #endif
   55 #include "lib_common.h"
   56 #include "lib_tc.h"
   57 #include "commons.h"
   58 
   59 #ifdef ENABLE_CRYPTO
   60 unsigned char *passwd = NULL;
   61 #endif
   62 
   63 #define die_dataerr(f...) { LFATAL(f); exit(EXIT_DATAERR); }
   64 
   65 struct option_info {
   66     char *configfile;
   67     int force;
   68 };
   69 struct option_info mkoptions;
   70 
   71 unsigned long working;
   72 
   73 void usage(char *name)
   74 {
   75     printf("Usage: %s /path_to_config.cfg\n", name);
   76     printf("     : %s [-f] -c /path_to_config.cfg\n\n", name);
   77     printf("-c Path and name of the configuation file\n");
   78     printf
   79         ("-f Overwrite existing databases and create directories when needed\n");
   80     printf("-h Displays this usage message\n");
   81     exit(-1);
   82 }
   83 
   84 int get_opts(int argc, char *argv[])
   85 {
   86 
   87     int c;
   88 
   89     mkoptions.force = 0;
   90     mkoptions.configfile = NULL;
   91     while ((c = getopt(argc, argv, "hfc:")) != -1)
   92         switch (c) {
   93         case 'c':
   94             if (optopt == 'c')
   95                 printf
   96                     ("Option -%c requires a lessfs configuration file as argument.\n",
   97                      optopt);
   98             else
   99                 mkoptions.configfile = optarg;
  100             break;
  101         case 'f':
  102             mkoptions.force = 1;
  103             break;
  104         case 'h':
  105             usage(argv[0]);
  106             break;
  107         default:
  108             abort();
  109         }
  110     return 0;
  111 }
  112 
  113 int main(int argc, char *argv[])
  114 {
  115     char *dbg = NULL;
  116 #ifdef ENABLE_CRYPTO
  117     int rnd;
  118     char *ckpasswd;
  119     char *p;
  120 #endif
  121 
  122     if (argc < 2)
  123         usage(argv[0]);
  124 
  125     get_opts(argc, argv);
  126     if (!mkoptions.configfile)
  127         mkoptions.configfile = argv[1];
  128     if (-1 == r_env_cfg(mkoptions.configfile))
  129         usage(argv[0]);
  130     dbg = getenv("DEBUG");
  131     if (dbg)
  132         debug = atoi(dbg);
  133     if (mkoptions.force == 1) {
  134         parseconfig(2, 0);
  135     } else
  136         parseconfig(1, 0);
  137     config->encryptdata = 0;
  138     config->passwd = NULL;
  139     config->iv = NULL;
  140 #ifdef ENABLE_CRYPTO
  141     config->iv = s_malloc(8);
  142     memset(config->iv, 0, 8);
  143     config->encryptdata = 0;
  144     p = read_val("ENCRYPT_DATA");
  145     if (p) {
  146         if (0 == strcasecmp(p, "ON"))
  147             config->encryptdata = 1;
  148     }
  149 #endif
  150     config->encryptmeta = 1;
  151 #ifdef ENABLE_CRYPTO
  152     if (config->encryptdata) {
  153         rnd = s_open("/dev/random", O_RDONLY);
  154         if (8 != s_read(rnd, config->iv, 8)) {
  155             die_dataerr("Could not read 8 bytes from /dev/random");
  156         }
  157         if (!getenv("PASSWORD")) {
  158             config->passwd =
  159                 (unsigned char *) s_strdup(getpass("Password: "));
  160             ckpasswd = s_strdup(getpass("Re-Enter Password: "));
  161             if (0 != strcmp((char *) config->passwd, ckpasswd)) {
  162                 fprintf(stderr, "Password values do not match.\n");
  163                 exit(EXIT_SYSTEM);
  164             }
  165             s_free(ckpasswd);
  166         } else {
  167             config->passwd =
  168                 (unsigned char *) s_strdup(getenv("PASSWORD"));
  169             unsetenv("PASSWORD");       /* Eat it after receiving.. */
  170         }
  171         p = getenv("ENCRYPT_META");
  172         if (p) {
  173             if (0 != strcasecmp(p, "ON")) {
  174                 LINFO("Metadata encryption is off");
  175                 config->encryptmeta = 0;
  176             }
  177         }
  178     }
  179 #endif
  180     config->replication = 0;
  181     config->commithash =
  182         thash((unsigned char *) "COMMITSTAMP", strlen("COMMITSTAMP"));
  183     formatfs();
  184     sync();
  185 #ifdef ENABLE_CRYPTO
  186     if (config->encryptdata) {
  187         s_free(config->passwd);
  188         s_free(config->iv);
  189     }
  190 #endif
  191     s_free(config);
  192     exit(0);
  193 }