Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions headers/os_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright <2012> <Vincent Le Guilloux,Peter Schmidtke, Pierre Tuffery>
* Copyright <2013-2018> <Peter Schmidtke, Vincent Le Guilloux>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/


#ifndef DH_OS_COMPAT
#define DH_OS_COMPAT

/* ------------------------------ INCLUDES ---------------------------------- */

#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif

/* ------------------------------ DESCRIPTION ------------------------------- */
/**
* Directory creation and permissions, which are the only places where fpocket
* needs to know which operating system it runs on.
*
* These used to be done by handing a command to system(): "mkdir -p", "chmod +x".
* That made the output directories depend on a POSIX shell being present, and it
* passed a filename straight to that shell. Doing the same work through the C
* library removes both problems and is what lets fpocket build on Windows, where
* mkdir() takes no mode and chmod +x has no meaning.
*/

/* ------------------------------- PUBLIC MACROS ---------------------------- */

#define M_MAX_MKDIR_PATH 2048 /**< longest path m_mkdir_p will walk */

/* ------------------------------ PUBLIC FUNCTIONS -------------------------- */

/**
## FUNCTION:
m_mkdir

## SPECIFICATION:
Creates a single directory. An already existing directory is a success:
fpocket rewrites its output folder on every run.

## PARAMETRES:
@ const char *path : directory to create

## RETURN:
int : 0 on success, -1 otherwise

*/
static int m_mkdir(const char *path)
{
int status;

#ifdef _WIN32
status = _mkdir(path);
#else
status = mkdir(path, 0755);
#endif

if (status != 0 && errno == EEXIST) return 0;

return status;
}

/**
## FUNCTION:
m_mkdir_p

## SPECIFICATION:
Creates a directory and every missing parent, as "mkdir -p" did. Both
separators are accepted because fpocket builds its paths with '/' even
when running on Windows.

## PARAMETRES:
@ const char *path : directory to create

## RETURN:
int : 0 on success, -1 otherwise

*/
static int m_mkdir_p(const char *path)
{
char tmp[M_MAX_MKDIR_PATH];
size_t len,
i;

len = strlen(path);
if (len == 0 || len >= sizeof(tmp)) return -1;

strcpy(tmp, path);

/* A trailing separator would make the final mkdir act on an empty name. */
while (len > 1 && (tmp[len-1] == '/' || tmp[len-1] == '\\')) {
tmp[len-1] = '\0';
len --;
}

/* i starts at 1 so that a leading '/' is not mistaken for a component. */
for (i = 1; i < len; i++) {
if (tmp[i] == '/' || tmp[i] == '\\') {
char sep = tmp[i];
tmp[i] = '\0';
if (m_mkdir(tmp) != 0) return -1;
tmp[i] = sep;
}
}

return m_mkdir(tmp);
}

/**
## FUNCTION:
m_tmpdir

## SPECIFICATION:
Directory for the scratch files handed to qhull. TMPDIR is honoured first
so an existing setup keeps working; Windows names the same thing TEMP or
TMP and has no /tmp, which is why the hardcoded fallback used to leave
fopen() returning NULL and the tessellation writing through it.

## PARAMETRES:
void

## RETURN:
const char * : an existing directory, without a trailing separator

*/
static const char *m_tmpdir(void)
{
const char *d;

d = getenv("TMPDIR");
if (d && *d) return d;

#ifdef _WIN32
d = getenv("TEMP");
if (d && *d) return d;

d = getenv("TMP");
if (d && *d) return d;

return ".";
#else
return "/tmp";
#endif
}

