--- sourceforge.net/trunk/rdesktop/disk.c 2004/08/25 09:48:11 758 +++ sourceforge.net/trunk/rdesktop/disk.c 2004/12/23 20:43:47 807 @@ -31,7 +31,7 @@ #include #include /* ctime */ -#if (defined(HAVE_DIRFD) || defined(HAVE_DECL_DIRFD)) +#if (defined(HAVE_DIRFD) || (HAVE_DECL_DIRFD == 1)) #define DIRFD(a) (dirfd(a)) #else #define DIRFD(a) ((a)->DIR_FD_MEMBER_NAME) @@ -52,7 +52,7 @@ #define STATFS_T statvfs #define F_NAMELEN(buf) ((buf).f_namemax) -#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)) +#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)) #include #include #define STATFS_FN(path, buf) (statfs(path,buf)) @@ -66,6 +66,12 @@ #define STATFS_T statvfs #define F_NAMELEN(buf) ((buf).f_namemax) +#elif (defined(__alpha) && !defined(linux)) +#include /* osf1 statfs */ +#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf))) +#define STATFS_T statfs +#define F_NAMELEN(buf) (255) + #else #include /* linux statfs */ #include @@ -81,6 +87,7 @@ extern RDPDR_DEVICE g_rdpdr_device[]; FILEINFO g_fileinfo[MAX_OPEN_FILES]; +BOOL g_notify_stamp = False; typedef struct { @@ -90,6 +97,7 @@ char type[256]; } FsInfoType; +static NTSTATUS NotifyInfo(NTHANDLE handle, uint32 info_class, NOTIFY * p); static time_t get_create_time(struct stat *st) @@ -132,6 +140,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. */ @@ -169,9 +286,9 @@ /* Opens or creates a file or directory */ 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]; @@ -182,8 +299,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); @@ -220,7 +336,7 @@ 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 if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode))) @@ -276,7 +392,7 @@ flags |= O_RDONLY; } - handle = open(path, flags, mode); + handle = open_weak_exclusive(path, flags, mode); if (handle == -1) { switch (errno) @@ -316,36 +432,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; } static NTSTATUS -disk_close(HANDLE handle) +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; } static NTSTATUS -disk_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +disk_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; @@ -369,7 +518,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; @@ -382,7 +534,7 @@ } static NTSTATUS -disk_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +disk_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; @@ -409,7 +561,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; @@ -490,12 +642,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; @@ -503,6 +654,7 @@ struct STATFS_T stat_fs; pfinfo = &(g_fileinfo[handle]); + g_notify_stamp = True; switch (info_class) { @@ -562,7 +714,7 @@ 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; } @@ -620,25 +772,16 @@ FileDispositionInformation requests with DeleteFile set to FALSE should unschedule the delete. See - http://www.osronline.com/article.cfm?article=245. Currently, - we are deleting the file immediately. I - guess this is a FIXME. */ - - //in_uint32_le(in, delete_on_close); + http://www.osronline.com/article.cfm?article=245. */ - /* Make sure we close the file before - unlinking it. Not doing so would trigger - silly-delete if using NFS, which might fail - on FAT floppies, for example. */ - disk_close(handle); + in_uint32_le(in, delete_on_close); - if ((pfinfo->flags_and_attributes & FILE_DIRECTORY_FILE)) // remove a directory + 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; @@ -655,14 +798,11 @@ /* 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) + if (stat_fs.f_bfree * stat_fs.f_bsize < length) return STATUS_DISK_FULL; - /* FIXME: Growing file with ftruncate doesn't - work with Linux FAT fs */ - if (ftruncate(handle, length) != 0) + if (ftruncate_growable(handle, length) != 0) { - perror("ftruncate"); return STATUS_DISK_FULL; } @@ -675,20 +815,129 @@ return STATUS_SUCCESS; } +NTSTATUS +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) { -#ifdef HAVE_MNTENT_H FILE *fdfs; - struct mntent *e; static FsInfoType info; +#ifdef HAVE_MNTENT_H + struct mntent *e; +#endif /* initialize */ memset(&info, 0, sizeof(info)); strcpy(info.label, "RDESKTOP"); strcpy(info.type, "RDPFS"); +#ifdef HAVE_MNTENT_H fdfs = setmntent(MNTENT_PATH, "r"); if (!fdfs) return &info; @@ -731,8 +980,6 @@ } endmntent(fdfs); #else - static FsInfoType info; - /* initialize */ memset(&info, 0, sizeof(info)); strcpy(info.label, "RDESKTOP"); @@ -744,7 +991,7 @@ NTSTATUS -disk_query_volume_information(HANDLE handle, uint32 info_class, STREAM out) +disk_query_volume_information(NTHANDLE handle, uint32 info_class, STREAM out) { struct STATFS_T stat_fs; struct fileinfo *pfinfo; @@ -809,7 +1056,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]; @@ -919,10 +1166,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;