Skip to content

Instantly share code, notes, and snippets.

@jitomesky
Created May 15, 2014 15:59
Show Gist options
  • Save jitomesky/db13799d93a84030e92e to your computer and use it in GitHub Desktop.
Save jitomesky/db13799d93a84030e92e to your computer and use it in GitHub Desktop.
高校生の時に作ったrsyncもどきのプログラム
/*
* file_sync.c
*
* Created on: 2010/04/25
* Author: NishinagaToshifumi
*/
//#define WIN32
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#define READBYTE 5000000 //送信する際一度に読み込むデータのビット数
#define DEBUG 1 //1:デバッグモード
int FileSync(char *);
// 2つの文字列を/(or \)を挟んで結合し、3つ目の文字列ポインタに格納する
int CombinationPath(char *,char *,char *);
// ファイルを転送する
int file_send(const char *,const char *);
char *root_path="D:\\Programming\\file_sync\\test_mainPC"; // 参照するディレクトリのパス名
char *send_path="D:\\Programming\\file_sync\\test_server";
int root_path_len;
int send_path_len;
int main(int argc,char **argv){
root_path_len = strlen(root_path);
send_path_len = strlen(send_path);
FileSync("");
return 0;
}
int FileSync(char *BeforePath){
int i=0; // 最初に出てくる . と .. を無視するためのカウンタ
struct dirent *dp; // readdirから渡される構造体のアドレスを格納するポインタ
struct stat root_file_inf; // 送り元のファイル情報を入れる構造体
struct stat send_file_inf; // 送り先のファイル情報を入れる構造体
int existf=0; // ファイルが存在するかどうかのフラグ
int BeforePath_len; // 受け取ったパスの文字数を入れる
char *cd_path; // 検索するディレクトリのパスを入れるポインタ
char *FileName; //ファイルの名前が格納されたアドレスを入れるポインタ
char *FilePath; //ファイルの絶対パスが格納されたアドレスを入れるポインタ
char *re_path; // 前パス+ファイル名が格納されたアドレスを入れるポインタ
char *SendPath; // 先パス+前パス+ファイル名が格納されたアドレスを入れるポインタ
DIR *dir; //opendirの値を格納する構造体のポインタ
// cd_pathに検索するディレクトリのパスを入れる
BeforePath_len = strlen(BeforePath);
//mallocでcd_pathのメモリ領域確保
//cd_pathの大きさは(root_point_path + '(/ or \)' + before_path + '\0')分
cd_path = (char *)malloc(root_path_len + 1 + BeforePath_len + 1);
CombinationPath(root_path,BeforePath,cd_path);
//debug
if(DEBUG)
printf("cd_path = %s\n",cd_path);
// cd_pathを開く
if( (dir=opendir(cd_path)) == NULL){
perror("opendir");
exit(-1);
}
//ディレクトリを再帰的に読み込む。
for(dp = readdir(dir);dp != NULL;dp=readdir(dir) ){
if(i >= 2){// 最初に出てくる. と .. を無視する
FileName = (dp->d_name); //ファイルの名前を取得
// 前パス+ファイルの名
re_path = (char *)malloc(BeforePath_len + 1 + strlen(FileName) + 1);
CombinationPath(BeforePath,FileName,re_path);
// #1 end
// 元パス + re_path
FilePath = (char *)malloc(root_path_len + 1 + strlen(re_path) + 1);
CombinationPath(root_path,re_path,FilePath);
//#2 end
SendPath = (char *)malloc(send_path_len + 1 + strlen(re_path) + 1);
CombinationPath(send_path,re_path,SendPath);
printf("FilePath = %s\n",FilePath);
if( stat(FilePath, &root_file_inf) == -1){// stat関数が正常に動いているかチェック
perror("stat");
exit(-1);
}//end if stat
if( stat(SendPath, &send_file_inf) == -1){// stat関数が正常に動いているかチェック
existf=1;
printf("File is not exist\n");
}//end if stat
else
printf("File was existed\n");
if((root_file_inf.st_mode & S_IFMT) == S_IFDIR){//ディレクトリ?
if(existf == 1){
//送り先にディレクトリを作る
#ifdef WIN32
if(mkdir(SendPath) == -1)
#else
if(mkdir(SendPath,0x777) == -1)
#endif
{
perror("mkdir");
exit(-1);
}
}
//ディレクトリに入る
printf("This file is directory\n");
FileSync(re_path);
}
else{
if(existf == 0){
//ファイルの更新日時を比較して転送
if(difftime(root_file_inf.st_mtime,send_file_inf.st_mtime) > 0){
file_send(FilePath,SendPath);
}
else
printf("This file was not changed\n");
}
else{
//ファイルを転送する
file_send(FilePath,SendPath);
}
}
free(re_path);
free(FileName);
free(FilePath);
free(SendPath);
}
free(cd_path);
i++;
}
return 0;
}
int CombinationPath(char *first_string,char *second_string,char *put_path){
strcpy(put_path,first_string);
#ifdef WIN32
strcat(put_path,"\\");
#else
strcat(put_path,"/");
#endif
strcat(put_path,second_string);
return 0;
}
int file_send(const char *path_src,const char *dest_path){
FILE *fp;
FILE *fp2;
char *data;
size_t size;
fp = fopen(path_src,"rb");//herepにファイルストリームを入れる
fp2 = fopen(dest_path,"w+b");
if(fp == NULL){
printf("%sが読み込めませんでした\n",path_src);
return -1;
}
if(fp2 == NULL){
printf("%sが読み込めませんでした\n",dest_path);
return -1;
}
//fopen,fwrite関数を使って読み書きする。
//容量の大きいファイルを扱うために5Mbずつ読み書きする。
data = (char*)malloc(READBYTE);
while((size = fread(data,1,READBYTE,fp))>0){
fwrite(data,1,size,fp2);
}
fclose(fp);//ファイルを閉じる
fclose(fp2);//ファイルを閉じる
free(data);
printf("sended\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment