-
-
Save mrabault/e717a32d201001fe9b18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include "helpers.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
#include <string.h> | |
void exode_dir() | |
{ | |
if (chdir(getenv("HOME")) == -1) | |
{ | |
printf("Couldn't get to the home directory"); | |
perror("Error"); | |
exit(EXIT_FAILURE); | |
} | |
if (mkdir(".exode", S_IRUSR | S_IWUSR | S_IXUSR) == 0 || errno == EEXIST) | |
{ | |
if (chdir(".exode")) | |
{ | |
printf("Couldn't get to exode's directory"); | |
perror("Error"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
} | |
void repo_dir(char* repo) | |
{ | |
exode_dir(); | |
if (chdir(repo) == -1) | |
{ | |
printf("Couldn't get to the repo directory: %s \n", repo); | |
perror("Error"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
char* absolute_path(const char* filepath) | |
{ | |
char* name = NULL; | |
char* dirname = NULL; | |
/* TODO: Fix the warning about the cast int > char* */ | |
dirname = get_current_dir_name(); | |
if (dirname == NULL) | |
{ | |
perror("Error:"); | |
exit(EXIT_FAILURE); | |
} | |
if (asprintf(&name, "%s/%s", dirname, filepath) == -1) | |
{ | |
printf("Error: asprintf() returned -1"); | |
exit(EXIT_FAILURE); | |
} | |
free(dirname); | |
return name; | |
} | |
char* destination_path(const char* filepath) | |
{ | |
char *name, *home = NULL; | |
/* We need to remove the tree root's name from the string, and then we can find the destiantion path */ | |
home = getenv("HOME"); | |
if (home == NULL) | |
{ | |
printf("Error: getenv(\"HOME\") returned NULL"); | |
exit(EXIT_FAILURE); | |
} | |
/* We don't check the return value of strchr as we know for a certainty that filepath contains at least one '\' */ | |
if (asprintf(&name, "%s/%s", home, strchr(filepath, '/')+1) == -1) | |
{ | |
printf("Error: asprintf() returned -1"); | |
exit(EXIT_FAILURE); | |
} | |
return name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HELPERS_H | |
#define HELPERS_H | |
void exode_dir(); /* Should be ~/.exode */ | |
void repo_dir(char* repo); | |
/* These functions return dynamically allocated strings, free them after usage */ | |
char* absolute_path(const char* filepath); | |
char* destination_path(const char* filepath); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "repository.h" | |
const char* badsyntax = "Wrong syntax, RTFM"; | |
int main(int argc, char** argv) | |
{ | |
if (argc == 1 || argc > 4) | |
{ | |
printf("%s \n", badsyntax); | |
return 0; | |
} | |
if (strcmp(argv[1], "add") == 0 && (argc == 3)) | |
{ | |
add_dotfiles_repository(argv[2]); | |
} | |
else if (strcmp(argv[1], "del") == 0 && (argc == 3)) | |
{ | |
rm_dotfiles_repository(argv[2]); | |
} | |
else if (strcmp(argv[1], "install") == 0 && (argc == 4)) | |
{ | |
install_target(argv[2], argv[3]); | |
} | |
else | |
{ | |
printf("%s \n", badsyntax); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HELPERS_H | |
#define HELPERS_H | |
void exode_dir(); /* Should be ~/.exode */ | |
void repo_dir(char* repo); | |
/* These functions return dynamically allocated strings, free them after usage */ | |
char* absolute_path(const char* filepath); | |
char* destination_path(const char* filepath); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HELPERS_H | |
#define HELPERS_H | |
void exode_dir(); /* Should be ~/.exode */ | |
void repo_dir(char* repo); | |
/* These functions return dynamically allocated strings, free them after usage */ | |
char* absolute_path(const char* filepath); | |
char* destination_path(const char* filepath); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _XOPEN_SOURCE 500 | |
#define _GNU_SOURCE | |
#include "repository.h" | |
#include "helpers.h" | |
#include "nftwcallbacks.h" | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <dirent.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <error.h> | |
void add_dotfiles_repository(char* url) | |
{ | |
exode_dir(); | |
char* args[] = {"git", "clone", url, NULL}; | |
execvp(args[0], args); | |
} | |
void rm_dotfiles_repository(char* repo) | |
{ | |
repo_dir(repo); | |
/* Remove the repository's files recursively | |
* TODO: Remove the symbolic links in ~ before removing the repo | |
* We remove the repository, target by target */ | |
DIR* dir = NULL; | |
struct dirent* file = NULL; | |
struct stat stat_data; | |
dir = opendir("."); | |
if (dir == NULL) | |
{ | |
perror("Error:"); | |
exit(EXIT_FAILURE); | |
} | |
file = readdir(dir); | |
while ((file = readdir(dir)) != NULL) | |
{ | |
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) | |
{ | |
/* TODO: why isn't .git listed, even if .gitmodules is listed ? After tests, it seems that .something repositories which are non-empty | |
* aren't listed*/ | |
if(stat(file->d_name, &stat_data)) | |
{ | |
perror("Error"); | |
exit(EXIT_FAILURE); | |
} | |
if (S_ISDIR(stat_data.st_mode)) | |
{ | |
remove_target(repo, file->d_name); | |
} | |
else | |
{ | |
printf("Remove file %s\n", file->d_name); | |
} | |
} | |
} | |
if (closedir(dir)) | |
{ | |
perror("Error:"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
void install_target(char* repo, char* target) | |
{ | |
repo_dir(repo); | |
if (nftw(target, install, 4, 0)) | |
{ | |
exit(EXIT_FAILURE); | |
} | |
} | |
void remove_target(char* repo, char* target) | |
{ | |
printf("Remove target %s from repo %s\n", target, repo); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _XOPEN_SOURCE 500 | |
#define _GNU_SOURCE | |
#include "repository.h" | |
#include "helpers.h" | |
#include "nftwcallbacks.h" | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <dirent.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <error.h> | |
void add_dotfiles_repository(char* url) | |
{ | |
exode_dir(); | |
char* args[] = {"git", "clone", url, NULL}; | |
execvp(args[0], args); | |
} | |
void rm_dotfiles_repository(char* repo) | |
{ | |
repo_dir(repo); | |
/* Remove the repository's files recursively | |
* TODO: Remove the symbolic links in ~ before removing the repo | |
* We remove the repository, target by target */ | |
DIR* dir = NULL; | |
struct dirent* file = NULL; | |
struct stat stat_data; | |
dir = opendir("."); | |
if (dir == NULL) | |
{ | |
perror("Error:"); | |
exit(EXIT_FAILURE); | |
} | |
file = readdir(dir); | |
while ((file = readdir(dir)) != NULL) | |
{ | |
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) | |
{ | |
/* TODO: why isn't .git listed, even if .gitmodules is listed ? After tests, it seems that .something repositories which are non-empty | |
* aren't listed*/ | |
if(stat(file->d_name, &stat_data)) | |
{ | |
perror("Error"); | |
exit(EXIT_FAILURE); | |
} | |
if (S_ISDIR(stat_data.st_mode)) | |
{ | |
remove_target(repo, file->d_name); | |
} | |
else | |
{ | |
printf("Remove file %s\n", file->d_name); | |
} | |
} | |
} | |
if (closedir(dir)) | |
{ | |
perror("Error:"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
void install_target(char* repo, char* target) | |
{ | |
repo_dir(repo); | |
if (nftw(target, install, 4, 0)) | |
{ | |
exit(EXIT_FAILURE); | |
} | |
} | |
void remove_target(char* repo, char* target) | |
{ | |
printf("Remove target %s from repo %s\n", target, repo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment