Changeset 13699 for trunk/libtransmission/platform.c
- Timestamp:
- Dec 27, 2012, 8:19:41 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/platform.c
r13698 r13699 720 720 #ifndef WIN32 721 721 static char * 722 getdev ( const char * path)722 getdev (const char * path) 723 723 { 724 724 #ifdef HAVE_GETMNTENT 725 FILE *fp; 726 struct mntent *mnt; 727 728 if ((fp = setmntent(_PATH_MOUNTED, "r")) == NULL) 729 { 730 return NULL; 731 } 732 while ((mnt = getmntent(fp)) != NULL) 733 { 734 if (strcmp(path, mnt->mnt_dir) == 0) 735 { 736 break; 737 } 738 } 739 endmntent(fp); 740 return mnt ? mnt->mnt_fsname : NULL; 725 726 FILE * fp; 727 struct mntent * mnt; 728 729 fp = setmntent(_PATH_MOUNTED, "r"); 730 if (fp == NULL) 731 return NULL; 732 733 while ((mnt = getmntent(fp)) != NULL) 734 if (!tr_strcmp0 (path, mnt->mnt_dir)) 735 break; 736 737 endmntent(fp); 738 return mnt ? mnt->mnt_fsname : NULL; 739 741 740 #else /* BSD derived systems */ 742 int n, i; 743 struct statfs *mnt;744 745 if ((n = getmntinfo(&mnt, MNT_WAIT)) == 0) 746 { 747 return NULL;748 } 749 for (i=0; i<n; i++) 750 { 751 if (strcmp(path, mnt[i].f_mntonname) == 0)752 { 753 754 } 755 } 756 return (i < n) ? mnt[i].f_mntfromname : NULL; 741 742 int i; 743 int n; 744 struct statfs * mnt; 745 746 n = getmntinfo(&mnt, MNT_WAIT); 747 if (!n) 748 return NULL; 749 750 for (i=0; i<n; i++) 751 if (!tr_strcmp0 (path, mnt[i].f_mntonname)) 752 break; 753 754 return (i < n) ? mnt[i].f_mntfromname : NULL; 755 757 756 #endif 758 757 } 759 758 760 759 static char * 761 getfstype( const char * device ) 762 { 760 getfstype (const char * device) 761 { 762 763 763 #ifdef HAVE_GETMNTENT 764 FILE *fp; 765 struct mntent *mnt; 766 767 if ((fp = setmntent(_PATH_MOUNTED, "r")) == NULL) 768 { 769 return NULL; 770 } 771 while ((mnt = getmntent(fp)) != NULL) 772 { 773 if (strcmp(device, mnt->mnt_fsname) == 0) 774 { 775 break; 776 } 777 } 778 endmntent(fp); 779 return mnt ? mnt->mnt_type : NULL; 764 765 FILE * fp; 766 struct mntent *mnt; 767 768 fp = setmntent (_PATH_MOUNTED, "r"); 769 if (fp == NULL) 770 return NULL; 771 772 while ((mnt = getmntent (fp)) != NULL) 773 if (!tr_strcmp0 (device, mnt->mnt_fsname)) 774 break; 775 776 endmntent(fp); 777 return mnt ? mnt->mnt_type : NULL; 778 780 779 #else /* BSD derived systems */ 781 int n, i; 782 struct statfs *mnt;783 784 if ((n = getmntinfo(&mnt, MNT_WAIT)) == 0) 785 { 786 return NULL;787 } 788 for (i=0; i<n; i++) 789 { 790 if (strcmp(device, mnt[i].f_mntfromname) == 0)791 { 792 793 } 794 } 795 return (i < n) ? mnt[i].f_fstypename : NULL; 780 781 int i; 782 int n; 783 struct statfs *mnt; 784 785 n = getmntinfo(&mnt, MNT_WAIT); 786 if (!n) 787 return NULL; 788 789 for (i=0; i<n; i++) 790 if (!tr_strcmp0 (device, mnt[i].f_mntfromname)) 791 break; 792 793 return (i < n) ? mnt[i].f_fstypename : NULL; 794 796 795 #endif 797 796 } 798 797 799 798 static char * 800 getblkdev ( const char * path)801 { 802 char *dir, *c;803 char *device;804 805 dir = tr_strdup(path); 806 while (1) 807 { 808 if ((device = getdev(dir)) != NULL)809 810 break;811 } 812 if ((c = strrchr(dir, '/')) != NULL) 813 { 814 *c = '\0';815 } 816 else 817 { 818 819 820 } 821 tr_free(dir);822 799 getblkdev (const char * path) 800 { 801 char * c; 802 char * dir; 803 char * device; 804 805 dir = tr_strdup(path); 806 807 for (;;) 808 { 809 device = getdev (dir); 810 if (device != NULL) 811 break; 812 813 c = strrchr (dir, '/'); 814 if (c != NULL) 815 *c = '\0'; 816 else 817 break; 818 } 819 820 tr_free (dir); 821 return device; 823 822 } 824 823 825 824 static int64_t 826 getquota( char * device ) 827 { 828 struct dqblk dq; 829 int64_t freespace, limit; 830 831 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device, getuid(), 832 (caddr_t) & dq) == 0) 833 { 834 if (dq.dqb_bsoftlimit > 0) 835 { 836 /* Use soft limit first */ 837 limit = dq.dqb_bsoftlimit; 838 } 839 else if (dq.dqb_bhardlimit > 0) 840 { 841 limit = dq.dqb_bhardlimit; 842 } 843 else 844 { 845 /* No quota enabled for this user */ 846 return -1; 847 } 848 freespace = limit - btodb(dq.dqb_curspace); 849 return (freespace < 0) ? 0 : freespace * 1024; 850 } 851 852 /* Something went wrong */ 853 return -1; 825 getquota (char * device) 826 { 827 struct dqblk dq; 828 int64_t limit; 829 int64_t freespace; 830 831 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device, getuid(), (caddr_t) & dq) == 0) 832 { 833 if (dq.dqb_bsoftlimit > 0) 834 { 835 /* Use soft limit first */ 836 limit = dq.dqb_bsoftlimit; 837 } 838 else if (dq.dqb_bhardlimit > 0) 839 { 840 limit = dq.dqb_bhardlimit; 841 } 842 else 843 { 844 /* No quota enabled for this user */ 845 return -1; 846 } 847 848 freespace = limit - btodb(dq.dqb_curspace); 849 return (freespace < 0) ? 0 : freespace * 1024; 850 } 851 852 /* something went wrong */ 853 return -1; 854 854 } 855 855 856 856 #ifdef HAVE_XQM 857 857 static int64_t 858 getxfsquota( char * device ) 859 { 860 struct fs_disk_quota dq; 861 int64_t freespace, limit; 862 863 if (quotactl(QCMD(Q_XGETQUOTA, USRQUOTA), device, getuid(), 864 (caddr_t) &dq) == 0) 865 { 866 if (dq.d_blk_softlimit > 0) 867 { 868 /* Use soft limit first */ 869 limit = dq.d_blk_softlimit >> 1; 870 } 871 else if (dq.d_blk_hardlimit > 0) 872 { 873 limit = dq.d_blk_hardlimit >> 1; 874 } 875 else 876 { 877 /* No quota enabled for this user */ 878 return -1; 879 } 880 freespace = limit - (dq.d_bcount >> 1); 881 return (freespace < 0) ? 0 : freespace * 1024; 882 } 883 884 /* Something went wrong */ 885 return -1; 858 getxfsquota (char * device) 859 { 860 int64_t limit; 861 int64_t freespace; 862 struct fs_disk_quota dq; 863 864 if (quotactl(QCMD(Q_XGETQUOTA, USRQUOTA), device, getuid(), (caddr_t) &dq) == 0) 865 { 866 if (dq.d_blk_softlimit > 0) 867 { 868 /* Use soft limit first */ 869 limit = dq.d_blk_softlimit >> 1; 870 } 871 else if (dq.d_blk_hardlimit > 0) 872 { 873 limit = dq.d_blk_hardlimit >> 1; 874 } 875 else 876 { 877 /* No quota enabled for this user */ 878 return -1; 879 } 880 881 freespace = limit - (dq.d_bcount >> 1); 882 return (freespace < 0) ? 0 : freespace * 1024; 883 } 884 885 /* something went wrong */ 886 return -1; 886 887 } 887 888 #endif /* HAVE_XQM */ … … 889 890 890 891 static int64_t 891 tr_getQuotaFreeSpace( const char * path, char * device, char * fstype ) 892 { 893 int64_t ret=-1; 892 tr_getQuotaFreeSpace (const char * path, char * device, char * fstype) 893 { 894 int64_t ret=-1; 895 894 896 #ifndef WIN32 895 char *d; 896 char *fs; 897 898 if (strlen(device) == 0) 899 { 900 if ((d = getblkdev(path)) == NULL) 901 { 902 return ret; 903 } 904 /* Save device for future use */ 905 tr_strlcpy(device, d, PATH_MAX + 1); 906 } 907 908 if (strlen(fstype) == 0) 909 { 910 if ((fs = getfstype(device)) != NULL) 911 { 912 /* Save FS type for future use */ 913 tr_strlcpy(fstype, fs, PATH_MAX + 1); 914 } 915 } 916 917 if (strcasecmp(fstype, "xfs") == 0) 897 898 /* save device for future use */ 899 if (!*device) 900 { 901 char * d = getblkdev (path); 902 if (d == NULL) 903 return ret; 904 tr_strlcpy (device, d, PATH_MAX + 1); 905 } 906 907 /* save FS type for future use */ 908 if (!*fstype) 909 { 910 char * fs = getfstype (device); 911 if (fs != NULL) 912 tr_strlcpy (fstype, fs, PATH_MAX + 1); 913 } 914 915 if (strcasecmp(fstype, "xfs") == 0) 918 916 { 919 917 #ifdef HAVE_XQM 920 ret = getxfsquota(device); 921 #endif 922 } else { 923 ret = getquota(device); 918 ret = getxfsquota(device); 919 #endif 920 } 921 else 922 { 923 ret = getquota(device); 924 924 } 925 925 926 926 #endif /* WIN32 */ 927 927 return ret; 928 928 } 929 929 930 930 static int64_t 931 tr_getDiskFreeSpace( const char * path ) 932 { 933 #ifdef WIN32 934 uint64_t freeBytesAvailable = 0; 935 return GetDiskFreeSpaceEx( path, &freeBytesAvailable, NULL, NULL) 936 ? (int64_t)freeBytesAvailable 937 : -1; 931 tr_getDiskFreeSpace (const char * path) 932 { 933 #ifdef WIN32 934 935 uint64_t freeBytesAvailable = 0; 936 return GetDiskFreeSpaceEx (path, &freeBytesAvailable, NULL, NULL) 937 ? (int64_t)freeBytesAvailable 938 : -1; 939 938 940 #elif defined(HAVE_STATVFS) 939 struct statvfs buf; 940 return statvfs( path, &buf ) ? -1 : (int64_t)buf.f_bavail * (int64_t)buf.f_frsize; 941 #else 942 #warning FIXME: not implemented 943 return -1; 941 942 struct statvfs buf; 943 return statvfs(path, &buf) ? -1 : (int64_t)buf.f_bavail * (int64_t)buf.f_frsize; 944 945 #else 946 947 #warning FIXME: not implemented 948 return -1; 949 944 950 #endif 945 951 } 946 952 947 953 int64_t 948 tr_getFreeSpace( const char * path, char * device, char * fstype ) 949 { 950 int64_t i = tr_getQuotaFreeSpace( path, device, fstype ); 951 if( i < 0 ) 952 i = tr_getDiskFreeSpace( path ); 953 return i; 954 tr_getFreeSpace (const char * path, char * device, char * fstype) 955 { 956 int64_t i = tr_getQuotaFreeSpace (path, device, fstype); 957 958 if (i < 0) 959 i = tr_getDiskFreeSpace (path); 960 961 return i; 954 962 } 955 963
Note: See TracChangeset
for help on using the changeset viewer.