U disk data automatic processing system

Requirement:
1) after inserting u disk, it can automatically identify and mount
2) a u tomatically traverse the contents of USB flash disk and classify them
3) copy to local directory (/ var/jpeg and / var/mp3)

Expand:
1) loop. mp3 as background music
2) loop. jpeg picture

I. u disk automatic identification and mounting

2. Automatically traverse the contents of the u disk and copy them to the local directory

//usetest.h

#ifndef __USBTEST_H
#define __USBTEST_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

#include <errno.h>

#include "link.h"

#define MAX_PATH 512


void copy_file(char *pathname,char *path);
bool path_exit(char *pathname,char last[],jpeg_linklist *jpg);
void print_file_info(char *pathname,jpeg_linklist *jpg);
void dir_order(char *pathname,jpeg_linklist *jpg);


#endif

In usbtest.c

1) determine suffix

bool path_exit(char *pathname,char last[],jpeg_linklist *jpg)
{
	char *ptr,*fileExit;
	ptr = strrchr(pathname,'.');

	if(strcmp(ptr,last) == 0)
	{
		printf("%s exit:%s\n",last,pathname);
		copy_file(pathname,last+1);
		insert_list(jpg,pathname);

		return true;
	}
	else
		return false;
}

2) copy. mp3/.jpeg files to / var/mp3 and / var/jpeg

void copy_file(char *pathname,char *path)
{
	char buf[1024] = {0};
	char *name,newname[MAX_PATH];
	FILE *fp,*fp1;
	int cnt;

	fp = fopen(pathname,"a+");
	if(fp == NULL)
	{
		printf("open test fail\n");
		return;
	}

	sprintf(newname,"%s/%s","/vim",path);	//Define the path to which you want to copy the file. Here is / vim/jpeg
	name = strrchr(pathname,'/')+1;			//Original file name (excluding path)
	sprintf(newname,"%s/%s",newname,name);	//Copy past file paths
	printf("newname:%s\n",newname);

	fp1 = fopen(newname,"w+");
	if(fp1 == NULL)
	{
		printf("open test1 fail\n");
		return;
	}

	while((cnt = fread(buf,sizeof(char),sizeof(buf),fp)) > 0)
	{
		fwrite(buf,sizeof(char),cnt,fp1);
		printf("cnt:%d\n",cnt);
	}	

	fclose(fp);
	fclose(fp1);
}

3) traverse the contents of u disk

void print_file_info(char *pathname,jpeg_linklist *jpg)		//Get node information
{
	struct stat filestat;
	if(stat(pathname,&filestat) == -1)
	{
		printf("cannot access file %s\n",pathname);
		return;
	}
	printf("%s %8ld\n",pathname,filestat.st_size);

	path_exit(pathname,".jpeg",jpg);		//Call path_exit() to find the file path matching the. jpeg suffix during traversal
	path_exit(pathname,".mp3");

	if((filestat.st_mode & S_IFMT) == S_IFDIR)		//If it's a directory, call recursively
		dir_order(pathname,jpg);
}
void dir_order(char *pathname,jpeg_linklist *jpg)
{
	DIR *dfd;
	char name[MAX_PATH];
	struct dirent *dp;

	if((dfd = opendir(pathname)) == NULL)		//Open Directory
	{
		printf("dir_order:cannot open %s\n %s",pathname,strerror(errno));
		return;
	}

	while((dp = readdir(dfd)) != NULL)		//Read directory
	{
		if(strncmp(dp->d_name,".",1) == 0)
			continue;

		if(strlen(pathname)+strlen(dp->d_name)+2 > sizeof(name))
			printf("dir_order:name %s %s too long\n",pathname,dp->d_name);
		else
		{
			memset(name,0,sizeof(name));
			sprintf(name,"%s/%s",pathname,dp->d_name);
			print_file_info(name,jpg);
		}
	}
	closedir(dfd);
}

3. Loop. jpeg picture

Create a one-way circular list to store the pathname of. jpeg

//link.h medium

ifndef __LINK_H
#define __LINK_H

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct Jpeg_linklist
{
	char namepath[256];
	struct Jpeg_linklist *next;
}jpeg_linklist;

jpeg_linklist *create_list(char *namepath);
bool insert_list(jpeg_linklist *head,char *namepath);
void show_list(jpeg_linklist *head);

#endif
jpeg_linklist *create_list(char *namepath)
{
	jpeg_linklist *head = (jpeg_linklist*)malloc(sizeof(jpeg_linklist));
	if(head == NULL)
	{
		printf("create fail\n");
		return NULL;
	}

	strcpy(head->namepath,namepath);
	head->next = head;

	return head;
}
bool insert_list(jpeg_linklist *head,char *namepath)
{
	jpeg_linklist *new = create_list(namepath);
	if(new == NULL)
	{
		printf("insert jpeg fail\n");
		return false;
	}

	new->next = head->next;
	head->next = new;

	return true;
}
void show_list(jpeg_linklist *head)
{
	char newname[256];

	if(head == NULL)
		return;

	jpeg_linklist *p = head->next;
	
	while(1)		//Loop Playback
	{
		printf("list:%s\t",p->namepath);
		sprintf(newname,"%s %s","xdg-open",p->namepath);
		system(newname);
		sleep(2);
		p = p->next;
	}
	printf("\n");
}

IV. main function

#include "usbtest.h"
#include "link.h"

int main(int argc,char *argv[])
{
	jpeg_linklist *jpg = NULL;
	jpg = create_list("NULL");

	if(jpg == NULL)
	{
		printf("create jpeg list fail\n");
		return -1;
	}


	if(argc == 1)
		dir_order(".",jpg);
	else
		dir_order(argv[1],jpg);


	show_list(jpg);
				
	return 0;
}

Tags: vim

Posted on Thu, 28 Nov 2019 12:26:13 -0800 by pleek