+ totalRead = -1;
+ } else if (!fd->reading) {
+ /* Not a reading handle */
+ yaffsfs_SetError(-EINVAL);
+ totalRead = -1;
+ } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
+ yaffsfs_SetError(-EINVAL);
+ totalRead = -1;
+ } else {
+ if (isPread)
+ startPos = offset;
+ else
+ startPos = fd->position;
+
+ pos = startPos;
+
+ if (yaffs_get_obj_length(obj) > pos)
+ maxRead = yaffs_get_obj_length(obj) - pos;
+ else
+ maxRead = 0;
+
+ if (nbyte > maxRead)
+ nbyte = maxRead;
+
+ yaffsfs_GetHandle(handle);
+
+ endPos = pos + nbyte;
+
+ if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
+ nbyte > YAFFS_MAX_FILE_SIZE ||
+ endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
+ totalRead = -1;
+ nbyte = 0;
+ }
+
+ while (nbyte > 0) {
+ nToRead = YAFFSFS_RW_SIZE -
+ (pos & (YAFFSFS_RW_SIZE - 1));
+ if (nToRead > nbyte)
+ nToRead = nbyte;
+
+ /* Tricky bit...
+ * Need to reverify object in case the device was
+ * unmounted in another thread.
+ */
+ obj = yaffsfs_HandleToObject(handle);
+ if (!obj)
+ nRead = 0;
+ else
+ nRead = yaffs_file_rd(obj, buf, pos, nToRead);
+
+ if (nRead > 0) {
+ totalRead += nRead;
+ pos += nRead;
+ buf += nRead;
+ }
+
+ if (nRead == nToRead)
+ nbyte -= nRead;
+ else
+ nbyte = 0; /* no more to read */
+
+ if (nbyte > 0) {
+ yaffsfs_Unlock();
+ yaffsfs_Lock();
+ }
+
+ }
+
+ yaffsfs_PutHandle(handle);
+
+ if (!isPread) {
+ if (totalRead >= 0)
+ fd->position = startPos + totalRead;
+ else
+ yaffsfs_SetError(-EINVAL);
+ }
+
+ }
+
+ yaffsfs_Unlock();
+
+ return (totalRead >= 0) ? totalRead : -1;
+
+}
+
+int yaffs_read(int handle, void *buf, unsigned int nbyte)
+{
+ return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
+}
+
+int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
+{
+ return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
+}
+
+static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
+ int isPwrite, Y_LOFF_T offset)
+{
+ struct yaffsfs_FileDes *fd = NULL;
+ struct yaffs_obj *obj = NULL;
+ Y_LOFF_T pos = 0;
+ Y_LOFF_T startPos = 0;
+ Y_LOFF_T endPos;
+ int nWritten = 0;
+ int totalWritten = 0;
+ int write_trhrough = 0;
+ int nToWrite = 0;
+ const u8 *buf = (const u8 *)vbuf;
+
+ if(yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0){
+ yaffsfs_SetError(-EFAULT);
+ return -1;
+ }
+
+ yaffsfs_Lock();
+ fd = yaffsfs_HandleToFileDes(handle);
+ obj = yaffsfs_HandleToObject(handle);
+
+ if (!fd || !obj) {
+ /* bad handle */
+ yaffsfs_SetError(-EBADF);
+ totalWritten = -1;
+ } else if (!fd->writing) {
+ yaffsfs_SetError(-EINVAL);
+ totalWritten = -1;
+ } else if (obj->my_dev->read_only) {
+ yaffsfs_SetError(-EROFS);
+ totalWritten = -1;
+ } else {
+ if (fd->append)
+ startPos = yaffs_get_obj_length(obj);
+ else if (isPwrite)
+ startPos = offset;
+ else
+ startPos = fd->position;
+
+ yaffsfs_GetHandle(handle);
+ pos = startPos;
+ endPos = pos + nbyte;
+
+ if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
+ nbyte > YAFFS_MAX_FILE_SIZE ||
+ endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
+ totalWritten = -1;
+ nbyte = 0;
+ }
+
+ while (nbyte > 0) {
+
+ nToWrite = YAFFSFS_RW_SIZE -
+ (pos & (YAFFSFS_RW_SIZE - 1));
+ if (nToWrite > nbyte)
+ nToWrite = nbyte;
+
+ /* Tricky bit...
+ * Need to reverify object in case the device was
+ * remounted or unmounted in another thread.
+ */
+ obj = yaffsfs_HandleToObject(handle);
+ if (!obj || obj->my_dev->read_only)
+ nWritten = 0;
+ else
+ nWritten =
+ yaffs_wr_file(obj, buf, pos, nToWrite,
+ write_trhrough);
+ if (nWritten > 0) {
+ totalWritten += nWritten;
+ pos += nWritten;
+ buf += nWritten;
+ }
+
+ if (nWritten == nToWrite)
+ nbyte -= nToWrite;
+ else
+ nbyte = 0;
+
+ if (nWritten < 1 && totalWritten < 1) {
+ yaffsfs_SetError(-ENOSPC);
+ totalWritten = -1;
+ }
+
+ if (nbyte > 0) {
+ yaffsfs_Unlock();
+ yaffsfs_Lock();
+ }
+ }
+
+ yaffsfs_PutHandle(handle);
+
+ if (!isPwrite) {
+ if (totalWritten > 0)
+ fd->position = startPos + totalWritten;
+ else
+ yaffsfs_SetError(-EINVAL);
+ }
+ }
+
+ yaffsfs_Unlock();
+
+ return (totalWritten >= 0) ? totalWritten : -1;
+}
+
+int yaffs_write(int fd, const void *buf, unsigned int nbyte)
+{
+ return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
+}
+
+int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
+{
+ return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
+}
+
+int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
+{
+ struct yaffs_obj *obj = NULL;
+ struct yaffs_obj *dir = NULL;
+ int result = YAFFS_FAIL;
+ int notDir = 0;
+ int loop = 0;
+
+ if(yaffsfs_CheckMemRegion(path, 0, 0) < 0){
+ yaffsfs_SetError(-EFAULT);
+ return -1;
+ }
+
+ if (yaffsfs_CheckPath(path) < 0) {
+ yaffsfs_SetError(-ENAMETOOLONG);
+ return -1;
+ }
+
+ yaffsfs_Lock();
+
+ obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
+ obj = yaffsfs_FollowLink(obj, 0, &loop);
+
+ if (!dir && notDir)
+ yaffsfs_SetError(-ENOTDIR);
+ else if (loop)
+ yaffsfs_SetError(-ELOOP);
+ else if (!dir || !obj)
+ yaffsfs_SetError(-ENOENT);
+ else if (obj->my_dev->read_only)
+ yaffsfs_SetError(-EROFS);
+ else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
+ yaffsfs_SetError(-EISDIR);
+ else if (obj->my_dev->read_only)
+ yaffsfs_SetError(-EROFS);
+ else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
+ yaffsfs_SetError(-EINVAL);
+ else
+ result = yaffs_resize_file(obj, new_size);
+
+ yaffsfs_Unlock();
+
+ return (result) ? 0 : -1;
+}
+
+int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
+{
+ struct yaffsfs_FileDes *fd = NULL;
+ struct yaffs_obj *obj = NULL;
+ int result = 0;
+
+ yaffsfs_Lock();
+ fd = yaffsfs_HandleToFileDes(handle);
+ obj = yaffsfs_HandleToObject(handle);
+
+ if (!fd || !obj)
+ /* bad handle */
+ yaffsfs_SetError(-EBADF);
+ else if (!fd->writing)
+ yaffsfs_SetError(-EINVAL);
+ else if (obj->my_dev->read_only)
+ yaffsfs_SetError(-EROFS);
+ else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
+ yaffsfs_SetError(-EINVAL);
+ else
+ /* resize the file */
+ result = yaffs_resize_file(obj, new_size);
+ yaffsfs_Unlock();
+
+ return (result) ? 0 : -1;
+
+}
+
+Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
+{
+ struct yaffsfs_FileDes *fd = NULL;
+ struct yaffs_obj *obj = NULL;
+ Y_LOFF_T pos = -1;
+ Y_LOFF_T fSize = -1;
+
+ yaffsfs_Lock();
+ fd = yaffsfs_HandleToFileDes(handle);
+ obj = yaffsfs_HandleToObject(handle);
+
+ if (!fd || !obj)
+ yaffsfs_SetError(-EBADF);
+ else if (offset > YAFFS_MAX_FILE_SIZE)
+ yaffsfs_SetError(-EINVAL);
+ else {
+ if (whence == SEEK_SET) {
+ if (offset >= 0)
+ pos = offset;
+ } else if (whence == SEEK_CUR) {
+ if ((fd->position + offset) >= 0)
+ pos = (fd->position + offset);
+ } else if (whence == SEEK_END) {
+ fSize = yaffs_get_obj_length(obj);
+ if (fSize >= 0 && (fSize + offset) >= 0)
+ pos = fSize + offset;
+ }
+
+ if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
+ fd->position = pos;
+ else {
+ yaffsfs_SetError(-EINVAL);
+ pos = -1;
+ }
+ }
+
+ yaffsfs_Unlock();
+
+ return pos;
+}
+
+static int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
+{
+ struct yaffs_obj *dir = NULL;
+ struct yaffs_obj *obj = NULL;
+ YCHAR *name;
+ int result = YAFFS_FAIL;
+ int notDir = 0;
+ int loop = 0;
+
+ if(yaffsfs_CheckMemRegion(path, 0, 0) < 0){
+ yaffsfs_SetError(-EFAULT);
+ return -1;
+ }
+
+ if (yaffsfs_CheckPath(path) < 0) {
+ yaffsfs_SetError(-ENAMETOOLONG);
+ return -1;
+ }
+
+ yaffsfs_Lock();
+
+ obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
+ dir = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
+
+ if (!dir && notDir)
+ yaffsfs_SetError(-ENOTDIR);
+ else if (loop)
+ yaffsfs_SetError(-ELOOP);
+ else if (!dir)
+ yaffsfs_SetError(-ENOENT);
+ else if (yaffs_strncmp(name, _Y("."), 2) == 0)
+ yaffsfs_SetError(-EINVAL);
+ else if (!obj)
+ yaffsfs_SetError(-ENOENT);
+ else if (obj->my_dev->read_only)
+ yaffsfs_SetError(-EROFS);
+ else if (!isDirectory &&
+ obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
+ yaffsfs_SetError(-EISDIR);
+ else if (isDirectory &&
+ obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
+ yaffsfs_SetError(-ENOTDIR);
+ else if (isDirectory && obj == obj->my_dev->root_dir)
+ yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
+ else {
+ result = yaffs_unlinker(dir, name);
+
+ if (result == YAFFS_FAIL && isDirectory)
+ yaffsfs_SetError(-ENOTEMPTY);