当一个U盘插入linux系统后,自动在其上面创建music document文件

前题:

  本代码涉及:自动发现U盘的插入、自动寻找U盘在系统中挂载的路径、自动在该U盘上创建一些文件夹<这里是music 和document>,如果这些文件夹已经存在,则不创建。

  代码不添加详细注释,有需要则自动man手册,适合linux编程新手练习使用。如果读者需要发现U盘拔出linux系统可自己实现,在代码中相关注释中有提示了。

 

ps:编程风格不是很好,慢慢看,不心急。

  

  1 /*

  2  *-----------------------------------------

  3  * Name            : auto_mkdir_on_u.c

  4  * Author        : jason

  5  * Email        : [email protected]

  6  *

  7  * Function        : When the U disk into the linux system,automatically to 

  8  * creat music and document directory on it.If them existing,do it not.

  9  *

 10  *-----------------------------------------

 11  */

 12 #include <stdio.h>  

 13 #include <stdlib.h>  

 14 #include <errno.h>

 15 #include <string.h>  

 16 #include <unistd.h>

 17 #include <fcntl.h>  

 18 #include <mntent.h>

 19 #include <sys/types.h>

 20 #include <sys/socket.h>  

 21 #include <linux/netlink.h>  

 22 #include <sys/stat.h>

 23 #include <sys/types.h>

 24 

 25 #define UEVENT_BUFFER_SIZE 2048  

 26 #define mtab_file (access("/proc/mounts",F_OK)?"/proc/mounts":"/etc/mtab")

 27 

 28 char * compale(const char *buf);

 29 char * search_path(const char *com_rec);

 30 int Mkdir(const char * sea_rec);

 31 

 32 int main(void)  

 33 {  

 34     struct sockaddr_nl client;  

 35     struct timeval tv;  

 36     char * com_rec = NULL;

 37     char * sea_rec = NULL;

 38     int netLink_fd, rcvlen, ret;  

 39     fd_set fds;  

 40     int buffersize = 1024;  

 41 

 42     netLink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);  

 43 

 44     memset(&client, 0, sizeof(client));  

 45     client.nl_family = AF_NETLINK;  

 46     client.nl_pid = getpid();  

 47     client.nl_groups = 1; /* receive broadcast message*/  

 48     

 49     //set options on sockets

 50     setsockopt(netLink_fd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));  

 51     

 52     //bind

 53     bind(netLink_fd, (struct sockaddr*)&client, sizeof(client));  

 54 

 55     while (1) {  

 56         char buf[UEVENT_BUFFER_SIZE] = { 0 };  

 57         

 58         FD_ZERO(&fds); //clean 

 59         FD_SET(netLink_fd, &fds); // write 1 on bit netLink_fd

 60 

 61         tv.tv_sec = 0;  

 62         tv.tv_usec = 100 * 1000;  

 63 

 64         ret = select(netLink_fd + 1, &fds, NULL, NULL, &tv);  

 65         if(ret < 0)  

 66             continue;  

 67         if(!(ret > 0 && FD_ISSET(netLink_fd, &fds)))  

 68             continue;  

 69 /*

 70  * receive data 

 71  * There is a buf space,many important info in it.If you want to knonw more, * you can use code like this:

 72  * printf("buf = %s\n",buf);

 73  * and do it that you like.

 74  * detail please man recv or man 2 recv.

 75  *

 76  */  

 77         rcvlen = recv(netLink_fd, &buf, sizeof(buf), 0);  

 78         if (rcvlen > 0) {  

 79             if((com_rec = compale(buf)) != NULL){

 80                 sleep(2);

 81                 printf("com_rec = %s\n",com_rec);

 82             

 83                 sea_rec = search_path(com_rec);

 84                 if(sea_rec != NULL){

 85                     printf("sea_rec = %s\n",sea_rec);

 86                     if(Mkdir(sea_rec) < 0){

 87                         printf("create Failed!\n");

 88                     }else {

 89                         printf("create OK !\n");

 90                     }

 91                 }

 92             }

 93         }

 94         else{

 95             printf("error:%s\n",strerror(errno));

 96         }

 97     }  

 98     close(netLink_fd);  

 99     return 0;  

100 }  

101 

102 /* find /dev/sdx */

103 char * compale(const char * buf) 

104 {

105     const char *s1 = "add@";

106     const char *s2 = "/block/sd";

107     char *p = NULL;

108 

109     if(strstr(buf,s1) != NULL){

110         if((p = strstr(buf,s2)) != NULL){

111             return p;

112         }

113     }

114     return NULL;

115 }

116 

117 /* find systemMount point */

118 char * search_path(const char * com_rec)

119 {

120 /* com_rec = /block/sdb  or /block/sdb1 */

121 

122     struct mntent *mnt;

123     FILE *file = NULL;

124     int i = 0;

125     const char * p = com_rec;

126     //char * retu_value = NULL;

127     for(i = 0;i < 6;i++){

128         p++;

129     }

130     printf("search_path p = %s\n",p);

131     

132     file = setmntent(mtab_file,"r");

133     if(!file){

134         printf("error:%s\n",strerror(errno));

135 //        return NULL;

136     }

137     while((mnt = getmntent(file))){

138 //        printf("Driver %s,name %s,type %s,opt %s\n",mnt->mnt_dir,mnt->mnt_fsname,mnt->mnt_type,mnt->mnt_opts);

139 //        printf("mnt->mnt_fsname = %s\n",mnt->mnt_fsname);

140         if(strstr(mnt->mnt_fsname,p) != NULL){

141             printf("here!\n");

142             endmntent(file);

143             return (char *)mnt->mnt_dir;

144         }

145 

146 //        retu_value = strstr(mnt->mnt_fsname,p);

147 //        printf("retu_value = %s\n",retu_value);

148     }

149     endmntent(file);

150     return NULL;

151 }

152 

153 /* find music and document dirctory existing or not */

154 int Mkdir(const char * sea_rec)

155 {

156     const char * const  music_s = "/music";

157     const char * const  document_s = "/document";

158 //    char * p_rec = sea_rec;

159 

160     char * p_music = (char*)malloc(sizeof(char)*128);

161     if(NULL == p_music){

162         perror("p_music");

163         exit(1);

164     }

165     memset(p_music,0,sizeof(char)*128);

166     p_music = strcpy(p_music,sea_rec);

167 

168     char * p_document = (char*)malloc(sizeof(char)*128);

169     if(NULL == p_document){

170         perror("p_document");

171         exit(1);

172     }

173     memset(p_document,0,sizeof(char)*128);

174     p_document = strcpy(p_document,sea_rec);

175     

176     printf("p_music = %s\n",p_music);

177     printf("p_document = %s\n",p_document);

178     //auto create music dirctory on disk

179     p_music = strcat(p_music,music_s);

180     if(access(p_music,F_OK) < 0){

181         printf("No music\n");

182         perror("access!");

183         if(mkdir(p_music,S_IRWXU) < 0){

184             perror("mkdir");

185     //        return -1;

186         }

187     }else {

188         printf("music existing!\n");

189     }

190     

191     printf("p_music = %s\n",p_music);

192     printf("p_document = %s\n",p_document);

193 

194     //auto create document dirctory on disk 

195     p_document = strcat(p_document,document_s);

196     if(access(p_document,F_OK) < 0){

197         printf("No document\n");

198         perror("access");

199         if(mkdir(p_document,S_IRWXU) < 0){

200             perror("mkdir");

201     //        return -1;

202         }

203     }else{

204         printf("document existing !\n");

205     }

206 

207     free(p_music);

208     free(p_document);

209     return 0;

210 }

 

你可能感兴趣的:(document)