Set lower limit of internal sdcard Storage

Source: Internet
Author: User

In Android, some areas of internal storage must be reserved for the system to run applications.

However, this is not taken into account in the native Android design. internal storage can be fully filled.

This will cause sqlitefullexception errors when the system is running programs, especially those that need to operate the database.

The solution is to add a restriction to the sdcard. c file. For example, when the storage limit is lower than MB, third-party applications are no longer allowed to store media files.

Such as ADB push file to/storage/sdcard0/, or recording, take a photo, and save it to/storage/sdcard0.

This m space is reserved to ensure the running of system programs.

The path of the sdcard. c file is in the source code: System/COM/sdcard. c

After modification, you can directly compile the sdcard directory. An sdcard file is generated and pushed to the system/bin directory. After restart, the file takes effect.

Modify the Code:

1. Define the macro at the beginning of sdcard. C.

// Begin: sqlitefullexception happened when device memory is empty.

# Define limit_usedata_size (100*1024*1024)

// End: sqlitefullexception happened when device memory is empty.

2. Add the variable free_blksize to the fuse struct.
Struct fuse {
......
_ U64 next_generation;

// Begin: sqlitefullexception happened when device memory is empty.
_ U64 free_blksize;

// End: sqlitefullexception happened when device memory is empty.

......

}

3. initialize the init method.

Static void fuse_init (struct fuse * fuse, int FD, const char * source_path,
Gid_t write_gid, derive_t derive, bool split_perms ){
......
Fuse-> next_generation = 0;
// Begin: sqlitefullexception happened when device memory is empty.
Struct statfs Stat;
If (statfs (source_path, & Stat) <0 ){
Fuse-> free_blksize = 0;
} Else {
Fuse-> free_blksize = Stat. f_bfree * Stat. f_bsize;
}
// End: sqlitefullexception happened when device memory is empty.

Fuse-> derive = derive;

......

}

4. Implement functions in the handle_write Method

Static int handle_write (struct fuse * fuse, struct fuse_handler * Handler,
Const struct fuse_in_header * HDR, const struct fuse_write_in * req,
Const void * buffer ){
......
Trace ("[% d] Write % P (% d) % [email protected] % LlU \ n", Handler-> token,
H, H-> FD, req-> size, req-> offset );
// Begin: sqlitefullexception happened when device memory is empty.
If (! Strncmp (Fuse-> root. Name, "/data/Media", fuse-> root. namelen )){
Pthread_mutex_lock (& fuse-> lock );
Fuse-> free_blksize-= req-> size;
Pthread_mutex_unlock (& fuse-> lock );

If (Fuse-> free_blksize <= limit_usedata_size ){
Struct statfs Stat;
If (statfs (Fuse-> root. Name, & Stat) <0 ){
Fuse-> free_blksize = 0;
Return-errno;
} Else {
Pthread_mutex_lock (& fuse-> lock );
Fuse-> free_blksize = Stat. f_bfree * Stat. f_bsize;
Pthread_mutex_unlock (& fuse-> lock );
}
Errno = enospc;
Return-errno;
}
}
// End: sqlitefullexception happened when device memory is empty.
Res = pwrite64 (H-> FD, buffer, req-> size, req-> offset );

......

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.