aboutsummaryrefslogtreecommitdiff
path: root/sdk-modifications/libsrc/fs/fat_misc.c
blob: c0f62ac7ee441fa83e1cf63d47fec70def74d786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//fat_misc.c
//v1.0

#include "fat_misc.h"
#include "fs_api.h"

static unsigned int _usedSecNums;

static int strFindFromEnd( char *str,char strValue  )
{
	int pos = 0,i = 0,strNum = 0;
	while(1)
	{
		if( (*str)!=0 )
		{
			strNum++;
			str++;
		}
		else
		{
			break;
		}
	}
	pos = strNum;
	for( i=0;i<strNum;i++ )
	{
		str--;
		pos--;
		if( (*str) == strValue||(*str) == ':' )
		{
			return pos;
		}
		if( (*str)== 0 )
		{
			return -1;
		}
	}
	return -1; 
}

int getDirSize( const char * path, int includeSubdirs, unsigned int * dirSize )
{
    char dirPath[MAX_FILENAME_LENGTH];
	unsigned int size = 0;
    if( "" == path ){
        return false;
    }

    memset( dirPath,0,MAX_FILENAME_LENGTH );
    strcpy( dirPath,path );

    if( dirPath[strlen(dirPath)-1] != '/' )
        dirPath[strlen(dirPath)] = '/';
    if( strlen(dirPath) > MAX_FILENAME_LENGTH )
        return false;

    DIR_STATE_STRUCT *dir;
	dir = fat_opendir((const char*)dirPath);
    if (dir == NULL)
        return false;
    
    struct stat stat_buf;
	DIR_ENTRY *currentEntry;
    char* filename;

	while(fat_readdir_ex(dir, &stat_buf) != NULL)
	{
		filename = currentEntry->d_name;

        if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
            continue;

        if (!(stat_buf.st_mode & S_IFDIR)) {
			size += (stat_buf.st_size+511)/512;
			_usedSecNums +=(stat_buf.st_size+511)/512;
		}
		else if (includeSubdirs)
		{
			// calculate the size recursively 
            unsigned int subDirSize = 0;
			char dirPathBuffer[MAX_FILENAME_LENGTH];

            memset( dirPathBuffer,0,MAX_FILENAME_LENGTH );
            strcpy( dirPathBuffer,dirPath );
            memset( dirPath,0,MAX_FILENAME_LENGTH );
            sprintf( dirPath,"%s%s",dirPathBuffer,filename );
            int succ = getDirSize( dirPath, includeSubdirs, &subDirSize );
            if( succ ) {
                size += (subDirSize+511)/512;
                _usedSecNums +=(subDirSize+511)/512;
            }
            memset( dirPath,0,MAX_FILENAME_LENGTH );
            strcpy( dirPath,dirPathBuffer );
        }
    }

	fat_closedir(dir);

    *dirSize = size;
    return true;
}

int fat_getDiskTotalSpace( char * diskName, unsigned int * diskSpace )
{
	if( !strcmp("",diskName) )
		return false;

	unsigned int len = strlen(diskName);
	if( *(diskName+len-1) != '/' ){
		*(diskName+len) = '/';
	}

	PARTITION * diskPartition = _FAT_partition_getPartitionFromPath( diskName );
	if( NULL == diskPartition )
		return false;

	*diskSpace = (unsigned int)diskPartition->numberOfSectors;
	return true;
}

int fat_getDiskSpaceInfo( char * diskName, unsigned int * total, unsigned int * used, unsigned int * freeSpace )
{
	_usedSecNums = 0;

	if( !strcmp("",diskName) )
		return -1;
    if( !fat_getDiskTotalSpace(diskName, total) )
		return -1;
	if( !getDirSize(diskName, true, used) )
		return -1;

    *used = _usedSecNums;
    if( *total <= *used ){
   	 	*freeSpace = 0;
   	}else{
    	*freeSpace = *total - *used;
  	}

    return 0;
}