gboolean file_copy(gchar *source, gchar *dest) { GnomeVFSHandle *read_handle, *write_handle; GnomeVFSFileSize bytes_read, bytes_written; guint buffer[BYTES_TO_PROCESS]; GnomeVFSResult result; gchar *OnDiEn_source, *OnDiEn_dest; OnDiEn_source = get_filename_on_disk_encoding(source); OnDiEn_dest = get_filename_on_disk_encoding(dest); result = gnome_vfs_open(&read_handle, OnDiEn_source, GNOME_VFS_OPEN_READ); g_free(OnDiEn_source); if (result != GNOME_VFS_OK) return FALSE; result = gnome_vfs_create(&write_handle, OnDiEn_dest, GNOME_VFS_OPEN_WRITE, FALSE, 0644); g_free(OnDiEn_dest); if (result != GNOME_VFS_OK) { gnome_vfs_close(read_handle); return FALSE; } result = gnome_vfs_read (read_handle, buffer, BYTES_TO_PROCESS, &bytes_read); while (result == GNOME_VFS_OK) { result = gnome_vfs_write (write_handle, buffer, bytes_read, &bytes_written); if (result != GNOME_VFS_OK || bytes_written != bytes_read) { DEBUG_MSG("file_copy, return FALSE, write result=%d, written=%ld, read=%ld\n",result,(long)bytes_written,(long)bytes_read); gnome_vfs_close(write_handle); gnome_vfs_close(read_handle); return FALSE; } result = gnome_vfs_read(read_handle, buffer, BYTES_TO_PROCESS, &bytes_read); } gnome_vfs_close(write_handle); gnome_vfs_close(read_handle); return TRUE; }
static gchar * _load_uri (const gchar *uri) { GnomeVFSHandle *handle = NULL; GnomeVFSFileSize bytes_read; gsize bytesRead = 0; gsize bytesWritten = 0; GError* error = NULL; gchar* uri_local = g_filename_from_utf8( uri, -1, &bytesRead, &bytesWritten, &error); if ( uri_local == NULL ) { g_warning( "Error converting filename to locale encoding."); } GnomeVFSResult result = gnome_vfs_open (&handle, uri_local, GNOME_VFS_OPEN_READ); if (result != GNOME_VFS_OK) { g_warning("%s", gnome_vfs_result_to_string(result)); } std::vector<gchar> doc; while (result == GNOME_VFS_OK) { gchar buffer[BUF_SIZE]; result = gnome_vfs_read (handle, buffer, BUF_SIZE, &bytes_read); doc.insert(doc.end(), buffer, buffer+bytes_read); } return g_strndup(&doc[0], doc.size()); }
gboolean parse_ram (const gchar *path) { gchar **lines, **alternatives; guint i, nalt; GnomeVFSHandle *h; char buf[4]; GnomeVFSFileSize read, left; g_message ("parsing ram: %s", path); if (gnome_vfs_open (&h, path, GNOME_VFS_OPEN_READ)) return FALSE; left = 4; while (left && gnome_vfs_read (h, buf + (4 - left), left, &read) == GNOME_VFS_OK) left -= read; gnome_vfs_close (h); if (left) return FALSE; /* from gxine: if this is true, then its an actual stream, not a playlist file */ if (buf[0] == '.' && buf[1] == 'R' && buf[2] == 'M' && buf[3] == 'F') return TRUE; if ((lines = read_file (path))) { alternatives = g_new (gchar*, 1); alternatives[0] = NULL; nalt = 0; for (i = 0; lines[i]; ++i) { lines[i] = g_strstrip (lines[i]); if (strlen (lines[i])) { /* comment */ if (lines[i][0] == '#') continue; /* --stop-- lines */ if (strstr (lines[i], "--stop--")) break; /* from gxine: Either it's a rtsp, or a pnm mrl, but we also match http mrls here. */ if (g_str_has_prefix (lines[i], "rtsp://") || g_str_has_prefix (lines[i], "pnm://") || g_str_has_prefix (lines[i], "http://")) { alternatives = g_renew (gchar*, alternatives, nalt+2); alternatives[nalt] = g_strdup (lines[i]); alternatives[nalt+1] = NULL; ++nalt; } } }
static gboolean copy_uri (const gchar *src_uri, const gchar *dest_uri, const gchar *copying_format_str, const gchar *copied_format_str, GError **error) { GnomeVFSHandle *read_handle; GnomeVFSHandle *write_handle; GnomeVFSFileInfo *src_info; GnomeVFSFileSize file_size = 0; GnomeVFSFileSize bytes_read = 0; guchar buffer[BUFSIZE]; GnomeVFSResult result; gchar *memsize; GTimeVal last_time = { 0, 0 }; gimp_progress_init (_("Connecting to server")); src_info = gnome_vfs_file_info_new (); result = gnome_vfs_get_file_info (src_uri, src_info, 0); /* ignore errors here, they will be noticed below */ if (result == GNOME_VFS_OK && (src_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)) { file_size = src_info->size; } gnome_vfs_file_info_unref (src_info); result = gnome_vfs_open (&read_handle, src_uri, GNOME_VFS_OPEN_READ); if (result != GNOME_VFS_OK) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Could not open '%s' for reading: %s"), src_uri, gnome_vfs_result_to_string (result)); return FALSE; } result = gnome_vfs_create (&write_handle, dest_uri, GNOME_VFS_OPEN_WRITE, FALSE, 0644); if (result != GNOME_VFS_OK) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Could not open '%s' for writing: %s"), dest_uri, gnome_vfs_result_to_string (result)); gnome_vfs_close (read_handle); return FALSE; } memsize = g_format_size_for_display (file_size); gimp_progress_init_printf (file_size > 0 ? copying_format_str : copied_format_str, memsize); g_free (memsize); while (TRUE) { GnomeVFSFileSize chunk_read; GnomeVFSFileSize chunk_written; GTimeVal now; result = gnome_vfs_read (read_handle, buffer, sizeof (buffer), &chunk_read); if (chunk_read == 0) { if (result != GNOME_VFS_ERROR_EOF) { memsize = g_format_size_for_display (sizeof (buffer)); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to read %s from '%s': %s"), memsize, src_uri, gnome_vfs_result_to_string (result)); g_free (memsize); gnome_vfs_close (read_handle); gnome_vfs_close (write_handle); return FALSE; } else { gimp_progress_update (1.0); break; } } bytes_read += chunk_read; /* update the progress only up to 10 times a second */ g_get_current_time (&now); if (((now.tv_sec - last_time.tv_sec) * 1000 + (now.tv_usec - last_time.tv_usec) / 1000) > 100) { if (file_size > 0) { gimp_progress_update ((gdouble) bytes_read / (gdouble) file_size); } else { memsize = g_format_size_for_display (bytes_read); gimp_progress_set_text_printf (copied_format_str, memsize); gimp_progress_pulse (); g_free (memsize); } last_time = now; } result = gnome_vfs_write (write_handle, buffer, chunk_read, &chunk_written); if (chunk_written < chunk_read) { memsize = g_format_size_for_display (chunk_read); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to write %s to '%s': %s"), memsize, dest_uri, gnome_vfs_result_to_string (result)); g_free (memsize); gnome_vfs_close (read_handle); gnome_vfs_close (write_handle); return FALSE; } } gnome_vfs_close (read_handle); gnome_vfs_close (write_handle); return TRUE; }
/* * Read a new buffer from src->reqoffset, takes care of events * and seeking and such. */ static GstFlowReturn gst_gnome_vfs_src_create (GstBaseSrc * basesrc, guint64 offset, guint size, GstBuffer ** buffer) { GnomeVFSResult res; GstBuffer *buf; GnomeVFSFileSize readbytes; guint8 *data; guint todo; GstGnomeVFSSrc *src; src = GST_GNOME_VFS_SRC (basesrc); GST_DEBUG ("now at %" G_GINT64_FORMAT ", reading from %" G_GUINT64_FORMAT ", size %u", src->curoffset, offset, size); /* seek if required */ if (G_UNLIKELY (src->curoffset != offset)) { GST_DEBUG ("need to seek"); if (src->seekable) { GST_DEBUG ("seeking to %" G_GUINT64_FORMAT, offset); res = gnome_vfs_seek (src->handle, GNOME_VFS_SEEK_START, offset); if (res != GNOME_VFS_OK) goto seek_failed; src->curoffset = offset; } else { goto cannot_seek; } } buf = gst_buffer_try_new_and_alloc (size); if (G_UNLIKELY (buf == NULL)) { GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size); return GST_FLOW_ERROR; } data = GST_BUFFER_DATA (buf); todo = size; while (todo > 0) { /* this can return less that we ask for */ res = gnome_vfs_read (src->handle, data, todo, &readbytes); if (G_UNLIKELY (res == GNOME_VFS_ERROR_EOF || (res == GNOME_VFS_OK && readbytes == 0))) goto eos; if (G_UNLIKELY (res != GNOME_VFS_OK)) goto read_failed; if (readbytes < todo) { data = &data[readbytes]; todo -= readbytes; } else { todo = 0; } GST_LOG (" got size %" G_GUINT64_FORMAT, readbytes); } GST_BUFFER_OFFSET (buf) = src->curoffset; src->curoffset += size; /* we're done, return the buffer */ *buffer = buf; return GST_FLOW_OK; seek_failed: { GST_ELEMENT_ERROR (src, RESOURCE, SEEK, (NULL), ("Failed to seek to requested position %" G_GINT64_FORMAT ": %s", offset, gnome_vfs_result_to_string (res))); return GST_FLOW_ERROR; } cannot_seek: { GST_ELEMENT_ERROR (src, RESOURCE, SEEK, (NULL), ("Requested seek from %" G_GINT64_FORMAT " to %" G_GINT64_FORMAT " on non-seekable stream", src->curoffset, offset)); return GST_FLOW_ERROR; } read_failed: { gst_buffer_unref (buf); GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("Failed to read data: %s", gnome_vfs_result_to_string (res))); return GST_FLOW_ERROR; } eos: { gst_buffer_unref (buf); GST_DEBUG_OBJECT (src, "Reading data gave EOS"); return GST_FLOW_UNEXPECTED; } }
GnomeVFSResult nsGnomeVFSInputStream::DoRead(char *aBuf, PRUint32 aCount, PRUint32 *aCountRead) { GnomeVFSResult rv; if (mHandle) { GnomeVFSFileSize bytesRead; rv = gnome_vfs_read(mHandle, aBuf, aCount, &bytesRead); if (rv == GNOME_VFS_OK) { // XXX 64-bit here *aCountRead = (PRUint32) bytesRead; mBytesRemaining -= *aCountRead; } } else if (mDirOpen) { rv = GNOME_VFS_OK; while (aCount && rv != GNOME_VFS_ERROR_EOF) { // Copy data out of our buffer PRUint32 bufLen = mDirBuf.Length() - mDirBufCursor; if (bufLen) { PRUint32 n = PR_MIN(bufLen, aCount); memcpy(aBuf, mDirBuf.get() + mDirBufCursor, n); *aCountRead += n; aBuf += n; aCount -= n; mDirBufCursor += n; } if (!mDirListPtr) // Are we at the end of the directory list? { rv = GNOME_VFS_ERROR_EOF; } else if (aCount) // Do we need more data? { GnomeVFSFileInfo *info = (GnomeVFSFileInfo *) mDirListPtr->data; // Prune '.' and '..' from directory listing. if (info->name[0] == '.' && (info->name[1] == '\0' || (info->name[1] == '.' && info->name[2] == '\0'))) { mDirListPtr = mDirListPtr->next; continue; } mDirBuf.Assign("201: "); // The "filename" field nsCString escName; nsCOMPtr<nsINetUtil> nu = do_GetService(NS_NETUTIL_CONTRACTID); if (nu) { nu->EscapeString(nsDependentCString(info->name), nsINetUtil::ESCAPE_URL_PATH, escName); mDirBuf.Append(escName); mDirBuf.Append(' '); } // The "content-length" field // XXX truncates size from 64-bit to 32-bit mDirBuf.AppendInt(PRInt32(info->size)); mDirBuf.Append(' '); // The "last-modified" field // // NSPR promises: PRTime is compatible with time_t // we just need to convert from seconds to microseconds PRExplodedTime tm; PRTime pt = ((PRTime) info->mtime) * 1000000; PR_ExplodeTime(pt, PR_GMTParameters, &tm); { char buf[64]; PR_FormatTimeUSEnglish(buf, sizeof(buf), "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm); mDirBuf.Append(buf); } // The "file-type" field switch (info->type) { case GNOME_VFS_FILE_TYPE_REGULAR: mDirBuf.Append("FILE "); break; case GNOME_VFS_FILE_TYPE_DIRECTORY: mDirBuf.Append("DIRECTORY "); break; case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: mDirBuf.Append("SYMBOLIC-LINK "); break; default: break; } mDirBuf.Append('\n'); mDirBufCursor = 0; mDirListPtr = mDirListPtr->next; } } } else { NS_NOTREACHED("reading from what?"); rv = GNOME_VFS_ERROR_GENERIC; } return rv; }
/* FIXME: must also handle \r and \r\n terminators */ GnomeVFSResult mn_vfs_read_line (MNVFSReadLineContext **context, GnomeVFSHandle *handle, const char **line) { GnomeVFSResult result; gboolean first_pass = TRUE; g_return_val_if_fail(context != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); g_return_val_if_fail(handle != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); g_return_val_if_fail(line != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); if (*context) { g_return_val_if_fail((*context)->handle == handle, GNOME_VFS_ERROR_BAD_PARAMETERS); if ((*context)->terminator) { g_string_erase((*context)->buf, 0, (*context)->terminator - (*context)->buf->str + 1); (*context)->terminator = NULL; } else if ((*context)->eof) return GNOME_VFS_ERROR_EOF; /* we're done */ } else { *context = g_new0(MNVFSReadLineContext, 1); (*context)->handle = handle; (*context)->buf = g_string_new(NULL); } while (TRUE) { if (! (*context)->buf->str[0] || ! first_pass) { char buf[READ_LINE_BLOCK_SIZE]; GnomeVFSFileSize bytes_read; result = (*context)->last_result = gnome_vfs_read(handle, buf, sizeof(buf), &bytes_read); if (result == GNOME_VFS_OK || result == GNOME_VFS_ERROR_EOF) g_string_append_len((*context)->buf, buf, bytes_read); else break; /* error */ } (*context)->terminator = strchr((*context)->buf->str, '\n'); if ((*context)->terminator || (*context)->last_result == GNOME_VFS_ERROR_EOF) { result = (*context)->last_result; if ((*context)->terminator || (*context)->buf->str[0]) { *line = (*context)->buf->str; if (result == GNOME_VFS_ERROR_EOF) result = GNOME_VFS_OK; } if ((*context)->terminator) (*context)->terminator[0] = 0; else if ((*context)->last_result == GNOME_VFS_ERROR_EOF) (*context)->eof = TRUE; break; /* line found, or last line */ } first_pass = FALSE; } return result; }