From a56fe85819bb281919925f81d058aaabdf68976a Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Fri, 12 Aug 2016 18:15:54 -0400 Subject: [PATCH] initial commit --- .Rbuildignore | 7 +++ .gitignore | 7 +++ .travis.yml | 17 ++++++ DESCRIPTION | 20 +++++++ NAMESPACE | 6 ++ NEWS.md | 2 + R/RcppExports.R | 10 ++++ R/filemagic-package.R | 9 +++ README.Rmd | 41 +++++++++++++ filemagic.Rproj | 21 +++++++ man/filemagic.Rd | 14 +++++ man/get_mimetype.Rd | 12 ++++ src/.gitignore | 3 + src/Makevars | 3 + src/RcppExports.cpp | 18 ++++++ src/filemagic.cpp | 51 ++++++++++++++++ src/magic.h | 126 ++++++++++++++++++++++++++++++++++++++++ tests/test-all.R | 2 + tests/testthat/test-filemagic.R | 6 ++ 19 files changed, 375 insertions(+) create mode 100644 .Rbuildignore create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 DESCRIPTION create mode 100644 NAMESPACE create mode 100644 NEWS.md create mode 100644 R/RcppExports.R create mode 100644 R/filemagic-package.R create mode 100644 README.Rmd create mode 100644 filemagic.Rproj create mode 100644 man/filemagic.Rd create mode 100644 man/get_mimetype.Rd create mode 100644 src/.gitignore create mode 100644 src/Makevars create mode 100644 src/RcppExports.cpp create mode 100644 src/filemagic.cpp create mode 100644 src/magic.h create mode 100644 tests/test-all.R create mode 100644 tests/testthat/test-filemagic.R diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..45e5762 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,7 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +^\.travis\.yml$ +^README\.*md$ +^README\.*html$ +^NOTES\.*Rmd$ +^NOTES\.*html$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60332de --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.Rproj.user +.Rhistory +.RData +.Rproj +src/*.o +src/*.so +src/*.dll diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d465915 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: r +warnings_are_errors: true +sudo: required + +apt_packages: + - binutils + - libproj-dev + - gdal-bin + +env: + global: + - CRAN: http://cran.rstudio.com + +notifications: + email: + on_success: change + on_failure: change diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..fe00f0e --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,20 @@ +Package: filemagic +Type: Package +Title: filemagic title goes here +Version: 0.1.0 +Date: 2016-08-12 +Author: Bob Rudis (@hrbrmstr) +Maintainer: Bob Rudis +Description: filemagic description goes here +URL: http://github.com/hrbrmstr/filemagic +BugReports: https://github.com/hrbrmstr/filemagic/issues +License: AGPL +Suggests: + testthat +Depends: + R (>= 3.0.0) +Imports: + purrr, + Rcpp +LinkingTo: Rcpp +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..cb70fab --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,6 @@ +# Generated by roxygen2: do not edit by hand + +export(get_mimetype) +import(purrr) +importFrom(Rcpp,sourceCpp) +useDynLib(filemagic) diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000..9b4679b --- /dev/null +++ b/NEWS.md @@ -0,0 +1,2 @@ +0.1.0 +* Initial release diff --git a/R/RcppExports.R b/R/RcppExports.R new file mode 100644 index 0000000..3fbf320 --- /dev/null +++ b/R/RcppExports.R @@ -0,0 +1,10 @@ +# This file was generated by Rcpp::compileAttributes +# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +#' Return file info +#' +#' @export +get_mimetype <- function(path) { + .Call('filemagic_get_mimetype', PACKAGE = 'filemagic', path) +} + diff --git a/R/filemagic-package.R b/R/filemagic-package.R new file mode 100644 index 0000000..ecf643f --- /dev/null +++ b/R/filemagic-package.R @@ -0,0 +1,9 @@ +#' A package to ... +#' +#' @name filemagic +#' @docType package +#' @author Bob Rudis (@@hrbrmstr) +#' @import purrr +#' @useDynLib filemagic +#' @importFrom Rcpp sourceCpp +NULL diff --git a/README.Rmd b/README.Rmd new file mode 100644 index 0000000..3e913ed --- /dev/null +++ b/README.Rmd @@ -0,0 +1,41 @@ +--- +output: rmarkdown::github_document +--- + +`filemagic` : ... + +The following functions are implemented: + +The following data sets are included: + +### Installation + +```{r eval=FALSE} +devtools::install_github("hrbrmstr/filemagic") +``` + +```{r echo=FALSE, message=FALSE, warning=FALSE, error=FALSE} +options(width=120) +``` + +### Usage + +```{r} +library(filemagic) + +# current verison +packageVersion("filemagic") + +``` + +### Test Results + +```{r} +library(filemagic) +library(testthat) + +date() + +test_dir("tests/") +``` + diff --git a/filemagic.Rproj b/filemagic.Rproj new file mode 100644 index 0000000..446d9e1 --- /dev/null +++ b/filemagic.Rproj @@ -0,0 +1,21 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageBuildArgs: --resave-data +PackageRoxygenize: rd,collate,namespace diff --git a/man/filemagic.Rd b/man/filemagic.Rd new file mode 100644 index 0000000..4d232ad --- /dev/null +++ b/man/filemagic.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/filemagic-package.R +\docType{package} +\name{filemagic} +\alias{filemagic} +\alias{filemagic-package} +\title{A package to ...} +\description{ +A package to ... +} +\author{ +Bob Rudis (@hrbrmstr) +} + diff --git a/man/get_mimetype.Rd b/man/get_mimetype.Rd new file mode 100644 index 0000000..2141f3f --- /dev/null +++ b/man/get_mimetype.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/RcppExports.R +\name{get_mimetype} +\alias{get_mimetype} +\title{Return file info} +\usage{ +get_mimetype(path) +} +\description{ +Return file info +} + diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..22034c4 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,3 @@ +*.o +*.so +*.dll diff --git a/src/Makevars b/src/Makevars new file mode 100644 index 0000000..0575d35 --- /dev/null +++ b/src/Makevars @@ -0,0 +1,3 @@ +CPPFLAGS = +CXXFLAGS = -std=gnu99 -Wall -Wextra -O2 -g -fstack-protector +PKG_LIBS = -lmagic \ No newline at end of file diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp new file mode 100644 index 0000000..0977b54 --- /dev/null +++ b/src/RcppExports.cpp @@ -0,0 +1,18 @@ +// This file was generated by Rcpp::compileAttributes +// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +#include + +using namespace Rcpp; + +// get_mimetype +DataFrame get_mimetype(CharacterVector path); +RcppExport SEXP filemagic_get_mimetype(SEXP pathSEXP) { +BEGIN_RCPP + Rcpp::RObject __result; + Rcpp::RNGScope __rngScope; + Rcpp::traits::input_parameter< CharacterVector >::type path(pathSEXP); + __result = Rcpp::wrap(get_mimetype(path)); + return __result; +END_RCPP +} diff --git a/src/filemagic.cpp b/src/filemagic.cpp new file mode 100644 index 0000000..e8937ba --- /dev/null +++ b/src/filemagic.cpp @@ -0,0 +1,51 @@ +#include +using namespace Rcpp; + +#include "magic.h" +#include "limits.h" + +//' Return file info +//' +//' @export +// [[Rcpp::export]] +DataFrame get_mimetype(CharacterVector path) { + + unsigned int input_size = path.size(); + StringVector mime_type(input_size); + + for (unsigned int i=0; i(path[i]); + std::string fullPath(R_ExpandFileName(path_str.c_str())); + + int flags = MAGIC_MIME_TYPE; + magic_t cookie = magic_open(flags); + + if (cookie == NULL) { + + mime_type[i] = NA_STRING; + + } else { + + magic_load(cookie, NULL); + + const char *magic_result = magic_file(cookie, fullPath.c_str()); + + if (magic_result == NULL) { + mime_type[i] = NA_STRING; + } else { + std::string res = std::string(magic_result, strnlen(magic_result, 1024)); + mime_type(i) = res; + } + + } + + } + + return DataFrame::create(_["file"] = path, + _["mime_type"] = mime_type, + _["stringsAsFactors"] = false); + +} diff --git a/src/magic.h b/src/magic.h new file mode 100644 index 0000000..bf1e60b --- /dev/null +++ b/src/magic.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) Christos Zoulas 2003. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#ifndef _MAGIC_H +#define _MAGIC_H + +#include + +#define MAGIC_NONE 0x0000000 /* No flags */ +#define MAGIC_DEBUG 0x0000001 /* Turn on debugging */ +#define MAGIC_SYMLINK 0x0000002 /* Follow symlinks */ +#define MAGIC_COMPRESS 0x0000004 /* Check inside compressed files */ +#define MAGIC_DEVICES 0x0000008 /* Look at the contents of devices */ +#define MAGIC_MIME_TYPE 0x0000010 /* Return the MIME type */ +#define MAGIC_CONTINUE 0x0000020 /* Return all matches */ +#define MAGIC_CHECK 0x0000040 /* Print warnings to stderr */ +#define MAGIC_PRESERVE_ATIME 0x0000080 /* Restore access time on exit */ +#define MAGIC_RAW 0x0000100 /* Don't convert unprintable chars */ +#define MAGIC_ERROR 0x0000200 /* Handle ENOENT etc as real errors */ +#define MAGIC_MIME_ENCODING 0x0000400 /* Return the MIME encoding */ +#define MAGIC_MIME (MAGIC_MIME_TYPE|MAGIC_MIME_ENCODING) +#define MAGIC_APPLE 0x0000800 /* Return the Apple creator/type */ +#define MAGIC_EXTENSION 0x1000000 /* Return a /-separated list of + * extensions */ +#define MAGIC_COMPRESS_TRANSP 0x2000000 /* Check inside compressed files + * but not report compression */ +#define MAGIC_NODESC (MAGIC_EXTENSION|MAGIC_MIME|MAGIC_APPLE) + +#define MAGIC_NO_CHECK_COMPRESS 0x0001000 /* Don't check for compressed files */ +#define MAGIC_NO_CHECK_TAR 0x0002000 /* Don't check for tar files */ +#define MAGIC_NO_CHECK_SOFT 0x0004000 /* Don't check magic entries */ +#define MAGIC_NO_CHECK_APPTYPE 0x0008000 /* Don't check application type */ +#define MAGIC_NO_CHECK_ELF 0x0010000 /* Don't check for elf details */ +#define MAGIC_NO_CHECK_TEXT 0x0020000 /* Don't check for text files */ +#define MAGIC_NO_CHECK_CDF 0x0040000 /* Don't check for cdf files */ +#define MAGIC_NO_CHECK_TOKENS 0x0100000 /* Don't check tokens */ +#define MAGIC_NO_CHECK_ENCODING 0x0200000 /* Don't check text encodings */ + +/* No built-in tests; only consult the magic file */ +#define MAGIC_NO_CHECK_BUILTIN ( \ + MAGIC_NO_CHECK_COMPRESS | \ + MAGIC_NO_CHECK_TAR | \ +/* MAGIC_NO_CHECK_SOFT | */ \ + MAGIC_NO_CHECK_APPTYPE | \ + MAGIC_NO_CHECK_ELF | \ + MAGIC_NO_CHECK_TEXT | \ + MAGIC_NO_CHECK_CDF | \ + MAGIC_NO_CHECK_TOKENS | \ + MAGIC_NO_CHECK_ENCODING | \ + 0 \ +) + +/* Defined for backwards compatibility (renamed) */ +#define MAGIC_NO_CHECK_ASCII MAGIC_NO_CHECK_TEXT + +/* Defined for backwards compatibility; do nothing */ +#define MAGIC_NO_CHECK_FORTRAN 0x000000 /* Don't check ascii/fortran */ +#define MAGIC_NO_CHECK_TROFF 0x000000 /* Don't check ascii/troff */ + +#define MAGIC_VERSION 528 /* This implementation */ + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct magic_set *magic_t; +magic_t magic_open(int); +void magic_close(magic_t); + +const char *magic_getpath(const char *, int); +const char *magic_file(magic_t, const char *); +const char *magic_descriptor(magic_t, int); +const char *magic_buffer(magic_t, const void *, size_t); + +const char *magic_error(magic_t); +int magic_setflags(magic_t, int); + +int magic_version(void); +int magic_load(magic_t, const char *); +int magic_load_buffers(magic_t, void **, size_t *, size_t); + +int magic_compile(magic_t, const char *); +int magic_check(magic_t, const char *); +int magic_list(magic_t, const char *); +int magic_errno(magic_t); + +#define MAGIC_PARAM_INDIR_MAX 0 +#define MAGIC_PARAM_NAME_MAX 1 +#define MAGIC_PARAM_ELF_PHNUM_MAX 2 +#define MAGIC_PARAM_ELF_SHNUM_MAX 3 +#define MAGIC_PARAM_ELF_NOTES_MAX 4 +#define MAGIC_PARAM_REGEX_MAX 5 +#define MAGIC_PARAM_BYTES_MAX 6 + +int magic_setparam(magic_t, int, const void *); +int magic_getparam(magic_t, int, void *); + +#ifdef __cplusplus +}; +#endif + +#endif /* _MAGIC_H */ diff --git a/tests/test-all.R b/tests/test-all.R new file mode 100644 index 0000000..44cbb72 --- /dev/null +++ b/tests/test-all.R @@ -0,0 +1,2 @@ +library(testthat) +test_check("filemagic") diff --git a/tests/testthat/test-filemagic.R b/tests/testthat/test-filemagic.R new file mode 100644 index 0000000..ab6f62f --- /dev/null +++ b/tests/testthat/test-filemagic.R @@ -0,0 +1,6 @@ +context("basic functionality") +test_that("we can do something", { + + #expect_that(some_function(), is_a("data.frame")) + +})