Fabcoin Core  0.16.2
P2P Digital Currency
io_posix.c
Go to the documentation of this file.
1 /*
2  This file is part of ethash.
3 
4  ethash is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  ethash is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with ethash. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "io.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <libgen.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <pwd.h>
31 
32 FILE* ethash_fopen(char const* file_name, char const* mode)
33 {
34  return fopen(file_name, mode);
35 }
36 
37 char* ethash_strncat(char* dest, size_t dest_size, char const* src, size_t count)
38 {
39  return strlen(dest) + count + 1 <= dest_size ? strncat(dest, src, count) : NULL;
40 }
41 
42 bool ethash_mkdir(char const* dirname)
43 {
44  int rc = mkdir(dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
45  return rc != -1 || errno == EEXIST;
46 }
47 
48 int ethash_fileno(FILE *f)
49 {
50  return fileno(f);
51 }
52 
54  char const* dirname,
55  char const* filename,
56  size_t filename_length
57 )
58 {
59  size_t dirlen = strlen(dirname);
60  size_t dest_size = dirlen + filename_length + 1;
61  if (dirname[dirlen] != '/') {
62  dest_size += 1;
63  }
64  char* name = malloc(dest_size);
65  if (!name) {
66  return NULL;
67  }
68 
69  name[0] = '\0';
70  ethash_strncat(name, dest_size, dirname, dirlen);
71  if (dirname[dirlen] != '/') {
72  ethash_strncat(name, dest_size, "/", 1);
73  }
74  ethash_strncat(name, dest_size, filename, filename_length);
75  return name;
76 }
77 
78 bool ethash_file_size(FILE* f, size_t* ret_size)
79 {
80  struct stat st;
81  int fd;
82  if ((fd = fileno(f)) == -1 || fstat(fd, &st) != 0) {
83  return false;
84  }
85  *ret_size = st.st_size;
86  return true;
87 }
88 
89 bool ethash_get_default_dirname(char* strbuf, size_t buffsize)
90 {
91  static const char dir_suffix[] = ".ethash/";
92  strbuf[0] = '\0';
93  char* home_dir = getenv("HOME");
94  if (!home_dir || strlen(home_dir) == 0)
95  {
96  struct passwd* pwd = getpwuid(getuid());
97  if (pwd)
98  home_dir = pwd->pw_dir;
99  if (!home_dir)
100  return false;
101  }
102 
103  size_t len = strlen(home_dir);
104  if (!ethash_strncat(strbuf, buffsize, home_dir, len)) {
105  return false;
106  }
107  if (home_dir[len] != '/') {
108  if (!ethash_strncat(strbuf, buffsize, "/", 1)) {
109  return false;
110  }
111  }
112  return ethash_strncat(strbuf, buffsize, dir_suffix, sizeof(dir_suffix));
113 }
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:5
char * ethash_strncat(char *dest, size_t dest_size, char const *src, size_t count)
An strncat wrapper for no-warnings crossplatform strncat.
Definition: io_posix.c:37
FILE * ethash_fopen(char const *file_name, char const *mode)
An fopen wrapper for no-warnings crossplatform fopen.
Definition: io_posix.c:32
size_t count
Definition: ExecStats.cpp:37
evm_mode mode
Definition: SmartVM.cpp:47
bool ethash_mkdir(char const *dirname)
A cross-platform mkdir wrapper to create a directory or assert it&#39;s there.
Definition: io_posix.c:42
bool ethash_file_size(FILE *f, size_t *ret_size)
Get a file&#39;s size.
Definition: io_posix.c:78
int ethash_fileno(FILE *f)
Get a file descriptor number from a FILE stream.
Definition: io_posix.c:48
const char * name
Definition: rest.cpp:36
char * ethash_io_create_filename(char const *dirname, char const *filename, size_t filename_length)
Create the filename for the DAG.
Definition: io_posix.c:53
#define f(x)
Definition: gost.cpp:57
#define fd(x)
Definition: rijndael.cpp:172
bool ethash_get_default_dirname(char *strbuf, size_t buffsize)
Gets the default directory name for the DAG depending on the system.
Definition: io_posix.c:89