--- sourceforge.net/trunk/rdesktop/disk.c 2004/02/23 09:58:16 612 +++ sourceforge.net/trunk/rdesktop/disk.c 2005/04/03 18:08:05 879 @@ -18,77 +18,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#define FILE_ATTRIBUTE_READONLY 0x00000001 -#define FILE_ATTRIBUTE_HIDDEN 0x00000002 -#define FILE_ATTRIBUTE_SYSTEM 0x00000004 -#define FILE_ATTRIBUTE_DIRECTORY 0x00000010 -#define FILE_ATTRIBUTE_ARCHIVE 0x00000020 -#define FILE_ATTRIBUTE_DEVICE 0x00000040 -#define FILE_ATTRIBUTE_UNKNOWNXXX0 0x00000060 /* ??? ACTION i.e. 0x860 == compress this file ? */ -#define FILE_ATTRIBUTE_NORMAL 0x00000080 -#define FILE_ATTRIBUTE_TEMPORARY 0x00000100 -#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200 -#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400 -#define FILE_ATTRIBUTE_COMPRESSED 0x00000800 -#define FILE_ATTRIBUTE_OFFLINE 0x00001000 -#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000 -#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000 - -#define FILE_FLAG_OPEN_NO_RECALL 0x00100000 -#define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000 -#define FILE_FLAG_POSIX_SEMANTICS 0x01000000 -#define FILE_FLAG_BACKUP_SEMANTICS 0x02000000 /* sometimes used to create a directory */ -#define FILE_FLAG_DELETE_ON_CLOSE 0x04000000 -#define FILE_FLAG_SEQUENTIAL_SCAN 0x08000000 -#define FILE_FLAG_RANDOM_ACCESS 0x10000000 -#define FILE_FLAG_NO_BUFFERING 0x20000000 -#define FILE_FLAG_OVERLAPPED 0x40000000 -#define FILE_FLAG_WRITE_THROUGH 0x80000000 - -#define FILE_SHARE_READ 0x01 -#define FILE_SHARE_WRITE 0x02 -#define FILE_SHARE_DELETE 0x04 - -#define FILE_BASIC_INFORMATION 0x04 -#define FILE_STANDARD_INFORMATION 0x05 - -#define FS_CASE_SENSITIVE 0x00000001 -#define FS_CASE_IS_PRESERVED 0x00000002 -#define FS_UNICODE_STORED_ON_DISK 0x00000004 -#define FS_PERSISTENT_ACLS 0x00000008 -#define FS_FILE_COMPRESSION 0x00000010 -#define FS_VOLUME_QUOTAS 0x00000020 -#define FS_SUPPORTS_SPARSE_FILES 0x00000040 -#define FS_SUPPORTS_REPARSE_POINTS 0x00000080 -#define FS_SUPPORTS_REMOTE_STORAGE 0X00000100 -#define FS_VOL_IS_COMPRESSED 0x00008000 -#define FILE_READ_ONLY_VOLUME 0x00080000 - -#define OPEN_EXISTING 1 -#define CREATE_NEW 2 -#define OPEN_ALWAYS 3 -#define TRUNCATE_EXISTING 4 -#define CREATE_ALWAYS 5 - -#define GENERIC_READ 0x80000000 -#define GENERIC_WRITE 0x40000000 -#define GENERIC_EXECUTE 0x20000000 -#define GENERIC_ALL 0x10000000 - -#define ERROR_FILE_NOT_FOUND 2L -#define ERROR_ALREADY_EXISTS 183L - -#define MAX_OPEN_FILES 0x100 - -#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) -#define SOLARIS -#endif - -#if (defined(SOLARIS) || defined(__hpux)) -#define DIRFD(a) ((a)->dd_fd) -#else -#define DIRFD(a) (dirfd(a)) -#endif +#include "disk.h" #include #include @@ -101,43 +31,127 @@ #include #include /* ctime */ +#if (defined(HAVE_DIRFD) || (HAVE_DECL_DIRFD == 1)) +#define DIRFD(a) (dirfd(a)) +#else +#define DIRFD(a) ((a)->DIR_FD_MEMBER_NAME) +#endif -#if (defined(SOLARIS) || defined (__hpux) || defined(__BEOS__)) -#include /* solaris statvfs */ -#define STATFS_FN(path, buf) (statvfs(path,buf)) -#define STATFS_T statvfs -#define F_NAMELEN(buf) ((buf).f_namemax) +/* TODO: Fix mntent-handling for solaris + * #include */ +#if (defined(HAVE_MNTENT_H) && defined(HAVE_SETMNTENT)) +#include +#define MNTENT_PATH "/etc/mtab" +#define USE_SETMNTENT +#endif -#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)) +#ifdef HAVE_SYS_VFS_H +#include +#endif + +#ifdef HAVE_SYS_STATVFS_H +#include +#endif + +#ifdef HAVE_SYS_STATFS_H +#include +#endif + +#ifdef HAVE_SYS_PARAM_H #include +#endif + +#ifdef HAVE_SYS_MOUNT_H #include -#define STATFS_FN(path, buf) (statfs(path,buf)) +#endif + +#include "rdesktop.h" + +#ifdef STAT_STATFS3_OSF1 +#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf))) #define STATFS_T statfs -#define F_NAMELEN(buf) (NAME_MAX) +#define USE_STATFS +#endif -#else -#include /* linux statfs */ +#ifdef STAT_STATVFS +#define STATFS_FN(path, buf) (statvfs(path,buf)) +#define STATFS_T statvfs +#define USE_STATVFS +#endif + +#ifdef STAT_STATVFS64 +#define STATFS_FN(path, buf) (statvfs64(path,buf)) +#define STATFS_T statvfs64 +#define USE_STATVFS +#endif + +#if (defined(STAT_STATFS2_FS_DATA) || defined(STAT_STATFS2_BSIZE) || defined(STAT_STATFS2_FSIZE)) #define STATFS_FN(path, buf) (statfs(path,buf)) #define STATFS_T statfs +#define USE_STATFS +#endif + +#ifdef STAT_STATFS4 +#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf),0)) +#define STATFS_T statfs +#define USE_STATFS +#endif + +#if ((defined(USE_STATFS) && defined(HAVE_STRUCT_STATFS_F_NAMEMAX)) || (defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_NAMEMAX))) +#define F_NAMELEN(buf) ((buf).f_namemax) +#endif + +#if ((defined(USE_STATFS) && defined(HAVE_STRUCT_STATFS_F_NAMELEN)) || (defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_NAMELEN))) #define F_NAMELEN(buf) ((buf).f_namelen) #endif -#include "rdesktop.h" +#ifndef F_NAMELEN +#define F_NAMELEN(buf) (255) +#endif -extern RDPDR_DEVICE g_rdpdr_device[]; +/* Dummy statfs fallback */ +#ifndef STATFS_T +struct dummy_statfs_t +{ + long f_bfree; + long f_bsize; + long f_blocks; + int f_namelen; + int f_namemax; +}; -struct fileinfo +int +dummy_statfs(struct dummy_statfs_t *buf) { - uint32 device_id, flags_and_attributes; - char path[256]; - DIR *pdir; - struct dirent *pdirent; - char pattern[64]; - BOOL delete_on_close; + buf->f_blocks = 262144; + buf->f_bfree = 131072; + buf->f_bsize = 512; + buf->f_namelen = 255; + buf->f_namemax = 255; + + return 0; } -g_fileinfo[MAX_OPEN_FILES]; -time_t +#define STATFS_T dummy_statfs_t +#define STATFS_FN(path,buf) (dummy_statfs(buf)) +#endif + +extern RDPDR_DEVICE g_rdpdr_device[]; + +FILEINFO g_fileinfo[MAX_OPEN_FILES]; +BOOL g_notify_stamp = False; + +typedef struct +{ + char name[256]; + char label[256]; + unsigned long serial; + char type[256]; +} FsInfoType; + +static NTSTATUS NotifyInfo(NTHANDLE handle, uint32 info_class, NOTIFY * p); + +static time_t get_create_time(struct stat *st) { time_t ret, ret1; @@ -152,7 +166,7 @@ } /* Convert seconds since 1970 to a filetime */ -void +static void seconds_since_1970_to_filetime(time_t seconds, uint32 * high, uint32 * low) { unsigned long long ticks; @@ -163,7 +177,7 @@ } /* Convert seconds since 1970 back to filetime */ -time_t +static time_t convert_1970_to_filetime(uint32 high, uint32 low) { unsigned long long ticks; @@ -178,6 +192,115 @@ } +/* A wrapper for ftruncate which supports growing files, even if the + native ftruncate doesn't. This is needed on Linux FAT filesystems, + for example. */ +static int +ftruncate_growable(int fd, off_t length) +{ + int ret; + off_t pos; + static const char zero; + + /* Try the simple method first */ + if ((ret = ftruncate(fd, length)) != -1) + { + return ret; + } + + /* + * Some kind of error. Perhaps we were trying to grow. Retry + * in a safe way. + */ + + /* Get current position */ + if ((pos = lseek(fd, 0, SEEK_CUR)) == -1) + { + perror("lseek"); + return -1; + } + + /* Seek to new size */ + if (lseek(fd, length, SEEK_SET) == -1) + { + perror("lseek"); + return -1; + } + + /* Write a zero */ + if (write(fd, &zero, 1) == -1) + { + perror("write"); + return -1; + } + + /* Truncate. This shouldn't fail. */ + if (ftruncate(fd, length) == -1) + { + perror("ftruncate"); + return -1; + } + + /* Restore position */ + if (lseek(fd, pos, SEEK_SET) == -1) + { + perror("lseek"); + return -1; + } + + return 0; +} + +/* Just like open(2), but if a open with O_EXCL fails, retry with + GUARDED semantics. This might be necessary because some filesystems + (such as NFS filesystems mounted from a unfsd server) doesn't + support O_EXCL. GUARDED semantics are subject to race conditions, + but we can live with that. +*/ +static int +open_weak_exclusive(const char *pathname, int flags, mode_t mode) +{ + int ret; + struct stat statbuf; + + ret = open(pathname, flags, mode); + if (ret != -1 || !(flags & O_EXCL)) + { + /* Success, or not using O_EXCL */ + return ret; + } + + /* An error occured, and we are using O_EXCL. In case the FS + doesn't support O_EXCL, some kind of error will be + returned. Unfortunately, we don't know which one. Linux + 2.6.8 seems to return 524, but I cannot find a documented + #define for this case. So, we'll return only on errors that + we know aren't related to O_EXCL. */ + switch (errno) + { + case EACCES: + case EEXIST: + case EINTR: + case EISDIR: + case ELOOP: + case ENAMETOOLONG: + case ENOENT: + case ENOTDIR: + return ret; + } + + /* Retry with GUARDED semantics */ + if (stat(pathname, &statbuf) != -1) + { + /* File exists */ + errno = EEXIST; + return -1; + } + else + { + return open(pathname, flags & ~O_EXCL, mode); + } +} /* Enumeration of devices from rdesktop.c */ /* returns numer of units found and initialized. */ @@ -190,21 +313,19 @@ char *pos2; int count = 0; - // skip the first colon + /* skip the first colon */ optarg++; while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) { pos2 = next_arg(optarg, '='); - strcpy(g_rdpdr_device[*id].name, optarg); - - toupper_str(g_rdpdr_device[*id].name); - /* add trailing colon to name. */ - strcat(g_rdpdr_device[*id].name, ":"); + strncpy(g_rdpdr_device[*id].name, optarg, sizeof(g_rdpdr_device[*id].name) - 1); + if (strlen(optarg) > (sizeof(g_rdpdr_device[*id].name) - 1)) + fprintf(stderr, "share name %s truncated to %s\n", optarg, + g_rdpdr_device[*id].name); g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1); strcpy(g_rdpdr_device[*id].local_path, pos2); - printf("DISK %s to %s\n", g_rdpdr_device[*id].name, g_rdpdr_device[*id].local_path); g_rdpdr_device[*id].device_type = DEVICE_TYPE_DISK; count++; (*id)++; @@ -215,11 +336,11 @@ } /* Opens or creates a file or directory */ -NTSTATUS +static NTSTATUS disk_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition, - uint32 flags_and_attributes, char *filename, HANDLE * phandle) + uint32 flags_and_attributes, char *filename, NTHANDLE * phandle) { - HANDLE handle; + NTHANDLE handle; DIR *dirp; int flags, mode; char path[256]; @@ -230,8 +351,7 @@ flags = 0; mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; - - if (filename[strlen(filename) - 1] == '/') + if (*filename && filename[strlen(filename) - 1] == '/') filename[strlen(filename) - 1] = 0; sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename); @@ -239,38 +359,38 @@ { case CREATE_ALWAYS: - // Delete existing file/link. + /* Delete existing file/link. */ unlink(path); flags |= O_CREAT; break; case CREATE_NEW: - // If the file already exists, then fail. + /* If the file already exists, then fail. */ flags |= O_CREAT | O_EXCL; break; case OPEN_ALWAYS: - // Create if not already exists. + /* Create if not already exists. */ flags |= O_CREAT; break; case OPEN_EXISTING: - // Default behaviour + /* Default behaviour */ break; case TRUNCATE_EXISTING: - // If the file does not exist, then fail. + /* If the file does not exist, then fail. */ flags |= O_TRUNC; break; } - //printf("Open: \"%s\" flags: %u, accessmask: %u sharemode: %u create disp: %u\n", path, flags_and_attributes, accessmask, sharemode, create_disposition); + /*printf("Open: \"%s\" flags: %X, accessmask: %X sharemode: %X create disp: %X\n", path, flags_and_attributes, accessmask, sharemode, create_disposition); */ - // Get information about file and set that flag ourselfs + /* Get information about file and set that flag ourselfs */ if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode))) { if (flags_and_attributes & FILE_NON_DIRECTORY_FILE) @@ -324,7 +444,7 @@ flags |= O_RDONLY; } - handle = open(path, flags, mode); + handle = open_weak_exclusive(path, flags, mode); if (handle == -1) { switch (errno) @@ -364,36 +484,69 @@ if (dirp) g_fileinfo[handle].pdir = dirp; + else + g_fileinfo[handle].pdir = NULL; + g_fileinfo[handle].device_id = device_id; g_fileinfo[handle].flags_and_attributes = flags_and_attributes; + g_fileinfo[handle].accessmask = accessmask; strncpy(g_fileinfo[handle].path, path, 255); + g_fileinfo[handle].delete_on_close = False; + g_notify_stamp = True; *phandle = handle; return STATUS_SUCCESS; } -NTSTATUS -disk_close(HANDLE handle) +static NTSTATUS +disk_close(NTHANDLE handle) { struct fileinfo *pfinfo; pfinfo = &(g_fileinfo[handle]); - if (pfinfo->flags_and_attributes & FILE_DIRECTORY_FILE) + g_notify_stamp = True; + + rdpdr_abort_io(handle, 0, STATUS_CANCELLED); + + if (pfinfo->pdir) { - closedir(pfinfo->pdir); - //FIXME: Should check exit code + if (closedir(pfinfo->pdir) < 0) + { + perror("closedir"); + return STATUS_INVALID_HANDLE; + } + + if (pfinfo->delete_on_close) + if (rmdir(pfinfo->path) < 0) + { + perror(pfinfo->path); + return STATUS_ACCESS_DENIED; + } + pfinfo->delete_on_close = False; } else { - close(handle); + if (close(handle) < 0) + { + perror("close"); + return STATUS_INVALID_HANDLE; + } + if (pfinfo->delete_on_close) + if (unlink(pfinfo->path) < 0) + { + perror(pfinfo->path); + return STATUS_ACCESS_DENIED; + } + + pfinfo->delete_on_close = False; } return STATUS_SUCCESS; } -NTSTATUS -disk_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +static NTSTATUS +disk_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; @@ -407,8 +560,8 @@ } #endif - if (offset) - lseek(handle, offset, SEEK_SET); + lseek(handle, offset, SEEK_SET); + n = read(handle, data, length); if (n < 0) @@ -417,7 +570,10 @@ switch (errno) { case EISDIR: - return STATUS_FILE_IS_A_DIRECTORY; + /* Implement 24 Byte directory read ?? + with STATUS_NOT_IMPLEMENTED server doesn't read again */ + /* return STATUS_FILE_IS_A_DIRECTORY; */ + return STATUS_NOT_IMPLEMENTED; default: perror("read"); return STATUS_INVALID_PARAMETER; @@ -429,13 +585,12 @@ return STATUS_SUCCESS; } -NTSTATUS -disk_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +static NTSTATUS +disk_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; - if (offset) - lseek(handle, offset, SEEK_SET); + lseek(handle, offset, SEEK_SET); n = write(handle, data, length); @@ -458,7 +613,7 @@ } NTSTATUS -disk_query_information(HANDLE handle, uint32 info_class, STREAM out) +disk_query_information(NTHANDLE handle, uint32 info_class, STREAM out) { uint32 file_attributes, ft_high, ft_low; struct stat filestat; @@ -466,7 +621,7 @@ path = g_fileinfo[handle].path; - // Get information about file + /* Get information about file */ if (fstat(handle, &filestat) != 0) { perror("stat"); @@ -474,7 +629,7 @@ return STATUS_ACCESS_DENIED; } - // Set file attributes + /* Set file attributes */ file_attributes = 0; if (S_ISDIR(filestat.st_mode)) file_attributes |= FILE_ATTRIBUTE_DIRECTORY; @@ -489,42 +644,42 @@ if (!(filestat.st_mode & S_IWUSR)) file_attributes |= FILE_ATTRIBUTE_READONLY; - // Return requested data + /* Return requested data */ switch (info_class) { - case 4: /* FileBasicInformation */ + case FileBasicInformation: seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high, &ft_low); - out_uint32_le(out, ft_low); //create_access_time + out_uint32_le(out, ft_low); /* create_access_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_access_time + out_uint32_le(out, ft_low); /* last_access_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_mtime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_write_time + out_uint32_le(out, ft_low); /* last_write_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_change_time + out_uint32_le(out, ft_low); /* last_change_time */ out_uint32_le(out, ft_high); out_uint32_le(out, file_attributes); break; - case 5: /* FileStandardInformation */ + case FileStandardInformation: - out_uint32_le(out, filestat.st_size); //Allocation size + out_uint32_le(out, filestat.st_size); /* Allocation size */ out_uint32_le(out, 0); - out_uint32_le(out, filestat.st_size); //End of file + out_uint32_le(out, filestat.st_size); /* End of file */ out_uint32_le(out, 0); - out_uint32_le(out, filestat.st_nlink); //Number of links - out_uint8(out, 0); //Delete pending - out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); //Directory + out_uint32_le(out, filestat.st_nlink); /* Number of links */ + out_uint8(out, 0); /* Delete pending */ + out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); /* Directory */ break; - case 35: /* FileObjectIdInformation */ + case FileObjectIdInformation: out_uint32_le(out, file_attributes); /* File Attributes */ out_uint32_le(out, 0); /* Reparse Tag */ @@ -539,12 +694,11 @@ } NTSTATUS -disk_set_information(HANDLE handle, uint32 info_class, STREAM in, STREAM out) +disk_set_information(NTHANDLE handle, uint32 info_class, STREAM in, STREAM out) { - uint32 device_id, length, file_attributes, ft_high, ft_low; + uint32 length, file_attributes, ft_high, ft_low, delete_on_close; char newname[256], fullpath[256]; struct fileinfo *pfinfo; - int mode; struct stat filestat; time_t write_time, change_time, access_time, mod_time; @@ -552,32 +706,33 @@ struct STATFS_T stat_fs; pfinfo = &(g_fileinfo[handle]); + g_notify_stamp = True; switch (info_class) { - case 4: /* FileBasicInformation */ + case FileBasicInformation: write_time = change_time = access_time = 0; in_uint8s(in, 4); /* Handle of root dir? */ in_uint8s(in, 24); /* unknown */ - // CreationTime + /* CreationTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); - // AccessTime + /* AccessTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); if (ft_low || ft_high) access_time = convert_1970_to_filetime(ft_high, ft_low); - // WriteTime + /* WriteTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); if (ft_low || ft_high) write_time = convert_1970_to_filetime(ft_high, ft_low); - // ChangeTime + /* ChangeTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); if (ft_low || ft_high) @@ -611,12 +766,12 @@ printf("FileBasicInformation modification time %s", ctime(&tvs.modtime)); #endif - if (utime(pfinfo->path, &tvs)) + if (utime(pfinfo->path, &tvs) && errno != EPERM) return STATUS_ACCESS_DENIED; } if (!file_attributes) - break; // not valid + break; /* not valid */ mode = filestat.st_mode; @@ -633,14 +788,9 @@ if (fchmod(handle, mode)) return STATUS_ACCESS_DENIED; - /* prevents start of writing if not enough space left on device */ - if (STATFS_FN(g_rdpdr_device[pfinfo->device_id].local_path, &stat_fs) == 0) - if (stat_fs.f_bsize * stat_fs.f_bfree < length) - return STATUS_DISK_FULL; - break; - case 10: /* FileRenameInformation */ + case FileRenameInformation: in_uint8s(in, 4); /* Handle of root dir? */ in_uint8s(in, 0x1a); /* unknown */ @@ -666,35 +816,48 @@ } break; - case 13: /* FileDispositionInformation */ - - //unimpl("IRP Set File Information class: FileDispositionInformation\n"); - - //in_uint32_le(in, delete_on_close); - // disk_close(handle); - if ((pfinfo->flags_and_attributes & FILE_DIRECTORY_FILE)) // remove a directory + case FileDispositionInformation: + /* As far as I understand it, the correct + thing to do here is to *schedule* a delete, + so it will be deleted when the file is + closed. Subsequent + FileDispositionInformation requests with + DeleteFile set to FALSE should unschedule + the delete. See + http://www.osronline.com/article.cfm?article=245. */ + + in_uint32_le(in, delete_on_close); + + if (delete_on_close || + (pfinfo-> + accessmask & (FILE_DELETE_ON_CLOSE | FILE_COMPLETE_IF_OPLOCKED))) { - if (rmdir(pfinfo->path) < 0) - return STATUS_ACCESS_DENIED; + pfinfo->delete_on_close = True; } - else if (unlink(pfinfo->path) < 0) // unlink a file - return STATUS_ACCESS_DENIED; break; - case 19: /* FileAllocationInformation */ - - unimpl("IRP Set File Information class: FileAllocationInformation\n"); - break; + case FileAllocationInformation: + /* Fall through to FileEndOfFileInformation, + which uses ftrunc. This is like Samba with + "strict allocation = false", and means that + we won't detect out-of-quota errors, for + example. */ - case 20: /* FileEndOfFileInformation */ + case FileEndOfFileInformation: in_uint8s(in, 28); /* unknown */ in_uint32_le(in, length); /* file size */ - printf("FileEndOfFileInformation length = %d\n", length); - // ???????????? + /* prevents start of writing if not enough space left on device */ + if (STATFS_FN(g_rdpdr_device[pfinfo->device_id].local_path, &stat_fs) == 0) + if (stat_fs.f_bfree * stat_fs.f_bsize < length) + return STATUS_DISK_FULL; + + if (ftruncate_growable(handle, length) != 0) + { + return STATUS_DISK_FULL; + } - unimpl("IRP Set File Information class: FileEndOfFileInformation\n"); break; default: @@ -705,15 +868,188 @@ } NTSTATUS -disk_query_volume_information(HANDLE handle, uint32 info_class, STREAM out) +disk_check_notify(NTHANDLE handle) +{ + struct fileinfo *pfinfo; + NTSTATUS status = STATUS_PENDING; + + NOTIFY notify; + + pfinfo = &(g_fileinfo[handle]); + if (!pfinfo->pdir) + return STATUS_INVALID_DEVICE_REQUEST; + + + + status = NotifyInfo(handle, pfinfo->info_class, ¬ify); + + if (status != STATUS_PENDING) + return status; + + if (memcmp(&pfinfo->notify, ¬ify, sizeof(NOTIFY))) + { + /*printf("disk_check_notify found changed event\n"); */ + memcpy(&pfinfo->notify, ¬ify, sizeof(NOTIFY)); + status = STATUS_NOTIFY_ENUM_DIR; + } + + return status; + + +} + +NTSTATUS +disk_create_notify(NTHANDLE handle, uint32 info_class) +{ + + struct fileinfo *pfinfo; + NTSTATUS ret = STATUS_PENDING; + + /* printf("start disk_create_notify info_class %X\n", info_class); */ + + pfinfo = &(g_fileinfo[handle]); + pfinfo->info_class = info_class; + + ret = NotifyInfo(handle, info_class, &pfinfo->notify); + + if (info_class & 0x1000) + { /* ???? */ + if (ret == STATUS_PENDING) + return STATUS_SUCCESS; + } + + /* printf("disk_create_notify: num_entries %d\n", pfinfo->notify.num_entries); */ + + + return ret; + +} + +static NTSTATUS +NotifyInfo(NTHANDLE handle, uint32 info_class, NOTIFY * p) +{ + struct fileinfo *pfinfo; + struct stat buf; + struct dirent *dp; + char *fullname; + DIR *dpr; + + pfinfo = &(g_fileinfo[handle]); + if (fstat(handle, &buf) < 0) + { + perror("NotifyInfo"); + return STATUS_ACCESS_DENIED; + } + p->modify_time = buf.st_mtime; + p->status_time = buf.st_ctime; + p->num_entries = 0; + p->total_time = 0; + + + dpr = opendir(pfinfo->path); + if (!dpr) + { + perror("NotifyInfo"); + return STATUS_ACCESS_DENIED; + } + + + while ((dp = readdir(dpr))) + { + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) + continue; + p->num_entries++; + fullname = xmalloc(strlen(pfinfo->path) + strlen(dp->d_name) + 2); + sprintf(fullname, "%s/%s", pfinfo->path, dp->d_name); + + if (!stat(fullname, &buf)) + { + p->total_time += (buf.st_mtime + buf.st_ctime); + } + + xfree(fullname); + } + closedir(dpr); + + return STATUS_PENDING; +} + +static FsInfoType * +FsVolumeInfo(char *fpath) +{ + + static FsInfoType info; +#ifdef USE_SETMNTENT + FILE *fdfs; + struct mntent *e; +#endif + + /* initialize */ + memset(&info, 0, sizeof(info)); + strcpy(info.label, "RDESKTOP"); + strcpy(info.type, "RDPFS"); + +#ifdef USE_SETMNTENT + fdfs = setmntent(MNTENT_PATH, "r"); + if (!fdfs) + return &info; + + while ((e = getmntent(fdfs))) + { + if (strncmp(fpath, e->mnt_dir, strlen(fpath)) == 0) + { + strcpy(info.type, e->mnt_type); + strcpy(info.name, e->mnt_fsname); + if (strstr(e->mnt_opts, "vfat") || strstr(e->mnt_opts, "iso9660")) + { + int fd = open(e->mnt_fsname, O_RDONLY); + if (fd >= 0) + { + unsigned char buf[512]; + memset(buf, 0, sizeof(buf)); + if (strstr(e->mnt_opts, "vfat")) + /*FAT*/ + { + strcpy(info.type, "vfat"); + read(fd, buf, sizeof(buf)); + info.serial = + (buf[42] << 24) + (buf[41] << 16) + + (buf[40] << 8) + buf[39]; + strncpy(info.label, buf + 43, 10); + info.label[10] = '\0'; + } + else if (lseek(fd, 32767, SEEK_SET) >= 0) /* ISO9660 */ + { + read(fd, buf, sizeof(buf)); + strncpy(info.label, buf + 41, 32); + info.label[32] = '\0'; + /* info.Serial = (buf[128]<<24)+(buf[127]<<16)+(buf[126]<<8)+buf[125]; */ + } + close(fd); + } + } + } + } + endmntent(fdfs); +#else + /* initialize */ + memset(&info, 0, sizeof(info)); + strcpy(info.label, "RDESKTOP"); + strcpy(info.type, "RDPFS"); + +#endif + return &info; +} + + +NTSTATUS +disk_query_volume_information(NTHANDLE handle, uint32 info_class, STREAM out) { - char *volume, *fs_type; struct STATFS_T stat_fs; struct fileinfo *pfinfo; + FsInfoType *fsinfo; pfinfo = &(g_fileinfo[handle]); - volume = "RDESKTOP"; - fs_type = "RDPFS"; if (STATFS_FN(pfinfo->path, &stat_fs) != 0) { @@ -721,19 +1057,23 @@ return STATUS_ACCESS_DENIED; } + fsinfo = FsVolumeInfo(pfinfo->path); + switch (info_class) { - case 1: /* FileFsVolumeInformation */ + case FileFsVolumeInformation: out_uint32_le(out, 0); /* volume creation time low */ out_uint32_le(out, 0); /* volume creation time high */ - out_uint32_le(out, 0); /* serial */ - out_uint32_le(out, 2 * strlen(volume)); /* length of string */ + out_uint32_le(out, fsinfo->serial); /* serial */ + + out_uint32_le(out, 2 * strlen(fsinfo->label)); /* length of string */ + out_uint8(out, 0); /* support objects? */ - rdp_out_unistr(out, volume, 2 * strlen(volume) - 2); + rdp_out_unistr(out, fsinfo->label, 2 * strlen(fsinfo->label) - 2); break; - case 3: /* FileFsSizeInformation */ + case FileFsSizeInformation: out_uint32_le(out, stat_fs.f_blocks); /* Total allocation units low */ out_uint32_le(out, 0); /* Total allocation high units */ @@ -743,20 +1083,22 @@ out_uint32_le(out, 0x200); /* Bytes per sector */ break; - case 5: /* FileFsAttributeInformation */ + case FileFsAttributeInformation: out_uint32_le(out, FS_CASE_SENSITIVE | FS_CASE_IS_PRESERVED); /* fs attributes */ out_uint32_le(out, F_NAMELEN(stat_fs)); /* max length of filename */ - out_uint32_le(out, 2 * strlen(fs_type)); /* length of fs_type */ - rdp_out_unistr(out, fs_type, 2 * strlen(fs_type) - 2); + + out_uint32_le(out, 2 * strlen(fsinfo->type)); /* length of fs_type */ + rdp_out_unistr(out, fsinfo->type, 2 * strlen(fsinfo->type) - 2); break; - case 2: /* FileFsLabelInformation */ - case 4: /* FileFsDeviceInformation */ - case 6: /* FileFsControlInformation */ - case 7: /* FileFsFullSizeInformation */ - case 8: /* FileFsObjectIdInformation */ - case 9: /* FileFsMaximumInformation */ + case FileFsLabelInformation: + case FileFsDeviceInformation: + case FileFsControlInformation: + case FileFsFullSizeInformation: + case FileFsObjectIdInformation: + case FileFsMaximumInformation: + default: unimpl("IRP Query Volume Information class: 0x%x\n", info_class); @@ -766,7 +1108,7 @@ } NTSTATUS -disk_query_directory(HANDLE handle, uint32 info_class, char *pattern, STREAM out) +disk_query_directory(NTHANDLE handle, uint32 info_class, char *pattern, STREAM out) { uint32 file_attributes, ft_low, ft_high; char *dirname, fullpath[256]; @@ -782,16 +1124,16 @@ switch (info_class) { - case 3: //FIXME: Why 3? + case FileBothDirectoryInformation: - // If a search pattern is received, remember this pattern, and restart search + /* If a search pattern is received, remember this pattern, and restart search */ if (pattern[0] != 0) { strncpy(pfinfo->pattern, 1 + strrchr(pattern, '/'), 64); rewinddir(pdir); } - // find next dirent matching pattern + /* find next dirent matching pattern */ pdirent = readdir(pdir); while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0) pdirent = readdir(pdir); @@ -799,16 +1141,26 @@ if (pdirent == NULL) return STATUS_NO_MORE_FILES; - // Get information for directory entry + /* Get information for directory entry */ sprintf(fullpath, "%s/%s", dirname, pdirent->d_name); - /* JIF - printf("Stat: %s\n", fullpath); */ if (stat(fullpath, &fstat)) { - perror("stat"); - out_uint8(out, 0); - return STATUS_ACCESS_DENIED; + switch (errno) + { + case ENOENT: + case ELOOP: + case EACCES: + /* These are non-fatal errors. */ + memset(&fstat, 0, sizeof(fstat)); + break; + default: + /* Fatal error. By returning STATUS_NO_SUCH_FILE, + the directory list operation will be aborted */ + perror(fullpath); + out_uint8(out, 0); + return STATUS_NO_SUCH_FILE; + } } if (S_ISDIR(fstat.st_mode)) @@ -820,38 +1172,41 @@ if (!(fstat.st_mode & S_IWUSR)) file_attributes |= FILE_ATTRIBUTE_READONLY; - // Return requested information - out_uint8s(out, 8); //unknown zero + /* Return requested information */ + out_uint8s(out, 8); /* unknown zero */ seconds_since_1970_to_filetime(get_create_time(&fstat), &ft_high, &ft_low); - out_uint32_le(out, ft_low); // create time + out_uint32_le(out, ft_low); /* create time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_access_time + out_uint32_le(out, ft_low); /* last_access_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_mtime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_write_time + out_uint32_le(out, ft_low); /* last_write_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_ctime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //change_write_time + out_uint32_le(out, ft_low); /* change_write_time */ out_uint32_le(out, ft_high); - out_uint32_le(out, fstat.st_size); //filesize low - out_uint32_le(out, 0); //filesize high - out_uint32_le(out, fstat.st_size); //filesize low - out_uint32_le(out, 0); //filesize high + out_uint32_le(out, fstat.st_size); /* filesize low */ + out_uint32_le(out, 0); /* filesize high */ + out_uint32_le(out, fstat.st_size); /* filesize low */ + out_uint32_le(out, 0); /* filesize high */ out_uint32_le(out, file_attributes); - out_uint8(out, 2 * strlen(pdirent->d_name) + 2); //unicode length - out_uint8s(out, 7); //pad? - out_uint8(out, 0); //8.3 file length - out_uint8s(out, 2 * 12); //8.3 unicode length + out_uint8(out, 2 * strlen(pdirent->d_name) + 2); /* unicode length */ + out_uint8s(out, 7); /* pad? */ + out_uint8(out, 0); /* 8.3 file length */ + out_uint8s(out, 2 * 12); /* 8.3 unicode length */ rdp_out_unistr(out, pdirent->d_name, 2 * strlen(pdirent->d_name)); break; default: + /* FIXME: Support FileDirectoryInformation, + FileFullDirectoryInformation, and + FileNamesInformation */ unimpl("IRP Query Directory sub: 0x%x\n", info_class); return STATUS_INVALID_PARAMETER; @@ -863,10 +1218,8 @@ static NTSTATUS -disk_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out) +disk_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out) { - uint32 result; - if (((request >> 16) != 20) || ((request >> 16) != 9)) return STATUS_INVALID_PARAMETER; @@ -878,8 +1231,8 @@ switch (request) { - case 25: // ? - case 42: // ? + case 25: /* ? */ + case 42: /* ? */ default: unimpl("DISK IOCTL %d\n", request); return STATUS_INVALID_PARAMETER;