Created
March 18, 2014 05:29
-
-
Save rossy/9614041 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unistd.h> | |
| #include <stdio.h> | |
| #include <stdbool.h> | |
| #include <fcntl.h> | |
| #include <io.h> | |
| #include <windows.h> | |
| #include <winternl.h> | |
| #ifndef FILE_REMOTE_DEVICE | |
| #define FILE_REMOTE_DEVICE (0x10) | |
| #endif | |
| static NTSTATUS (NTAPI *pNtQueryVolumeInformationFile)(HANDLE, | |
| PIO_STATUS_BLOCK, PVOID, ULONG, FS_INFORMATION_CLASS); | |
| static void load_functions(void) | |
| { | |
| HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); | |
| pNtQueryVolumeInformationFile = (NTSTATUS (NTAPI*)(HANDLE, | |
| PIO_STATUS_BLOCK, PVOID, ULONG, FS_INFORMATION_CLASS)) | |
| GetProcAddress(ntdll, "NtQueryVolumeInformationFile"); | |
| } | |
| static bool is_network_file(int fd) | |
| { | |
| if (!pNtQueryVolumeInformationFile) | |
| return false; | |
| HANDLE h = (HANDLE)_get_osfhandle(fd); | |
| FILE_FS_DEVICE_INFORMATION info; | |
| IO_STATUS_BLOCK io; | |
| NTSTATUS status = pNtQueryVolumeInformationFile(h, &io, &info, | |
| sizeof(info), FileFsDeviceInformation); | |
| if (status) | |
| return false; | |
| return info.DeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM || | |
| (info.Characteristics & FILE_REMOTE_DEVICE); | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| load_functions(); | |
| if (!argv[1]) { | |
| fprintf(stderr, "usage: %s path\n", argv[0]); | |
| return 1; | |
| } | |
| int fd = open(argv[1], O_RDONLY | O_BINARY); | |
| if (fd < 0) { | |
| perror(argv[1]); | |
| return 2; | |
| } | |
| if (is_network_file(fd)) | |
| puts("network file"); | |
| else | |
| puts("local file"); | |
| close(fd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment