Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 3097327

Browse files
authored
Merge pull request #3 from milosz275/install
BitLab installation/uninstallation
2 parents 560c866 + 695e401 commit 3097327

File tree

12 files changed

+216
-14
lines changed

12 files changed

+216
-14
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"codespaces",
77
"Doxyfile",
88
"Doxygen",
9-
"lineptr"
9+
"lineptr",
10+
"Uninstallation"
1011
],
1112
"task.allowAutomaticTasks": "on",
1213
"editor.formatOnSave": true,

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# BitLab
1+
# BitLab: A Bitcoin P2P Network and Blockchain Explorer
22

33
[![Make](https://github.com/milosz275/bitlab/actions/workflows/makefile.yml/badge.svg)](https://github.com/milosz275/bitlab/actions/workflows/makefile.yml)
44
[![CodeQL](https://github.com/milosz275/bitlab/actions/workflows/codeql.yml/badge.svg)](https://github.com/milosz275/bitlab/actions/workflows/codeql.yml)
@@ -14,9 +14,52 @@ BitLab acts as a powerful educational resource and debugging tool for understand
1414
- [GitHub Repo](https://github.com/milosz275/bitlab)
1515
- [Doxygen Docs](https://milosz275.github.io/bitlab)
1616

17+
> [!NOTE]
18+
> This project is a work in progress.
19+
1720
## Table of Contents
1821

19-
## Getting Started
22+
- [Features](#features)
23+
- [Installation](#installation)
24+
- [Uninstallation](#uninstallation)
25+
- [Usage](#usage)
26+
- [Config directory](#config-directory)
27+
- [Acknowledgements](#acknowledgements)
28+
- [License](#license)
29+
30+
## Features
31+
32+
## Installation
33+
34+
> [!NOTE]
35+
> Default installation directory is `/usr/local/bin`
36+
37+
To install [BitLab](https://github.com/milosz275/bitlab), simply run the following command in your terminal:
38+
39+
```bash
40+
curl -s https://raw.githubusercontent.com/milosz275/bitlab/main/install.sh | sudo bash
41+
```
42+
43+
## Uninstallation
44+
45+
To uninstall [BitLab](https://github.com/milosz275/bitlab), simply run the following command in your terminal:
46+
47+
```bash
48+
curl -s https://raw.githubusercontent.com/milosz275/bitlab/main/uninstall.sh | sudo bash -s -- -y
49+
```
50+
51+
## Usage
52+
53+
## Config directory
54+
55+
The default configuration directory is `~/.bitlab`. The configuration directory contains the following files:
56+
57+
- `logs/bitlab.log`: BitLab main log file
58+
- `history/cli_history.txt`: BitLab CLI command history file
59+
<!-- - `bitlab.conf`: BitLab configuration file
60+
- `peers.dat`: Peer list file
61+
- `blocks.dat`: Block list file
62+
- `txs.dat`: Transaction list file -->
2063

2164
## Acknowledgements
2265

assets/logo.png

29.2 MB
Loading

bitlab/include/cli.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define CLI_BUFSIZE 64
99
#define CLI_DELIM " "
1010
#define CLI_COMMANDS_NUM (int) (sizeof(cli_commands) / sizeof(cli_command))
11-
#define CLI_HISTORY_FILE ".bitlab_history.txt"
11+
#define CLI_HISTORY_FILE "cli_history.txt"
1212

1313
/**
1414
* CLI command structure.
@@ -90,4 +90,11 @@ int cli_exec_line(char* line);
9090
*/
9191
void* handle_cli(void* arg);
9292

93+
/**
94+
* Create history directory.
95+
*
96+
* @return The history directory.
97+
*/
98+
const char* create_history_dir();
99+
93100
#endif // __CLI_H

bitlab/include/log.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#define __LOG_H
33

44
#include <stdio.h>
5+
#include <stdlib.h>
56
#include <stdarg.h>
67
#include <pthread.h>
78

89
#define MAX_LOG_FILES 10
910
#define MAX_FILENAME_LENGTH 256
10-
#define LOGS_DIR "logs"
1111
#define BITLAB_LOG "bitlab.log"
1212
#define LOG_BITLAB_STARTED "BitLab started ----------------------------------------------------------------------------------------"
1313
#define LOG_BITLAB_FINISHED "BitLab finished successfully"
@@ -59,6 +59,13 @@ typedef struct loggers
5959
int is_initializing;
6060
} loggers;
6161

62+
/**
63+
* Create logs directory used to create the logs directory if it does not exist.
64+
*
65+
* @return The logs directory.
66+
*/
67+
const char* create_logs_dir();
68+
6269
/**
6370
* Initialize logging used to initialize the logging system, open and preserve the log file.
6471
*

bitlab/include/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ void get_formatted_timestamp(char* buffer, size_t buffer_size);
2929
*/
3030
void clear_cli();
3131

32+
/**
33+
* Initialize the configuration directory.
34+
*/
35+
int init_config_dir();
36+
3237
#endif // __UTILS_H

bitlab/src/bitlab.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bitlab_result run_bitlab(int argc, char* argv[])
1616
if (argc != 1 && argv != NULL) {}
1717

1818
// init
19+
init_config_dir();
1920
init_logging(BITLAB_LOG);
2021
log_message(LOG_INFO, BITLAB_LOG, __FILE__, LOG_BITLAB_STARTED);
2122
init_program_state(&state);

bitlab/src/cli.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
#include <readline/readline.h>
44
#include <readline/history.h>
55
#include <errno.h>
6+
#include <sys/stat.h>
67

78
#include "state.h"
89
#include "log.h"
910
#include "utils.h"
1011

12+
static const char* cli_history_dir = NULL;
13+
1114
// BitLab command array
1215
static cli_command cli_commands[] =
1316
{
@@ -175,15 +178,23 @@ int cli_exec_line(char* line)
175178
void* handle_cli(void* arg)
176179
{
177180
char* line = NULL;
178-
read_history(CLI_HISTORY_FILE);
181+
cli_history_dir = create_history_dir();
182+
struct stat st = { 0 };
183+
184+
if (stat(cli_history_dir, &st) == -1 && mkdir(cli_history_dir, 0700) != 0)
185+
log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Failed to create history directory");
186+
187+
char full_path[256];
188+
snprintf(full_path, sizeof(full_path), "%s/%s", cli_history_dir, CLI_HISTORY_FILE);
189+
read_history(full_path);
179190
usleep(50000); // 50 ms
180191
while (!get_exit_flag(&state))
181192
{
182193
line = cli_read_line();
183194
if (line && *line)
184195
{
185196
cli_exec_line(line);
186-
write_history(CLI_HISTORY_FILE);
197+
write_history(full_path);
187198
}
188199
if (line)
189200
{
@@ -192,6 +203,25 @@ void* handle_cli(void* arg)
192203
}
193204
}
194205
if (arg) {} // dummy code to avoid unused parameter warning
206+
if (cli_history_dir != NULL)
207+
{
208+
free((void*)cli_history_dir);
209+
cli_history_dir = NULL;
210+
}
195211
log_message(LOG_INFO, BITLAB_LOG, __FILE__, "Exiting CLI thread");
196212
pthread_exit(NULL);
197213
}
214+
215+
const char* create_history_dir()
216+
{
217+
const char* home = getenv("HOME");
218+
if (home == NULL)
219+
return NULL;
220+
const char* suffix = "/.bitlab/history";
221+
char* logs_dir = malloc(strlen(home) + strlen(suffix) + 1);
222+
if (logs_dir == NULL)
223+
return NULL;
224+
strcpy(logs_dir, home);
225+
strcat(logs_dir, suffix);
226+
return logs_dir;
227+
}

bitlab/src/log.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,31 @@ static struct loggers logs = { PTHREAD_MUTEX_INITIALIZER, {NULL}, 0 };
1717
int fileno(FILE* __stream);
1818
void usleep(unsigned int usec);
1919

20+
static const char* logs_dir = NULL;
21+
22+
const char* create_logs_dir()
23+
{
24+
const char* home = getenv("HOME");
25+
if (home == NULL)
26+
return NULL;
27+
const char* suffix = "/.bitlab/logs";
28+
char* logs_dir = malloc(strlen(home) + strlen(suffix) + 1);
29+
if (logs_dir == NULL)
30+
return NULL;
31+
strcpy(logs_dir, home);
32+
strcat(logs_dir, suffix);
33+
return logs_dir;
34+
}
35+
2036
void init_logging(const char* filename)
2137
{
2238
logs.is_initializing = 1;
23-
const char* log_dir = LOGS_DIR;
39+
logs_dir = create_logs_dir();
2440
struct stat st = { 0 };
2541

26-
if (stat(log_dir, &st) == -1)
42+
if (stat(logs_dir, &st) == -1)
2743
{
28-
if (mkdir(log_dir, 0700) != 0)
44+
if (mkdir(logs_dir, 0700) != 0)
2945
{
3046
perror("Failed to create logs directory");
3147
logs.is_initializing = 0;
@@ -34,7 +50,7 @@ void init_logging(const char* filename)
3450
}
3551

3652
char full_path[256];
37-
snprintf(full_path, sizeof(full_path), "%s/%s", log_dir, filename);
53+
snprintf(full_path, sizeof(full_path), "%s/%s", logs_dir, filename);
3854

3955
pthread_mutex_lock(&logs.log_mutex);
4056
for (int i = 0; i < MAX_LOG_FILES; ++i)
@@ -68,10 +84,8 @@ void init_logging(const char* filename)
6884

6985
void log_message(log_level level, const char* filename, const char* source_file, const char* format, ...)
7086
{
71-
const char* log_dir = LOGS_DIR;
72-
7387
char full_path[256];
74-
snprintf(full_path, sizeof(full_path), "%s/%s", log_dir, filename);
88+
snprintf(full_path, sizeof(full_path), "%s/%s", logs_dir, filename);
7589

7690
pthread_mutex_lock(&logs.log_mutex);
7791

@@ -160,4 +174,9 @@ void finish_logging()
160174
}
161175
}
162176
pthread_mutex_unlock(&logs.log_mutex);
177+
if (logs_dir != NULL)
178+
{
179+
free((void*)logs_dir);
180+
logs_dir = NULL;
181+
}
163182
}

bitlab/src/utils.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "utils.h"
22

3+
#include <stdlib.h>
34
#include <time.h>
5+
#include <string.h>
6+
#include <sys/file.h>
7+
#include <sys/stat.h>
48

59
struct tm* localtime_r(const time_t* timer, struct tm* buf);
610

@@ -24,3 +28,20 @@ void clear_cli()
2428
{
2529
printf("\033[H\033[J");
2630
}
31+
32+
int init_config_dir()
33+
{
34+
const char* home = getenv("HOME");
35+
if (home == NULL)
36+
return 1;
37+
const char* suffix = "/.bitlab";
38+
char* init_dir = malloc(strlen(home) + strlen(suffix) + 1);
39+
if (init_dir == NULL)
40+
return 1;
41+
strcpy(init_dir, home);
42+
strcat(init_dir, suffix);
43+
44+
mkdir(init_dir, 0700);
45+
free(init_dir);
46+
return 0;
47+
}

0 commit comments

Comments
 (0)