File size of Google docs when mounted

182 views
Skip to first unread message

Mandolyte

unread,
Oct 5, 2023, 11:31:18 AM10/5/23
to golang-nuts
I have a program that given a starting folder will walk the directory structure and report file sizes and other things.

I recently tried to run this code on a mounted Google Drive on ChromeOS Linux. All the google docs report a size 170 bytes. Snippet:

Screenshot 2023-10-05 11.29.44 AM.png

Any pointers appreciated!

Kurtis Rader

unread,
Oct 5, 2023, 1:12:48 PM10/5/23
to Mandolyte, golang-nuts
It would help if you showed us the relevant code. My first guess is those files are symlinks. Or perhaps this is simply what Google Drive reports when a file is stat()'d. What does `ls -l` show?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8faf6106-2fdd-49e3-a193-e8f41605fbffn%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Mandolyte

unread,
Oct 5, 2023, 4:34:32 PM10/5/23
to golang-nuts
Thanks for responding! The entire program may be found here: https://go.dev/play/p/4kLaeWeQG0Z

The size comes from os.FileInfo, which is:

type FileInfo interface {
Name() string // base name of the file Size() int64 // length in bytes for regular files; system-dependent for others Mode() FileMode // file mode bits ModTime() time.Time // modification time IsDir() bool // abbreviation for Mode().IsDir() Sys() any // underlying data source (can return nil) }

These are collected around line 112 and following.

Kurtis Rader

unread,
Oct 5, 2023, 6:06:50 PM10/5/23
to Mandolyte, golang-nuts
Note that ioutil.ReadDir() has been deprecated since Go 1.16. Which is a very long time. Its documentation is ambiguous but given the behavior you're reporting I would guess it is using the equivalent of os.Lstat() and the "files" you're dealing with are symlinks. Note that os.ReadDir() (which replaced ioutil.ReadDir) explicitly states that is what it does:

Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.

Mandolyte

unread,
Oct 5, 2023, 7:30:56 PM10/5/23
to golang-nuts
Here is what I see:
$ ls -l Household\ Cleaning.gdoc
-rw-r----- 1 cecil chronos-access 170 Apr 22 15:30 'Household Cleaning.gdoc'


I'll update the code to remove the deprecated and see if that is any different.

Kurtis Rader

unread,
Oct 5, 2023, 7:43:08 PM10/5/23
to Mandolyte, golang-nuts
Did you notice the `ls` command reports the file size is 170? The same as your program? Also, `ls` does not indicate the file is a symlink so the fact that ioutil.ReadDir presumably behaves the same as os.ReadDir (i.e., both use os.Lstat()) that isn't an issue. So for whatever reason the Google Drive FS is either not reporting the actual size of the file or the file is really just 170 bytes in length.

Mandolyte

unread,
Oct 5, 2023, 10:12:25 PM10/5/23
to golang-nuts
Yeah, I need someone who knows something about how gdrive does things. Thanks for the help!

Mandolyte

unread,
Oct 7, 2023, 11:00:26 AM10/7/23
to golang-nuts
For the sake of posterity... here is what Bard says.

Prompt: In golang, develop code to find the size of a google doc

Response:

To find the size of a Google Doc in Golang, you can use the following steps:

  1. Import the necessary packages:
import ( "context" "fmt" "io/ioutil" "net/http" "github.com/google/go-auth/oauth2" "github.com/googleapis/google-api-go/docs/v1" )
  1. Create a new OAuth2 client and obtain an access token:
ctx := context.Background() config := oauth2.Config{ ClientID: "YOUR_CLIENT_ID", ClientSecret: "YOUR_CLIENT_SECRET", Scopes: []string{"https://www.googleapis.com/auth/drive.readonly"}, RedirectURL: "http://localhost:8080/oauth2callback", } token, err := config.Exchange(ctx, "YOUR_AUTHORIZATION_CODE") if err != nil { log.Fatalf("Unable to exchange code: %v", err) }
  1. Create a new Google Docs client:
client := docs.NewService(http.Client{ Transport: &oauth2.Transport{ Source: token, }, })
  1. Get the size of the Google Doc:
docID := "YOUR_DOCUMENT_ID" req, err := client.Documents.Get(docID).Do(ctx) if err != nil { log.Fatalf("Unable to get document: %v", err) } size := req.ContentLength
  1. Print the size of the Google Doc:
fmt.Printf("The size of the Google Doc is %d bytes\n", size)

Here is a complete example of a Golang program to find the size of a Google Doc:

package main import ( "context" "fmt" "io/ioutil" "net/http" "github.com/google/go-auth/oauth2" "github.com/googleapis/google-api-go/docs/v1" ) func main() { ctx := context.Background() config := oauth2.Config{ ClientID: "YOUR_CLIENT_ID", ClientSecret: "YOUR_CLIENT_SECRET", Scopes: []string{"https://www.googleapis.com/auth/drive.readonly"}, RedirectURL: "http://localhost:8080/oauth2callback", } token, err := config.Exchange(ctx, "YOUR_AUTHORIZATION_CODE") if err != nil { log.Fatalf("Unable to exchange code: %v", err) } client := docs.NewService(http.Client{ Transport: &oauth2.Transport{ Source: token, }, }) docID := "YOUR_DOCUMENT_ID" req, err := client.Documents.Get(docID).Do(ctx) if err != nil { log.Fatalf("Unable to get document: %v", err) } size := req.ContentLength fmt.Printf("The size of the Google Doc is %d bytes\n", size) }

To use this code, you will need to replace the following values:

  • YOUR_CLIENT_ID: Your Google Cloud Platform client ID.
  • YOUR_CLIENT_SECRET: Your Google Cloud Platform client secret.
  • YOUR_AUTHORIZATION_CODE: An authorization code that you can obtain by following the OAuth2 flow.
  • YOUR_DOCUMENT_ID: The ID of the Google Doc that you want to find the size of.

Once you have replaced these values, you can compile and run the code. The program will print the size of the Google Doc to the console.

Reply all
Reply to author
Forward
0 new messages