Created
November 20, 2014 10:44
-
-
Save goog/48cc26481436ceb09bb0 to your computer and use it in GitHub Desktop.
my c
This file contains hidden or 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 <sys/types.h> | |
#include <sys/stat.h> | |
#include <dirent.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include "c_dbg.h" | |
#define MAX_PATH_LEN 1024 | |
#define FILE_INFO_LEN 2048 | |
// path to save file tree information of current directory | |
#define PLAYLIST_FILE_PATH "/mnt/sda1/playlist.local" | |
// build a file tree and write it to a text file | |
int build_files_tree(const char *path) | |
{ | |
DIR *dp; | |
struct dirent *dir; | |
char full_path[MAX_PATH_LEN]; | |
char file_infos[FILE_INFO_LEN]; | |
struct stat buf; | |
FILE* fp = fopen(PLAYLIST_FILE_PATH, "w+"); | |
int ret; | |
char size_str[20]; | |
int length_cnt = 0; // record file info length | |
if(path ==NULL || access(path, R_OK) ) | |
{ | |
perror("Read file failed.\n"); | |
return -1; | |
} | |
if( (dp = opendir(path)) == NULL) | |
{ | |
fprintf(stderr,"%s: cannot open.\n",path); | |
return -1; | |
} | |
while((dir = readdir(dp))!= NULL) | |
{ | |
strncpy(full_path,path,strlen(path)); | |
full_path[strlen(path)]='\0'; | |
strcat(full_path,dir->d_name); | |
if(lstat(full_path,&buf) < 0) | |
{ | |
c_dbg_stmt("<%s.%d> lstat failed\n",__FUNCTION__,__LINE__); | |
continue; | |
} | |
// procedure for different file types , type 0 for regular file ; 1 for directory | |
if( S_ISREG(buf.st_mode) ) | |
{ | |
// for transfer, send necessary information | |
if( strcmp(full_path,PLAYLIST_FILE_PATH)==0 ) | |
{ | |
c_dbg_stmt("we filter %s\n",PLAYLIST_FILE_PATH); | |
continue; | |
} | |
strncat(file_infos,dir->d_name,strlen(dir->d_name)); | |
strncat(file_infos," ",3); | |
strncat(file_infos,full_path,strlen(full_path)); | |
strncat(file_infos," ",3); | |
strncat(file_infos,"0",1); | |
strncat(file_infos," ",3); | |
sprintf(size_str, "%llu",(unsigned long long)buf.st_size); | |
strncat(file_infos,size_str,strlen(size_str)); | |
length_cnt = strlen(dir->d_name) + strlen(full_path)+ strlen(size_str) + 10; | |
if(length_cnt > FILE_INFO_LEN-1) | |
{ | |
printf("the memory size of file info is not enough\n"); | |
return -1; | |
} | |
file_infos[length_cnt] = '\0'; | |
c_dbg_stmt("<%s> The file informations: %s\n",__FUNCTION__,file_infos); | |
fwrite(file_infos, 1 , strlen(file_infos), fp); | |
} | |
else if( S_ISDIR(buf.st_mode) ) | |
{ | |
#if 0 | |
if (ftw(full_path, &sum, 1)) | |
{ | |
perror("ftw"); | |
return 2; | |
} | |
#endif | |
// filter two special directories | |
if( strcmp(dir->d_name,".")==0 || strcmp(dir->d_name,"..")==0 ) | |
continue; | |
strncat(file_infos,dir->d_name,strlen(dir->d_name)); | |
strncat(file_infos," ",3); | |
strncat(file_infos,full_path,strlen(full_path)); | |
strncat(file_infos," ",3); | |
strncat(file_infos,"1",1); | |
length_cnt = strlen(dir->d_name) + strlen(full_path) + 7; | |
if(length_cnt > FILE_INFO_LEN-1) | |
{ | |
printf("the memory size of file info is not enough\n"); | |
return -1; | |
} | |
file_infos[length_cnt] = '\0'; | |
c_dbg_stmt("<%s> The file informations: %s\n",__FUNCTION__,file_infos); | |
fwrite(file_infos, 1 , strlen(file_infos), fp); | |
} | |
// add newline | |
ret = fputs("\n",fp); | |
if(ret == EOF) | |
return -1; | |
memset(full_path,0,MAX_PATH_LEN); | |
memset(file_infos,0,FILE_INFO_LEN); | |
} | |
closedir(dp); | |
// write-through all data then close file | |
ret = fflush(fp); | |
if(ret == EOF) | |
perror("fflush failed.\n"); | |
fclose(fp); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment