##// END OF EJS Templates
statfs: avoid static allocation...
Jun Wu -
r31623:a01e1132 default
parent child Browse files
Show More
@@ -795,290 +795,284 b' static PyObject *setprocname(PyObject *s'
795 795 #endif /* ndef SETPROCNAME_USE_NONE */
796 796
797 797 #if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
798 /* given a directory path, return filesystem type (best-effort), or None */
799 const char *getfstype(const char *path) {
800 #ifdef HAVE_BSD_STATFS
801 /* need to return a string field */
802 static struct statfs buf;
803 #else
804 struct statfs buf;
805 #endif
798 /* given a directory path and a zero-initialized statfs buffer, return
799 * filesystem type name (best-effort), or NULL. */
800 const char *getfstype(const char *path, struct statfs *pbuf) {
806 801 int r;
807 memset(&buf, 0, sizeof(buf));
808 r = statfs(path, &buf);
802 r = statfs(path, pbuf);
809 803 if (r != 0)
810 804 return NULL;
811 805 #if defined(HAVE_BSD_STATFS)
812 806 /* BSD or OSX provides a f_fstypename field */
813 return buf.f_fstypename;
807 return pbuf->f_fstypename;
814 808 #elif defined(HAVE_LINUX_STATFS)
815 809 /* Begin of Linux filesystems */
816 810 #ifdef ADFS_SUPER_MAGIC
817 if (buf.f_type == ADFS_SUPER_MAGIC)
811 if (pbuf->f_type == ADFS_SUPER_MAGIC)
818 812 return "adfs";
819 813 #endif
820 814 #ifdef AFFS_SUPER_MAGIC
821 if (buf.f_type == AFFS_SUPER_MAGIC)
815 if (pbuf->f_type == AFFS_SUPER_MAGIC)
822 816 return "affs";
823 817 #endif
824 818 #ifdef BDEVFS_MAGIC
825 if (buf.f_type == BDEVFS_MAGIC)
819 if (pbuf->f_type == BDEVFS_MAGIC)
826 820 return "bdevfs";
827 821 #endif
828 822 #ifdef BEFS_SUPER_MAGIC
829 if (buf.f_type == BEFS_SUPER_MAGIC)
823 if (pbuf->f_type == BEFS_SUPER_MAGIC)
830 824 return "befs";
831 825 #endif
832 826 #ifdef BFS_MAGIC
833 if (buf.f_type == BFS_MAGIC)
827 if (pbuf->f_type == BFS_MAGIC)
834 828 return "bfs";
835 829 #endif
836 830 #ifdef BINFMTFS_MAGIC
837 if (buf.f_type == BINFMTFS_MAGIC)
831 if (pbuf->f_type == BINFMTFS_MAGIC)
838 832 return "binfmtfs";
839 833 #endif
840 834 #ifdef BTRFS_SUPER_MAGIC
841 if (buf.f_type == BTRFS_SUPER_MAGIC)
835 if (pbuf->f_type == BTRFS_SUPER_MAGIC)
842 836 return "btrfs";
843 837 #endif
844 838 #ifdef CGROUP_SUPER_MAGIC
845 if (buf.f_type == CGROUP_SUPER_MAGIC)
839 if (pbuf->f_type == CGROUP_SUPER_MAGIC)
846 840 return "cgroup";
847 841 #endif
848 842 #ifdef CIFS_MAGIC_NUMBER
849 if (buf.f_type == CIFS_MAGIC_NUMBER)
843 if (pbuf->f_type == CIFS_MAGIC_NUMBER)
850 844 return "cifs";
851 845 #endif
852 846 #ifdef CODA_SUPER_MAGIC
853 if (buf.f_type == CODA_SUPER_MAGIC)
847 if (pbuf->f_type == CODA_SUPER_MAGIC)
854 848 return "coda";
855 849 #endif
856 850 #ifdef COH_SUPER_MAGIC
857 if (buf.f_type == COH_SUPER_MAGIC)
851 if (pbuf->f_type == COH_SUPER_MAGIC)
858 852 return "coh";
859 853 #endif
860 854 #ifdef CRAMFS_MAGIC
861 if (buf.f_type == CRAMFS_MAGIC)
855 if (pbuf->f_type == CRAMFS_MAGIC)
862 856 return "cramfs";
863 857 #endif
864 858 #ifdef DEBUGFS_MAGIC
865 if (buf.f_type == DEBUGFS_MAGIC)
859 if (pbuf->f_type == DEBUGFS_MAGIC)
866 860 return "debugfs";
867 861 #endif
868 862 #ifdef DEVFS_SUPER_MAGIC
869 if (buf.f_type == DEVFS_SUPER_MAGIC)
863 if (pbuf->f_type == DEVFS_SUPER_MAGIC)
870 864 return "devfs";
871 865 #endif
872 866 #ifdef DEVPTS_SUPER_MAGIC
873 if (buf.f_type == DEVPTS_SUPER_MAGIC)
867 if (pbuf->f_type == DEVPTS_SUPER_MAGIC)
874 868 return "devpts";
875 869 #endif
876 870 #ifdef EFIVARFS_MAGIC
877 if (buf.f_type == EFIVARFS_MAGIC)
871 if (pbuf->f_type == EFIVARFS_MAGIC)
878 872 return "efivarfs";
879 873 #endif
880 874 #ifdef EFS_SUPER_MAGIC
881 if (buf.f_type == EFS_SUPER_MAGIC)
875 if (pbuf->f_type == EFS_SUPER_MAGIC)
882 876 return "efs";
883 877 #endif
884 878 #ifdef EXT_SUPER_MAGIC
885 if (buf.f_type == EXT_SUPER_MAGIC)
879 if (pbuf->f_type == EXT_SUPER_MAGIC)
886 880 return "ext";
887 881 #endif
888 882 #ifdef EXT2_OLD_SUPER_MAGIC
889 if (buf.f_type == EXT2_OLD_SUPER_MAGIC)
883 if (pbuf->f_type == EXT2_OLD_SUPER_MAGIC)
890 884 return "ext2";
891 885 #endif
892 886 #ifdef EXT2_SUPER_MAGIC
893 if (buf.f_type == EXT2_SUPER_MAGIC)
887 if (pbuf->f_type == EXT2_SUPER_MAGIC)
894 888 return "ext2";
895 889 #endif
896 890 #ifdef EXT3_SUPER_MAGIC
897 if (buf.f_type == EXT3_SUPER_MAGIC)
891 if (pbuf->f_type == EXT3_SUPER_MAGIC)
898 892 return "ext3";
899 893 #endif
900 894 #ifdef EXT4_SUPER_MAGIC
901 if (buf.f_type == EXT4_SUPER_MAGIC)
895 if (pbuf->f_type == EXT4_SUPER_MAGIC)
902 896 return "ext4";
903 897 #endif
904 898 #ifdef FUSE_SUPER_MAGIC
905 if (buf.f_type == FUSE_SUPER_MAGIC)
899 if (pbuf->f_type == FUSE_SUPER_MAGIC)
906 900 return "fuse";
907 901 #endif
908 902 #ifdef FUTEXFS_SUPER_MAGIC
909 if (buf.f_type == FUTEXFS_SUPER_MAGIC)
903 if (pbuf->f_type == FUTEXFS_SUPER_MAGIC)
910 904 return "futexfs";
911 905 #endif
912 906 #ifdef HFS_SUPER_MAGIC
913 if (buf.f_type == HFS_SUPER_MAGIC)
907 if (pbuf->f_type == HFS_SUPER_MAGIC)
914 908 return "hfs";
915 909 #endif
916 910 #ifdef HOSTFS_SUPER_MAGIC
917 if (buf.f_type == HOSTFS_SUPER_MAGIC)
911 if (pbuf->f_type == HOSTFS_SUPER_MAGIC)
918 912 return "hostfs";
919 913 #endif
920 914 #ifdef HPFS_SUPER_MAGIC
921 if (buf.f_type == HPFS_SUPER_MAGIC)
915 if (pbuf->f_type == HPFS_SUPER_MAGIC)
922 916 return "hpfs";
923 917 #endif
924 918 #ifdef HUGETLBFS_MAGIC
925 if (buf.f_type == HUGETLBFS_MAGIC)
919 if (pbuf->f_type == HUGETLBFS_MAGIC)
926 920 return "hugetlbfs";
927 921 #endif
928 922 #ifdef ISOFS_SUPER_MAGIC
929 if (buf.f_type == ISOFS_SUPER_MAGIC)
923 if (pbuf->f_type == ISOFS_SUPER_MAGIC)
930 924 return "isofs";
931 925 #endif
932 926 #ifdef JFFS2_SUPER_MAGIC
933 if (buf.f_type == JFFS2_SUPER_MAGIC)
927 if (pbuf->f_type == JFFS2_SUPER_MAGIC)
934 928 return "jffs2";
935 929 #endif
936 930 #ifdef JFS_SUPER_MAGIC
937 if (buf.f_type == JFS_SUPER_MAGIC)
931 if (pbuf->f_type == JFS_SUPER_MAGIC)
938 932 return "jfs";
939 933 #endif
940 934 #ifdef MINIX_SUPER_MAGIC
941 if (buf.f_type == MINIX_SUPER_MAGIC)
935 if (pbuf->f_type == MINIX_SUPER_MAGIC)
942 936 return "minix";
943 937 #endif
944 938 #ifdef MINIX2_SUPER_MAGIC
945 if (buf.f_type == MINIX2_SUPER_MAGIC)
939 if (pbuf->f_type == MINIX2_SUPER_MAGIC)
946 940 return "minix2";
947 941 #endif
948 942 #ifdef MINIX3_SUPER_MAGIC
949 if (buf.f_type == MINIX3_SUPER_MAGIC)
943 if (pbuf->f_type == MINIX3_SUPER_MAGIC)
950 944 return "minix3";
951 945 #endif
952 946 #ifdef MQUEUE_MAGIC
953 if (buf.f_type == MQUEUE_MAGIC)
947 if (pbuf->f_type == MQUEUE_MAGIC)
954 948 return "mqueue";
955 949 #endif
956 950 #ifdef MSDOS_SUPER_MAGIC
957 if (buf.f_type == MSDOS_SUPER_MAGIC)
951 if (pbuf->f_type == MSDOS_SUPER_MAGIC)
958 952 return "msdos";
959 953 #endif
960 954 #ifdef NCP_SUPER_MAGIC
961 if (buf.f_type == NCP_SUPER_MAGIC)
955 if (pbuf->f_type == NCP_SUPER_MAGIC)
962 956 return "ncp";
963 957 #endif
964 958 #ifdef NFS_SUPER_MAGIC
965 if (buf.f_type == NFS_SUPER_MAGIC)
959 if (pbuf->f_type == NFS_SUPER_MAGIC)
966 960 return "nfs";
967 961 #endif
968 962 #ifdef NILFS_SUPER_MAGIC
969 if (buf.f_type == NILFS_SUPER_MAGIC)
963 if (pbuf->f_type == NILFS_SUPER_MAGIC)
970 964 return "nilfs";
971 965 #endif
972 966 #ifdef NTFS_SB_MAGIC
973 if (buf.f_type == NTFS_SB_MAGIC)
967 if (pbuf->f_type == NTFS_SB_MAGIC)
974 968 return "ntfs-sb";
975 969 #endif
976 970 #ifdef OCFS2_SUPER_MAGIC
977 if (buf.f_type == OCFS2_SUPER_MAGIC)
971 if (pbuf->f_type == OCFS2_SUPER_MAGIC)
978 972 return "ocfs2";
979 973 #endif
980 974 #ifdef OPENPROM_SUPER_MAGIC
981 if (buf.f_type == OPENPROM_SUPER_MAGIC)
975 if (pbuf->f_type == OPENPROM_SUPER_MAGIC)
982 976 return "openprom";
983 977 #endif
984 978 #ifdef PIPEFS_MAGIC
985 if (buf.f_type == PIPEFS_MAGIC)
979 if (pbuf->f_type == PIPEFS_MAGIC)
986 980 return "pipefs";
987 981 #endif
988 982 #ifdef PROC_SUPER_MAGIC
989 if (buf.f_type == PROC_SUPER_MAGIC)
983 if (pbuf->f_type == PROC_SUPER_MAGIC)
990 984 return "proc";
991 985 #endif
992 986 #ifdef PSTOREFS_MAGIC
993 if (buf.f_type == PSTOREFS_MAGIC)
987 if (pbuf->f_type == PSTOREFS_MAGIC)
994 988 return "pstorefs";
995 989 #endif
996 990 #ifdef QNX4_SUPER_MAGIC
997 if (buf.f_type == QNX4_SUPER_MAGIC)
991 if (pbuf->f_type == QNX4_SUPER_MAGIC)
998 992 return "qnx4";
999 993 #endif
1000 994 #ifdef QNX6_SUPER_MAGIC
1001 if (buf.f_type == QNX6_SUPER_MAGIC)
995 if (pbuf->f_type == QNX6_SUPER_MAGIC)
1002 996 return "qnx6";
1003 997 #endif
1004 998 #ifdef RAMFS_MAGIC
1005 if (buf.f_type == RAMFS_MAGIC)
999 if (pbuf->f_type == RAMFS_MAGIC)
1006 1000 return "ramfs";
1007 1001 #endif
1008 1002 #ifdef REISERFS_SUPER_MAGIC
1009 if (buf.f_type == REISERFS_SUPER_MAGIC)
1003 if (pbuf->f_type == REISERFS_SUPER_MAGIC)
1010 1004 return "reiserfs";
1011 1005 #endif
1012 1006 #ifdef ROMFS_MAGIC
1013 if (buf.f_type == ROMFS_MAGIC)
1007 if (pbuf->f_type == ROMFS_MAGIC)
1014 1008 return "romfs";
1015 1009 #endif
1016 1010 #ifdef SELINUX_MAGIC
1017 if (buf.f_type == SELINUX_MAGIC)
1011 if (pbuf->f_type == SELINUX_MAGIC)
1018 1012 return "selinux";
1019 1013 #endif
1020 1014 #ifdef SMACK_MAGIC
1021 if (buf.f_type == SMACK_MAGIC)
1015 if (pbuf->f_type == SMACK_MAGIC)
1022 1016 return "smack";
1023 1017 #endif
1024 1018 #ifdef SMB_SUPER_MAGIC
1025 if (buf.f_type == SMB_SUPER_MAGIC)
1019 if (pbuf->f_type == SMB_SUPER_MAGIC)
1026 1020 return "smb";
1027 1021 #endif
1028 1022 #ifdef SOCKFS_MAGIC
1029 if (buf.f_type == SOCKFS_MAGIC)
1023 if (pbuf->f_type == SOCKFS_MAGIC)
1030 1024 return "sockfs";
1031 1025 #endif
1032 1026 #ifdef SQUASHFS_MAGIC
1033 if (buf.f_type == SQUASHFS_MAGIC)
1027 if (pbuf->f_type == SQUASHFS_MAGIC)
1034 1028 return "squashfs";
1035 1029 #endif
1036 1030 #ifdef SYSFS_MAGIC
1037 if (buf.f_type == SYSFS_MAGIC)
1031 if (pbuf->f_type == SYSFS_MAGIC)
1038 1032 return "sysfs";
1039 1033 #endif
1040 1034 #ifdef SYSV2_SUPER_MAGIC
1041 if (buf.f_type == SYSV2_SUPER_MAGIC)
1035 if (pbuf->f_type == SYSV2_SUPER_MAGIC)
1042 1036 return "sysv2";
1043 1037 #endif
1044 1038 #ifdef SYSV4_SUPER_MAGIC
1045 if (buf.f_type == SYSV4_SUPER_MAGIC)
1039 if (pbuf->f_type == SYSV4_SUPER_MAGIC)
1046 1040 return "sysv4";
1047 1041 #endif
1048 1042 #ifdef TMPFS_MAGIC
1049 if (buf.f_type == TMPFS_MAGIC)
1043 if (pbuf->f_type == TMPFS_MAGIC)
1050 1044 return "tmpfs";
1051 1045 #endif
1052 1046 #ifdef UDF_SUPER_MAGIC
1053 if (buf.f_type == UDF_SUPER_MAGIC)
1047 if (pbuf->f_type == UDF_SUPER_MAGIC)
1054 1048 return "udf";
1055 1049 #endif
1056 1050 #ifdef UFS_MAGIC
1057 if (buf.f_type == UFS_MAGIC)
1051 if (pbuf->f_type == UFS_MAGIC)
1058 1052 return "ufs";
1059 1053 #endif
1060 1054 #ifdef USBDEVICE_SUPER_MAGIC
1061 if (buf.f_type == USBDEVICE_SUPER_MAGIC)
1055 if (pbuf->f_type == USBDEVICE_SUPER_MAGIC)
1062 1056 return "usbdevice";
1063 1057 #endif
1064 1058 #ifdef V9FS_MAGIC
1065 if (buf.f_type == V9FS_MAGIC)
1059 if (pbuf->f_type == V9FS_MAGIC)
1066 1060 return "v9fs";
1067 1061 #endif
1068 1062 #ifdef VXFS_SUPER_MAGIC
1069 if (buf.f_type == VXFS_SUPER_MAGIC)
1063 if (pbuf->f_type == VXFS_SUPER_MAGIC)
1070 1064 return "vxfs";
1071 1065 #endif
1072 1066 #ifdef XENFS_SUPER_MAGIC
1073 if (buf.f_type == XENFS_SUPER_MAGIC)
1067 if (pbuf->f_type == XENFS_SUPER_MAGIC)
1074 1068 return "xenfs";
1075 1069 #endif
1076 1070 #ifdef XENIX_SUPER_MAGIC
1077 if (buf.f_type == XENIX_SUPER_MAGIC)
1071 if (pbuf->f_type == XENIX_SUPER_MAGIC)
1078 1072 return "xenix";
1079 1073 #endif
1080 1074 #ifdef XFS_SUPER_MAGIC
1081 if (buf.f_type == XFS_SUPER_MAGIC)
1075 if (pbuf->f_type == XFS_SUPER_MAGIC)
1082 1076 return "xfs";
1083 1077 #endif
1084 1078 /* End of Linux filesystems */
@@ -1089,10 +1083,12 b' const char *getfstype(const char *path) '
1089 1083 static PyObject *pygetfstype(PyObject *self, PyObject *args)
1090 1084 {
1091 1085 const char *path = NULL;
1086 struct statfs buf;
1092 1087 if (!PyArg_ParseTuple(args, "s", &path))
1093 1088 return NULL;
1094 1089
1095 const char *type = getfstype(path);
1090 memset(&buf, 0, sizeof(buf));
1091 const char *type = getfstype(path, &buf);
1096 1092 if (type == NULL)
1097 1093 Py_RETURN_NONE;
1098 1094
General Comments 0
You need to be logged in to leave comments. Login now