/**
## FUNCTION:
m_make_executable

## SPECIFICATION:
Marks a file as executable. fpocket uses it on the shell wrappers it
writes for VMD and PyMOL. Windows decides by extension, so there is
nothing to do there.

## PARAMETRES:
@ const char *path : file to mark

## RETURN:
int : 0 on success, -1 otherwise

*/
static int m_make_executable(const char *path)
{
#ifdef _WIN32
(void) path;
return 0;
#else
struct stat st;

if (stat(path, &st) != 0) return -1;

return chmod(path, st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
#endif
}

#endif
1 change: 1 addition & 0 deletions headers/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#endif /* /GSL */

#include "memhandler.h"
#include "os_compat.h"

/* ------------------------------- PUBLIC MACROS ---------------------------- */

Expand Down
7 changes: 2 additions & 5 deletions src/energy.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ void calculate_pocket_energy_grids(c_lst_pockets *pockets, s_fparams *params, s_
if (strlen(pdb_path) > 0) sprintf(out_path, "%s/%s_out", pdb_path, pdb_code);
else sprintf(out_path, "%s_out", pdb_code);

//sprintf(command, "mkdir %s", out_path) ;
status = mkdir(out_path, 0755);
//status = system(command) ;
status = m_mkdir_p(out_path);
sprintf(out_path_pockets, "%s/pockets", out_path);
status = mkdir(out_path_pockets, 0755);
//status = system(command) ;
status = m_mkdir(out_path_pockets);
if(status != 0) {
return ;
}
Expand Down
12 changes: 11 additions & 1 deletion src/fpmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ void process_pdb(char *pdbname, s_fparams *params)

if (params->topology_path[0] != 0)
{
#ifdef M_NO_MOLFILE
fprintf(stderr, "! This build ignores --topology_file; it needs the molfile plugins.\n");
#else
read_topology(params->topology_path, pdb);
#endif
}

if (pdb)
Expand Down Expand Up @@ -230,9 +234,13 @@ void process_pdb(char *pdbname, s_fparams *params)

s_pdb *open_file_format(char *fpath, const char *ligan, const int keep_lig, int model_number, s_fparams *par)
{
s_pdb *pdb;
s_pdb *pdb = NULL;
if (strstr(par->pdb_path, ".cif")) /*strstr finds the substring and here we search for the file extension we want */
#ifdef M_NO_MOLFILE
fprintf(stderr, "! This build reads PDB only; mmCIF needs the molfile plugins.\n");
#else
pdb = open_mmcif(fpath, NULL, keep_lig, par->model_number, par);
#endif
else if (strstr(par->pdb_path, ".pdb"))
pdb = rpdb_open(fpath, NULL, keep_lig, par->model_number, par);

Expand All @@ -244,7 +252,9 @@ void read_file_format(s_pdb *pdb, const char *ligan, const int keep_lig, int mod

if (strstr(par->pdb_path, ".cif"))
{ /*strstr finds the substring and here we search for the file extension we want */
#ifndef M_NO_MOLFILE
read_mmcif(pdb, NULL, keep_lig, par->model_number, par);
#endif
}
else if (strstr(par->pdb_path, ".pdb"))
rpdb_read(pdb, NULL, keep_lig, par->model_number, par);
Expand Down
9 changes: 3 additions & 6 deletions src/fpout.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ void write_out_fpocket(c_lst_pockets *pockets, s_pdb *pdb, char *pdbname)
else
sprintf(out_path, "%s_out", pdb_code);

sprintf(command, "mkdir -p %s", out_path);
status = system(command);
status = m_mkdir_p(out_path);
if (status != 0)
{
return;
Expand Down Expand Up @@ -123,8 +122,7 @@ void write_out_fpocket(c_lst_pockets *pockets, s_pdb *pdb, char *pdbname)
sprintf(out_path, "%s_out", pdb_code);

sprintf(out_path_tmp, "%s/pockets", out_path);
sprintf(command, "mkdir %s", out_path_tmp);
status = system(command);
status = m_mkdir(out_path_tmp);
/*if(status != 0) {
return ;
}*/
Expand Down Expand Up @@ -238,8 +236,7 @@ void write_out_fpocket_DB(c_lst_pockets *pockets, s_pdb *pdb, char *input_name)
sprintf(out_path, "%s/%s_out", pdb_path, pdb_code);
else
sprintf(out_path, "%s_out", pdb_code);
sprintf(command, "mkdir -p %s", out_path);
int status = system(command);
int status = m_mkdir_p(out_path);
// Writing full pdb
sprintf(pdb_out_path, "%s_out.pdb", out_path);

Expand Down
22 changes: 16 additions & 6 deletions src/voronoi.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ s_lst_vvertice *load_vvertices(s_pdb *pdb, s_fparams *params, float xshift, floa
char tmpn1[250] = "";
char tmpn2[250] = "";

const char *env = getenv("TMPDIR");
const char *env = m_tmpdir();
pid_t pid = getpid();
if (!env)
env = "/tmp/";
sprintf(tmpn1, "%s/qvoro_in_fpocket_%d.dat", env, pid);
sprintf(tmpn2, "%s/qvoro_out_fpocket_%d.dat", env, pid);

Expand Down Expand Up @@ -110,6 +108,13 @@ s_lst_vvertice *load_vvertices(s_pdb *pdb, s_fparams *params, float xshift, floa
{
FILE *ftmp = fopen(tmpn2, "w");
FILE *fvoro = fopen(tmpn1, "w+");
if (!ftmp || !fvoro)
{
fprintf(stderr, "! Cannot write scratch files in %s. Set TMPDIR to a writable directory.\n", env);
if (ftmp) fclose(ftmp);
if (fvoro) fclose(fvoro);
return NULL;
}
/* Write the header for qvoronoi */
fprintf(fvoro, "3 rbox D3\n%d\n", lvvert->n_h_tr);
// fprintf(fvoro, "3 rbox D3\n%d\n", 100) ;
Expand Down Expand Up @@ -1244,13 +1249,18 @@ float get_convex_hull_volume(s_vvertice **verts, int nvert)
if (nvert < 10)
return (0.0);

const char *env = getenv("TMPDIR");
if (!env)
env = "/tmp/";
const char *env = m_tmpdir();
sprintf(tmpn1, "%s/qhull_in_fpocket_%d.dat", env, pid);
sprintf(tmpn2, "%s/qhull_out_fpocket_%d.dat", env, pid);
FILE *ftmp = fopen(tmpn2, "w");
FILE *fvoro = fopen(tmpn1, "w+");
if (!ftmp || !fvoro)
{
fprintf(stderr, "! Cannot write scratch files in %s. Set TMPDIR to a writable directory.\n", env);
if (ftmp) fclose(ftmp);
if (fvoro) fclose(fvoro);
return (0.0);
}
/* Write the header for qvoronoi */

fprintf(fvoro, "3 rbox D3\n%d\n", nvert);
Expand Down
12 changes: 4 additions & 8 deletions src/write_visu.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ void write_vmd(char *pdb_name, char *pdb_out_name)
fclose(f);

/* Make tcl script executable, and Write tcl script */
sprintf(sys_cmd, "chmod +x %s", fout);
status = system(sys_cmd);
status = m_make_executable(fout);

fprintf(f_tcl, "proc highlighting { colorId representation id selection } {\n");
fprintf(f_tcl, " puts \"highlighting $id\"\n");
Expand Down Expand Up @@ -181,8 +180,7 @@ void write_vmd_mmcif(char *pdb_name, char *pdb_out_name)
fclose(f);

/* Make tcl script executable, and Write tcl script */
sprintf(sys_cmd, "chmod +x %s", fout);
status = system(sys_cmd);
status = m_make_executable(fout);

fprintf(f_tcl, "proc highlighting { colorId representation id selection } {\n");
fprintf(f_tcl, " puts \"highlighting $id\"\n");
Expand Down Expand Up @@ -262,8 +260,7 @@ void write_pymol(char *pdb_name, char *pdb_out_name)
fflush(f);
fclose(f);

sprintf(sys_cmd, "chmod +x %s", fout);
status = system(sys_cmd);
status = m_make_executable(fout);
/* Write pml script */
fprintf(f_pml, "from pymol import cmd,stored\n");
fprintf(f_pml, "load %s\n", pdb_out_name);
Expand Down Expand Up @@ -318,8 +315,7 @@ void write_pymol_mmcif(char *pdb_name, char *pdb_out_name)
fflush(f);
fclose(f);

sprintf(sys_cmd, "chmod +x %s", fout);
status = system(sys_cmd);
status = m_make_executable(fout);
/* Write pml script */
fprintf(f_pml, "from pymol import cmd,stored\n");
fprintf(f_pml, "load %s\n", pdb_out_name);
Expand Down