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