Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/fs.h>
0003 #include <linux/export.h>
0004 
0005 /*
0006  * fs on-disk file type to dirent file type conversion
0007  */
0008 static const unsigned char fs_dtype_by_ftype[FT_MAX] = {
0009     [FT_UNKNOWN]    = DT_UNKNOWN,
0010     [FT_REG_FILE]   = DT_REG,
0011     [FT_DIR]    = DT_DIR,
0012     [FT_CHRDEV] = DT_CHR,
0013     [FT_BLKDEV] = DT_BLK,
0014     [FT_FIFO]   = DT_FIFO,
0015     [FT_SOCK]   = DT_SOCK,
0016     [FT_SYMLINK]    = DT_LNK
0017 };
0018 
0019 /**
0020  * fs_ftype_to_dtype() - fs on-disk file type to dirent type.
0021  * @filetype: The on-disk file type to convert.
0022  *
0023  * This function converts the on-disk file type value (FT_*) to the directory
0024  * entry type (DT_*).
0025  *
0026  * Context: Any context.
0027  * Return:
0028  * * DT_UNKNOWN     - Unknown type
0029  * * DT_FIFO        - FIFO
0030  * * DT_CHR     - Character device
0031  * * DT_DIR     - Directory
0032  * * DT_BLK     - Block device
0033  * * DT_REG     - Regular file
0034  * * DT_LNK     - Symbolic link
0035  * * DT_SOCK        - Local-domain socket
0036  */
0037 unsigned char fs_ftype_to_dtype(unsigned int filetype)
0038 {
0039     if (filetype >= FT_MAX)
0040         return DT_UNKNOWN;
0041 
0042     return fs_dtype_by_ftype[filetype];
0043 }
0044 EXPORT_SYMBOL_GPL(fs_ftype_to_dtype);
0045 
0046 /*
0047  * dirent file type to fs on-disk file type conversion
0048  * Values not initialized explicitly are FT_UNKNOWN (0).
0049  */
0050 static const unsigned char fs_ftype_by_dtype[DT_MAX] = {
0051     [DT_REG]    = FT_REG_FILE,
0052     [DT_DIR]    = FT_DIR,
0053     [DT_LNK]    = FT_SYMLINK,
0054     [DT_CHR]    = FT_CHRDEV,
0055     [DT_BLK]    = FT_BLKDEV,
0056     [DT_FIFO]   = FT_FIFO,
0057     [DT_SOCK]   = FT_SOCK,
0058 };
0059 
0060 /**
0061  * fs_umode_to_ftype() - file mode to on-disk file type.
0062  * @mode: The file mode to convert.
0063  *
0064  * This function converts the file mode value to the on-disk file type (FT_*).
0065  *
0066  * Context: Any context.
0067  * Return:
0068  * * FT_UNKNOWN     - Unknown type
0069  * * FT_REG_FILE    - Regular file
0070  * * FT_DIR     - Directory
0071  * * FT_CHRDEV      - Character device
0072  * * FT_BLKDEV      - Block device
0073  * * FT_FIFO        - FIFO
0074  * * FT_SOCK        - Local-domain socket
0075  * * FT_SYMLINK     - Symbolic link
0076  */
0077 unsigned char fs_umode_to_ftype(umode_t mode)
0078 {
0079     return fs_ftype_by_dtype[S_DT(mode)];
0080 }
0081 EXPORT_SYMBOL_GPL(fs_umode_to_ftype);
0082 
0083 /**
0084  * fs_umode_to_dtype() - file mode to dirent file type.
0085  * @mode: The file mode to convert.
0086  *
0087  * This function converts the file mode value to the directory
0088  * entry type (DT_*).
0089  *
0090  * Context: Any context.
0091  * Return:
0092  * * DT_UNKNOWN     - Unknown type
0093  * * DT_FIFO        - FIFO
0094  * * DT_CHR     - Character device
0095  * * DT_DIR     - Directory
0096  * * DT_BLK     - Block device
0097  * * DT_REG     - Regular file
0098  * * DT_LNK     - Symbolic link
0099  * * DT_SOCK        - Local-domain socket
0100  */
0101 unsigned char fs_umode_to_dtype(umode_t mode)
0102 {
0103     return fs_ftype_to_dtype(fs_umode_to_ftype(mode));
0104 }
0105 EXPORT_SYMBOL_GPL(fs_umode_to_dtype);