@@ -98,47 +98,6 @@ func (r *DefaultReader) safeReadFile(path string) ([]byte, error) {
98
98
return os .ReadFile (absPath )
99
99
}
100
100
101
- // safeOpenFile opens a file with security checks
102
- func (r * DefaultReader ) safeOpenFile (path string ) (* os.File , error ) {
103
- if err := r .validatePath (path ); err != nil {
104
- return nil , fmt .Errorf ("invalid path: %w" , err )
105
- }
106
-
107
- // Get absolute path
108
- absPath := path
109
- if ! filepath .IsAbs (path ) {
110
- absPath = filepath .Join (r .workDir , path )
111
- }
112
-
113
- // Clean the path
114
- absPath = filepath .Clean (absPath )
115
-
116
- // Verify file exists and get info
117
- info , err := os .Stat (absPath )
118
- if err != nil {
119
- return nil , err
120
- }
121
-
122
- // Check if it's a regular file
123
- if ! info .Mode ().IsRegular () {
124
- return nil , fmt .Errorf ("not a regular file: %s" , path )
125
- }
126
-
127
- // Check file size
128
- if info .Size () > maxFileSize {
129
- return nil , fmt .Errorf ("file too large: %s" , path )
130
- }
131
-
132
- // Check file extension for allowed types
133
- ext := strings .ToLower (filepath .Ext (path ))
134
- if ! isAllowedExtension (ext ) {
135
- return nil , fmt .Errorf ("unsupported file type: %s" , ext )
136
- }
137
-
138
- // Open file with read-only mode
139
- return os .OpenFile (absPath , os .O_RDONLY , 0 )
140
- }
141
-
142
101
// GetFileTree returns the file tree starting from the given root
143
102
func (r * DefaultReader ) GetFileTree (ctx context.Context , root string , opts TreeOptions ) (* FileTreeNode , error ) {
144
103
if err := r .validatePath (root ); err != nil {
@@ -205,7 +164,7 @@ func (r *DefaultReader) GetFileTree(ctx context.Context, root string, opts TreeO
205
164
return nil
206
165
}
207
166
case FileTypeGenerated :
208
- content , err := os . ReadFile (path )
167
+ content , err := r . safeReadFile (path )
209
168
if err != nil {
210
169
return err
211
170
}
@@ -237,19 +196,16 @@ func (r *DefaultReader) GetFileTree(ctx context.Context, root string, opts TreeO
237
196
// Find parent node
238
197
if path != absRoot {
239
198
parentPath := filepath .Dir (relPath )
240
- parent := findParentNode (tree , parentPath )
241
- if parent != nil {
242
- parent .Children = append (parent .Children , node )
243
- sortTree (parent )
244
- return nil
199
+ parentNode := findNode (tree , parentPath )
200
+ if parentNode != nil {
201
+ parentNode .Children = append (parentNode .Children , node )
202
+ // Sort children by name
203
+ sort .Slice (parentNode .Children , func (i , j int ) bool {
204
+ return parentNode .Children [i ].Name < parentNode .Children [j ].Name
205
+ })
245
206
}
246
207
}
247
208
248
- // If no parent found (should only happen for root), add to tree
249
- if path == absRoot {
250
- * tree = * node
251
- }
252
-
253
209
return nil
254
210
})
255
211
@@ -371,57 +327,35 @@ func (r *DefaultReader) ReadSourceFile(ctx context.Context, path string, opts Re
371
327
372
328
// isGeneratedFile checks if a file is generated based on its content
373
329
func isGeneratedFile (content []byte ) bool {
374
- // Common markers for generated files
330
+ contentStr := string ( content )
375
331
markers := []string {
376
- "Code generated" ,
377
- "DO NOT EDIT" ,
332
+ "Code generated" , "DO NOT EDIT" ,
378
333
"@generated" ,
379
- "Generated by" ,
334
+ "// Generated by" ,
335
+ "/* Generated by" ,
380
336
}
381
337
382
- contentStr := string (content )
383
338
for _ , marker := range markers {
384
339
if strings .Contains (contentStr , marker ) {
385
340
return true
386
341
}
387
342
}
388
-
389
343
return false
390
344
}
391
345
392
- // findParentNode finds a parent node in the tree by path
393
- func findParentNode (root * FileTreeNode , parentPath string ) * FileTreeNode {
394
- if root .Path == parentPath {
346
+ // findNode finds a node in the tree by its path
347
+ func findNode (root * FileTreeNode , path string ) * FileTreeNode {
348
+ if root .Path == path {
395
349
return root
396
350
}
351
+
397
352
for _ , child := range root .Children {
398
353
if child .Type == "directory" {
399
- if node := findParentNode (child , parentPath ); node != nil {
354
+ if node := findNode (child , path ); node != nil {
400
355
return node
401
356
}
402
357
}
403
358
}
404
- return nil
405
- }
406
359
407
- // sortTree sorts the children of a node by name
408
- func sortTree (node * FileTreeNode ) {
409
- if node == nil || len (node .Children ) == 0 {
410
- return
411
- }
412
-
413
- sort .Slice (node .Children , func (i , j int ) bool {
414
- // Directories come first
415
- if node .Children [i ].Type != node .Children [j ].Type {
416
- return node .Children [i ].Type == "directory"
417
- }
418
- return node .Children [i ].Name < node .Children [j ].Name
419
- })
420
-
421
- // Sort children recursively
422
- for _ , child := range node .Children {
423
- if child .Type == "directory" {
424
- sortTree (child )
425
- }
426
- }
360
+ return nil
427
361
}
0 commit comments